Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 7D4416328EA for ; Thu, 6 Aug 2009 10:49:26 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 63124-06 for ; Thu, 6 Aug 2009 13:49:15 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp2f.orange.fr (smtp2f.orange.fr [80.12.242.150]) by mail.postgresql.org (Postfix) with ESMTP id 9A9D9632E1F for ; Thu, 6 Aug 2009 10:49:15 -0300 (ADT) Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2f02.orange.fr (SMTP Server) with ESMTP id 511ED80000A3 for ; Thu, 6 Aug 2009 15:49:12 +0200 (CEST) Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2f02.orange.fr (SMTP Server) with ESMTP id 4511580000A5 for ; Thu, 6 Aug 2009 15:49:12 +0200 (CEST) Received: from [10.129.36.10] (Mix-Rennes201-1-42.w193-250.abo.wanadoo.fr [193.250.26.42]) by mwinf2f02.orange.fr (SMTP Server) with ESMTP id 7543B80000A3 for ; Thu, 6 Aug 2009 15:49:11 +0200 (CEST) X-ME-UUID: 20090806134911480.7543B80000A3@mwinf2f02.orange.fr Message-ID: <4A7ADF53.2070203@wanadoo.fr> Date: Thu, 06 Aug 2009 15:49:07 +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: Re: Using BigInteger as argument to AbstractJdbc2Statement.setObject References: <4A79587F.6010603@wanadoo.fr> <4A79694F.9060404@opencloud.com> <4A798325.4030607@wanadoo.fr> <4A798682.9080204@opencloud.com> In-Reply-To: <4A798682.9080204@opencloud.com> Content-Type: multipart/mixed; boundary="------------000502090003080100070009" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.185 tagged_above=-10 required=5 tests=BAYES_40=-0.185 X-Spam-Level: X-Archive-Number: 200908/13 X-Sequence-Number: 19862 This is a multi-part message in MIME format. --------------000502090003080100070009 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit > 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 --------------000502090003080100070009 Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0"; name="pgjdbc-biginteger.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="pgjdbc-biginteger.patch" 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. + * + * 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 + */ + 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) --------------000502090003080100070009--