You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by do...@apache.org on 2019/07/11 12:14:51 UTC
[empire-db] branch master updated: EMPIREDB-294: Allow functions
and command expression to be used as record field values
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 1eb6dd1 EMPIREDB-294: Allow functions and command expression to be used as record field values
1eb6dd1 is described below
commit 1eb6dd16dde3d1bf4abba01c0d0c6ff243d68b70
Author: Rainer Döbele <do...@apache.org>
AuthorDate: Thu Jul 11 14:14:46 2019 +0200
EMPIREDB-294: Allow functions and command expression to be used as record field values
---
.../main/java/org/apache/empire/data/DataType.java | 23 +++++++++
.../java/org/apache/empire/db/DBTableColumn.java | 55 ++++++++++++++++------
2 files changed, 64 insertions(+), 14 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 bb1eda5..ab3b2e9 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
@@ -138,4 +138,27 @@ public enum DataType
{
return (this==DataType.BOOL);
}
+
+ /**
+ * Returns whether or not two DataTypes are compatible
+ * @param other the other one
+ * @return true of types are compatible or false otherwise
+ */
+ public boolean isCompatible(DataType other)
+ {
+ if (this==other)
+ return true; // the same
+ // Special case: UNKNOWN
+ if (this==DataType.UNKNOWN || other==DataType.UNKNOWN)
+ return true; // assume compatible
+ // Check compatability
+ if (isText() && other.isText())
+ return true;
+ if (isNumeric() && other.isNumeric())
+ return true;
+ if (isDate() && other.isDate())
+ return true;
+ // no match
+ return false;
+ }
}
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java b/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
index 1599384..53f3716 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
@@ -362,8 +362,41 @@ public class DBTableColumn extends DBColumn
public Object validate(Object value)
{
// Check for NULL
- if (isRequired() && ObjectUtils.isEmpty(value))
- throw new FieldNotNullException(this);
+ if (ObjectUtils.isEmpty(value))
+ { // Null value
+ if (isRequired())
+ throw new FieldNotNullException(this);
+ // Null is allowed
+ return null;
+ }
+ // Check for Column expression
+ if (value instanceof DBColumnExpr)
+ { DataType funcType = ((DBColumnExpr)value).getDataType();
+ if (!type.isCompatible(funcType))
+ { // Incompatible data types
+ log.info("Incompatible data types in expression for column {} using function {}!", getName(), value.toString());
+ throw new FieldIllegalValueException(this, String.valueOf(value));
+ }
+ // allowed
+ return value;
+ }
+ // Check for Command expression
+ if (value instanceof DBCommandExpr)
+ { DBColumnExpr[] exprList = ((DBCommandExpr)value).getSelectExprList();
+ if (exprList.length!=1)
+ { // Incompatible data types
+ log.info("Invalid command expression for column {} using command {}!", getName(), ((DBCommandExpr)value).getSelect());
+ throw new FieldIllegalValueException(this, ((DBCommandExpr)value).getSelect());
+ }
+ // Compare types
+ if (!type.isCompatible(exprList[0].getDataType()))
+ { // Incompatible data types
+ log.info("Incompatible data types in expression for column {} using function {}!", getName(), value.toString());
+ throw new FieldIllegalValueException(this, String.valueOf(value));
+ }
+ // allowed
+ return value;
+ }
// Is value valid
switch (type)
{
@@ -371,7 +404,7 @@ public class DBTableColumn extends DBColumn
case DATETIME:
case TIMESTAMP:
// Check whether value is a valid date/time value!
- if (value!=null && !(value instanceof Date) && !DBDatabase.SYSDATE.equals(value))
+ if (!(value instanceof Date) && !DBDatabase.SYSDATE.equals(value))
{ // Parse String
String dateValue = value.toString();
if (dateValue.length()==0)
@@ -381,7 +414,7 @@ public class DBTableColumn extends DBColumn
if ((type==DataType.DATE || dateValue.length()<=12) && datePattern.indexOf(' ')>0)
datePattern = datePattern.substring(0, datePattern.indexOf(' ')); // Strip off time
try
- { // Parse date time value
+ { // Parse date time value
SimpleDateFormat sdFormat = new SimpleDateFormat(datePattern);
sdFormat.setLenient(true);
value = sdFormat.parse(dateValue);
@@ -395,10 +428,8 @@ public class DBTableColumn extends DBColumn
break;
case DECIMAL:
- if (value==null)
- break;
// check enum
- if (value!=null && value.getClass().isEnum())
+ if (value.getClass().isEnum())
{
value = ((Enum<?>)value).ordinal();
}
@@ -419,8 +450,6 @@ public class DBTableColumn extends DBColumn
break;
case FLOAT:
- if (value==null)
- break;
if (!(value instanceof java.lang.Number))
{ try
{ // Convert to String and check
@@ -437,10 +466,8 @@ public class DBTableColumn extends DBColumn
break;
case INTEGER:
- if (value==null)
- break;
// check enum
- if (value!=null && value.getClass().isEnum())
+ if (value.getClass().isEnum())
{
value = ((Enum<?>)value).ordinal();
}
@@ -463,11 +490,11 @@ public class DBTableColumn extends DBColumn
case TEXT:
case VARCHAR:
case CHAR:
- if (value!=null && value.getClass().isEnum())
+ if (value.getClass().isEnum())
{ // check enum
value = ((Enum<?>)value).name();
}
- if (value!=null && value.toString().length() > size)
+ if (value.toString().length() > size)
throw new FieldValueTooLongException(this);
break;