8
8
import org .postgresql .fastpath .*;
9
9
import org .postgresql .largeobject .*;
10
10
import org .postgresql .util .*;
11
+ import org .postgresql .core .Encoding ;
11
12
12
13
/**
13
- * $Id: Connection.java,v 1.18 2001/07/15 04:21:26 momjian Exp $
14
+ * $Id: Connection.java,v 1.19 2001/07/21 18:52:10 momjian Exp $
14
15
*
15
16
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
16
17
* JDBC2 versions of the Connection class.
@@ -33,11 +34,8 @@ public abstract class Connection
33
34
34
35
/**
35
36
* The encoding to use for this connection.
36
- * If <b>null</b>, the encoding has not been specified by the
37
- * user, and the default encoding for the platform should be
38
- * used.
39
37
*/
40
- private String encoding ;
38
+ private Encoding encoding = Encoding . defaultEncoding () ;
41
39
42
40
public boolean CONNECTION_OK = true ;
43
41
public boolean CONNECTION_BAD = false ;
@@ -162,7 +160,7 @@ protected void openConnection(String host, int port, Properties info, String dat
162
160
// The most common one to be thrown here is:
163
161
// "User authentication failed"
164
162
//
165
- throw new SQLException (pg_stream .ReceiveString (getEncoding () ));
163
+ throw new SQLException (pg_stream .ReceiveString (encoding ));
166
164
167
165
case 'R' :
168
166
// Get the type of request
@@ -232,7 +230,7 @@ protected void openConnection(String host, int port, Properties info, String dat
232
230
break ;
233
231
case 'E' :
234
232
case 'N' :
235
- throw new SQLException (pg_stream .ReceiveString (getEncoding () ));
233
+ throw new SQLException (pg_stream .ReceiveString (encoding ));
236
234
default :
237
235
throw new PSQLException ("postgresql.con.setup" );
238
236
}
@@ -244,111 +242,34 @@ protected void openConnection(String host, int port, Properties info, String dat
244
242
break ;
245
243
case 'E' :
246
244
case 'N' :
247
- throw new SQLException (pg_stream .ReceiveString (getEncoding () ));
245
+ throw new SQLException (pg_stream .ReceiveString (encoding ));
248
246
default :
249
247
throw new PSQLException ("postgresql.con.setup" );
250
248
}
251
249
252
- // Originally we issued a SHOW DATESTYLE statement to find the databases default
253
- // datestyle. However, this caused some problems with timestamps, so in 6.5, we
254
- // went the way of ODBC, and set the connection to ISO.
255
- //
256
- // This may cause some clients to break when they assume anything other than ISO,
257
- // but then - they should be using the proper methods ;-)
258
- //
259
- // We also ask the DB for certain properties (i.e. DatabaseEncoding at this time)
260
- //
261
250
firstWarning = null ;
262
251
263
- java .sql .ResultSet initrset = ExecSQL ("set datestyle to 'ISO'; " +
264
- "select case when pg_encoding_to_char(1) = 'SQL_ASCII' then 'UNKNOWN' else getdatabaseencoding() end" );
265
-
266
- String dbEncoding = null ;
267
- //retrieve DB properties
268
- if (initrset .next ()) {
269
-
270
- //handle DatabaseEncoding
271
- dbEncoding = initrset .getString (1 );
272
- //convert from the PostgreSQL name to the Java name
273
- if (dbEncoding .equals ("SQL_ASCII" )) {
274
- dbEncoding = "ASCII" ;
275
- } else if (dbEncoding .equals ("UNICODE" )) {
276
- dbEncoding = "UTF8" ;
277
- } else if (dbEncoding .equals ("LATIN1" )) {
278
- dbEncoding = "ISO8859_1" ;
279
- } else if (dbEncoding .equals ("LATIN2" )) {
280
- dbEncoding = "ISO8859_2" ;
281
- } else if (dbEncoding .equals ("LATIN3" )) {
282
- dbEncoding = "ISO8859_3" ;
283
- } else if (dbEncoding .equals ("LATIN4" )) {
284
- dbEncoding = "ISO8859_4" ;
285
- } else if (dbEncoding .equals ("LATIN5" )) {
286
- dbEncoding = "ISO8859_5" ;
287
- } else if (dbEncoding .equals ("LATIN6" )) {
288
- dbEncoding = "ISO8859_6" ;
289
- } else if (dbEncoding .equals ("LATIN7" )) {
290
- dbEncoding = "ISO8859_7" ;
291
- } else if (dbEncoding .equals ("LATIN8" )) {
292
- dbEncoding = "ISO8859_8" ;
293
- } else if (dbEncoding .equals ("LATIN9" )) {
294
- dbEncoding = "ISO8859_9" ;
295
- } else if (dbEncoding .equals ("EUC_JP" )) {
296
- dbEncoding = "EUC_JP" ;
297
- } else if (dbEncoding .equals ("EUC_CN" )) {
298
- dbEncoding = "EUC_CN" ;
299
- } else if (dbEncoding .equals ("EUC_KR" )) {
300
- dbEncoding = "EUC_KR" ;
301
- } else if (dbEncoding .equals ("EUC_TW" )) {
302
- dbEncoding = "EUC_TW" ;
303
- } else if (dbEncoding .equals ("KOI8" )) {
304
- // try first if KOI8_U is present, it's a superset of KOI8_R
305
- try {
306
- dbEncoding = "KOI8_U" ;
307
- "test" .getBytes (dbEncoding );
308
- }
309
- catch (UnsupportedEncodingException uee ) {
310
- // well, KOI8_U is still not in standard JDK, falling back to KOI8_R :(
311
- dbEncoding = "KOI8_R" ;
312
- }
252
+ String dbEncoding ;
313
253
314
- } else if (dbEncoding .equals ("WIN" )) {
315
- dbEncoding = "Cp1252" ;
316
- } else if (dbEncoding .equals ("UNKNOWN" )) {
317
- //This isn't a multibyte database so we don't have an encoding to use
318
- //We leave dbEncoding null which will cause the default encoding for the
319
- //JVM to be used
320
- dbEncoding = null ;
321
- } else {
322
- dbEncoding = null ;
323
- }
324
- }
254
+ // "pg_encoding_to_char(1)" will return 'EUC_JP' for a backend compiled with multibyte,
255
+ // otherwise it's hardcoded to 'SQL_ASCII'.
256
+ // If the backend doesn't know about multibyte we can't assume anything about the encoding
257
+ // used, so we denote this with 'UNKNOWN'.
258
+
259
+ final String encodingQuery =
260
+ "select case when pg_encoding_to_char(1) = 'SQL_ASCII' then 'UNKNOWN' else getdatabaseencoding() end" ;
325
261
262
+ // Set datestyle and fetch db encoding in a single call, to avoid making
263
+ // more than one round trip to the backend during connection startup.
326
264
327
- //Set the encoding for this connection
328
- //Since the encoding could be specified or obtained from the DB we use the
329
- //following order:
330
- // 1. passed as a property
331
- // 2. value from DB if supported by current JVM
332
- // 3. default for JVM (leave encoding null)
333
- String passedEncoding = info .getProperty ("charSet" ); // could be null
334
-
335
- if (passedEncoding != null ) {
336
- encoding = passedEncoding ;
337
- } else {
338
- if (dbEncoding != null ) {
339
- //test DB encoding
340
- try {
341
- "TEST" .getBytes (dbEncoding );
342
- //no error the encoding is supported by the current JVM
343
- encoding = dbEncoding ;
344
- } catch (UnsupportedEncodingException uee ) {
345
- //dbEncoding is not supported by the current JVM
346
- encoding = null ;
347
- }
348
- } else {
349
- encoding = null ;
350
- }
265
+ java .sql .ResultSet resultSet =
266
+ ExecSQL ("set datestyle to 'ISO'; " + encodingQuery );
267
+
268
+ if (! resultSet .next ()) {
269
+ throw new PSQLException ("postgresql.con.failed" , "failed getting backend encoding" );
351
270
}
271
+ dbEncoding = resultSet .getString (1 );
272
+ encoding = Encoding .getEncoding (dbEncoding , info .getProperty ("charSet" ));
352
273
353
274
// Initialise object handling
354
275
initObjectTypes ();
@@ -448,22 +369,7 @@ public java.sql.ResultSet ExecSQL(String sql,java.sql.Statement stat) throws SQL
448
369
int insert_oid = 0 ;
449
370
SQLException final_error = null ;
450
371
451
- // Commented out as the backend can now handle queries
452
- // larger than 8K. Peter June 6 2000
453
- //if (sql.length() > 8192)
454
- //throw new PSQLException("postgresql.con.toolong",sql);
455
-
456
- if (getEncoding () == null )
457
- buf = sql .getBytes ();
458
- else {
459
- try {
460
- buf = sql .getBytes (getEncoding ());
461
- } catch (UnsupportedEncodingException unse ) {
462
- throw new PSQLException ("postgresql.con.encoding" ,
463
- unse );
464
- }
465
- }
466
-
372
+ buf = encoding .encode (sql );
467
373
try
468
374
{
469
375
pg_stream .SendChar ('Q' );
@@ -484,7 +390,7 @@ public java.sql.ResultSet ExecSQL(String sql,java.sql.Statement stat) throws SQL
484
390
{
485
391
case 'A' : // Asynchronous Notify
486
392
pid = pg_stream .ReceiveInteger (4 );
487
- msg = pg_stream .ReceiveString (getEncoding () );
393
+ msg = pg_stream .ReceiveString (encoding );
488
394
break ;
489
395
case 'B' : // Binary Data Transfer
490
396
if (fields == null )
@@ -495,7 +401,7 @@ public java.sql.ResultSet ExecSQL(String sql,java.sql.Statement stat) throws SQL
495
401
tuples .addElement (tup );
496
402
break ;
497
403
case 'C' : // Command Status
498
- recv_status = pg_stream .ReceiveString (getEncoding () );
404
+ recv_status = pg_stream .ReceiveString (encoding );
499
405
500
406
// Now handle the update count correctly.
501
407
if (recv_status .startsWith ("INSERT" ) || recv_status .startsWith ("UPDATE" ) || recv_status .startsWith ("DELETE" ) || recv_status .startsWith ("MOVE" )) {
@@ -537,7 +443,7 @@ public java.sql.ResultSet ExecSQL(String sql,java.sql.Statement stat) throws SQL
537
443
tuples .addElement (tup );
538
444
break ;
539
445
case 'E' : // Error Message
540
- msg = pg_stream .ReceiveString (getEncoding () );
446
+ msg = pg_stream .ReceiveString (encoding );
541
447
final_error = new SQLException (msg );
542
448
hfr = true ;
543
449
break ;
@@ -552,10 +458,10 @@ public java.sql.ResultSet ExecSQL(String sql,java.sql.Statement stat) throws SQL
552
458
hfr = true ;
553
459
break ;
554
460
case 'N' : // Error Notification
555
- addWarning (pg_stream .ReceiveString (getEncoding () ));
461
+ addWarning (pg_stream .ReceiveString (encoding ));
556
462
break ;
557
463
case 'P' : // Portal Name
558
- String pname = pg_stream .ReceiveString (getEncoding () );
464
+ String pname = pg_stream .ReceiveString (encoding );
559
465
break ;
560
466
case 'T' : // MetaData Field Description
561
467
if (fields != null )
@@ -588,7 +494,7 @@ private Field[] ReceiveFields() throws SQLException
588
494
589
495
for (i = 0 ; i < nf ; ++i )
590
496
{
591
- String typname = pg_stream .ReceiveString (getEncoding () );
497
+ String typname = pg_stream .ReceiveString (encoding );
592
498
int typid = pg_stream .ReceiveIntegerR (4 );
593
499
int typlen = pg_stream .ReceiveIntegerR (2 );
594
500
int typmod = pg_stream .ReceiveIntegerR (4 );
@@ -653,11 +559,9 @@ public String getUserName() throws SQLException
653
559
}
654
560
655
561
/**
656
- * Get the character encoding to use for this connection.
657
- * @return the encoding to use, or <b>null</b> for the
658
- * default encoding.
562
+ * Get the character encoding to use for this connection.
659
563
*/
660
- public String getEncoding () throws SQLException {
564
+ public Encoding getEncoding () throws SQLException {
661
565
return encoding ;
662
566
}
663
567
0 commit comments