Arrays Was (Re: ERROR in 8.0 driver, but not 7.4: SELECT DISTINCT, ORDER BY expressions must appear in select list)

From: sumit shah <shah(at)ucla(dot)edu>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Arrays Was (Re: ERROR in 8.0 driver, but not 7.4: SELECT DISTINCT, ORDER BY expressions must appear in select list)
Date: 2005-05-13 15:20:50
Message-ID: FAC36BBC-3504-4AC6-B72B-9562FA08A443@ucla.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Thanks, Dave for the pointer. I guess I didn't have the right key
words in my googling.

Also I had one more question, concerning sql Arrays. I didn't find
too much documentation on this other than mailing list discussions.
I've created some minimal utility wrapper classes to create
java.sql.Array objects for inserting/updating (i.e. using
PreparedStatement.setArray(x, x)) int, float, double, and String
arrays. The functions for int and String are at the bottom of this
message and the float and double versions are similar.

Now my question. I think the int/float/double are reasonably ok, but
the String version needs some help. Is there a way I can escape the
individual elements? Also any other comments on the general
implementation? I believe this should be ok but if future versions
need other methods implemented besides Array.toString() and Array.
than this needs to be modified. Also can this example be placed in
the documentation for the JDBC driver? I don't mind adding some more
pointers and explanation if people would find it useful. I spent
quite bit of time googling on how to do this, but I ended up having
to dig around the code to see what methods in java.sql.Array required
more than a stub implementation.

Thanks,
Sumit

public static Array convertStringToPgSqlArray(final String[] s)
throws Exception {
if(s == null || s.length < 1) return null;
Array a = new Array() {
public String getBaseTypeName() {return "text";}
public int getBaseType() {return 0;}
public Object getArray() {return null;}
public Object getArray(Map<String, Class<?>> map) {return null;}
public Object getArray(long index, int count) {return null;}
public Object getArray(long index, int count, Map<String,
Class<?>> map) {return null;}
public ResultSet getResultSet() {return null;}
public ResultSet getResultSet(Map<String, Class<?>> map)
{return null;}
public ResultSet getResultSet(long index, int count) {return
null;}
public ResultSet getResultSet(long index, int count,
Map<String, Class<?>> map) {return null;}

public String toString() {
String p = "{";
if(s.length == 0) {
} else {
for(int i = 0; i < s.length - 1; i++) p += s[i] + ",";
p += "'" + s[s.length - 1] + "'";
}
p+="}";
return p;
}
};
return a;
}

public static Array convertIntegerToPgSqlArray(final int[] p) {
if(p == null || p.length < 1) return null;
Array a = new Array() {
public String getBaseTypeName() {return "int4";}
public int getBaseType() {return 0;}
public Object getArray() {return null;}
public Object getArray(Map<String, Class<?>> map) {return null;}
public Object getArray(long index, int count) {return null;}
public Object getArray(long index, int count, Map<String,
Class<?>> map) {return null;}
public ResultSet getResultSet() {return null;}
public ResultSet getResultSet(Map<String, Class<?>> map)
{return null;}
public ResultSet getResultSet(long index, int count) {return
null;}
public ResultSet getResultSet(long index, int count,
Map<String, Class<?>> map) {return null;}

public String toString() {
String fp = "{";
if(p.length == 0) {
} else {
for(int i = 0; i < p.length - 1; i++) fp += p[i] + ",";
fp += p[p.length - 1];
}
fp+="}";
return fp;
}
};
return a;
}

On May 12, 2005, at 9:57 AM, Dave Cramer wrote:

> Yes, it is expected behaviour
>
> see
>
> http://archives.postgresql.org/pgsql-jdbc/2005-05/msg00013.php
>
> for details.
>
> sumit shah wrote:
>
>
>> Dear Postgres JDBC hackers:
>>
>> I've just recently started testing the postgres JDBC 3 driver
>> (8.0-311) after using JDBC 3 (pg74.216.jdbc). I now get an error
>> on a query of the type:
>>
>> SELECT DISTINCT column1, column2 FROM table1 ORDER BY 1
>>
>> where the "1" in the ORDER BY is set by PreparedStatement.setInt
>> method
>>
>> The error is: "SELECT DISTINCT, ORDER BY expressions must appear
>> in select list"
>>
>> The error disappears when I change the query to:
>>
>> SELECT DISTINCT column1, column2 FROM table1 ORDER BY column1
>>
>> or when I hard code the ORDER BY 1 in the query instead of setting
>> it via setInt.
>>
>> This error occurs only with the 8.0-311 driver or the 8.1dev-400
>> jdbc drivers, it does not occur with the pg74-216 driver. This
>> occurs with both the 7.4 and 8.0 backends.
>>
>> Is this change in behavior expected and our code was simply
>> relying on old semantics or misinterpretation of the JDBC standard?
>>
>> Thanks,
>> Sumit
>>
>> ---------------------------(end of
>> broadcast)---------------------------
>> TIP 4: Don't 'kill -9' the postmaster
>>
>>
>>
>
> --
> Dave Cramer
> http://www.postgresintl.com
> 519 939 0336
> ICQ#14675561
>
>

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dilan 2005-05-17 00:38:19 Re: Saving currupting binary data into the database when saving a image into the database
Previous Message Guillaume Cottenceau 2005-05-13 13:54:58 convert 'foobar'::text to the string foobar