pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PreparedStatement and setting an array is now broken
2+ messages / 2 participants
[nested] [flat]

* PreparedStatement and setting an array is now broken
@ 2004-12-22 22:06  Andrew Lazarus <drlaz@attglobal.net>
  0 siblings, 1 reply; 2+ messages in thread

From: Andrew Lazarus @ 2004-12-22 22:06 UTC (permalink / raw)
  To: pgsql-jdbc

Until version 8 (now using build 308), I used a prepared statement

INSERT INTO mytable(array_column) VALUES(?);

and

setString(1, my_formatter(java_array));

where my_formatter turns a Java double[] into a string in the form {1.0} .

This doesn't work any more, with the error message that character 
varying can not be converted to real[] . I think it's with the driver, 
because the braces form still works fine with psql. I changed the 
formatter to produce a string in ARRAY[1.0] notation with the same error 
message.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: PreparedStatement and setting an array is now broken
@ 2004-12-22 23:33  Kris Jurka <books@ejurka.com>
  parent: Andrew Lazarus <drlaz@attglobal.net>
  0 siblings, 0 replies; 2+ messages in thread

From: Kris Jurka @ 2004-12-22 23:33 UTC (permalink / raw)
  To: Andrew Lazarus <drlaz@attglobal.net>; +Cc: pgsql-jdbc



On Wed, 22 Dec 2004, Andrew Lazarus wrote:

> Until version 8 (now using build 308), I used a prepared statement
> 
> INSERT INTO mytable(array_column) VALUES(?);
> 
> setString(1, my_formatter(java_array));
> 
> This doesn't work any more, with the error message that character 
> varying can not be converted to real[] . I think it's with the driver, 
> because the braces form still works fine with psql.

This is an expected error message.  The 8.0 driver uses strongly typed 
parameters.  By using setString you are claiming that you have varchar 
data.  The psql equivalent is:

The old driver:

jurka=# select '{1}'::int[];
 int4 
------
 {1}
(1 row)

The 8.0 driver:

jurka=# select '{1}'::varchar::int[];
ERROR:  cannot cast type character varying to integer[]

The correct JDBC solution is to use setArray() or setObject().  The
postgresql JDBC driver doesn't support using setObject on Java arrays, so
that's out.  The pg implementation of setArray is very fragile and
requires a specific java.sql.Array implementation.  So that's less than
ideal (especially considering the extra code/work on the client side to
construct such an array).

The move to strong typing, especially without providing workarounds (like 
this case), is definitely going to be a problem in the 8.0 release.  This 
is part of the growing pains the driver has to go through to use the V3 
protocol to its fullest.

Unfortunately your options at this point are:
 - Use the 7.4 driver.
 - Use the 8.0 with the protocolVersion=2 URL parameter.
 - Provide a java.sql.Array implementation
 - Add support for java arrays in setObject
   -  or complain loudly enough that someone else will

Kris Jurka



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2004-12-22 23:33 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-12-22 22:06 PreparedStatement and setting an array is now broken Andrew Lazarus <drlaz@attglobal.net>
2004-12-22 23:33 ` Kris Jurka <books@ejurka.com>

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