diff options
author | Bruce Momjian <bruce@momjian.us> | 2000-09-12 04:58:50 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 2000-09-12 04:58:50 +0000 |
commit | 65edb541865032b5750cfe58cb8f7affbe1fc298 (patch) | |
tree | 6f7c1aae279e75e5be4dceae692c8d3b2c9386a4 /src/interfaces/jdbc/org/postgresql | |
parent | 4f5cdadf03ae97de4fbd9ef90e15a080291b6a13 (diff) |
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
Diffstat (limited to 'src/interfaces/jdbc/org/postgresql')
-rw-r--r-- | src/interfaces/jdbc/org/postgresql/Connection.java | 41 | ||||
-rw-r--r-- | src/interfaces/jdbc/org/postgresql/Driver.java | 10 | ||||
-rw-r--r-- | src/interfaces/jdbc/org/postgresql/PG_Stream.java | 20 | ||||
-rw-r--r-- | src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java | 11 | ||||
-rw-r--r-- | src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java | 11 |
5 files changed, 83 insertions, 10 deletions
diff --git a/src/interfaces/jdbc/org/postgresql/Connection.java b/src/interfaces/jdbc/org/postgresql/Connection.java index 539faecd3e0..33834118bda 100644 --- a/src/interfaces/jdbc/org/postgresql/Connection.java +++ b/src/interfaces/jdbc/org/postgresql/Connection.java @@ -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. diff --git a/src/interfaces/jdbc/org/postgresql/Driver.java b/src/interfaces/jdbc/org/postgresql/Driver.java index 779c58eb008..d5a5c8d24fe 100644 --- a/src/interfaces/jdbc/org/postgresql/Driver.java +++ b/src/interfaces/jdbc/org/postgresql/Driver.java @@ -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> diff --git a/src/interfaces/jdbc/org/postgresql/PG_Stream.java b/src/interfaces/jdbc/org/postgresql/PG_Stream.java index 35bd6df364b..22c41bdb3a3 100644 --- a/src/interfaces/jdbc/org/postgresql/PG_Stream.java +++ b/src/interfaces/jdbc/org/postgresql/PG_Stream.java @@ -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; } diff --git a/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java b/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java index 497e401bde8..e65cf80b55b 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java @@ -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); + } + } } /** diff --git a/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java b/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java index 85cb3c1f434..e9f6c79f41b 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java @@ -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); + } + } } /** |