Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T41f4-0000vG-Qh for pgsql-jdbc@postgresql.org; Wed, 22 Aug 2012 03:31:18 +0000 Received: from mail-pb0-f46.google.com ([209.85.160.46]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T41f3-0008Ig-HZ for pgsql-jdbc@postgresql.org; Wed, 22 Aug 2012 03:31:18 +0000 Received: by pbbrr13 with SMTP id rr13so843923pbb.19 for ; Tue, 21 Aug 2012 20:31:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=DpJ/AhkNsx64HjfFBtaPR7uNTQGzCuFC1u4PhQ3EZzw=; b=nPlHOgFO6uzkdWbIb4KS2hUAvvR6vRdNoQTyyq8D+tO5AUA84h39JjX8b5p1EgtnA5 2tV4WR/mXlqAkDVqjwdja0/FsRDK5kzEaaqJMJP2HQe/tciKjviIxWYuFs06QQVb2OL3 RZ3zhJsSCeLLQKtFPA1PbniUG3cBxLLFy5CIfeMvD+jSQU6hr+ZpmC+vCpQeLEUlrPD1 92djv5Sz8UUkawsWuKGTrsok6e/rZ0DmpIE8iQTXeqJ5QGRSyWVLeTKK+rLwOvzND4BR Lf9MY0ToiRwOjMWZBQOlB65pLcj6sz+mfteAP8jsuwBVnthZQknRaSY7cu0+cLbK/rMz 8SOA== Received: by 10.68.238.166 with SMTP id vl6mr48692149pbc.96.1345606276318; Tue, 21 Aug 2012 20:31:16 -0700 (PDT) Received: from ayaki.localdomain (office1.postnewspapers.com.au. [150.101.171.128]) by mx.google.com with ESMTPS id nr2sm2742562pbc.48.2012.08.21.20.31.13 (version=SSLv3 cipher=OTHER); Tue, 21 Aug 2012 20:31:15 -0700 (PDT) Message-ID: <5034527F.5040609@ringerc.id.au> Date: Wed, 22 Aug 2012 11:31:11 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120717 Thunderbird/14.0 MIME-Version: 1.0 To: PostgreSQL JDBC Subject: Re: setObject(...) with native Java arrays like String[] ? References: <50344D58.9010401@ringerc.id.au> In-Reply-To: <50344D58.9010401@ringerc.id.au> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQlxkq+ozvws78t/4FKmmVtmqDhWG6dyg4dLpoQFW2hAQHw3mJq8U82lHarZXJjvMI8SKHmH X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201208/54 X-Sequence-Number: 23209 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 100644 --- 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