X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5FC9D32A111; Tue, 5 Oct 2004 17:25:51 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 67333-04; Tue, 5 Oct 2004 16:25:47 +0000 (GMT) Received: from dns.cegetel-passerelle.com (dns.cegetel-passerelle.com [213.223.187.134]) by svr1.postgresql.org (Postfix) with ESMTP id A930E32A0FE; Tue, 5 Oct 2004 17:25:40 +0100 (BST) Received: from escmail.esc-lille.fr (unknown [213.223.237.160]) by dns.cegetel-passerelle.com (Postfix) with ESMTP id 0AD462533A; Tue, 5 Oct 2004 18:25:36 +0200 (CEST) Received: from srvmail.esc-lille.fr (srvmail.esc-lille.fr [10.1.10.5]) by escmail.esc-lille.fr (Postfix) with ESMTP id C1DEA67C134; Tue, 5 Oct 2004 18:24:43 +0200 (CEST) Received: from Johann ([10.10.1.165]) by srvmail.esc-lille.fr with Microsoft SMTPSVC(5.0.2195.6713); Tue, 5 Oct 2004 18:25:35 +0200 From: "Johann Robette" To: Cc: Subject: Postgres 8.0 + JDBC Date: Tue, 5 Oct 2004 18:25:52 +0200 Message-ID: <002901c4aaf7$feabc310$a5010a0a@Johann> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_002A_01C4AB08.C2349310" X-Priority: 1 (Highest) X-MSMail-Priority: High X-Mailer: Microsoft Outlook, Build 10.0.2627 Importance: High X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 X-OriginalArrivalTime: 05 Oct 2004 16:25:35.0319 (UTC) FILETIME=[F12E1670:01C4AAF7] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.8 tagged_above=0.0 required=5.0 tests=HTML_30_40, HTML_MESSAGE, X_MSMAIL_PRIORITY_HIGH, X_PRIORITY_HIGH X-Spam-Level: * X-Archive-Number: 200410/184 X-Sequence-Number: 66709 This is a multi-part message in MIME format. ------=_NextPart_000_002A_01C4AB08.C2349310 Content-Type: multipart/alternative; boundary="----=_NextPart_001_002B_01C4AB08.C2349310" ------=_NextPart_001_002B_01C4AB08.C2349310 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hello, I have an application running under JBoss. Up to today, I was using Postgres 7.3 and the appropriate version of the jdbc driver. In my application, I have to call a user-defined function which accept in parameters 2 arrays. Here is the header of my function : CREATE OR REPLACE FUNCTION getmembers(int8, int8, _text, _float8) So I called it using a prepared statement with setArray() : double[] weights = {0.5}; String[] names = {"foo1", "foo2"}; java.sql.Array a_names = PostgresArray.create(names); java.sql.Array a_weights = PostgresArray.create(weights); ps = conn.prepareStatement("SELECT * FROM getmembers(?,?,?::_text,?::_float8);"); ps.setLong(1, 1); ps.setLong(2, 2); ps.setArray(3, a_names); ps.setArray(4, a_weights); ps.executeQuery(); PostgresArray is a class which I found on the archives.postgresql.org. The code is given is attached. All worked fine. But today, I decided to upgrade to Postgres 8.0 beta 3. No problem with the definition of my function. I downloaded the appropriate JDBC driver : pgdev.306.jdbc3.jar. Now running the same code as before, I get the error while executing the query : java.sql.SQLException: ERROR: cannot cast type text to text[] So, what am I doing wrong? Is it a beta bug or is my code incorrect? What is the correct way to use SetArray()? Thanks JR ------=_NextPart_001_002B_01C4AB08.C2349310 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Hello,

 

I have an application ru= nning under JBoss.

Up to today, I was using= Postgres 7.3 and the appropriate version of the jdbc driver.

In my application, I hav= e to call a user-defined function which accept in parameters 2 arrays. Here is the header of my function = :

            =     CREATE OR REPLACE FUNCTION getmembers(int8, int8, _text, _float8)

