mirror of
https://github.com/postgres/postgres.git
synced 2025-12-06 00:02:13 +03:00
Fix for java to allow password, european dates,from Peter T Mount
This commit is contained in:
@@ -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,13 +443,36 @@ public class Connection implements java.sql.Connection
|
||||
*/
|
||||
public void clearWarnings() throws SQLException
|
||||
{
|
||||
// Not handles since we handle wanrings as errors
|
||||
firstWarning = null;
|
||||
}
|
||||
|
||||
// **********************************************************
|
||||
// END OF PUBLIC INTERFACE
|
||||
// **********************************************************
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
@@ -377,17 +377,22 @@ public class ResultSet implements java.sql.ResultSet
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (s.length() != 10)
|
||||
throw new NumberFormatException("Wrong Length!");
|
||||
int mon = Integer.parseInt(s.substring(0,2));
|
||||
int day = Integer.parseInt(s.substring(3,5));
|
||||
int yr = Integer.parseInt(s.substring(6));
|
||||
return new java.sql.Date(yr - 1900, mon -1, day);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new SQLException("Bad Date Form: " + s);
|
||||
try {
|
||||
if (s.length() != 10)
|
||||
throw new NumberFormatException("Wrong Length!");
|
||||
int mon = Integer.parseInt(s.substring(0,2));
|
||||
int day = Integer.parseInt(s.substring(3,5));
|
||||
int yr = Integer.parseInt(s.substring(6));
|
||||
if(connection.europeanDates) {
|
||||
// We europeans prefer dd mm yyyy
|
||||
int t = mon;
|
||||
mon = day;
|
||||
day = t;
|
||||
}
|
||||
return new java.sql.Date(yr - 1900, mon -1, day);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new SQLException("Bad Date Form: " + s);
|
||||
}
|
||||
}
|
||||
return null; // SQL NULL
|
||||
}
|
||||
@@ -432,19 +437,24 @@ public class ResultSet implements java.sql.ResultSet
|
||||
public Timestamp getTimestamp(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
DateFormat df = DateFormat.getDateInstance();
|
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz");
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
java.sql.Date d = (java.sql.Date)df.parse(s);
|
||||
return new Timestamp(d.getTime());
|
||||
} catch (ParseException e) {
|
||||
throw new SQLException("Bad Timestamp Format: " + s);
|
||||
}
|
||||
int TZ = new Float(s.substring(19)).intValue();
|
||||
TZ = TZ * 60 * 60 * 1000;
|
||||
TimeZone zone = TimeZone.getDefault();
|
||||
zone.setRawOffset(TZ);
|
||||
String nm = zone.getID();
|
||||
s = s.substring(0,18) + nm;
|
||||
try {
|
||||
java.util.Date d = df.parse(s);
|
||||
return new Timestamp(d.getTime());
|
||||
} catch (ParseException e) {
|
||||
throw new SQLException("Bad Timestamp Format: at " + e.getErrorOffset() + " in " + s);
|
||||
}
|
||||
}
|
||||
return null; // SQL NULL
|
||||
return null; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user