Commit 3f59cc08 authored by Peter Mount's avatar Peter Mount

Minor bug fixes. Replaced DateStyle support with ISO.

parent c2b75c83
Mon May 17 23:40:00 BST 1999
- PG_Stream.close() now attempts to send the close connection message
to the backend before closing the streams
- Added batch support in the JDBC2, supplied by Yutaka Tanida <yutaka@marin.or.jp>
- Removed the old datestyle code. Now the driver uses only ISO.
- Removed some files in the postgresql directory still in CVS that were
moved since 6.4.x (DatabaseMetaData.java PreparedStatement.java
ResultSetMetaData.java Statement.java)
- Internationalisation of the error messages is partially implemented, however
it's not enabled as it only works when the jar file is _not_ used, and
work needs to be done.
Sun Apr 11 17:00:00 BST 1999
- getUpdateCount() now returns the actual update count (before it
simply returned 1 for everything).
......
......@@ -10,7 +10,7 @@ import postgresql.largeobject.*;
import postgresql.util.*;
/**
* $Id: Connection.java,v 1.15 1999/04/11 18:03:00 peter Exp $
* $Id: Connection.java,v 1.16 1999/05/17 22:43:23 peter Exp $
*
* This abstract class is used by postgresql.Driver to open either the JDBC1 or
* JDBC2 versions of the Connection class.
......@@ -67,38 +67,6 @@ public abstract class Connection
// be across all connections, which could be to different backends.
public Hashtable fieldCache = new Hashtable();
/**
* This is the current date style of the backend
*/
public int currentDateStyle;
/**
* This defines the formats for dates, according to the various date styles.
*
* <p>There are two strings for each entry. The first is the string to search
* for in the datestyle message, and the second the format to use.
*
* <p>To add a new date style, work out the format. Then with psql running
* in the date style you wish to add, type: show datestyle;
*
* <p>eg:
* <br><pre>
* => show datestyle;
* NOTICE: Datestyle is SQL with European conventions
* ^^^^^^^^^^^^^^^^^
* </pre>The marked part of the string is the first string below. The second
* is your format. If a style (like ISO) ignores the US/European variants,
* then you can ignore the "with" part of the string.
*/
protected static final String dateStyles[] = {
"Postgres with European", "dd-MM-yyyy",
"Postgres with US", "MM-dd-yyyy",
"ISO", "yyyy-MM-dd",
"SQL with European", "dd/MM/yyyy",
"SQL with US", "MM/dd/yyyy",
"German", "dd.MM.yyyy"
};
// Now handle notices as warnings, so things like "show" now work
public SQLWarning firstWarning = null;
......@@ -242,22 +210,24 @@ public abstract class Connection
throw new SQLException("Connection failed: " + e.toString());
}
// 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.
//
// Originally we issued a SHOW DATESTYLE statement to find the databases default
// datestyle. However, this caused some problems with timestamps, so in 6.5, we
// went the way of ODBC, and set the connection to ISO.
//
// This may cause some clients to break when they assume anything other than ISO,
// but then - they should be using the proper methods ;-)
//
//
firstWarning = null;
ExecSQL("show datestyle");
// Initialise object handling
initObjectTypes();
// Mark the connection as ok, and cleanup
ExecSQL("set datestyle to 'ISO'");
// Initialise object handling
initObjectTypes();
// Mark the connection as ok, and cleanup
firstWarning = null;
PG_STATUS = CONNECTION_OK;
PG_STATUS = CONNECTION_OK;
}
// These methods used to be in the main Connection implementation. As they
......@@ -280,23 +250,18 @@ public abstract class Connection
// Now check for some specific messages
// This is obsolete in 6.5, but I've left it in here so if we need to use this
// technique again, we'll know where to place it.
//
// This is generated by the SQL "show datestyle"
if(msg.startsWith("NOTICE:") && msg.indexOf("DateStyle")>0) {
// 13 is the length off "DateStyle is "
msg = msg.substring(msg.indexOf("DateStyle is ")+13);
for(int i=0;i<dateStyles.length;i+=2)
if(msg.startsWith(dateStyles[i]))
currentDateStyle=i+1; // this is the index of the format
}
}
/**
* @return the date format for the current date style of the backend
*/
public String getDateStyle()
{
return dateStyles[currentDateStyle];
//if(msg.startsWith("NOTICE:") && msg.indexOf("DateStyle")>0) {
//// 13 is the length off "DateStyle is "
//msg = msg.substring(msg.indexOf("DateStyle is ")+13);
//
//for(int i=0;i<dateStyles.length;i+=2)
//if(msg.startsWith(dateStyles[i]))
//currentDateStyle=i+1; // this is the index of the format
//}
}
/**
......
This diff is collapsed.
......@@ -3,6 +3,13 @@ package postgresql;
import java.sql.*;
import java.util.*;
// You will find some mentions to a PSQLException class. This was intended
// to allow internationalisation of error messages. However, this is not
// working quite to plan, so the class exists in the source, but it's not
// quite implemented yet. Peter May 17 1999.
//
//import postgresql.util.PSQLException;
/**
* The Java SQL framework allows for multiple database drivers. Each
* driver should supply a class that implements the Driver interface
......@@ -101,8 +108,10 @@ public class Driver implements java.sql.Driver
con.openConnection (host(), port(), props, database(), url, this);
return (java.sql.Connection)con;
} catch(ClassNotFoundException ex) {
//throw new PSQLException("postgresql.jvm.version",ex);
throw new SQLException("The postgresql.jar file does not contain the correct JDBC classes for this JVM. Try rebuilding.\nException thrown was "+ex.toString());
} catch(Exception ex2) {
//throw new PSQLException("postgresql.unusual",ex2);
throw new SQLException("Something unusual has occured to cause the driver to fail. Please report this exception: "+ex2.toString());
}
// The old call - remove before posting
......@@ -346,6 +355,7 @@ public class Driver implements java.sql.Driver
*/
public static SQLException notImplemented()
{
//return new PSQLException("postgresql.unimplemented");
return new SQLException("This method is not yet implemented.");
}
}
......
......@@ -378,6 +378,8 @@ public class PG_Stream
*/
public void close() throws IOException
{
pg_output.write("X\0".getBytes());
pg_output.flush();
pg_output.close();
pg_input.close();
connection.close();
......
This diff is collapsed.
This diff is collapsed.
package postgresql;
import java.sql.*;
/**
* A Statement object is used for executing a static SQL statement and
* obtaining the results produced by it.
*
* <p>Only one ResultSet per Statement can be open at any point in time.
* Therefore, if the reading of one ResultSet is interleaved with the
* reading of another, each must have been generated by different
* Statements. All statement execute methods implicitly close a
* statement's current ResultSet if an open one exists.
*
* @see java.sql.Statement
* @see ResultSet
*/
public class Statement implements java.sql.Statement
{
Connection connection; // The connection who created us
ResultSet result = null; // The current results
SQLWarning warnings = null; // The warnings chain.
int timeout = 0; // The timeout for a query (not used)
boolean escapeProcessing = true;// escape processing flag
/**
* Constructor for a Statement. It simply sets the connection
* that created us.
*
* @param c the Connection instantation that creates us
*/
public Statement (Connection c)
{
connection = c;
}
/**
* Execute a SQL statement that returns a single ResultSet
*
* @param sql typically a static SQL SELECT statement
* @return a ResulSet that contains the data produced by the query
* @exception SQLException if a database access error occurs
*/
public java.sql.ResultSet executeQuery(String sql) throws SQLException
{
this.execute(sql);
while (result != null && !result.reallyResultSet())
result = result.getNext();
if (result == null)
throw new SQLException("no results returned");
return result;
}
/**
* Execute a SQL INSERT, UPDATE or DELETE statement. In addition
* SQL statements that return nothing such as SQL DDL statements
* can be executed
*
* @param sql a SQL statement
* @return either a row count, or 0 for SQL commands
* @exception SQLException if a database access error occurs
*/
public int executeUpdate(String sql) throws SQLException
{
this.execute(sql);
if (result.reallyResultSet())
throw new SQLException("results returned");
return this.getUpdateCount();
}
/**
* In many cases, it is desirable to immediately release a
* Statement's database and JDBC resources instead of waiting
* for this to happen when it is automatically closed. The
* close method provides this immediate release.
*
* <p><B>Note:</B> A Statement is automatically closed when it is
* garbage collected. When a Statement is closed, its current
* ResultSet, if one exists, is also closed.
*
* @exception SQLException if a database access error occurs (why?)
*/
public void close() throws SQLException
{
result = null;
}
/**
* The maxFieldSize limit (in bytes) is the maximum amount of
* data returned for any column value; it only applies to
* BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR and LONGVARCHAR
* columns. If the limit is exceeded, the excess data is silently
* discarded.
*
* @return the current max column size limit; zero means unlimited
* @exception SQLException if a database access error occurs
*/
public int getMaxFieldSize() throws SQLException
{
return 8192; // We cannot change this
}
/**
* Sets the maxFieldSize - NOT! - We throw an SQLException just
* to inform them to stop doing this.
*
* @param max the new max column size limit; zero means unlimited
* @exception SQLException if a database access error occurs
*/
public void setMaxFieldSize(int max) throws SQLException
{
throw new SQLException("Attempt to setMaxFieldSize failed - compile time default");
}
/**
* The maxRows limit is set to limit the number of rows that
* any ResultSet can contain. If the limit is exceeded, the
* excess rows are silently dropped.
*
* @return the current maximum row limit; zero means unlimited
* @exception SQLException if a database access error occurs
*/
public int getMaxRows() throws SQLException
{
return connection.maxrows;
}
/**
* Set the maximum number of rows
*
* @param max the new max rows limit; zero means unlimited
* @exception SQLException if a database access error occurs
* @see getMaxRows
*/
public void setMaxRows(int max) throws SQLException
{
connection.maxrows = max;
}
/**
* If escape scanning is on (the default), the driver will do escape
* substitution before sending the SQL to the database.
*
* @param enable true to enable; false to disable
* @exception SQLException if a database access error occurs
*/
public void setEscapeProcessing(boolean enable) throws SQLException
{
escapeProcessing = enable;
}
/**
* The queryTimeout limit is the number of seconds the driver
* will wait for a Statement to execute. If the limit is
* exceeded, a SQLException is thrown.
*
* @return the current query timeout limit in seconds; 0 = unlimited
* @exception SQLException if a database access error occurs
*/
public int getQueryTimeout() throws SQLException
{
return timeout;
}
/**
* Sets the queryTimeout limit
*
* @param seconds - the new query timeout limit in seconds
* @exception SQLException if a database access error occurs
*/
public void setQueryTimeout(int seconds) throws SQLException
{
timeout = seconds;
}
/**
* Cancel can be used by one thread to cancel a statement that
* is being executed by another thread. However, PostgreSQL is
* a sync. sort of thing, so this really has no meaning - we
* define it as a no-op (i.e. you can't cancel, but there is no
* error if you try.)
*
* @exception SQLException only because thats the spec.
*/
public void cancel() throws SQLException
{
// No-op
}
/**
* The first warning reported by calls on this Statement is
* returned. A Statement's execute methods clear its SQLWarning
* chain. Subsequent Statement warnings will be chained to this
* SQLWarning.
*
* <p>The Warning chain is automatically cleared each time a statement
* is (re)executed.
*
* <p><B>Note:</B> If you are processing a ResultSet then any warnings
* associated with ResultSet reads will be chained on the ResultSet
* object.
*
* @return the first SQLWarning on null
* @exception SQLException if a database access error occurs
*/
public SQLWarning getWarnings() throws SQLException
{
return warnings;
}
/**
* After this call, getWarnings returns null until a new warning
* is reported for this Statement.
*
* @exception SQLException if a database access error occurs (why?)
*/
public void clearWarnings() throws SQLException
{
warnings = null;
}
/**
* setCursorName defines the SQL cursor name that will be used by
* subsequent execute methods. This name can then be used in SQL
* positioned update/delete statements to identify the current row
* in the ResultSet generated by this statement. If a database
* doesn't support positioned update/delete, this method is a
* no-op.
*
* <p><B>Note:</B> By definition, positioned update/delete execution
* must be done by a different Statement than the one which
* generated the ResultSet being used for positioning. Also, cursor
* names must be unique within a Connection.
*
* <p>We throw an additional constriction. There can only be one
* cursor active at any one time.
*
* @param name the new cursor name
* @exception SQLException if a database access error occurs
*/
public void setCursorName(String name) throws SQLException
{
connection.setCursorName(name);
}
/**
* Execute a SQL statement that may return multiple results. We
* don't have to worry about this since we do not support multiple
* ResultSets. You can use getResultSet or getUpdateCount to
* retrieve the result.
*
* @param sql any SQL statement
* @return true if the next result is a ResulSet, false if it is
* an update count or there are no more results
* @exception SQLException if a database access error occurs
*/
public boolean execute(String sql) throws SQLException
{
result = connection.ExecSQL(sql);
return (result != null && result.reallyResultSet());
}
/**
* getResultSet returns the current result as a ResultSet. It
* should only be called once per result.
*
* @return the current result set; null if there are no more
* @exception SQLException if a database access error occurs (why?)
*/
public java.sql.ResultSet getResultSet() throws SQLException
{
return result;
}
/**
* getUpdateCount returns the current result as an update count,
* if the result is a ResultSet or there are no more results, -1
* is returned. It should only be called once per result.
*
* @return the current result as an update count.
* @exception SQLException if a database access error occurs
*/
public int getUpdateCount() throws SQLException
{
if (result == null) return -1;
if (result.reallyResultSet()) return -1;
return result.getResultCount();
}
/**
* getMoreResults moves to a Statement's next result. If it returns
* true, this result is a ResulSet.
*
* @return true if the next ResultSet is valid
* @exception SQLException if a database access error occurs
*/
public boolean getMoreResults() throws SQLException
{
result = result.getNext();
return (result != null && result.reallyResultSet());
}
/**
* Returns the status message from the current Result.<p>
* This is used internally by the driver.
*
* @return status message from backend
*/
public String getResultStatusString()
{
if(result == null)
return null;
return result.getStatusString();
}
}
# This is the default errors
postgresql.jvm.version:The postgresql.jar file does not contain the correct JDBC classes for this JVM. Try rebuilding.\nException thrown was {1}
postgresql.unusual:Something unusual has occured to cause the driver to fail. Please report this exception: {1}
postgresql.unimplemented:This method is not yet implemented.
# This is the default errors
postgresql.jvm.version:Le fichier de postgresql.jar ne contient pas les classes correctes de JDBC pour ce JVM. Try que rebuilding.\nException jetées était {1}
postgresql.unusual:Quelque chose de peu commun s'est produit pour faire échouer le gestionnaire. Veuillez enregistrer cette exception: {1}
postgresql.unimplemented:Cette méthode n'est pas encore appliquée.
......@@ -312,7 +312,7 @@ public class PreparedStatement extends Statement implements java.sql.PreparedSta
*/
public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
{
SimpleDateFormat df = new SimpleDateFormat("''"+connection.getDateStyle()+"''");
SimpleDateFormat df = new SimpleDateFormat("''yyyy-MM-dd''");
set(parameterIndex, df.format(x));
......
......@@ -386,7 +386,7 @@ public class ResultSet extends postgresql.ResultSet implements java.sql.ResultSe
String s = getString(columnIndex);
if(s==null)
return null;
SimpleDateFormat df = new SimpleDateFormat(connection.getDateStyle());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return new java.sql.Date(df.parse(s).getTime());
} catch (ParseException e) {
......@@ -443,7 +443,7 @@ public class ResultSet extends postgresql.ResultSet implements java.sql.ResultSe
TimeZone zone = TimeZone.getDefault();
zone.setRawOffset(TZ);
String nm = zone.getID();
s = s.substring(0,18) + nm;
s = s.substring(0,19) + nm;
try {
java.util.Date d = df.parse(s);
return new Timestamp(d.getTime());
......
......@@ -312,7 +312,7 @@ public class PreparedStatement extends Statement implements java.sql.PreparedSta
*/
public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
{
SimpleDateFormat df = new SimpleDateFormat("''"+connection.getDateStyle()+"''");
SimpleDateFormat df = new SimpleDateFormat("''yyyy-MM-dd''");
set(parameterIndex, df.format(x));
......
......@@ -388,7 +388,7 @@ public class ResultSet extends postgresql.ResultSet implements java.sql.ResultSe
String s = getString(columnIndex);
if(s==null)
return null;
SimpleDateFormat df = new SimpleDateFormat(connection.getDateStyle());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return new java.sql.Date(df.parse(s).getTime());
} catch (ParseException e) {
......@@ -445,7 +445,7 @@ public class ResultSet extends postgresql.ResultSet implements java.sql.ResultSe
TimeZone zone = TimeZone.getDefault();
zone.setRawOffset(TZ);
String nm = zone.getID();
s = s.substring(0,18) + nm;
s = s.substring(0,19) + nm;
try {
java.util.Date d = df.parse(s);
return new Timestamp(d.getTime());
......
......@@ -6,6 +6,7 @@ package postgresql.jdbc2;
// postgresql.jdbc1 package.
import java.sql.*;
import java.util.Vector;
/**
* A Statement object is used for executing a static SQL statement and
......@@ -27,6 +28,7 @@ public class Statement implements java.sql.Statement
SQLWarning warnings = null; // The warnings chain.
int timeout = 0; // The timeout for a query (not used)
boolean escapeProcessing = true;// escape processing flag
private Vector batch=null;
/**
* Constructor for a Statement. It simply sets the connection
......@@ -325,17 +327,35 @@ public class Statement implements java.sql.Statement
public void addBatch(String sql) throws SQLException
{
throw postgresql.Driver.notImplemented();
if(batch==null)
batch=new Vector();
batch.addElement(sql);
}
public void clearBatch() throws SQLException
{
throw postgresql.Driver.notImplemented();
if(batch!=null)
batch.removeAllElements();
}
public int[] executeBatch() throws SQLException
{
throw postgresql.Driver.notImplemented();
if(batch==null || batch.isEmpty())
throw new SQLException("The batch is empty.");
int size=batch.size();
int[] result=new int[size];
int i=0;
this.execute("begin"); // PTM: check this when autoCommit is false
try {
for(i=0;i<size;i++)
result[i]=this.executeUpdate((String)batch.elementAt(i));
this.execute("commit"); // PTM: check this
} catch(SQLException e) {
this.execute("abort"); // PTM: check this
throw new SQLException("The result "+i+" \""+batch.elementAt(i)+"\" aborted.");
}
return result;
}
public java.sql.Connection getConnection() throws SQLException
......
package postgresql.util;
import java.sql.*;
import java.text.*;
import java.util.*;
/**
* This class extends SQLException, and provides our internationalisation handling
*/
public class PSQLException extends SQLException
{
private String message;
// Cache for future errors
static ResourceBundle bundle;
/**
* This provides the same functionality to SQLException
* @param error Error string
*/
public PSQLException(String error) {
super();
translate(error,null);
}
/**
* A more generic entry point.
* @param error Error string or standard message id
* @param args Array of arguments
*/
public PSQLException(String error,Object[] args)
{
super();
translate(error,args);
}
/**
* Helper version for 1 arg
*/
public PSQLException(String error,Object arg)
{
super();
Object[] argv = new Object[1];
argv[0] = arg;
translate(error,argv);
}
/**
* This does the actual translation
*/
private void translate(String id,Object[] args)
{
if(bundle == null) {
try {
bundle = ResourceBundle.getBundle("postgresql.errors");
} catch(MissingResourceException e) {
}
}
// Now look up a localized message. If one is not found, then use
// the supplied message instead.
message = null;
try {
message = bundle.getString(id);
} catch(MissingResourceException e) {
message = id;
}
// Expand any arguments
if(args!=null)
message = MessageFormat.format(message,args);
}
/**
* Overides Throwable
*/
public String getLocalizedMessage()
{
return message;
}
/**
* Overides Throwable
*/
public String getMessage()
{
return message;
}
/**
* Overides Object
*/
public String toString()
{
return message;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment