Changeset: 78e62eb2c684 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=78e62eb2c684
Modified Files:
        java/ChangeLog.Feb2013
        java/src/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
        java/tests/Test_PStypes.java
Branch: Feb2013
Log Message:

setBigDecimal: properly format input for server

The server returns odd and less odd error messages when we send
mismatching data, e.g. for DECIMAL(2,1):
  111111.0  -> decimal doesn't match decimal
  0.111111  -> float doesn't match decimal
so make sure we properly chop off excess data, and generate a meaningful
error when we detect we'll be sending a too large number to the server.


diffs (63 lines):

diff --git a/java/ChangeLog.Feb2013 b/java/ChangeLog.Feb2013
--- a/java/ChangeLog.Feb2013
+++ b/java/ChangeLog.Feb2013
@@ -2,6 +2,8 @@
 # This file is updated with Maddlog
 
 * Thu May 23 2013 Fabian Groffen <fab...@monetdb.org>
+- Fixed bug where PreparedStatement.setBigDecimal() wouldn't format its
+  input well enough for the server causing odd errors.
 - Allow PreparedStatement.setXXX() methods to be called with null
   arguments, bug #3288
 
diff --git a/java/src/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java 
b/java/src/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
--- a/java/src/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
+++ b/java/src/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
@@ -951,14 +951,25 @@ public class MonetPreparedStatement
         * The driver converts this to an SQL NUMERIC value when it sends it to 
the
         * database.
         *
-        * @param parameterIndex the first parameter is 1, the second is 2, ...
+        * @param i the first parameter is 1, the second is 2, ...
         * @param x the parameter value
         * @throws SQLException if a database access error occurs
         */
-       public void setBigDecimal(int parameterIndex, BigDecimal x)
+       public void setBigDecimal(int i, BigDecimal x)
                throws SQLException
        {
-               setValue(parameterIndex, x.toString());
+               // if we don't give the server the exact digits/scale thing, it
+               // barfs at us that we don't give it a correct value, so...
+               String ps = x.toPlainString();
+               // chop off excess "precision"
+               int di = ps.indexOf(".");
+               if (di >= 0 && ps.length() - di - 1 > scale[i])
+                       ps = ps.substring(0, di + scale[i] + (scale[i] == 0 ? 0 
: 1));
+               if (di < 0)
+                       di = ps.length();
+               if (di > (digits[i] - scale[i]))
+                       throw new SQLDataException("DECIMAL value exceeds 
allowed digits/scale: " + ps + " (" + digits[i] + "/" + scale[i] + ")", 
"22003");
+               setValue(i, ps);
        }
 
        /**
diff --git a/java/tests/Test_PStypes.java b/java/tests/Test_PStypes.java
--- a/java/tests/Test_PStypes.java
+++ b/java/tests/Test_PStypes.java
@@ -76,12 +76,13 @@ public class Test_PStypes {
 
                        // try an update like bug #1757923
                        pstmt = con.prepareStatement(
-"UPDATE HTMTEST set COMMENT=? WHERE HTMID=?"
+"UPDATE HTMTEST set COMMENT=?, TYPE=? WHERE HTMID=?"
 );
                        System.out.print("2. updating record...");
 
                        pstmt.setString(1, "some update");
-                       pstmt.setLong(2, 1L);
+                       pstmt.setObject(2, (float)3.2);
+                       pstmt.setLong(3, 1L);
                        pstmt.executeUpdate();
 
                        System.out.println("success :)");
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to