empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1378205 - in /empire-db/trunk/empire-db/src/main/java/org/apache/empire/db: DBDatabaseDriver.java expr/column/DBCalcExpr.java
Date Tue, 28 Aug 2012 16:23:55 GMT
Author: doebele
Date: Tue Aug 28 16:23:55 2012
New Revision: 1378205

URL: http://svn.apache.org/viewvc?rev=1378205&view=rev
Log:
EMPIREDB-164
new function getSQLNumberString() to validate numeric values encoded in sql commands

Modified:
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBCalcExpr.java

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java?rev=1378205&r1=1378204&r2=1378205&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java Tue
Aug 28 16:23:55 2012
@@ -660,10 +660,52 @@ public abstract class DBDatabaseDriver i
                 }
                 return getSQLPhrase((boolVal) ? SQL_BOOLEAN_TRUE : SQL_BOOLEAN_FALSE);
             }
+            case INTEGER:
+            case DECIMAL:
+            case FLOAT:
+                return getSQLNumberString(value, type);
+            case BLOB:
+                throw new NotSupportedException(this, "getValueString(?, DataType.BLOB)");

+            case AUTOINC:
+            case UNKNOWN:
+                /* Allow expressions */
+                return value.toString();
             default:
+                log.warn("Unknown DataType {} for getValueString().", type);
                 return value.toString();
         }
     }
+    
+    /**
+     * encodes a numeric value for an SQL command string. 
+     * @param value the numeric value
+     * @param type the number data type
+     * @return the string reprentation of the number
+     */
+    protected String getSQLNumberString(Object value, DataType type)
+    {
+        // already a number
+        if (value instanceof Number)
+            return value.toString();
+        
+        // check if it is a number
+        String s = value.toString();
+        boolean integerOnly = (type==DataType.INTEGER);
+        for (int i=0; i<s.length(); i++)
+        {
+            char c = s.charAt(i);
+            if (c>='0' && c<='9')
+                continue; // OK
+            if (c=='-' || c=='+')
+                continue; // OK
+            if (c==' ' && i>0)
+                return s.substring(0,i);
+            // check 
+            if (integerOnly || (c!='.' && c!=','))
+                throw new NumberFormatException(s);
+        }
+        return s;
+    }
 
     /**
      * encodes a Date value for an SQL command string. 

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBCalcExpr.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBCalcExpr.java?rev=1378205&r1=1378204&r2=1378205&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBCalcExpr.java
(original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/column/DBCalcExpr.java
Tue Aug 28 16:23:55 2012
@@ -19,6 +19,9 @@
 package org.apache.empire.db.expr.column;
 
 // Java
+import java.util.Date;
+import java.util.Set;
+
 import org.apache.empire.data.DataType;
 import org.apache.empire.db.DBColumn;
 import org.apache.empire.db.DBColumnExpr;
@@ -26,8 +29,6 @@ import org.apache.empire.db.DBDatabase;
 import org.apache.empire.db.DBExpr;
 import org.w3c.dom.Element;
 
-import java.util.Set;
-
 
 /**
  * This class is used for performing calculations in SQL<br>
@@ -79,7 +80,18 @@ public class DBCalcExpr extends DBColumn
     @Override
     public DataType getDataType()
     {
-        return DataType.DECIMAL;
+        DataType type = expr.getDataType();
+        // Special treatment for adding days to dates
+        if (type.isDate() && ((value instanceof Date) || value instanceof DBDatabase.DBSystemDate))
+            return DataType.DECIMAL;
+        if ((value instanceof DBColumnExpr))
+        {   // Use the value type?
+            DataType type2 =  ((DBColumnExpr)value).getDataType();
+            if (type2.isNumeric() && type2.ordinal()>type.ordinal())
+                return type2;
+        }
+        // type
+        return type;
     }
 
     /** Returns the given expression name. */
@@ -149,7 +161,12 @@ public class DBCalcExpr extends DBColumn
         // Zusammenbauen
         expr.addSQL(buf, context);
         buf.append(op);
-        buf.append(getObjectValue(getDataType(), value, context, op));
+        // Special treatment for adding days to dates
+        DataType type = expr.getDataType();
+        if (type.isNumeric()==false && (value instanceof Number))
+            type = DataType.DECIMAL;
+        // append
+        buf.append(getObjectValue(type, value, context, op));
     }
 
     @Override



Mime
View raw message