pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: John Lister <john.lister-ps@kickstone.com>
To: pgsql-jdbc@postgresql.org
Subject: Array helper patch
Date: Fri, 20 Jan 2012 19:32:26 +0000
Message-ID: <4F19C14A.2030704@kickstone.com> (raw)

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

Attachments:

pgjdbc.patch (text/plain, 5.0K) download 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<T>)"); } - + + 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<x.length;j++) + if (!o.getClass().equals(x[j].getClass())) + throw new PSQLException(GT.tr("Array has different types"), PSQLState.INVALID_PARAMETER_TYPE); + } else + o=x; + if (o instanceof Byte[] || o instanceof Byte) + typename = "int2"; + else if (o instanceof Short[] || o instanceof Short) + typename = "int2"; + else if (o instanceof Integer[] || o instanceof Integer) + typename = "int4"; + else if (o instanceof Long[] || o instanceof Long) + typename = "int8"; + else if (o instanceof Float[] || o instanceof Float) + typename = "float4"; + else if (o instanceof Double[] || o instanceof Double) + typename = "float8"; + else if (o instanceof Boolean[] || o instanceof Boolean) + typename = "bool"; + else if (o instanceof Character[] || o instanceof Character) + typename = "char"; + else + throw new PSQLException(GT.tr("Illegal array type ({0}), only builtins supported. Use setObject() with an explicit Types value to specify the type to use.", o.getClass().getName()), PSQLState.INVALID_PARAMETER_TYPE); + + setArray(i, this.connection.createArrayOf(typename, x)); + } + + private void setArray(int i, Object x) throws SQLException + { + checkClosed(); + + String typename=null; + Object[] newArray=null; + + if (x instanceof byte[]){ + typename = "int2"; + byte[] a=(byte[])x; + newArray=new Byte[a.length]; + for (int j=0;j<a.length;j++) + newArray[j]=a[j]; + } else if (x instanceof short[]){ + typename = "int2"; + short[] a=(short[])x; + newArray=new Short[a.length]; + for (int j=0;j<a.length;j++) + newArray[j]=a[j]; + } else if (x instanceof int[]){ + typename = "int4"; + int[] a=(int[])x; + newArray=new Integer[a.length]; + for (int j=0;j<a.length;j++) + newArray[j]=a[j]; + } else if (x instanceof long[]){ + typename = "int8"; + long[] a=(long[])x; + newArray=new Long[a.length]; + for (int j=0;j<a.length;j++) + newArray[j]=a[j]; + } else if (x instanceof float[]){ + typename = "float4"; + float[] a=(float[])x; + newArray=new Float[a.length]; + for (int j=0;j<a.length;j++) + newArray[j]=a[j]; + } else if (x instanceof double[]){ + typename = "float8"; + double[] a=(double[])x; + newArray=new Double[a.length]; + for (int j=0;j<a.length;j++) + newArray[j]=a[j]; + } else if (x instanceof boolean[]){ + typename = "bool"; + boolean[] a=(boolean[])x; + newArray=new Boolean[a.length]; + for (int j=0;j<a.length;j++) + newArray[j]=a[j]; + } else if (x instanceof char[]){ + typename = "char"; + char[] a=(char[])x; + newArray=new Character[a.length]; + for (int j=0;j<a.length;j++) + newArray[j]=a[j]; + } + setArray(i, this.connection.createArrayOf(typename, newArray)); + } }

Message-ID: <4F19C14A.2030704@kickstone.com>
Permalink:  ../4F19C14A.2030704@kickstone.com/
Also on:    postgresql.org/message-id/4F19C14A.2030704@kickstone.com

 ·  · 

reply

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: john.lister-ps@kickstone.com
  Subject: Re: Array helper patch
  In-Reply-To: <4F19C14A.2030704@kickstone.com>
  References: <4F19C14A.2030704@kickstone.com>

* 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