11
11
import org .postgresql .core .Encoding ;
12
12
13
13
/**
14
- * $Id: Connection.java,v 1.24 2001/08/07 17:45:29 momjian Exp $
14
+ * $Id: Connection.java,v 1.25 2001/08/10 14:42:07 momjian Exp $
15
15
*
16
16
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
17
17
* JDBC2 versions of the Connection class.
@@ -37,7 +37,6 @@ public abstract class Connection
37
37
*/
38
38
private Encoding encoding = Encoding .defaultEncoding ();
39
39
40
- private String dbVersionLong ;
41
40
private String dbVersionNumber ;
42
41
43
42
public boolean CONNECTION_OK = true ;
@@ -257,8 +256,6 @@ protected void openConnection(String host, int port, Properties info, String dat
257
256
258
257
firstWarning = null ;
259
258
260
- String dbEncoding ;
261
-
262
259
// "pg_encoding_to_char(1)" will return 'EUC_JP' for a backend compiled with multibyte,
263
260
// otherwise it's hardcoded to 'SQL_ASCII'.
264
261
// If the backend doesn't know about multibyte we can't assume anything about the encoding
@@ -276,8 +273,10 @@ protected void openConnection(String host, int port, Properties info, String dat
276
273
if (! resultSet .next ()) {
277
274
throw new PSQLException ("postgresql.con.failed" , "failed getting backend encoding" );
278
275
}
279
- dbVersionLong = resultSet .getString (1 );
280
- dbEncoding = resultSet .getString (2 );
276
+ String version = resultSet .getString (1 );
277
+ dbVersionNumber = extractVersionNumber (version );
278
+
279
+ String dbEncoding = resultSet .getString (2 );
281
280
encoding = Encoding .getEncoding (dbEncoding , info .getProperty ("charSet" ));
282
281
283
282
// Initialise object handling
@@ -1002,25 +1001,22 @@ public void setTransactionIsolation(int level) throws SQLException {
1002
1001
//this can be simplified
1003
1002
isolationLevel = level ;
1004
1003
String isolationLevelSQL ;
1005
- switch (isolationLevel ) {
1006
- case java .sql .Connection .TRANSACTION_READ_COMMITTED :
1007
- if (haveMinimumServerVersion ("7.1" )) {
1008
- isolationLevelSQL = "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED" ;
1009
- } else {
1010
- isolationLevelSQL = getIsolationLevelSQL ();
1011
- }
1012
- break ;
1013
-
1014
- case java .sql .Connection .TRANSACTION_SERIALIZABLE :
1015
- if (haveMinimumServerVersion ("7.1" )) {
1016
- isolationLevelSQL = "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE" ;
1017
- } else {
1018
- isolationLevelSQL = getIsolationLevelSQL ();
1019
- }
1020
- break ;
1021
1004
1022
- default :
1023
- throw new PSQLException ("postgresql.con.isolevel" ,new Integer (isolationLevel ));
1005
+ if (!haveMinimumServerVersion ("7.1" )) {
1006
+ isolationLevelSQL = getIsolationLevelSQL ();
1007
+ } else {
1008
+ isolationLevelSQL = "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL " ;
1009
+ switch (isolationLevel ) {
1010
+ case java .sql .Connection .TRANSACTION_READ_COMMITTED :
1011
+ isolationLevelSQL += "READ COMMITTED" ;
1012
+ break ;
1013
+ case java .sql .Connection .TRANSACTION_SERIALIZABLE :
1014
+ isolationLevelSQL += "SERIALIZABLE" ;
1015
+ break ;
1016
+ default :
1017
+ throw new PSQLException ("postgresql.con.isolevel" ,
1018
+ new Integer (isolationLevel ));
1019
+ }
1024
1020
}
1025
1021
ExecSQL (isolationLevelSQL );
1026
1022
}
@@ -1094,59 +1090,23 @@ public void finalize() throws Throwable
1094
1090
close ();
1095
1091
}
1096
1092
1097
- /**
1098
- * This is an attempt to implement SQL Escape clauses
1099
- */
1100
- public String EscapeSQL (String sql ) {
1101
- //if (DEBUG) { System.out.println ("parseSQLEscapes called"); }
1102
-
1103
- // If we find a "{d", assume we have a date escape.
1104
- //
1105
- // Since the date escape syntax is very close to the
1106
- // native Postgres date format, we just remove the escape
1107
- // delimiters.
1108
- //
1109
- // This implementation could use some optimization, but it has
1110
- // worked in practice for two years of solid use.
1111
- int index = sql .indexOf ("{d" );
1112
- while (index != -1 ) {
1113
- //System.out.println ("escape found at index: " + index);
1114
- StringBuffer buf = new StringBuffer (sql );
1115
- buf .setCharAt (index , ' ' );
1116
- buf .setCharAt (index + 1 , ' ' );
1117
- buf .setCharAt (sql .indexOf ('}' , index ), ' ' );
1118
- sql = new String (buf );
1119
- index = sql .indexOf ("{d" );
1120
- }
1121
- //System.out.println ("modified SQL: " + sql);
1122
- return sql ;
1123
- }
1124
-
1125
- /**
1126
- * What is the version of the server
1127
- *
1128
- * @return the database version
1129
- * @exception SQLException if a database access error occurs
1130
- */
1131
- public String getDBVersionNumber () throws SQLException
1093
+ private static String extractVersionNumber (String fullVersionString )
1132
1094
{
1133
- if (dbVersionNumber == null ) {
1134
- StringTokenizer versionParts = new StringTokenizer (dbVersionLong );
1095
+ StringTokenizer versionParts = new StringTokenizer (fullVersionString );
1135
1096
versionParts .nextToken (); /* "PostgreSQL" */
1136
- dbVersionNumber = versionParts .nextToken (); /* "X.Y.Z" */
1137
- }
1138
- return dbVersionNumber ;
1097
+ return versionParts .nextToken (); /* "X.Y.Z" */
1139
1098
}
1140
1099
1100
+ /**
1101
+ * Get server version number
1102
+ */
1103
+ public String getDBVersionNumber () {
1104
+ return dbVersionNumber ;
1105
+ }
1106
+
1141
1107
public boolean haveMinimumServerVersion (String ver ) throws SQLException
1142
1108
{
1143
- if (getDBVersionNumber ().compareTo (ver )>=0 )
1144
- return true ;
1145
- else
1146
- return false ;
1109
+ return (getDBVersionNumber ().compareTo (ver ) >= 0 );
1147
1110
}
1148
-
1149
-
1150
-
1151
1111
}
1152
1112
0 commit comments