|
10 | 10 | import org.postgresql.util.*;
|
11 | 11 |
|
12 | 12 | /**
|
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 $ |
14 | 14 | *
|
15 | 15 | * This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
|
16 | 16 | * JDBC2 versions of the Connection class.
|
@@ -125,8 +125,6 @@ protected void openConnection(String host, int port, Properties info, String dat
|
125 | 125 | PG_HOST = host;
|
126 | 126 | PG_STATUS = CONNECTION_BAD;
|
127 | 127 |
|
128 |
| - encoding = info.getProperty("charSet"); // could be null |
129 |
| - |
130 | 128 | // Now make the initial connection
|
131 | 129 | try
|
132 | 130 | {
|
@@ -265,10 +263,84 @@ protected void openConnection(String host, int port, Properties info, String dat
|
265 | 263 | // This may cause some clients to break when they assume anything other than ISO,
|
266 | 264 | // but then - they should be using the proper methods ;-)
|
267 | 265 | //
|
| 266 | + // We also ask the DB for certain properties (i.e. DatabaseEncoding at this time) |
268 | 267 | //
|
269 | 268 | firstWarning = null;
|
270 | 269 |
|
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 | + } |
272 | 344 |
|
273 | 345 | // Initialise object handling
|
274 | 346 | initObjectTypes();
|
|
0 commit comments