Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit abcec0c

Browse files
author
Barry Lind
committed
Better error message on character set mismatches during conversion to unicode.
Also applied patch from Lars Stenberg to make callable statements use the form select * from func() when running against a 7.3 server instead of select func() to allow for set returning functions to be called. Modified Files: jdbc/org/postgresql/errors.properties jdbc/org/postgresql/core/Encoding.java jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
1 parent 39b7ec3 commit abcec0c

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

src/interfaces/jdbc/org/postgresql/core/Encoding.java

+34-30
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/*
99
* Converts to and from the character encoding used by the backend.
1010
*
11-
* $Id: Encoding.java,v 1.8 2002/11/14 05:35:45 barry Exp $
11+
* $Id: Encoding.java,v 1.9 2003/02/09 23:14:55 barry Exp $
1212
*/
1313

1414
public class Encoding
@@ -235,36 +235,40 @@ private static boolean isAvailable(String encodingName)
235235
private static final int pow2_12 = 4096; // 212
236236
private char[] cdata = new char[50];
237237

238-
private synchronized String decodeUTF8(byte data[], int offset, int length) {
239-
char[] l_cdata = cdata;
240-
if (l_cdata.length < (length)) {
241-
l_cdata = new char[length];
242-
}
243-
int i = offset;
244-
int j = 0;
245-
int k = length + offset;
246-
int z, y, x, val;
247-
while (i < k) {
248-
z = data[i] & 0xFF;
249-
if (z < 0x80) {
250-
l_cdata[j++] = (char)data[i];
251-
i++;
252-
} else if (z >= 0xE0) { // length == 3
253-
y = data[i+1] & 0xFF;
254-
x = data[i+2] & 0xFF;
255-
val = (z-0xE0)*pow2_12 + (y-0x80)*pow2_6 + (x-0x80);
256-
l_cdata[j++] = (char) val;
257-
i+= 3;
258-
} else { // length == 2 (maybe add checking for length > 3, throw exception if it is
259-
y = data[i+1] & 0xFF;
260-
val = (z - 0xC0)* (pow2_6)+(y-0x80);
261-
l_cdata[j++] = (char) val;
262-
i+=2;
263-
}
264-
}
238+
private synchronized String decodeUTF8(byte data[], int offset, int length) throws SQLException {
239+
try {
240+
char[] l_cdata = cdata;
241+
if (l_cdata.length < (length)) {
242+
l_cdata = new char[length];
243+
}
244+
int i = offset;
245+
int j = 0;
246+
int k = length + offset;
247+
int z, y, x, val;
248+
while (i < k) {
249+
z = data[i] & 0xFF;
250+
if (z < 0x80) {
251+
l_cdata[j++] = (char)data[i];
252+
i++;
253+
} else if (z >= 0xE0) { // length == 3
254+
y = data[i+1] & 0xFF;
255+
x = data[i+2] & 0xFF;
256+
val = (z-0xE0)*pow2_12 + (y-0x80)*pow2_6 + (x-0x80);
257+
l_cdata[j++] = (char) val;
258+
i+= 3;
259+
} else { // length == 2 (maybe add checking for length > 3, throw exception if it is
260+
y = data[i+1] & 0xFF;
261+
val = (z - 0xC0)* (pow2_6)+(y-0x80);
262+
l_cdata[j++] = (char) val;
263+
i+=2;
264+
}
265+
}
265266

266-
String s = new String(l_cdata, 0, j);
267-
return s;
267+
String s = new String(l_cdata, 0, j);
268+
return s;
269+
} catch (Exception l_e) {
270+
throw new PSQLException("postgresql.con.invalidchar", l_e);
271+
}
268272
}
269273

270274
}

src/interfaces/jdbc/org/postgresql/errors.properties

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ postgresql.con.auth:The authentication type {0} is not supported. Check that you
55
postgresql.con.authfail:An error occured while getting the authentication request.
66
postgresql.con.backend:Backend start-up failed: {0}
77
postgresql.con.call:Callable Statements are not supported at this time.
8+
postgresql.con.invalidchar:Invalid character data was found. This is most likely caused by stored data containing characters that are invalid for the character set the database was created in. The most common example of this is storing 8bit data in a SQL_ASCII database.
89
postgresql.con.closed:Connection is closed. Operation is not permitted.
910
postgresql.con.creobj:Failed to create object for {0} {1}
1011
postgresql.con.failed:The connection attempt failed because {0}

src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.postgresql.largeobject.*;
99
import org.postgresql.util.*;
1010

11-
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.16 2003/02/04 10:09:32 barry Exp $
11+
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.17 2003/02/09 23:14:55 barry Exp $
1212
* This class defines methods of the jdbc1 specification. This class is
1313
* extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
1414
* methods. The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
@@ -63,7 +63,7 @@ public org.postgresql.PGConnection getPGConnection() {
6363

6464
//Used by the callablestatement style methods
6565
private static final String JDBC_SYNTAX = "{[? =] call <some_function> ([? [,?]*]) }";
66-
private static final String RESULT_COLUMN = "result";
66+
private static final String RESULT_ALIAS = "result";
6767
private String originalSql = "";
6868
private boolean isFunction;
6969
// functionReturnType contains the user supplied value to check
@@ -1957,6 +1957,7 @@ private void setSerialize(int parameterIndex, long x, String classname) throws S
19571957
* {? = call <some_function> (?, [?,..]) }
19581958
* into the PostgreSQL format which is
19591959
* select <some_function> (?, [?, ...]) as result
1960+
* or select * from <some_function> (?, [?, ...]) as result (7.3)
19601961
*
19611962
*/
19621963
private String modifyJdbcCall(String p_sql) throws SQLException
@@ -2000,7 +2001,11 @@ private String modifyJdbcCall(String p_sql) throws SQLException
20002001
// sure that the parameter numbers are the same as in the original
20012002
// sql we add a dummy parameter in this case
20022003
l_sql = (isFunction ? "?" : "") + l_sql.substring (index + 4);
2003-
l_sql = "select " + l_sql + " as " + RESULT_COLUMN + ";";
2004+
if (connection.haveMinimumServerVersion("7.3")) {
2005+
l_sql = "select * from " + l_sql + " as " + RESULT_ALIAS + ";";
2006+
} else {
2007+
l_sql = "select " + l_sql + " as " + RESULT_ALIAS + ";";
2008+
}
20042009
return l_sql;
20052010
}
20062011

0 commit comments

Comments
 (0)