This is an automated email from the ASF dual-hosted git repository.
doebele pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/empire-db.git
The following commit(s) were added to refs/heads/master by this push:
new 82fa77d EMPIREDB-362 removed querySingleValue(cmd, DataType, bool)
added querySingleValue(cmd, Class<T> type, bool)
82fa77d is described below
commit 82fa77d16b43a5ccc5b618b29990e46ce2976bf0
Author: Rainer Döbele <[email protected]>
AuthorDate: Mon Feb 28 20:26:25 2022 +0100
EMPIREDB-362 removed querySingleValue(cmd, DataType, bool) added
querySingleValue(cmd, Class<T> type, bool)
---
.../main/java/org/apache/empire/data/DataType.java | 48 ++++++++++++++++++++++
.../main/java/org/apache/empire/db/DBUtils.java | 23 ++++++-----
2 files changed, 61 insertions(+), 10 deletions(-)
diff --git a/empire-db/src/main/java/org/apache/empire/data/DataType.java
b/empire-db/src/main/java/org/apache/empire/data/DataType.java
index 3f3a212..fcf198c 100644
--- a/empire-db/src/main/java/org/apache/empire/data/DataType.java
+++ b/empire-db/src/main/java/org/apache/empire/data/DataType.java
@@ -18,6 +18,13 @@
*/
package org.apache.empire.data;
+import java.sql.Timestamp;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.Date;
+
+import org.apache.commons.beanutils.MethodUtils;
+
/**
* DataType is an enumeration of data types that are supported
* with the empire-db component.
@@ -153,4 +160,45 @@ public enum DataType
// no match
return false;
}
+
+ /**
+ * Returns the DataType from a given Java Type
+ * If the type is not mapped, then DataType.UNKNOWN is returned
+ * @param javaType the Java Type
+ * @return the corresponding DataType
+ */
+ public static DataType fromJavaType(Class<?> javaType)
+ {
+ if (javaType.isPrimitive())
+ javaType = MethodUtils.getPrimitiveWrapper(javaType);
+ // String
+ if (javaType==String.class)
+ return DataType.VARCHAR;
+ if (javaType==Character.class ||
+ javaType==Character[].class)
+ return DataType.CHAR;
+ // Check integer
+ if (javaType == Integer.class ||
+ javaType == Long.class ||
+ javaType == Short.class)
+ return DataType.INTEGER;
+ if (Number.class.isAssignableFrom(javaType))
+ return DataType.DECIMAL;
+ // Check Dates
+ if (javaType == Timestamp.class)
+ return DataType.TIMESTAMP;
+ if (Date.class.isAssignableFrom(javaType) ||
+ javaType == LocalDateTime.class)
+ return DataType.DATETIME;
+ if (javaType == LocalDate.class)
+ return DataType.DATE;
+ // Other
+ if (javaType == Boolean.class)
+ return DataType.BOOL;
+ if (javaType == byte[].class)
+ return DataType.BLOB;
+ // Unknown
+ return DataType.UNKNOWN;
+ }
+
}
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
b/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
index 84dd12b..35c01c0 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
@@ -157,11 +157,11 @@ public class DBUtils implements DBContextAware
* @param sqlCmd the SQL-Command
* @param sqlParams list of query parameter values
* @param dataType the expected data type
- * @param forceResult if true a QueryNoResultException result is thrown if
no record exists otherwise null is returned
+ * @param failOnNoResult if true a QueryNoResultException result is thrown
if no record exists otherwise null is returned
*
* @return the value of the first column in the first row of the query
*/
- public Object querySingleValue(String sqlCmd, Object[] sqlParams, DataType
dataType, boolean forceResult)
+ public Object querySingleValue(String sqlCmd, Object[] sqlParams, DataType
dataType, boolean failOnNoResult)
{
// Debug
long start = System.currentTimeMillis();
@@ -171,7 +171,7 @@ public class DBUtils implements DBContextAware
Object result = dbms.querySingleValue(sqlCmd, sqlParams, dataType,
context.getConnection());
if (result==ObjectUtils.NO_VALUE)
{ // Query returned no result
- if (forceResult)
+ if (failOnNoResult)
throw new QueryNoResultException(sqlCmd);
else
result = null;
@@ -195,9 +195,10 @@ public class DBUtils implements DBContextAware
*
* @return the value of the first column in the first row of the query
*/
- public final Object querySingleValue(DBCommandExpr cmd, DataType dataType,
boolean forceResult)
+ public final <T> T querySingleValue(DBCommandExpr cmd, Class<T>
resultType, boolean failOnNoResult)
{
- return querySingleValue(cmd.getSelect(), cmd.getParamValues(),
dataType, forceResult);
+ Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(),
DataType.fromJavaType(resultType), failOnNoResult);
+ return ObjectUtils.convert(resultType, value);
}
/**
@@ -208,9 +209,11 @@ public class DBUtils implements DBContextAware
*
* @return the value of the first column in the first row of the query
*/
- public final Object querySingleValue(DBCommandExpr cmd, boolean
forceResult)
+ public final Object querySingleValue(DBCommandExpr cmd, boolean
failOnNoResult)
{
- return querySingleValue(cmd, DataType.UNKNOWN, forceResult);
+ DBColumnExpr[] selExpr = cmd.getSelectExprList();
+ DataType dataType = (selExpr!=null && selExpr.length>0 ?
selExpr[0].getDataType() : DataType.UNKNOWN);
+ return querySingleValue(cmd.getSelect(), cmd.getParamValues(),
dataType, failOnNoResult);
}
/**
@@ -223,7 +226,7 @@ public class DBUtils implements DBContextAware
*/
public final Object querySingleValue(DBCommandExpr cmd)
{
- return querySingleValue(cmd, DataType.UNKNOWN, true);
+ return querySingleValue(cmd, true);
}
/**
@@ -796,12 +799,12 @@ public class DBUtils implements DBContextAware
/**
* Queries a single DataListEntry item
*/
- public final <T extends DataListEntry> T queryDataEntry(DBCommandExpr cmd,
Class<T> entryClass, boolean forceResult)
+ public final <T extends DataListEntry> T queryDataEntry(DBCommandExpr cmd,
Class<T> entryClass, boolean failOnNoResult)
{
DataListHead head = createDefaultDataListHead(cmd, entryClass);
List<T> dle = queryDataList(cmd,
createDefaultDataListFactory(entryClass, head), 0, 1);
if (dle.isEmpty())
- { if (forceResult)
+ { if (failOnNoResult)
throw new QueryNoResultException(cmd.getSelect());
return null;
}