1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-03 15:22:11 +03:00

Attached are a patch to allow the charset encoding used by the JDBC

driver to be set, and a description of said patch.  Please refer to
the latter for more information.

William
--
William Webber                               william@peopleweb.net.au
This commit is contained in:
Bruce Momjian
2000-09-12 04:58:50 +00:00
parent 4f5cdadf03
commit 65edb54186
7 changed files with 328 additions and 12 deletions

View File

@@ -10,7 +10,7 @@ import org.postgresql.largeobject.*;
import org.postgresql.util.*;
/**
* $Id: Connection.java,v 1.4 2000/06/06 11:05:59 peter Exp $
* $Id: Connection.java,v 1.5 2000/09/12 04:58:47 momjian Exp $
*
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
* JDBC2 versions of the Connection class.
@@ -30,6 +30,14 @@ public abstract class Connection
private String PG_PASSWORD;
private String PG_DATABASE;
private boolean PG_STATUS;
/**
* The encoding to use for this connection.
* If <b>null</b>, the encoding has not been specified by the
* user, and the default encoding for the platform should be
* used.
*/
private String encoding;
public boolean CONNECTION_OK = true;
public boolean CONNECTION_BAD = false;
@@ -111,6 +119,8 @@ public abstract class Connection
PG_PORT = port;
PG_HOST = new String(host);
PG_STATUS = CONNECTION_BAD;
encoding = info.getProperty("charSet"); // could be null
// Now make the initial connection
try
@@ -154,7 +164,8 @@ public abstract class Connection
// The most common one to be thrown here is:
// "User authentication failed"
//
throw new SQLException(pg_stream.ReceiveString(4096));
throw new SQLException(pg_stream.ReceiveString
(4096, getEncoding()));
case 'R':
// Get the type of request
@@ -224,7 +235,8 @@ public abstract class Connection
break;
case 'E':
case 'N':
throw new SQLException(pg_stream.ReceiveString(4096));
throw new SQLException(pg_stream.ReceiveString
(4096, getEncoding()));
default:
throw new PSQLException("postgresql.con.setup");
}
@@ -313,7 +325,7 @@ public abstract class Connection
Field[] fields = null;
Vector tuples = new Vector();
byte[] buf = new byte[sql.length()];
byte[] buf = null;
int fqp = 0;
boolean hfr = false;
String recv_status = null, msg;
@@ -325,6 +337,18 @@ public abstract class Connection
// larger than 8K. Peter June 6 2000
//if (sql.length() > 8192)
//throw new PSQLException("postgresql.con.toolong",sql);
if (getEncoding() == null)
buf = sql.getBytes();
else {
try {
buf = sql.getBytes(getEncoding());
} catch (UnsupportedEncodingException unse) {
throw new PSQLException("postgresql.con.encoding",
unse);
}
}
try
{
pg_stream.SendChar('Q');
@@ -513,6 +537,15 @@ public abstract class Connection
{
return PG_USER;
}
/**
* Get the character encoding to use for this connection.
* @return the encoding to use, or <b>null</b> for the
* default encoding.
*/
public String getEncoding() throws SQLException {
return encoding;
}
/**
* This returns the Fastpath API for the current connection.

View File

@@ -90,7 +90,15 @@ public class Driver implements java.sql.Driver
* <p>The java.util.Properties argument can be used to pass arbitrary
* string tag/value pairs as connection arguments. Normally, at least
* "user" and "password" properties should be included in the
* properties.
* properties. In addition, the "charSet" property can be used to
* set a character set encoding (e.g. "utf-8") other than the platform
* default (typically Latin1). This is necessary in particular if storing
* multibyte characters in the database. For a list of supported
* character encoding , see
* http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
* Note that you will probably want to have set up the Postgres database
* itself to use the same encoding, with the "-E <encoding>" argument
* to createdb.
*
* Our protocol takes the forms:
* <PRE>

View File

@@ -235,17 +235,22 @@ public class PG_Stream
}
return n;
}
public String ReceiveString(int maxsize) throws SQLException {
return ReceiveString(maxsize, null);
}
/**
* Receives a null-terminated string from the backend. Maximum of
* maxsiz bytes - if we don't see a null, then we assume something
* has gone wrong.
*
* @param maxsiz maximum length of string
* @param encoding the charset encoding to use.
* @param maxsiz maximum length of string in bytes
* @return string from back end
* @exception SQLException if an I/O error occurs
*/
public String ReceiveString(int maxsiz) throws SQLException
public String ReceiveString(int maxsiz, String encoding) throws SQLException
{
byte[] rst = new byte[maxsiz];
int s = 0;
@@ -267,7 +272,16 @@ public class PG_Stream
} catch (IOException e) {
throw new PSQLException("postgresql.stream.ioerror",e);
}
String v = new String(rst, 0, s);
String v = null;
if (encoding == null)
v = new String(rst, 0, s);
else {
try {
v = new String(rst, 0, s, encoding);
} catch (UnsupportedEncodingException unse) {
throw new PSQLException("postgresql.stream.encoding", unse);
}
}
return v;
}

View File

@@ -163,7 +163,16 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
wasNullFlag = (this_row[columnIndex - 1] == null);
if(wasNullFlag)
return null;
return new String(this_row[columnIndex - 1]);
String encoding = connection.getEncoding();
if (encoding == null)
return new String(this_row[columnIndex - 1]);
else {
try {
return new String(this_row[columnIndex - 1], encoding);
} catch (UnsupportedEncodingException unse) {
throw new PSQLException("postgresql.res.encoding", unse);
}
}
}
/**

View File

@@ -164,7 +164,16 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
wasNullFlag = (this_row[columnIndex - 1] == null);
if(wasNullFlag)
return null;
return new String(this_row[columnIndex - 1]);
String encoding = connection.getEncoding();
if (encoding == null)
return new String(this_row[columnIndex - 1]);
else {
try {
return new String(this_row[columnIndex - 1], encoding);
} catch (UnsupportedEncodingException unse) {
throw new PSQLException("postgresql.res.encoding", unse);
}
}
}
/**