pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sylvain Leroux <sl20@wanadoo.fr>
To: pgsql-jdbc@postgresql.org
Subject: Re: Using BigInteger as argument to AbstractJdbc2Statement.setObject
Date: Thu, 06 Aug 2009 15:49:07 +0200
Message-ID: <4A7ADF53.2070203@wanadoo.fr> (raw)
In-Reply-To: <4A798682.9080204@opencloud.com>
References: <4A79587F.6010603@wanadoo.fr>
	<4A79694F.9060404@opencloud.com>
	<4A798325.4030607@wanadoo.fr>
	<4A798682.9080204@opencloud.com>


> 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

Attachments:

pgjdbc-biginteger.patch (text/plain, 2.0K) download 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)

view thread (7+ messages)  latest in thread

Message-ID: <4A7ADF53.2070203@wanadoo.fr>
Permalink:  ../4A7ADF53.2070203@wanadoo.fr/
Also on:    postgresql.org/message-id/4A7ADF53.2070203@wanadoo.fr

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: sl20@wanadoo.fr
  Subject: Re: Using BigInteger as argument to AbstractJdbc2Statement.setObject
  In-Reply-To: <4A7ADF53.2070203@wanadoo.fr>
  References: <4A79587F.6010603@wanadoo.fr> <4A79694F.9060404@opencloud.com> <4A798325.4030607@wanadoo.fr> <4A798682.9080204@opencloud.com> <4A7ADF53.2070203@wanadoo.fr>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox