Changeset: c37a76cc1e6e for monetdb-java
URL: http://dev.monetdb.org/hg/monetdb-java?cmd=changeset;node=c37a76cc1e6e
Modified Files:
        ChangeLog
        src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
Branch: default
Log Message:

Corrected ResultSet methods: getByte(), getBigDecimal(), getShort(), getInt(), 
getLong(), getFloat() and getDouble()
 in case the conversion to the native type failed due to a Number Format 
conversion error.
It used to silently ignore the conversion error and return 0 instead, which is 
not correct.
Now it throws an SQLException with message "Could not convert value to a 
number."
 and SQLstate "22003" meaning: Numeric value out of range.


diffs (180 lines):

diff --git a/ChangeLog b/ChangeLog
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,15 @@
 # ChangeLog file for java
 # This file is updated with Maddlog
 
+* Thu Sep 29 2016 Martin van Dinther <[email protected]>
+- Corrected ResultSet methods: getByte(), getBigDecimal(), getShort(),
+  getInt(), getLong(), getFloat() and getDouble() in case the conversion
+  to the native type failed due to a Number Format conversion error.
+  It used to silently ignore the conversion error and return 0 instead,
+  which is not correct.  Now it throws an SQLException with message
+  "Could not convert value to a number." and SQLstate "22003" meaning:
+  Numeric value out of range.
+
 * Thu Sep 22 2016 Martin van Dinther <[email protected]>
 - Improved JdbcClient program when doing dump of table definition.  It now
   outputs CREATE TABLE definition more similar to mclient program.
diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java 
b/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
@@ -669,11 +669,9 @@ public class MonetResultSet extends Mone
                                return null;
                        }
                        lastReadWasNull = false;
-                       try {
-                               return new BigDecimal(val);
-                       } catch (NumberFormatException e) {
-                               return BigDecimal.ZERO;
-                       }
+                       return new BigDecimal(val);
+               } catch (NumberFormatException e) {
+                       throw newSQLNumberFormatException(e);
                } catch (IndexOutOfBoundsException e) {
                        throw newSQLInvalidColumnIndexException(columnIndex);
                }
@@ -701,13 +699,12 @@ public class MonetResultSet extends Mone
                                return null;
                        }
                        lastReadWasNull = false;
-                       try {
-                               BigDecimal bd = new BigDecimal(val);
-                               bd.setScale(scale);
-                               return bd;
-                       } catch (NumberFormatException e) {
-                               return BigDecimal.ZERO;
-                       }
+
+                       BigDecimal bd = new BigDecimal(val);
+                       bd.setScale(scale);
+                       return bd;
+               } catch (NumberFormatException e) {
+                       throw newSQLNumberFormatException(e);
                } catch (IndexOutOfBoundsException e) {
                        throw newSQLInvalidColumnIndexException(columnIndex);
                }
@@ -846,12 +843,9 @@ public class MonetResultSet extends Mone
                                return (byte) 0;
                        }
                        lastReadWasNull = false;
-                       try {
-                               return Byte.parseByte(val);
-                       } catch (NumberFormatException e) {
-                               // ignore parse error, return the default: 0
-                               return (byte) 0;
-                       }
+                       return Byte.parseByte(val);
+               } catch (NumberFormatException e) {
+                       throw newSQLNumberFormatException(e);
                } catch (IndexOutOfBoundsException e) {
                        throw newSQLInvalidColumnIndexException(columnIndex);
                }
@@ -994,12 +988,9 @@ public class MonetResultSet extends Mone
                                return 0;
                        }
                        lastReadWasNull = false;
-                       try {
-                               return Double.parseDouble(val);
-                       } catch (NumberFormatException e) {
-                               // ignore conversion error, return the default: 0
-                               return 0;
-                       }
+                       return Double.parseDouble(val);
+               } catch (NumberFormatException e) {
+                       throw newSQLNumberFormatException(e);
                } catch (IndexOutOfBoundsException e) {
                        throw newSQLInvalidColumnIndexException(columnIndex);
                }
@@ -1117,12 +1108,9 @@ public class MonetResultSet extends Mone
                                return 0;
                        }
                        lastReadWasNull = false;
-                       try {
-                               return Float.parseFloat(val);
-                       } catch (NumberFormatException e) {
-                               // ignore conversion error, return the default: 0
-                               return 0;
-                       }
+                       return Float.parseFloat(val);
+               } catch (NumberFormatException e) {
+                       throw newSQLNumberFormatException(e);
                } catch (IndexOutOfBoundsException e) {
                        throw newSQLInvalidColumnIndexException(columnIndex);
                }
@@ -1160,12 +1148,9 @@ public class MonetResultSet extends Mone
                                return 0;
                        }
                        lastReadWasNull = false;
-                       try {
-                               return Integer.parseInt(val);
-                       } catch (NumberFormatException e) {
-                               // ignore conversion error, return the default: 0
-                               return 0;
-                       }
+                       return Integer.parseInt(val);
+               } catch (NumberFormatException e) {
+                       throw newSQLNumberFormatException(e);
                } catch (IndexOutOfBoundsException e) {
                        throw newSQLInvalidColumnIndexException(columnIndex);
                }
@@ -1211,12 +1196,9 @@ public class MonetResultSet extends Mone
                                if (len > 2 && val.endsWith("@0"))
                                        val = val.substring(0, len-2);
                        }
-                       try {
-                               return Long.parseLong(val);
-                       } catch (NumberFormatException e) {
-                               // ignore conversion error, return the default: 0
-                               return 0;
-                       }
+                       return Long.parseLong(val);
+               } catch (NumberFormatException e) {
+                       throw newSQLNumberFormatException(e);
                } catch (IndexOutOfBoundsException e) {
                        throw newSQLInvalidColumnIndexException(columnIndex);
                }
@@ -2413,12 +2395,9 @@ public class MonetResultSet extends Mone
                                return 0;
                        }
                        lastReadWasNull = false;
-                       try {
-                               return Short.parseShort(val);
-                       } catch (NumberFormatException e) {
-                               // ignore conversion error, return the default: 0
-                               return 0;
-                       }
+                       return Short.parseShort(val);
+               } catch (NumberFormatException e) {
+                       throw newSQLNumberFormatException(e);
                } catch (IndexOutOfBoundsException e) {
                        throw newSQLInvalidColumnIndexException(columnIndex);
                }
@@ -3724,7 +3703,7 @@ public class MonetResultSet extends Mone
         * Small helper method that formats the "Invalid Column Index number 
..." message
         * and creates a new SQLException object whose SQLState is set to 
"M1M05".
         *
-        * @param name the method name
+        * @param colIdx the column index numberr
         * @return a new created SQLException object with SQLState M1M05
         */
        private final static SQLException newSQLInvalidColumnIndexException(int 
colIdx) {
@@ -3732,6 +3711,17 @@ public class MonetResultSet extends Mone
        }
 
        /**
+        * Small helper method that formats the "Could not convert value to a 
number" message
+        * and creates a new SQLException object whose SQLState is set to 
"22003": Numeric value out of range.
+        *
+        * @param error the NumberFormatException
+        * @return a new created SQLException object with SQLState 22003
+        */
+       private final static SQLException 
newSQLNumberFormatException(NumberFormatException error) {
+               return new SQLException("Could not convert value to a number. " 
+ error.getMessage(), "22003");
+       }
+
+       /**
         * Small helper method that formats the "Method ... not implemented" 
message
         * and creates a new SQLFeatureNotSupportedException object
         * whose SQLState is set to "0A000".
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to