empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1386661 - in /empire-db/trunk/empire-db/src/main/java/org/apache/empire/db: DBColumn.java DBTableColumn.java exceptions/FieldValueOutOfRangeException.java
Date Mon, 17 Sep 2012 15:32:23 GMT
Author: doebele
Date: Mon Sep 17 15:32:23 2012
New Revision: 1386661

URL: http://svn.apache.org/viewvc?rev=1386661&view=rev
Log:
EMPIREDB-157 
core: provide additional validity checks for numbers

Added:
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/exceptions/FieldValueOutOfRangeException.java
  (with props)
Modified:
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java?rev=1386661&r1=1386660&r2=1386661&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java Mon Sep 17
15:32:23 2012
@@ -28,7 +28,6 @@ import java.util.Set;
 import org.apache.empire.commons.Options;
 import org.apache.empire.commons.StringUtils;
 import org.apache.empire.data.Column;
-import org.apache.empire.data.DataType;
 import org.apache.empire.db.exceptions.DatabaseNotOpenException;
 import org.apache.empire.db.expr.set.DBSetExpr;
 import org.apache.empire.exceptions.InvalidArgumentException;

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java?rev=1386661&r1=1386660&r2=1386661&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java Mon Sep
17 15:32:23 2012
@@ -18,6 +18,7 @@
  */
 package org.apache.empire.db;
 
+import java.math.BigDecimal;
 import java.sql.Connection;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -29,6 +30,7 @@ import org.apache.empire.data.DataMode;
 import org.apache.empire.data.DataType;
 import org.apache.empire.db.exceptions.FieldIllegalValueException;
 import org.apache.empire.db.exceptions.FieldNotNullException;
+import org.apache.empire.db.exceptions.FieldValueOutOfRangeException;
 import org.apache.empire.db.exceptions.FieldValueTooLongException;
 import org.apache.empire.exceptions.InvalidArgumentException;
 import org.apache.empire.exceptions.NotSupportedException;
@@ -344,29 +346,48 @@ public class DBTableColumn extends DBCol
                 break;
 
             case DECIMAL:
+                if (value==null)
+                    break;
+                if (!(value instanceof java.lang.Number))
+                {   try
+                    {   // Convert to String and check
+                        value = ObjectUtils.toDecimal(value);
+                        // throws NumberFormatException if not a number!
+                    } catch (NumberFormatException e)
+                    {
+                        log.info("checkValue failed: " + e.toString() + " column=" + getName()
+ " value=" + value);
+                        throw new FieldIllegalValueException(this, String.valueOf(value),
e);
+                    }
+                }
+                // validate Number
+                validateNumber(type, (Number)value);
+                break;
+
             case FLOAT:
-                if (value!=null && (value instanceof java.lang.Number)==false)
+                if (value==null)
+                    break;
+                if (!(value instanceof java.lang.Number))
                 {   try
                     {   // Convert to String and check
-                        String val = value.toString();
-                        if (val.length() > 0)
-                            Double.parseDouble(val);
-                        // thows NumberFormatException if not a number!
+                        value = ObjectUtils.toDouble(value);
+                        // throws NumberFormatException if not a number!
                     } catch (NumberFormatException e)
                     {
                         log.info("checkValue failed: " + e.toString() + " column=" + getName()
+ " value=" + value);
                         throw new FieldIllegalValueException(this, String.valueOf(value),
e);
                     }
                 }
+                // validate Number
+                validateNumber(type, (Number)value);
                 break;
 
             case INTEGER:
-                if (value!=null && (value instanceof java.lang.Number)==false)
+                if (value==null)
+                    break;
+                if (!(value instanceof java.lang.Number))
                 {   try
                     {   // Convert to String and check
-                        String val = value.toString();
-                        if (val.length() > 0)
-                            Long.parseLong(val);
+                        value = ObjectUtils.toLong(value);
                         // throws NumberFormatException if not an integer!
                     } catch (NumberFormatException e)
                     {
@@ -374,6 +395,8 @@ public class DBTableColumn extends DBCol
                         throw new FieldIllegalValueException(this, String.valueOf(value),
e);
                     }
                 }
