Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 199D261AFBE for ; Fri, 20 Jan 2012 15:32:44 -0400 (AST) Received: from mk-filter-3-a-1.mail.tiscali.co.uk ([212.74.100.54] helo=mk-filter-3-a-1.mail.uk.tiscali.com) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RoKCW-00022M-L8 for pgsql-jdbc@postgresql.org; Fri, 20 Jan 2012 19:32:45 +0000 X-Trace: 720733404/mk-filter-3.mail.uk.tiscali.com/B2C/$b2c-THROTTLED-DYNAMIC/b2c-CUSTOMER-DYNAMIC-IP/79.75.74.161/None/john.lister-ps@kickstone.com X-SBRS: None X-RemoteIP: 79.75.74.161 X-IP-MAIL-FROM: john.lister-ps@kickstone.com X-SMTP-AUTH: X-Originating-Country: GB/UNITED KINGDOM X-MUA: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 X-IP-BHB: Once X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApIBANXAGU9PS0qh/2dsb2JhbAAMN7FQLz0WGAMCAQIBWAgBAcBhjCYEjjaBGYsMjQk X-IronPort-AV: E=Sophos;i="4.71,544,1320624000"; d="scan'208";a="720733404" Received: from 79-75-74-161.dynamic.dsl.as9105.com (HELO [192.168.6.20]) ([79.75.74.161]) by smtp.tiscali.co.uk with ESMTP; 20 Jan 2012 19:32:26 +0000 Message-ID: <4F19C14A.2030704@kickstone.com> Date: Fri, 20 Jan 2012 19:32:26 +0000 From: John Lister Organization: Kickstone Technologies Ltd User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Array helper patch Content-Type: multipart/mixed; boundary="------------020706020503020704050804" X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/67 X-Sequence-Number: 22607 This is a multi-part message in MIME format. --------------020706020503020704050804 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit When using a JPA provider such as EclipseLink, it is useful to be able to pass an array of values as a parameter, but they don't easily expose the raw Connection object in order to use createArrayOf to create a JDBC array. I've therefore attached a simple patch that modifies JDBC4 statements to accept arrays of primitive objects (and their object form variants). Patched against latest cvs version. Hopefully fairly obvious what it does, if an array is passed to setObject then it looks up the base type (rejecting mixed type arrays) and calls createArrayOf with the corresponding type. If an array of primitive types is passed, it converts these to their object form first. None primitive based types are rejected as well. Thanks and comments John -- www.pricegoblin.co.uk --------------020706020503020704050804 Content-Type: text/plain; name="pgjdbc.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="pgjdbc.patch" Index: AbstractJdbc4Statement.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc4/AbstractJdbc4Statement.java,v retrieving revision 1.7 diff -u -r1.7 AbstractJdbc4Statement.java --- AbstractJdbc4Statement.java 2 Aug 2011 13:49:23 -0000 1.7 +++ AbstractJdbc4Statement.java 20 Jan 2012 19:05:26 -0000 @@ -40,11 +40,15 @@ public void setObject(int parameterIndex, Object x) throws SQLException { if (x instanceof SQLXML) - { setSQLXML(parameterIndex, (SQLXML)x); - } else { + else if (x instanceof List) + setArray(parameterIndex, (List)x); + else if (x instanceof Object[]) + setArray(parameterIndex, (Object[])x); + else if (x.getClass().isArray()) + setArray(parameterIndex, (Object)x); + else super.setObject(parameterIndex, x); - } } public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException @@ -391,5 +395,101 @@ { throw org.postgresql.Driver.notImplemented(this.getClass(), "getObject(String, Class)"); } - + + private void setArray(int i, List x) throws SQLException { + setArray(i, x.toArray()); + } + + private void setArray(int i, Object[] x) throws SQLException { + String typename=null; + Object o; + if (x.getClass().equals(Object[].class)) + { + // make sure all values of the same type + o=x[0]; + for (int j=1;j