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
This commit is contained in:
Barry Lind 2003-02-09 23:14:55 +00:00
parent 39b7ec3309
commit abcec0c125
3 changed files with 43 additions and 33 deletions

View File

@ -8,7 +8,7 @@ import org.postgresql.util.*;
/*
* Converts to and from the character encoding used by the backend.
*
* $Id: Encoding.java,v 1.8 2002/11/14 05:35:45 barry Exp $
* $Id: Encoding.java,v 1.9 2003/02/09 23:14:55 barry Exp $
*/
public class Encoding
@ -235,36 +235,40 @@ public class Encoding
private static final int pow2_12 = 4096; // 212
private char[] cdata = new char[50];
private synchronized String decodeUTF8(byte data[], int offset, int length) {
char[] l_cdata = cdata;
if (l_cdata.length < (length)) {
l_cdata = new char[length];
}
int i = offset;
int j = 0;
int k = length + offset;
int z, y, x, val;
while (i < k) {
z = data[i] & 0xFF;
if (z < 0x80) {
l_cdata[j++] = (char)data[i];
i++;
} else if (z >= 0xE0) { // length == 3
y = data[i+1] & 0xFF;
x = data[i+2] & 0xFF;
val = (z-0xE0)*pow2_12 + (y-0x80)*pow2_6 + (x-0x80);
l_cdata[j++] = (char) val;
i+= 3;
} else { // length == 2 (maybe add checking for length > 3, throw exception if it is
y = data[i+1] & 0xFF;
val = (z - 0xC0)* (pow2_6)+(y-0x80);
l_cdata[j++] = (char) val;
i+=2;
}
}
private synchronized String decodeUTF8(byte data[], int offset, int length) throws SQLException {
try {
char[] l_cdata = cdata;
if (l_cdata.length < (length)) {
l_cdata = new char[length];
}
int i = offset;
int j = 0;
int k = length + offset;
int z, y, x, val;
while (i < k) {
z = data[i] & 0xFF;
if (z < 0x80) {
l_cdata[j++] = (char)data[i];
i++;
} else if (z >= 0xE0) { // length == 3
y = data[i+1] & 0xFF;
x = data[i+2] & 0xFF;
val = (z-0xE0)*pow2_12 + (y-0x80)*pow2_6 + (x-0x80);
l_cdata[j++] = (char) val;
i+= 3;
} else { // length == 2 (maybe add checking for length > 3, throw exception if it is
y = data[i+1] & 0xFF;
val = (z - 0xC0)* (pow2_6)+(y-0x80);
l_cdata[j++] = (char) val;
i+=2;
}
}
String s = new String(l_cdata, 0, j);
return s;
String s = new String(l_cdata, 0, j);
return s;
} catch (Exception l_e) {
throw new PSQLException("postgresql.con.invalidchar", l_e);
}
}
}

View File

@ -5,6 +5,7 @@ postgresql.con.auth:The authentication type {0} is not supported. Check that you
postgresql.con.authfail:An error occured while getting the authentication request.
postgresql.con.backend:Backend start-up failed: {0}
postgresql.con.call:Callable Statements are not supported at this time.
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.
postgresql.con.closed:Connection is closed. Operation is not permitted.
postgresql.con.creobj:Failed to create object for {0} {1}
postgresql.con.failed:The connection attempt failed because {0}

View File

@ -8,7 +8,7 @@ import java.util.Vector;
import org.postgresql.largeobject.*;
import org.postgresql.util.*;
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.16 2003/02/04 10:09:32 barry Exp $
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.17 2003/02/09 23:14:55 barry Exp $
* This class defines methods of the jdbc1 specification. This class is
* extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
* methods. The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
@ -63,7 +63,7 @@ public abstract class AbstractJdbc1Statement implements org.postgresql.PGStateme
//Used by the callablestatement style methods
private static final String JDBC_SYNTAX = "{[? =] call <some_function> ([? [,?]*]) }";
private static final String RESULT_COLUMN = "result";
private static final String RESULT_ALIAS = "result";
private String originalSql = "";
private boolean isFunction;
// functionReturnType contains the user supplied value to check
@ -1957,6 +1957,7 @@ public abstract class AbstractJdbc1Statement implements org.postgresql.PGStateme
* {? = call <some_function> (?, [?,..]) }
* into the PostgreSQL format which is
* select <some_function> (?, [?, ...]) as result
* or select * from <some_function> (?, [?, ...]) as result (7.3)
*
*/
private String modifyJdbcCall(String p_sql) throws SQLException
@ -2000,7 +2001,11 @@ public abstract class AbstractJdbc1Statement implements org.postgresql.PGStateme
// sure that the parameter numbers are the same as in the original
// sql we add a dummy parameter in this case
l_sql = (isFunction ? "?" : "") + l_sql.substring (index + 4);
l_sql = "select " + l_sql + " as " + RESULT_COLUMN + ";";
if (connection.haveMinimumServerVersion("7.3")) {
l_sql = "select * from " + l_sql + " as " + RESULT_ALIAS + ";";
} else {
l_sql = "select " + l_sql + " as " + RESULT_ALIAS + ";";
}
return l_sql;
}