So I called it using a prepared statement with setArray() :

            =     double[] weights =3D {0.5};

      String[] names =3D {“foo1”, “foo2= 221;};

      java.sql.Array a_names = =3D PostgresArray.create(names);

      java.sql.Array a_weights = =3D PostgresArray.create(weights);

      ps =3D conn.prepareStatement("SELECT * FROM getmembers(?,?,?::_text,?::_float8);&quo= t;);

      ps.setLong(1, 1);

      ps.setLong(2, 2);

      ps.setArray(3, a_names);

     = ps.setArray(4, a_weights);=

      ps.executeQuery();

 =

PostgresArr= ay is a class which I found on the archives.postgresql.org. The code is given is attached.

 =

All worked = fine.

 =

But today, I decided to upgrade to Postgres 8.0 beta 3.

 =

No problem with the defi= nition of my function.

I downloaded the appropr= iate JDBC driver : pgdev.306.jdbc3.jar.

 =

Now running the same cod= e as before, I get the error while executing the query :

            =     java.sql.SQLExcep= tion: ERROR: cannot cast type text to text[]

 =

So, what am I doing wron= g?

Is it a beta bug or is m= y code incorrect?

What is the correct way = to use SetArray()?

 =

Thanks=

 =

JR

 =

------=_NextPart_001_002B_01C4AB08.C2349310-- ------=_NextPart_000_002A_01C4AB08.C2349310 Content-Type: text/plain; name="PostgresArray.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="PostgresArray.txt" package com.postgresql; import java.math.BigDecimal; import java.sql.*; import java.util.ArrayList; import java.util.Map; /* * Array is used collect one column of query result data. * *

Read a field of type Array into either a natively-typed * Java array object or a ResultSet. Accessor methods provide * the ability to capture array slices. * *

Other than the constructor all methods are direct implementations * of those specified for java.sql.Array. Please refer to the javadoc * for java.sql.Array for detailed descriptions of the functionality * and parameters of the methods of this class. * * This class stolen from postgresql 7.2.1's source tree!!! * * @see ResultSet#getArray * */ public class PostgresArray implements Array { private String rawString =3D null; private String typeName =3D null; private int baseType =3D -1; private static final String jdbc2Types[] =3D { "int2", "int4", "oid", "int8", "cash", "money", "numeric", "float4", "float8", "bpchar", "char", "char2", "char4", "char8", "char16", "varchar", "text", "name", "filename", "bytea", "bool", "date", "time", "abstime", "timestamp", "timestamptz", "_bool", "_char", "_int2", "_int4", "_text", "_oid", "_varchar", "_int8", "_float4", "_float8", "_abstime", "_date", "_time", "_timestamp", "_numeric", "_bytea" }; /* * This table holds the JDBC type for each entry above. * * Note: This must be in the same order as above * * Tip: keep these grouped together by the Types. value */ private static final int jdbc2Typei[] =3D { Types.SMALLINT, Types.INTEGER, Types.INTEGER, Types.BIGINT, Types.DOUBLE, Types.DOUBLE, Types.NUMERIC, Types.REAL, Types.DOUBLE, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.C= HAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BINARY, Types.BIT, Types.DATE, Types.TIME, Types.TIMESTAMP, Types.TIMESTAMP, Types.TIMESTAMP, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY }; /* * Create a new Array * * @param conn a database connection * @param idx 1-based index of the query field to load into this Ar= ray * @param field the Field descriptor for the field to load into thi= s Array * @param rs the ResultSet from which to get the data for this Array */ PostgresArray(String rawString, int baseType, String typeName) throws S= QLException { this.rawString =3D rawString; this.baseType =3D baseType; this.typeName =3D typeName; if (this.typeName.startsWith("_")) { this.typeName =3D this.typeName.substring(1); } } public static Array create(Object[] array) throws SQLException { throw new SQLException("Not Implemented"); } public static Array create(int[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.INTEGER, "int"); } public static Array create(boolean[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.BIT, "boolean"); } public static Array create(long[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.BIT, "bigint"); } public static Array create(BigDecimal[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.NUMERIC, "NUMERIC"); } public static Array create(float[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.REAL, "float"); } public static Array create(double[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.DOUBLE, "double"); } public static Array create(char[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.CHAR, "char"); } public static Array create(String[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.VARCHAR, "varchar"); } public static Array create(Date[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.DATE, "date"); } public static Array create(java.util.Date[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(new Date(array[x].getTime())).append("\"= "); } sb.append("}"); return new PostgresArray(sb.toString(), Types.DATE, "date"); } public static Array create(Time[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.TIME, "time"); } public static Array create(Timestamp[] array) throws SQLException { if (array =3D=3D null) return new PostgresArray(null, -1, null); StringBuffer sb =3D new StringBuffer("{"); for (int x =3D 0; x < array.length; x++) { if (x > 0) sb.append(","); sb.append("\"").append(array[x]).append("\""); } sb.append("}"); return new PostgresArray(sb.toString(), Types.TIMESTAMP, "timestamp= "); } public Object getArray() throws SQLException { return getArray(1, 0, null); } public Object getArray(long index, int count) throws SQLException { return getArray(index, count, null); } public Object getArray(Map map) throws SQLException { return getArray(1, 0, map); } public Object getArray(long index, int count, Map map) throws SQLExcept= ion { /*if (map !=3D null) // For now maps aren't supported. throw new SQLException("Maps are not supported in Array.getArra= y"); if (index < 1) throw new SQLException("index < 1"); Object retVal =3D null; ArrayList array =3D new ArrayList(); if (rawString !=3D null) { char[] chars =3D rawString.toCharArray(); StringBuffer sbuf =3D new StringBuffer(); boolean foundOpen =3D false; boolean insideString =3D false; for (int i =3D 0; i < chars.length; i++) { if (chars[i] =3D=3D '{') { if (foundOpen) // Only supports 1-D arrays for now throw org.postgresql.Driver.notImplemented(); foundOpen =3D true; continue; } if (chars[i] =3D=3D '"') { insideString =3D !insideString; continue; } if ((!insideString && chars[i] =3D=3D ',') || chars[i] =3D= =3D '}' || i =3D=3D chars.length - 1) { if (chars[i] !=3D '"' && chars[i] !=3D '}' && chars[i] = !=3D ',') sbuf.append(chars[i]); array.add(sbuf.toString()); sbuf =3D new StringBuffer(); continue; } sbuf.append(chars[i]); } } String[] arrayContents =3D (String[]) array.toArray(new String[arra= y.size()]); if (count =3D=3D 0) count =3D arrayContents.length; index--; if (index + count > arrayContents.length) throw new SQLException("index + count > arrayContents"); int i =3D 0; switch (getBaseType()) { case Types.BIT: retVal =3D new boolean[count]; for (; count > 0; count--) ((boolean[]) retVal)[i++] =3D ResultSet_ClientSide.toBo= olean(arrayContents[(int) index++]); break; case Types.SMALLINT: case Types.INTEGER: retVal =3D new int[count]; for (; count > 0; count--) ((int[]) retVal)[i++] =3D ResultSet_ClientSide.toInt(ar= rayContents[(int) index++]); break; case Types.BIGINT: retVal =3D new long[count]; for (; count > 0; count--) ((long[]) retVal)[i++] =3D ResultSet_ClientSide.toLong(= arrayContents[(int) index++]); break; case Types.NUMERIC: retVal =3D new BigDecimal[count]; for (; count > 0; count--) ((BigDecimal[]) retVal)[i] =3D ResultSet_ClientSide.toB= igDecimal(arrayContents[(int) index++], 0); break; case Types.REAL: retVal =3D new float[count]; for (; count > 0; count--) ((float[]) retVal)[i++] =3D ResultSet_ClientSide.toFloa= t(arrayContents[(int) index++]); break; case Types.DOUBLE: retVal =3D new double[count]; for (; count > 0; count--) ((double[]) retVal)[i++] =3D ResultSet_ClientSide.toDou= ble(arrayContents[(int) index++]); break; case Types.CHAR: case Types.VARCHAR: retVal =3D new String[count]; for (; count > 0; count--) ((String[]) retVal)[i++] =3D arrayContents[(int) index+= +]; break; case Types.DATE: retVal =3D new Date[count]; for (; count > 0; count--) ((Date[]) retVal)[i++] =3D ResultSet_ClientSide.toDate(= arrayContents[(int) index++]); break; case Types.TIME: retVal =3D new Time[count]; for (; count > 0; count--) ((Time[]) retVal)[i++] =3D ResultSet_ClientSide.toTime(= arrayContents[(int) index++]); break; case Types.TIMESTAMP: retVal =3D new Timestamp[count]; for (; count > 0; count--) ((Timestamp[]) retVal)[i++] =3D ResultSet_ClientSide.to= Timestamp(arrayContents[(int) index]); break; // Other datatypes not currently supported. If you are rea= lly using other types ask // yourself if an array of non-trivial data types is really= good database design. default: throw new SQLException(getBaseType() + "(" + this.typeName = + ") is an unsupported array type"); } return retVal;*/ throw new SQLException("Array.getArray() not supported"); } public int getBaseType() throws SQLException { int sqlType =3D Types.OTHER; // default value for (int i =3D 0; i < jdbc2Types.length; i++) { if (this.typeName.equals(jdbc2Types[i])) { sqlType =3D jdbc2Typei[i]; break; } } return sqlType; } public String getBaseTypeName() throws SQLException { return this.typeName; } public java.sql.ResultSet getResultSet() throws SQLException { return getResultSet(1, 0, null); } public java.sql.ResultSet getResultSet(long index, int count) throws SQ= LException { return getResultSet(index, count, null); } public java.sql.ResultSet getResultSet(Map map) throws SQLException { return getResultSet(1, 0, map); } public java.sql.ResultSet getResultSet(long index, int count, Map map) = throws SQLException { throw new SQLException("Array.getResultSet() not supported"); } public String toString() { return rawString; } } ------=_NextPart_000_002A_01C4AB08.C2349310--