pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedPostgres 8.0 + JDBC
6+ messages / 4 participants
[nested] [flat]
* Postgres 8.0 + JDBC
@ 2004-10-05 16:25 Johann Robette <jrobette@onyme.com>
0 siblings, 1 reply; 6+ messages in thread
From: Johann Robette @ 2004-10-05 16:25 UTC (permalink / raw)
To: pgsql-jdbc; +Cc: pgsql-general@postgresql.org
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
Attachments:
PostgresArray.txt (text/plain, 14.8K) download
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.
*
* <p>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.
*
* <p>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.
*
* <b>This class stolen from postgresql 7.2.1's source tree!!!</b>
*
* @see ResultSet#getArray
*
*/
public class PostgresArray implements Array {
private String rawString = null;
private String typeName = null;
private int baseType = -1;
private static final String jdbc2Types[] = {
"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[] = {
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.CHAR,
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 Array
* @param field the Field descriptor for the field to load into this Array
* @param rs the ResultSet from which to get the data for this Array
*/
PostgresArray(String rawString, int baseType, String typeName) throws SQLException {
this.rawString = rawString;
this.baseType = baseType;
this.typeName = typeName;
if (this.typeName.startsWith("_")) {
this.typeName = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 == null)
return new PostgresArray(null, -1, null);
StringBuffer sb = new StringBuffer("{");
for (int x = 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 SQLException {
/*if (map != null) // For now maps aren't supported.
throw new SQLException("Maps are not supported in Array.getArray");
if (index < 1)
throw new SQLException("index < 1");
Object retVal = null;
ArrayList array = new ArrayList();
if (rawString != null) {
char[] chars = rawString.toCharArray();
StringBuffer sbuf = new StringBuffer();
boolean foundOpen = false;
boolean insideString = false;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '{') {
if (foundOpen) // Only supports 1-D arrays for now
throw org.postgresql.Driver.notImplemented();
foundOpen = true;
continue;
}
if (chars[i] == '"') {
insideString = !insideString;
continue;
}
if ((!insideString && chars[i] == ',') || chars[i] == '}' || i == chars.length - 1) {
if (chars[i] != '"' && chars[i] != '}' && chars[i] != ',')
sbuf.append(chars[i]);
array.add(sbuf.toString());
sbuf = new StringBuffer();
continue;
}
sbuf.append(chars[i]);
}
}
String[] arrayContents = (String[]) array.toArray(new String[array.size()]);
if (count == 0)
count = arrayContents.length;
index--;
if (index + count > arrayContents.length)
throw new SQLException("index + count > arrayContents");
int i = 0;
switch (getBaseType()) {
case Types.BIT:
retVal = new boolean[count];
for (; count > 0; count--)
((boolean[]) retVal)[i++] = ResultSet_ClientSide.toBoolean(arrayContents[(int) index++]);
break;
case Types.SMALLINT:
case Types.INTEGER:
retVal = new int[count];
for (; count > 0; count--)
((int[]) retVal)[i++] = ResultSet_ClientSide.toInt(arrayContents[(int) index++]);
break;
case Types.BIGINT:
retVal = new long[count];
for (; count > 0; count--)
((long[]) retVal)[i++] = ResultSet_ClientSide.toLong(arrayContents[(int) index++]);
break;
case Types.NUMERIC:
retVal = new BigDecimal[count];
for (; count > 0; count--)
((BigDecimal[]) retVal)[i] = ResultSet_ClientSide.toBigDecimal(arrayContents[(int) index++], 0);
break;
case Types.REAL:
retVal = new float[count];
for (; count > 0; count--)
((float[]) retVal)[i++] = ResultSet_ClientSide.toFloat(arrayContents[(int) index++]);
break;
case Types.DOUBLE:
retVal = new double[count];
for (; count > 0; count--)
((double[]) retVal)[i++] = ResultSet_ClientSide.toDouble(arrayContents[(int) index++]);
break;
case Types.CHAR:
case Types.VARCHAR:
retVal = new String[count];
for (; count > 0; count--)
((String[]) retVal)[i++] = arrayContents[(int) index++];
break;
case Types.DATE:
retVal = new Date[count];
for (; count > 0; count--)
((Date[]) retVal)[i++] = ResultSet_ClientSide.toDate(arrayContents[(int) index++]);
break;
case Types.TIME:
retVal = new Time[count];
for (; count > 0; count--)
((Time[]) retVal)[i++] = ResultSet_ClientSide.toTime(arrayContents[(int) index++]);
break;
case Types.TIMESTAMP:
retVal = new Timestamp[count];
for (; count > 0; count--)
((Timestamp[]) retVal)[i++] = ResultSet_ClientSide.toTimestamp(arrayContents[(int) index]);
break;
// Other datatypes not currently supported. If you are really 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 = Types.OTHER; // default value
for (int i = 0; i < jdbc2Types.length; i++) {
if (this.typeName.equals(jdbc2Types[i])) {
sqlType = 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 SQLException {
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;
}
}
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Postgres 8.0 + JDBC
@ 2004-10-05 20:10 Oliver Jowett <oliver@opencloud.com>
parent: Johann Robette <jrobette@onyme.com>
0 siblings, 2 replies; 6+ messages in thread
From: Oliver Jowett @ 2004-10-05 20:10 UTC (permalink / raw)
To: Johann Robette <jrobette@onyme.com>; +Cc: pgsql-jdbc
Johann Robette wrote:
> java.sql.SQLException: ERROR: cannot cast type text to
> text[]
setArray has never worked particularly well; you're seeing another
manifestation here.
The underlying problem here is that:
- setArray() is claiming that the type it is setting is a VARCHAR
- the parameter is being passed as a typed parameter via the V3
protocol, with type = 'text'
- apparently you can't cast directly from text -> text[] (although you
can interpret an untyped literal as text[], which is why it worked before).
It seems possible to fix the driver to handle this case by making
setArray() derive a proper array type name i.e. ("_" +
Array.getBaseType()), and using that rather than 'text' as the parameter
type.
I'll try to do this soon, but I'm a bit busy so it may be a few days.
A longer term fix is to properly implement array support in setArray()..
I've submitted patches in the past to do this but they've never made it
into the official driver.
-O
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Postgres 8.0 + JDBC
@ 2004-10-05 20:31 Kris Jurka <books@ejurka.com>
parent: Oliver Jowett <oliver@opencloud.com>
1 sibling, 1 reply; 6+ messages in thread
From: Kris Jurka @ 2004-10-05 20:31 UTC (permalink / raw)
To: Oliver Jowett <oliver@opencloud.com>; +Cc: Johann Robette <jrobette@onyme.com>; pgsql-jdbc
On Wed, 6 Oct 2004, Oliver Jowett wrote:
> It seems possible to fix the driver to handle this case by making
> setArray() derive a proper array type name i.e. ("_" +
> Array.getBaseType()), and using that rather than 'text' as the parameter
> type.
>
Wouldn't it be simpler to change setArray to call setString with 0 as the
type oid allowing the backend to figure out what to do with it? Perhaps
it would have trouble determining the type if the underlying function was
overloaded, but other than that I don't see a problem.
Kris Jurka
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Postgres 8.0 + JDBC
@ 2004-10-05 20:57 Oliver Jowett <oliver@opencloud.com>
parent: Kris Jurka <books@ejurka.com>
0 siblings, 1 reply; 6+ messages in thread
From: Oliver Jowett @ 2004-10-05 20:57 UTC (permalink / raw)
To: Kris Jurka <books@ejurka.com>; +Cc: Johann Robette <jrobette@onyme.com>; pgsql-jdbc
Kris Jurka wrote:
>
> On Wed, 6 Oct 2004, Oliver Jowett wrote:
>
>
>>It seems possible to fix the driver to handle this case by making
>>setArray() derive a proper array type name i.e. ("_" +
>>Array.getBaseType()), and using that rather than 'text' as the parameter
>>type.
>>
>
>
> Wouldn't it be simpler to change setArray to call setString with 0 as the
> type oid allowing the backend to figure out what to do with it? Perhaps
> it would have trouble determining the type if the underlying function was
> overloaded, but other than that I don't see a problem.
Using the unknown oid seems like it'd introduce more problems:
overloading, and the possibility you actually pass your array as some
other type unexpectedly.
I am wondering if the right way to find the array OID is to prepend "_"
and search on pg_type.typname, or to look for pg_type.typinput = (oid of
array_in) and pg_type.typelem = (oid of underlying type). Is there a
'standard' way of finding an array type OID?
-O
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Postgres 8.0 + JDBC
@ 2004-10-05 21:28 Tom Lane <tgl@sss.pgh.pa.us>
parent: Oliver Jowett <oliver@opencloud.com>
0 siblings, 0 replies; 6+ messages in thread
From: Tom Lane @ 2004-10-05 21:28 UTC (permalink / raw)
To: Oliver Jowett <oliver@opencloud.com>; +Cc: Kris Jurka <books@ejurka.com>; Johann Robette <jrobette@onyme.com>; pgsql-jdbc
Oliver Jowett <oliver@opencloud.com> writes:
> I am wondering if the right way to find the array OID is to prepend "_"
> and search on pg_type.typname, or to look for pg_type.typinput = (oid of
> array_in) and pg_type.typelem = (oid of underlying type). Is there a
> 'standard' way of finding an array type OID?
The backend does it by name, ie prepend '_' and lookup by typname (and
typnamespace). This is a mite unclean but it hasn't seemed worth
fixing. If you like you can use the typelem as an additional check that
you found the right thing.
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Postgres 8.0 + JDBC
@ 2004-10-07 04:52 Oliver Jowett <oliver@opencloud.com>
parent: Oliver Jowett <oliver@opencloud.com>
1 sibling, 0 replies; 6+ messages in thread
From: Oliver Jowett @ 2004-10-07 04:52 UTC (permalink / raw)
To: pgsql-jdbc
Oliver Jowett wrote:
> It seems possible to fix the driver to handle this case by making
> setArray() derive a proper array type name i.e. ("_" +
> Array.getBaseType()), and using that rather than 'text' as the parameter
> type.
I've done this (plus tests) and it seems to work OK. I can't generate a
patch right now as gborg seems to be having some problems (I get 70%
packet loss on the last hop, which pretty much hoses CVS access)
-O
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2004-10-07 04:52 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-10-05 16:25 Postgres 8.0 + JDBC Johann Robette <jrobette@onyme.com>
2004-10-05 20:10 ` Oliver Jowett <oliver@opencloud.com>
2004-10-05 20:31 ` Kris Jurka <books@ejurka.com>
2004-10-05 20:57 ` Oliver Jowett <oliver@opencloud.com>
2004-10-05 21:28 ` Tom Lane <tgl@sss.pgh.pa.us>
2004-10-07 04:52 ` Oliver Jowett <oliver@opencloud.com>
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