Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4ce226e

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 theencoding that is used isn't obtained from the DB. Since Java usesunicode UCS2 internally the character set encoding is used to translatestrings from/to the DB encoding. So it seems logical that the codewould get the encoding from the DB instead of the current method ofrequiring the user pass it as a parameter.Attached is a patch that gets the DB encoding from the DB in the samemanner as is done in libpq/fe-connect.c. The patch is created off ofthe latest CVS sources (Connection.java version 1.10).Barry Lind
1 parent6cc842a commit4ce226e

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
importorg.postgresql.util.*;
1111

1212
/**
13-
* $Id: Connection.java,v 1.10 2000/11/2008: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.ResultSetinitrset =ExecSQL("set datestyle to 'ISO'; select getdatabaseencoding()");
271+
272+
StringdbEncoding =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+
}elseif (dbEncoding.equals("UNICODE")) {
282+
dbEncoding ="UTF8";
283+
}elseif (dbEncoding.equals("LATIN1")) {
284+
dbEncoding ="ISO8859_1";
285+
}elseif (dbEncoding.equals("LATIN2")) {
286+
dbEncoding ="ISO8859_2";
287+
}elseif (dbEncoding.equals("LATIN3")) {
288+
dbEncoding ="ISO8859_3";
289+
}elseif (dbEncoding.equals("LATIN4")) {
290+
dbEncoding ="ISO8859_4";
291+
}elseif (dbEncoding.equals("LATIN5")) {
292+
dbEncoding ="ISO8859_5";
293+
}elseif (dbEncoding.equals("LATIN6")) {
294+
dbEncoding ="ISO8859_6";
295+
}elseif (dbEncoding.equals("LATIN7")) {
296+
dbEncoding ="ISO8859_7";
297+
}elseif (dbEncoding.equals("LATIN8")) {
298+
dbEncoding ="ISO8859_8";
299+
}elseif (dbEncoding.equals("LATIN9")) {
300+
dbEncoding ="ISO8859_9";
301+
}elseif (dbEncoding.equals("EUC_JP")) {
302+
dbEncoding ="EUC_JP";
303+
}elseif (dbEncoding.equals("EUC_CN")) {
304+
dbEncoding ="EUC_CN";
305+
}elseif (dbEncoding.equals("EUC_KR")) {
306+
dbEncoding ="EUC_KR";
307+
}elseif (dbEncoding.equals("EUC_TW")) {
308+
dbEncoding ="EUC_TW";
309+
}elseif (dbEncoding.equals("KOI8")) {
310+
dbEncoding ="KOI8_R";
311+
}elseif (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+
StringpassedEncoding =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 (UnsupportedEncodingExceptionuee) {
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)

[8]ページ先頭

©2009-2025 Movatter.jp