+                // validate Number
+                validateNumber(type, (Number)value);
                 break;
 
             case TEXT:
@@ -389,6 +412,51 @@ public class DBTableColumn extends DBCol
 
         }
     }
+    
+    protected void validateNumber(DataType type, Number n)
+    {
+        if (type==DataType.DECIMAL)
+        {   // Convert to Decimal
+            BigDecimal dv = ObjectUtils.toDecimal(n);
+            int prec = dv.precision();
+            int scale = dv.scale();
+            // check precision and scale
+            double size = getSize();
+            int reqPrec = (int)size;
+            int reqScale =((int)(size*10)-(reqPrec*10));
+            if ((prec-scale)>(reqPrec-reqScale) || scale>reqScale)
+                throw new FieldValueOutOfRangeException(this);
+        }
+        // Check Range
+        Object min = getAttribute(DBColumn.DBCOLATTR_MINVALUE);
+        Object max = getAttribute(DBColumn.DBCOLATTR_MAXVALUE);
+        if (min!=null && max!=null)
+        {   // Check Range
+            int minVal = ObjectUtils.getInteger(min);
+            int maxVal = ObjectUtils.getInteger(max);
+            if (n.intValue()<minVal || n.intValue()>maxVal)
+            {   // Out of Range
+                throw new FieldValueOutOfRangeException(this, minVal, maxVal);
+            }
+        }
+        else if (min!=null)
+        {   // Check Min Value
+            int minVal = ObjectUtils.getInteger(min);
+            if (n.intValue()<minVal)
+            {   // Out of Range
+                throw new FieldValueOutOfRangeException(this, minVal, false);
+            }
+        }
+        else if (max!=null)
+        {   // Check Max Value
+            int maxVal = ObjectUtils.getInteger(max);
+            if (n.intValue()>maxVal)
+            {   // Out of Range
+                throw new FieldValueOutOfRangeException(this, maxVal, true);
+            }
+        }
+            
+    }
 
     /**
      * Creates a foreign key relation for this column.

Added: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/exceptions/FieldValueOutOfRangeException.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/exceptions/FieldValueOutOfRangeException.java?rev=1386661&view=auto
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/exceptions/FieldValueOutOfRangeException.java
(added)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/exceptions/FieldValueOutOfRangeException.java
Mon Sep 17 15:32:23 2012
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.empire.db.exceptions;
+
+import org.apache.empire.commons.ErrorType;
+import org.apache.empire.data.Column;
+
+public class FieldValueOutOfRangeException extends FieldValueException
+{
+    /**
+     * Comment for <code>serialVersionUID</code>
+     */
+    private static final long serialVersionUID = 1L;
+    
+    public static final ErrorType outOfRangeErrorType      = new ErrorType("error.db.fieldValueOutOfRange",
  "The value supplied for field {0} is out of range.");
+    public static final ErrorType notInRangeErrorType      = new ErrorType("error.db.fieldValueNotInRagen",
  "The value supplied for field {0} must be between {1} and {2}.");
+    public static final ErrorType valueTooBigErrorType     = new ErrorType("error.db.fieldValueTooBig",
      "The value supplied for field {0} must be less or equal {1}.");
+    public static final ErrorType valueTooSmallErrorType   = new ErrorType("error.db.fieldValueTooSmall",
    "The value supplied for field {0} must be more or equal {1}.");
+    
+    public FieldValueOutOfRangeException(Column col)
+    {
+        super(col, outOfRangeErrorType, new String[] { getColumnTitle(col) });
+    }
+
+    public FieldValueOutOfRangeException(Column col, Number min, Number max)
+    {
+        super(col, notInRangeErrorType, new String[] { getColumnTitle(col), String.valueOf(min),
String.valueOf(max) });
+    }
+
+    public FieldValueOutOfRangeException(Column col, Number minMax, boolean isMax)
+    {
+        super(col, (isMax) ? valueTooBigErrorType : valueTooSmallErrorType, new String[]
{ getColumnTitle(col), String.valueOf(minMax) });
+    }
+}

Propchange: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/exceptions/FieldValueOutOfRangeException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message