1111import org .postgresql .core .Encoding ;
1212
1313/**
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 $
1515 *
1616 * This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
1717 * JDBC2 versions of the Connection class.
@@ -37,7 +37,6 @@ public abstract class Connection
3737 */
3838private Encoding encoding =Encoding .defaultEncoding ();
3939
40- private String dbVersionLong ;
4140private String dbVersionNumber ;
4241
4342public boolean CONNECTION_OK =true ;
@@ -257,8 +256,6 @@ protected void openConnection(String host, int port, Properties info, String dat
257256
258257firstWarning =null ;
259258
260- String dbEncoding ;
261-
262259// "pg_encoding_to_char(1)" will return 'EUC_JP' for a backend compiled with multibyte,
263260// otherwise it's hardcoded to 'SQL_ASCII'.
264261// 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
276273if (!resultSet .next ()) {
277274throw new PSQLException ("postgresql.con.failed" ,"failed getting backend encoding" );
278275 }
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 );
281280encoding =Encoding .getEncoding (dbEncoding ,info .getProperty ("charSet" ));
282281
283282// Initialise object handling
@@ -1002,25 +1001,22 @@ public void setTransactionIsolation(int level) throws SQLException {
10021001//this can be simplified
10031002isolationLevel =level ;
10041003String 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 ;
10211004
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+ }
10241020}
10251021ExecSQL (isolationLevelSQL );
10261022 }
@@ -1094,59 +1090,23 @@ public void finalize() throws Throwable
10941090close ();
10951091 }
10961092
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 )
11321094 {
1133- if (dbVersionNumber ==null ) {
1134- StringTokenizer versionParts =new StringTokenizer (dbVersionLong );
1095+ StringTokenizer versionParts =new StringTokenizer (fullVersionString );
11351096versionParts .nextToken ();/* "PostgreSQL" */
1136- dbVersionNumber =versionParts .nextToken ();/* "X.Y.Z" */
1137- }
1138- return dbVersionNumber ;
1097+ return versionParts .nextToken ();/* "X.Y.Z" */
11391098 }
11401099
1100+ /**
1101+ * Get server version number
1102+ */
1103+ public String getDBVersionNumber () {
1104+ return dbVersionNumber ;
1105+ }
1106+
11411107public boolean haveMinimumServerVersion (String ver )throws SQLException
11421108 {
1143- if (getDBVersionNumber ().compareTo (ver )>=0 )
1144- return true ;
1145- else
1146- return false ;
1109+ return (getDBVersionNumber ().compareTo (ver ) >=0 );
11471110 }
1148-
1149-
1150-
11511111}
11521112