diff options
author | Bruce Momjian | 1997-10-30 18:24:44 +0000 |
---|---|---|
committer | Bruce Momjian | 1997-10-30 18:24:44 +0000 |
commit | 0e583068579748ad528685fa6040636b374177c6 (patch) | |
tree | 4f546dc90e08ffa00480ce8e0acd184cfade3fe6 /src/interfaces/jdbc/postgresql/Connection.java | |
parent | 2cc73c0d420a43c0c4245ad76b72241e8610e39d (diff) |
Fix for java to allow password, european dates,from Peter T Mount
Diffstat (limited to 'src/interfaces/jdbc/postgresql/Connection.java')
-rw-r--r-- | src/interfaces/jdbc/postgresql/Connection.java | 77 |
1 files changed, 71 insertions, 6 deletions
diff --git a/src/interfaces/jdbc/postgresql/Connection.java b/src/interfaces/jdbc/postgresql/Connection.java index bb41dff01bb..e79d1fe953b 100644 --- a/src/interfaces/jdbc/postgresql/Connection.java +++ b/src/interfaces/jdbc/postgresql/Connection.java @@ -36,11 +36,15 @@ public class Connection implements java.sql.Connection private String PG_PASSWORD; private String PG_DATABASE; private boolean PG_STATUS; + private boolean PG_AUTH; // true, then password auth used public boolean CONNECTION_OK = true; public boolean CONNECTION_BAD = false; - private int STARTUP_CODE = 7; + private int STARTUP_CODE = STARTUP_USER; + private static final int STARTUP_USER = 7; // User auth + private static final int STARTUP_PASS = 14; // Password auth + private static final int STARTUP_LEN = 288; // Length of a startup packet private boolean autoCommit = true; private boolean readOnly = false; @@ -49,6 +53,12 @@ public class Connection implements java.sql.Connection private String this_url; private String cursor = null; // The positioned update cursor name + // This is false for US, true for European date formats + protected boolean europeanDates = false; + + // Now handle notices as warnings, so things like "show" now work + protected SQLWarning firstWarning = null; + /** * Connect to a PostgreSQL database back end. * @@ -63,7 +73,7 @@ public class Connection implements java.sql.Connection */ public Connection(String host, int port, Properties info, String database, String url, Driver d) throws SQLException { - int len = 288; // Length of a startup packet + int len = STARTUP_LEN; // Length of a startup packet this_driver = d; this_url = new String(url); @@ -74,6 +84,13 @@ public class Connection implements java.sql.Connection PG_HOST = new String(host); PG_STATUS = CONNECTION_BAD; + if(info.getProperty("auth") != null) { + PG_AUTH=true; + STARTUP_CODE=STARTUP_PASS; + } else { + STARTUP_CODE=STARTUP_USER; + } + try { pg_stream = new PG_Stream(host, port); @@ -88,10 +105,34 @@ public class Connection implements java.sql.Connection pg_stream.SendInteger(STARTUP_CODE, 4); len -= 4; pg_stream.Send(database.getBytes(), 64); len -= 64; pg_stream.Send(PG_USER.getBytes(), len); + + // Send the password packet if required + if(PG_AUTH) { + len=STARTUP_LEN; + pg_stream.SendInteger(len, 4); len -= 4; + pg_stream.SendInteger(STARTUP_PASS, 4); len -= 4; + pg_stream.Send(PG_USER.getBytes(), PG_USER.length()); + len-=PG_USER.length(); + pg_stream.SendInteger(0,1); len -= 1; + pg_stream.Send(PG_PASSWORD.getBytes(), len); + } + } catch (IOException e) { throw new SQLException("Connection failed: " + e.toString()); } - ExecSQL(" "); // Test connection + + // Find out the date style by issuing the SQL: show datestyle + // This actually issues a warning, and our own warning handling + // code handles this itself. + // + // Also, this query replaced the NULL query issued to test the + // connection. + // + clearWarnings(); + ExecSQL("show datestyle"); + + // Mark the connection as ok, and cleanup + clearWarnings(); PG_STATUS = CONNECTION_OK; } @@ -391,7 +432,7 @@ public class Connection implements java.sql.Connection */ public SQLWarning getWarnings() throws SQLException { - return null; // We handle warnings as errors + return firstWarning; } /** @@ -402,7 +443,7 @@ public class Connection implements java.sql.Connection */ public void clearWarnings() throws SQLException { - // Not handles since we handle wanrings as errors + firstWarning = null; } // ********************************************************** @@ -410,6 +451,29 @@ public class Connection implements java.sql.Connection // ********************************************************** /** + * This adds a warning to the warning chain + */ + public void addWarning(String msg) + { + // Add the warning to the chain + if(firstWarning!=null) + firstWarning.setNextWarning(new SQLWarning(msg)); + else + firstWarning = new SQLWarning(msg); + + // Now check for some specific messages + + // This is generated by the SQL "show datestyle" + if(msg.startsWith("NOTICE:DateStyle")) { + if(msg.indexOf("with US")==-1) + europeanDates=true; + else + europeanDates=false; + System.err.println("europeanDates="+europeanDates); + } + } + + /** * Send a query to the backend. Returns one of the ResultSet * objects. * @@ -497,7 +561,8 @@ public class Connection implements java.sql.Connection case 'N': // Error Notification msg = pg_stream.ReceiveString(4096); PrintStream log = DriverManager.getLogStream(); - log.println(msg); + if(log!=null) log.println(msg); + addWarning(msg); break; case 'P': // Portal Name String pname = pg_stream.ReceiveString(8192); |