Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 1E1896330CA for ; Wed, 5 Aug 2009 07:53:22 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 72548-08 for ; Wed, 5 Aug 2009 10:53:05 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp23.orange.fr (smtp23.orange.fr [80.12.242.50]) by mail.postgresql.org (Postfix) with ESMTP id 3BA39632EFB for ; Wed, 5 Aug 2009 07:53:11 -0300 (ADT) Received: from smtp23.orange.fr (mwinf2342 [10.232.4.142]) by mwinf2305.orange.fr (SMTP Server) with ESMTP id DF8C01C08233 for ; Wed, 5 Aug 2009 12:01:49 +0200 (CEST) Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2342.orange.fr (SMTP Server) with ESMTP id B19891C00086 for ; Wed, 5 Aug 2009 12:01:42 +0200 (CEST) Received: from [10.129.36.10] (Mix-Rennes201-2-69.w193-250.abo.wanadoo.fr [193.250.24.69]) by mwinf2342.orange.fr (SMTP Server) with ESMTP id A8BA91C00082 for ; Wed, 5 Aug 2009 12:01:40 +0200 (CEST) X-ME-UUID: 20090805100141691.A8BA91C00082@mwinf2342.orange.fr Message-ID: <4A79587F.6010603@wanadoo.fr> Date: Wed, 05 Aug 2009 12:01:35 +0200 From: Sylvain Leroux Organization: http://www.chicoree.fr User-Agent: Thunderbird 2.0.0.22 (Macintosh/20090605) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Using BigInteger as argument to AbstractJdbc2Statement.setObject Content-Type: multipart/mixed; boundary="------------080902050805040100010403" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.299 tagged_above=-10 required=5 tests=AWL=1.300, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/7 X-Sequence-Number: 19856 This is a multi-part message in MIME format. --------------080902050805040100010403 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi, I am using Jython to access a PostgreSQL database throught the JDBC driver. I have a problem with a code fragment like that: > stmt = self.conn.prepareStatement("INSERT INTO campagne(id) VALUES(?)") > stmt.setObject(1, 123456899999) Of course, in the real code, the value 123456899999 is not hard wired. Anyway, Jython ``autobox'' such values into a java.math.BigInteger object. But AbstractJdbc2Statement.setObject does not handle that kind of object. Which leads to the error: > stmt.setObject(1, 123456899999) > at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1735) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) > at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) > at java.lang.reflect.Method.invoke(Method.java:597) > > org.postgresql.util.PSQLException: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of > java.math.BigInteger. Use setObject() with an explicit Types value to specify the type to use. Since the column campagne(id) is a NUMERIC, as the error message suggests, I could write something like that instead: > stmt.setObject(1, 123456899999, Types.NUMERIC) But this defeats the purpose of using a scripting language both for simplicity and dynamic typing. Moreover other scripting languages for the Java platform may exhibit the same behavior. So, it would be great if we have support for passing BigInteger as argument to the setObject method in prepared statements. Even if it's not required by the JDBC API. Best of all, the patch is trivial as you will see: I send the result of a ``cvs diff -u8pN'' as an attachment. Best regards, Sylvain Leroux -- Website: http://www.chicoree.fr --------------080902050805040100010403 Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0"; name="AbstractJdbc2Statement.java.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="AbstractJdbc2Statement.java.diff" Index: org/postgresql/jdbc2/AbstractJdbc2Statement.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v retrieving revision 1.114 diff -u -8 -p -r1.114 AbstractJdbc2Statement.java --- org/postgresql/jdbc2/AbstractJdbc2Statement.java 27 May 2009 23:55:19 -0000 1.114 +++ org/postgresql/jdbc2/AbstractJdbc2Statement.java 5 Aug 2009 09:30:00 -0000 @@ -1224,16 +1224,36 @@ public abstract class AbstractJdbc2State */ public void setDouble(int parameterIndex, double x) throws SQLException { checkClosed(); bindLiteral(parameterIndex, Double.toString(x), Oid.FLOAT8); } /* + * Set a parameter to a java.lang.BigInteger value. The driver + * converts this to a SQL NUMERIC value when it sends it to the + * database. + * + * This is an extension to the JDBC API! + * + * @param parameterIndex the first parameter is 1... + * @param x the parameter value + * @exception SQLException if a database access error occurs + */ + public void setBigInteger(int parameterIndex, BigInteger x) throws SQLException + { + checkClosed(); + if (x == null) + setNull(parameterIndex, Types.DECIMAL); + 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. * * @param parameterIndex the first parameter is 1... * @param x the parameter value * @exception SQLException if a database access error occurs */ @@ -1731,16 +1751,18 @@ public abstract class AbstractJdbc2State { checkClosed(); if (x == null) setNull(parameterIndex, Types.OTHER); else if (x instanceof String) 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) setInt(parameterIndex, ((Integer)x).intValue()); else if (x instanceof Long) setLong(parameterIndex, ((Long)x).longValue()); else if (x instanceof Float) setFloat(parameterIndex, ((Float)x).floatValue()); --------------080902050805040100010403--