[PATCH] Ability to PreparedStatement.setObject(#, int[])

From: Doug Fields <dfields-postgres(at)pexicom(dot)com>
To: pgsql-patches(at)postgresql(dot)org, pgsql-jdbc(at)postgresql(dot)org
Subject: [PATCH] Ability to PreparedStatement.setObject(#, int[])
Date: 2002-02-21 01:39:35
Message-ID: 5.1.0.14.2.20020220203511.03550840@mail.pexicom.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc pgsql-patches

Hello,

(Find Unified Context Diff below against 7.2 shipping sources)

Currently there is no way to set an int[] object to an INTEGER[] in
Postgres with JDBC, unless you create a string and do it. However, reading
p266-267 of JDBC API Tutorial and Reference, 2nd Ed by White, Fisher,
Cattell, Hamilton, Hapner, indicates that we should be able to setObject it
and have it work.

The current 7.2 drivers do not support this functionality, that of calling
setObject() with an array. Instead, the driver tries to use object
serialization to store it.

In theory, any base type or String array should be converted to the
appropriate array format and stored in an array type in the database. I
have only patched it to recognize an int[] array to demonstrate what should
be done. If people wish, I can extend my patch to all base array types
other than byte (which is already handled).

Cheers,

Doug

diff -ru5 org-orig/postgresql/jdbc2/PreparedStatement.java
org/postgresql/jdbc2/PreparedStatement.java
--- org-orig/postgresql/jdbc2/PreparedStatement.java Tue Jan 15 02:37:33
2002
+++ org/postgresql/jdbc2/PreparedStatement.java Wed Feb 20 20:34:32 2002
@@ -754,11 +754,29 @@
setTimestamp(parameterIndex, (Timestamp)x);
else if (x instanceof Boolean)
setBoolean(parameterIndex,
((Boolean)x).booleanValue());
else if (x instanceof PGobject)
setString(parameterIndex, ((PGobject)x).getValue());
- else
+ else if (x instanceof int[]) {
+ /* Turn into a string and use setString instead.
+ * This should be used for other array types as well.
+ * See p266-267 of JDBC API Tutorial and Reference,
+ * 2nd Ed by White, Fisher, Cattell, Hamilton, Hapner
+ * for why this method should be done this way.
+ * Doug Fields <dfields-pg-jdbc(at)pexicom(dot)com>
+ * Feb 20, 2002 */
+ StringBuffer sb = new StringBuffer();
+ int[] y = (int[])x; // Ease of use
+ sb.append('{');
+ for (int i = 0; i < y.length; i++) {
+ if (i > 0)
+ sb.append(',');
+ sb.append(y[i]);
+ }
+ sb.append('}');
+ setString(parameterIndex, sb.toString());
+ } else
// Try to store java object in database
setSerialize(parameterIndex,
connection.storeObject(x), x.getClass().getName() );
}

/*

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Doug Fields 2002-02-21 02:28:18 [PATCH] Empty arrays cause SQLExceptions when using Array.getArray
Previous Message Barry Lind 2002-02-21 01:21:33 Re: [PATCHES] JDBC callable statement

Browse pgsql-patches by date

  From Date Subject
Next Message Paul Bethe 2002-02-21 20:00:29 Patch to JDBC Callable Stmt
Previous Message Barry Lind 2002-02-21 01:21:33 Re: [PATCHES] JDBC callable statement