Skip to content

Commit 4ce226e

Browse files
committed
In looking at the 7.1beta1 code for JDBC, I noticed that support was
added to support character set encodings. However I noticed that the encoding that is used isn't obtained from the DB. Since Java uses unicode UCS2 internally the character set encoding is used to translate strings from/to the DB encoding. So it seems logical that the code would get the encoding from the DB instead of the current method of requiring the user pass it as a parameter. Attached is a patch that gets the DB encoding from the DB in the same manner as is done in libpq/fe-connect.c. The patch is created off of the latest CVS sources (Connection.java version 1.10). Barry Lind
1 parent 6cc842a commit 4ce226e

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

src/interfaces/jdbc/org/postgresql/Connection.java

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.postgresql.util.*;
1111

1212
/**
13-
* $Id: Connection.java,v 1.10 2000/11/20 08:15:30 peter Exp $
13+
* $Id: Connection.java,v 1.11 2000/12/22 03:08:52 momjian Exp $
1414
*
1515
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
1616
* JDBC2 versions of the Connection class.
@@ -125,8 +125,6 @@ protected void openConnection(String host, int port, Properties info, String dat
125125
PG_HOST = host;
126126
PG_STATUS = CONNECTION_BAD;
127127

128-
encoding = info.getProperty("charSet"); // could be null
129-
130128
// Now make the initial connection
131129
try
132130
{
@@ -265,10 +263,84 @@ protected void openConnection(String host, int port, Properties info, String dat
265263
// This may cause some clients to break when they assume anything other than ISO,
266264
// but then - they should be using the proper methods ;-)
267265
//
266+
// We also ask the DB for certain properties (i.e. DatabaseEncoding at this time)
268267
//
269268
firstWarning = null;
270269

271-
ExecSQL("set datestyle to 'ISO'");
270+
java.sql.ResultSet initrset = ExecSQL("set datestyle to 'ISO'; select getdatabaseencoding()");
271+
272+
String dbEncoding = null;
273+
//retrieve DB properties
274+
if(initrset.next()) {
275+
276+
//handle DatabaseEncoding
277+
dbEncoding = initrset.getString(1);
278+
//convert from the PostgreSQL name to the Java name
279+
if (dbEncoding.equals("SQL_ASCII")) {
280+
dbEncoding = "ASCII";
281+
} else if (dbEncoding.equals("UNICODE")) {
282+
dbEncoding = "UTF8";
283+
} else if (dbEncoding.equals("LATIN1")) {
284+
dbEncoding = "ISO8859_1";
285+
} else if (dbEncoding.equals("LATIN2")) {
286+
dbEncoding = "ISO8859_2";
287+
} else if (dbEncoding.equals("LATIN3")) {
288+
dbEncoding = "ISO8859_3";
289+
} else if (dbEncoding.equals("LATIN4")) {
290+
dbEncoding = "ISO8859_4";
291+
} else if (dbEncoding.equals("LATIN5")) {
292+
dbEncoding = "ISO8859_5";
293+
} else if (dbEncoding.equals("LATIN6")) {
294+
dbEncoding = "ISO8859_6";
295+
} else if (dbEncoding.equals("LATIN7")) {
296+
dbEncoding = "ISO8859_7";
297+
} else if (dbEncoding.equals("LATIN8")) {
298+
dbEncoding = "ISO8859_8";
299+
} else if (dbEncoding.equals("LATIN9")) {
300+
dbEncoding = "ISO8859_9";
301+
} else if (dbEncoding.equals("EUC_JP")) {
302+
dbEncoding = "EUC_JP";
303+
} else if (dbEncoding.equals("EUC_CN")) {
304+
dbEncoding = "EUC_CN";
305+
} else if (dbEncoding.equals("EUC_KR")) {
306+
dbEncoding = "EUC_KR";
307+
} else if (dbEncoding.equals("EUC_TW")) {
308+
dbEncoding = "EUC_TW";
309+
} else if (dbEncoding.equals("KOI8")) {
310+
dbEncoding = "KOI8_R";
311+
} else if (dbEncoding.equals("WIN")) {
312+
dbEncoding = "Cp1252";
313+
} else {
314+
dbEncoding = null;
315+
}
316+
}
317+
318+
319+
//Set the encoding for this connection
320+
//Since the encoding could be specified or obtained from the DB we use the
321+
//following order:
322+
// 1. passed as a property
323+
// 2. value from DB if supported by current JVM
324+
// 3. default for JVM (leave encoding null)
325+
String passedEncoding = info.getProperty("charSet"); // could be null
326+
327+
if (passedEncoding != null) {
328+
encoding = passedEncoding;
329+
} else {
330+
if (dbEncoding != null) {
331+
//test DB encoding
332+
try {
333+
"TEST".getBytes(dbEncoding);
334+
//no error the encoding is supported by the current JVM
335+
encoding = dbEncoding;
336+
} catch (UnsupportedEncodingException uee) {
337+
//dbEncoding is not supported by the current JVM
338+
encoding = null;
339+
}
340+
} else {
341+
encoding = null;
342+
}
343+
}
272344

273345
// Initialise object handling
274346
initObjectTypes();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy