Re: Using BigInteger as argument to AbstractJdbc2Statement.setObject - Mailing list pgsql-jdbc

From Sylvain Leroux
Subject Re: Using BigInteger as argument to AbstractJdbc2Statement.setObject
Date
Msg-id 4A7ADF53.2070203@wanadoo.fr
Whole thread Raw
In response to Re: Using BigInteger as argument to AbstractJdbc2Statement.setObject  (Oliver Jowett <oliver@opencloud.com>)
Responses Re: Using BigInteger as argument to AbstractJdbc2Statement.setObject  (Kris Jurka <books@ejurka.com>)
List pgsql-jdbc
> So my concern with mapping Jython integer value -> BigInteger -> NUMERIC
> is that you end up with a statement parameter that's not actually an
> integer, and so "stmt.setObject(1,1234567899999)" will fail in cases
> where you would expect an integer value to work.
Ok, I understand.

>
> Selecting a target type based on the magnitude of the parameter value
> passed may work better; then at least you get obvious behavior for cases
> where the value can fit in an integer.
That's what I have done now: setBigInteger will choose the narrowest
integer representation. NUMERIC will still be used when no integer type
is wide enough to hold the value.

With this, I think we have coherent behavior between Jython and psql.


As a side note, MySQL JDBC Driver accepts BigInteger in a setObject call
too. But it takes less care of them, since they are simply mapped to
strings:
>         public void setObject(int parameterIndex, Object parameterObj)
>                         throws SQLException {
>     ...
>     ...
>                         } else if (parameterObj instanceof BigInteger) {
>                                 setString(parameterIndex, parameterObj.toString());


Sylvain


--
Website: http://www.chicoree.fr
Index: org/postgresql/jdbc2/AbstractJdbc2Statement.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v
retrieving revision 1.114
diff -u -r1.114 AbstractJdbc2Statement.java
--- org/postgresql/jdbc2/AbstractJdbc2Statement.java    27 May 2009 23:55:19 -0000    1.114
+++ org/postgresql/jdbc2/AbstractJdbc2Statement.java    6 Aug 2009 13:11:03 -0000
@@ -1229,6 +1229,35 @@
     }

     /*
+     * Set a parameter to a java.lang.BigInteger value.  The driver
+     * converts this to the narrowest integer representation available
+     * when it sends it to the database.
+     *
+     * <b>This is an extension to the JDBC API!</b>
+     *
+     * @param parameterIndex the first parameter is 1...
+     * @param x the parameter value
+     * @exception SQLException if a database access error occurs
+     */
+    private void setBigInteger(int parameterIndex, BigInteger x) throws SQLException
+    {
+        checkClosed();
+        if (x == null)
+            setNull(parameterIndex, Types.DECIMAL);
+        else {
+        int bl = x.bitLength();
+        if (bl < 16)
+        bindLiteral(parameterIndex, x.toString(), Oid.INT2);
+        else if (bl < 32)
+        bindLiteral(parameterIndex, x.toString(), Oid.INT4);
+        else if (bl < 64)
+        bindLiteral(parameterIndex, x.toString(), Oid.INT8);
+        else
+        bindLiteral(parameterIndex, x.toString(), Oid.NUMERIC);
+    }
+    }
+
+    /*
      * Set a parameter to a java.lang.BigDecimal value.  The driver
      * converts this to a SQL NUMERIC value when it sends it to the
      * database.
@@ -1736,6 +1765,8 @@
             setString(parameterIndex, (String)x);
         else if (x instanceof BigDecimal)
             setBigDecimal(parameterIndex, (BigDecimal)x);
+        else if (x instanceof BigInteger)
+            setBigInteger(parameterIndex, (BigInteger)x);
         else if (x instanceof Short)
             setShort(parameterIndex, ((Short)x).shortValue());
         else if (x instanceof Integer)

pgsql-jdbc by date:

Previous
From: "JUNG, Christian"
Date:
Subject: PATCH: SET ROLE as connection parameter
Next
From: dmp
Date:
Subject: Bytea Hex Format