From: Craig Ringer <ringerc@ringerc.id.au>
To: PostgreSQL JDBC <pgsql-jdbc@postgresql.org>
Subject: Re: setObject(...) with native Java arrays like String[] ?
Date: Wed, 22 Aug 2012 11:31:11 +0800
Message-ID: <5034527F.5040609@ringerc.id.au> (raw)
In-Reply-To: <50344D58.9010401@ringerc.id.au>
References: <50344D58.9010401@ringerc.id.au>
Here's a proposed patch to the tests to make the current behaviour explicit:
Add tests for String[] arrays
Shows that Connection.createArrayOf works, and that passing a raw
Java String[] to PreparedStatement.setObject() isn't accepted.
---
org/postgresql/test/jdbc2/ArrayTest.java | 31
+++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/org/postgresql/test/jdbc2/ArrayTest.java
b/org/postgresql/test/jdbc2/ArrayTest.java
index 16b0823..7b796be 100644inline patch 1: org/postgresql/test/jdbc2/ArrayTest.java (+4/-0 lines) download inline-1.patch--- a/org/postgresql/test/jdbc2/ArrayTest.java+++ b/org/postgresql/test/jdbc2/ArrayTest.java@@ -186,6 +186,37 @@ public class ArrayTest extends TestCase
assertEquals(3, resultCount);
}
+ public void testSetObjectFromJavaArray() throws SQLException {+ String[] strArray = new String[]{"a","b","c"};++ PreparedStatement pstmt = conn.prepareStatement("INSERT INTO arrtest(strarr) VALUES (?)");
+
+ // Incorrect, but commonly attempted by many ORMs:
+ try {
+ pstmt.setObject(1, strArray, Types.ARRAY);
+ pstmt.executeUpdate();
+ fail("setObject() with a Java array parameter and
Types.ARRAY shouldn't succeed");
+ } catch (org.postgresql.util.PSQLException ex) {
+ // Expected failure.
+ }
+
+ // Also incorrect, but commonly attempted by many ORMs:
+ try {
+ pstmt.setObject(1, strArray);
+ pstmt.executeUpdate();
+ fail("setObject() with a Java array parameter and no Types
argument shouldn't succeed");
+ } catch (org.postgresql.util.PSQLException ex) {
+ // Expected failure.
+ }
+
+ // Correct way, though the use of "text" as a type is non-portable.
+ Array sqlArray = conn.createArrayOf("text", strArray);
+ pstmt.setArray(1, sqlArray);
+ pstmt.executeUpdate();
+
+ pstmt.close();
+ }
+
/**
* Starting with 8.0 non-standard (beginning index isn't 1) bounds
* the dimensions are returned in the data. The following should
--
1.7.11.2
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: ringerc@ringerc.id.au
Subject: Re: setObject(...) with native Java arrays like String[] ?
In-Reply-To: <5034527F.5040609@ringerc.id.au>
References: <50344D58.9010401@ringerc.id.au> <5034527F.5040609@ringerc.id.au>
* 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