1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-18 17:42:25 +03:00

Backpatch jdbc fixes into 7.0.X.

This commit is contained in:
Bruce Momjian
2000-06-15 04:12:41 +00:00
parent 3a82b67b22
commit c545ec54f8
10 changed files with 156 additions and 41 deletions

View File

@ -1,3 +1,19 @@
Tue Jun 06 12:00:00 BST 2000 petermount@it.maidstone.gov.uk
- Added org/postgresql/DriverClass.java to the list of files removed
by make clean (it's dynamically built)
- Fixed Statement, so that the update count is valid when an SQL
DELETE operation is done.
- While fixing the update count, made it easier to get the OID of
the last insert as well. Example is in example/basic.java
Tue Jun 06 08:37:00 BST 2000 petermount@it.maidstone.gov.uk
- Removed a hardwired 8K limit on query strings
- Added some missing org.'s in Connection that prevented
the use of the geometric types.
Thu Jun 01 07:26:00 BST 2000 petermount@it.maidstone.gov.uk
- Removed timezone in getTimestamp() methods in ResultSet.
Mon May 15 22:30:00 BST 2000 peter@retep.org.uk Mon May 15 22:30:00 BST 2000 peter@retep.org.uk
- Fixed the message Makefile produces after compiling. It still said - Fixed the message Makefile produces after compiling. It still said
about the old Driver class, not the new package. Spotted by about the old Driver class, not the new package. Spotted by

View File

@ -4,7 +4,7 @@
# Makefile for Java JDBC interface # Makefile for Java JDBC interface
# #
# IDENTIFICATION # IDENTIFICATION
# $Id: Makefile,v 1.22 2000/05/15 21:32:51 peter Exp $ # $Id: Makefile,v 1.22.2.1 2000/06/15 04:12:23 momjian Exp $
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -181,7 +181,7 @@ clean:
$(FIND) . -name "*.class" -exec $(RM) {} \; $(FIND) . -name "*.class" -exec $(RM) {} \;
$(FIND) . -name "*.html" -exec $(RM) {} \; $(FIND) . -name "*.html" -exec $(RM) {} \;
-$(RM) -rf stock example/corba/stock.built -$(RM) -rf stock example/corba/stock.built
-$(RM) postgresql.jar -$(RM) postgresql.jar org/postgresql/DriverClass.java
-$(RM) -rf Package-postgresql *output -$(RM) -rf Package-postgresql *output
####################################################################### #######################################################################

View File

@ -6,7 +6,7 @@ import java.text.*;
/** /**
* *
* $Id: basic.java,v 1.4 2000/04/26 05:32:00 peter Exp $ * $Id: basic.java,v 1.4.2.1 2000/06/15 04:12:24 momjian Exp $
* *
* This example tests the basic components of the JDBC driver, and shows * This example tests the basic components of the JDBC driver, and shows
* how even the simplest of queries can be implemented. * how even the simplest of queries can be implemented.
@ -83,10 +83,19 @@ public class basic
st.executeUpdate("insert into basic values (2,1)"); st.executeUpdate("insert into basic values (2,1)");
st.executeUpdate("insert into basic values (3,1)"); st.executeUpdate("insert into basic values (3,1)");
// This shows how to get the oid of a just inserted row
st.executeUpdate("insert into basic values (4,1)");
int insertedOID = ((org.postgresql.ResultSet)st.getResultSet()).getInsertedOID();
System.out.println("Inserted row with oid "+insertedOID);
// Now change the value of b from 1 to 8 // Now change the value of b from 1 to 8
st.executeUpdate("update basic set b=8"); st.executeUpdate("update basic set b=8");
System.out.println("Updated "+st.getUpdateCount()+" rows"); System.out.println("Updated "+st.getUpdateCount()+" rows");
// Now delete 2 rows
st.executeUpdate("delete from basic where a<3");
System.out.println("deleted "+st.getUpdateCount()+" rows");
// For large inserts, a PreparedStatement is more efficient, because it // For large inserts, a PreparedStatement is more efficient, because it
// supports the idea of precompiling the SQL statement, and to store // supports the idea of precompiling the SQL statement, and to store
// directly, a Java object into any column. PostgreSQL doesnt support // directly, a Java object into any column. PostgreSQL doesnt support

View File

@ -10,7 +10,7 @@ import org.postgresql.largeobject.*;
import org.postgresql.util.*; import org.postgresql.util.*;
/** /**
* $Id: Connection.java,v 1.1 2000/04/26 05:39:32 peter Exp $ * $Id: Connection.java,v 1.1.2.1 2000/06/15 04:12:28 momjian Exp $
* *
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or * This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
* JDBC2 versions of the Connection class. * JDBC2 versions of the Connection class.
@ -317,11 +317,14 @@ public abstract class Connection
int fqp = 0; int fqp = 0;
boolean hfr = false; boolean hfr = false;
String recv_status = null, msg; String recv_status = null, msg;
int update_count = 1; int update_count = 1;
int insert_oid = 0;
SQLException final_error = null; SQLException final_error = null;
if (sql.length() > 8192) // Commented out as the backend can now handle queries
throw new PSQLException("postgresql.con.toolong",sql); // larger than 8K. Peter June 6 2000
//if (sql.length() > 8192)
//throw new PSQLException("postgresql.con.toolong",sql);
try try
{ {
pg_stream.SendChar('Q'); pg_stream.SendChar('Q');
@ -357,12 +360,19 @@ public abstract class Connection
recv_status = pg_stream.ReceiveString(8192); recv_status = pg_stream.ReceiveString(8192);
// Now handle the update count correctly. // Now handle the update count correctly.
if(recv_status.startsWith("INSERT") || recv_status.startsWith("UPDATE")) { if(recv_status.startsWith("INSERT") || recv_status.startsWith("UPDATE") || recv_status.startsWith("DELETE")) {
try { try {
update_count = Integer.parseInt(recv_status.substring(1+recv_status.lastIndexOf(' '))); update_count = Integer.parseInt(recv_status.substring(1+recv_status.lastIndexOf(' ')));
} catch(NumberFormatException nfe) { } catch(NumberFormatException nfe) {
throw new PSQLException("postgresql.con.fathom",recv_status); throw new PSQLException("postgresql.con.fathom",recv_status);
} }
if(recv_status.startsWith("INSERT")) {
try {
insert_oid = Integer.parseInt(recv_status.substring(1+recv_status.indexOf(' '),recv_status.lastIndexOf(' ')));
} catch(NumberFormatException nfe) {
throw new PSQLException("postgresql.con.fathom",recv_status);
}
}
} }
if (fields != null) if (fields != null)
hfr = true; hfr = true;
@ -423,7 +433,7 @@ public abstract class Connection
if (final_error != null) if (final_error != null)
throw final_error; throw final_error;
return getResultSet(this, fields, tuples, recv_status, update_count); return getResultSet(this, fields, tuples, recv_status, update_count, insert_oid);
} }
} }
@ -701,14 +711,14 @@ public abstract class Connection
// the full class name of the handling class. // the full class name of the handling class.
// //
private static final String defaultObjectTypes[][] = { private static final String defaultObjectTypes[][] = {
{"box", "postgresql.geometric.PGbox"}, {"box", "org.postgresql.geometric.PGbox"},
{"circle", "postgresql.geometric.PGcircle"}, {"circle", "org.postgresql.geometric.PGcircle"},
{"line", "postgresql.geometric.PGline"}, {"line", "org.postgresql.geometric.PGline"},
{"lseg", "postgresql.geometric.PGlseg"}, {"lseg", "org.postgresql.geometric.PGlseg"},
{"path", "postgresql.geometric.PGpath"}, {"path", "org.postgresql.geometric.PGpath"},
{"point", "postgresql.geometric.PGpoint"}, {"point", "org.postgresql.geometric.PGpoint"},
{"polygon", "postgresql.geometric.PGpolygon"}, {"polygon", "org.postgresql.geometric.PGpolygon"},
{"money", "postgresql.util.PGmoney"} {"money", "org.postgresql.util.PGmoney"}
}; };
// This initialises the objectTypes hashtable // This initialises the objectTypes hashtable
@ -725,7 +735,7 @@ public abstract class Connection
* This returns a resultset. It must be overridden, so that the correct * This returns a resultset. It must be overridden, so that the correct
* version (from jdbc1 or jdbc2) are returned. * version (from jdbc1 or jdbc2) are returned.
*/ */
protected abstract java.sql.ResultSet getResultSet(org.postgresql.Connection conn, Field[] fields, Vector tuples, String status, int updateCount) throws SQLException; protected abstract java.sql.ResultSet getResultSet(org.postgresql.Connection conn, Field[] fields, Vector tuples, String status, int updateCount,int insertOID) throws SQLException;
public abstract void close() throws SQLException; public abstract void close() throws SQLException;

View File

@ -19,6 +19,7 @@ public abstract class ResultSet
protected Field fields[]; // The field descriptions protected Field fields[]; // The field descriptions
protected String status; // Status of the result protected String status; // Status of the result
protected int updateCount; // How many rows did we get back? protected int updateCount; // How many rows did we get back?
protected int insertOID; // The oid of an inserted row
protected int current_row; // Our pointer to where we are at protected int current_row; // Our pointer to where we are at
protected byte[][] this_row; // the current row result protected byte[][] this_row; // the current row result
protected Connection connection; // the connection which we returned from protected Connection connection; // the connection which we returned from
@ -40,17 +41,35 @@ public abstract class ResultSet
* @param updateCount the number of rows affected by the operation * @param updateCount the number of rows affected by the operation
* @param cursor the positioned update/delete cursor name * @param cursor the positioned update/delete cursor name
*/ */
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount) public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount,int insertOID)
{ {
this.connection = conn; this.connection = conn;
this.fields = fields; this.fields = fields;
this.rows = tuples; this.rows = tuples;
this.status = status; this.status = status;
this.updateCount = updateCount; this.updateCount = updateCount;
this.insertOID = insertOID;
this.this_row = null; this.this_row = null;
this.current_row = -1; this.current_row = -1;
} }
/**
* Create a new ResultSet - Note that we create ResultSets to
* represent the results of everything.
*
* @param fields an array of Field objects (basically, the
* ResultSet MetaData)
* @param tuples Vector of the actual data
* @param status the status string returned from the back end
* @param updateCount the number of rows affected by the operation
* @param cursor the positioned update/delete cursor name
*/
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount)
{
this(conn,fields,tuples,status,updateCount,0);
}
/** /**
* We at times need to know if the resultSet we are working * We at times need to know if the resultSet we are working
* with is the result of an UPDATE, DELETE or INSERT (in which * with is the result of an UPDATE, DELETE or INSERT (in which
@ -148,6 +167,14 @@ public abstract class ResultSet
return fields[field-1].getOID(); return fields[field-1].getOID();
} }
/**
* returns the OID of the last inserted row
*/
public int getInsertedOID()
{
return insertOID;
}
/** /**
* This is part of the JDBC API, but is required by org.postgresql.Field * This is part of the JDBC API, but is required by org.postgresql.Field
*/ */

View File

@ -17,7 +17,7 @@ import org.postgresql.largeobject.*;
import org.postgresql.util.*; import org.postgresql.util.*;
/** /**
* $Id: Connection.java,v 1.1 2000/04/17 20:07:48 peter Exp $ * $Id: Connection.java,v 1.1.2.1 2000/06/15 04:12:36 momjian Exp $
* *
* A Connection represents a session with a specific database. Within the * A Connection represents a session with a specific database. Within the
* context of a Connection, SQL statements are executed and results are * context of a Connection, SQL statements are executed and results are
@ -378,9 +378,9 @@ public class Connection extends org.postgresql.Connection implements java.sql.Co
* This overides the method in org.postgresql.Connection and returns a * This overides the method in org.postgresql.Connection and returns a
* ResultSet. * ResultSet.
*/ */
protected java.sql.ResultSet getResultSet(org.postgresql.Connection conn, Field[] fields, Vector tuples, String status, int updateCount) throws SQLException protected java.sql.ResultSet getResultSet(org.postgresql.Connection conn, Field[] fields, Vector tuples, String status, int updateCount,int insertOID) throws SQLException
{ {
return new org.postgresql.jdbc1.ResultSet((org.postgresql.jdbc1.Connection)conn,fields,tuples,status,updateCount); return new org.postgresql.jdbc1.ResultSet((org.postgresql.jdbc1.Connection)conn,fields,tuples,status,updateCount,insertOID);
} }
} }

View File

@ -58,6 +58,22 @@ import org.postgresql.util.*;
*/ */
public class ResultSet extends org.postgresql.ResultSet implements java.sql.ResultSet public class ResultSet extends org.postgresql.ResultSet implements java.sql.ResultSet
{ {
/**
* Create a new ResultSet - Note that we create ResultSets to
* represent the results of everything.
*
* @param fields an array of Field objects (basically, the
* ResultSet MetaData)
* @param tuples Vector of the actual data
* @param status the status string returned from the back end
* @param updateCount the number of rows affected by the operation
* @param cursor the positioned update/delete cursor name
*/
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount,int insertOID)
{
super(conn,fields,tuples,status,updateCount,insertOID);
}
/** /**
* Create a new ResultSet - Note that we create ResultSets to * Create a new ResultSet - Note that we create ResultSets to
* represent the results of everything. * represent the results of everything.
@ -71,7 +87,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
*/ */
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount) public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount)
{ {
super(conn,fields,tuples,status,updateCount); super(conn,fields,tuples,status,updateCount,0);
} }
/** /**
@ -437,7 +453,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
if(s==null) if(s==null)
return null; return null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try { try {
return new Timestamp(df.parse(s).getTime()); return new Timestamp(df.parse(s).getTime());

View File

@ -17,7 +17,7 @@ import org.postgresql.largeobject.*;
import org.postgresql.util.*; import org.postgresql.util.*;
/** /**
* $Id: Connection.java,v 1.1 2000/04/17 20:07:50 peter Exp $ * $Id: Connection.java,v 1.1.2.1 2000/06/15 04:12:41 momjian Exp $
* *
* A Connection represents a session with a specific database. Within the * A Connection represents a session with a specific database. Within the
* context of a Connection, SQL statements are executed and results are * context of a Connection, SQL statements are executed and results are
@ -378,9 +378,9 @@ public class Connection extends org.postgresql.Connection implements java.sql.Co
* This overides the method in org.postgresql.Connection and returns a * This overides the method in org.postgresql.Connection and returns a
* ResultSet. * ResultSet.
*/ */
protected java.sql.ResultSet getResultSet(org.postgresql.Connection conn, Field[] fields, Vector tuples, String status, int updateCount) throws SQLException protected java.sql.ResultSet getResultSet(org.postgresql.Connection conn, Field[] fields, Vector tuples, String status, int updateCount, int insertOID) throws SQLException
{ {
return new org.postgresql.jdbc2.ResultSet((org.postgresql.jdbc2.Connection)conn,fields,tuples,status,updateCount); return new org.postgresql.jdbc2.ResultSet((org.postgresql.jdbc2.Connection)conn,fields,tuples,status,updateCount,insertOID);
} }
// ***************** // *****************

View File

@ -70,11 +70,27 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
* @param updateCount the number of rows affected by the operation * @param updateCount the number of rows affected by the operation
* @param cursor the positioned update/delete cursor name * @param cursor the positioned update/delete cursor name
*/ */
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount) public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount,int insertOID)
{ {
super(conn,fields,tuples,status,updateCount); super(conn,fields,tuples,status,updateCount,insertOID);
} }
/**
* Create a new ResultSet - Note that we create ResultSets to
* represent the results of everything.
*
* @param fields an array of Field objects (basically, the
* ResultSet MetaData)
* @param tuples Vector of the actual data
* @param status the status string returned from the back end
* @param updateCount the number of rows affected by the operation
* @param cursor the positioned update/delete cursor name
*/
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount)
{
super(conn,fields,tuples,status,updateCount,0);
}
/** /**
* A ResultSet is initially positioned before its first row, * A ResultSet is initially positioned before its first row,
* the first call to next makes the first row the current row; * the first call to next makes the first row the current row;
@ -331,6 +347,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new PSQLException ("postgresql.res.badbigdec",s); throw new PSQLException ("postgresql.res.badbigdec",s);
} }
if (scale==-1) return val;
try try
{ {
return val.setScale(scale); return val.setScale(scale);
@ -439,7 +456,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
if(s==null) if(s==null)
return null; return null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try { try {
return new Timestamp(df.parse(s).getTime()); return new Timestamp(df.parse(s).getTime());
@ -723,7 +740,8 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
case Types.BIGINT: case Types.BIGINT:
return new Long(getLong(columnIndex)); return new Long(getLong(columnIndex));
case Types.NUMERIC: case Types.NUMERIC:
return getBigDecimal(columnIndex, ((field.mod-4) & 0xffff)); return getBigDecimal
(columnIndex, (field.mod==-1)?-1:((field.mod-4) & 0xffff));
case Types.REAL: case Types.REAL:
return new Float(getFloat(columnIndex)); return new Float(getFloat(columnIndex));
case Types.DOUBLE: case Types.DOUBLE:
@ -783,15 +801,34 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
public boolean absolute(int index) throws SQLException public boolean absolute(int index) throws SQLException
{ {
// Peter: Added because negative indices read from the end of the // index is 1-based, but internally we use 0-based indices
// ResultSet int internalIndex;
if(index<0)
index=rows.size()+index; if (index==0)
throw new SQLException("Cannot move to index of 0");
if (index==0 || index > rows.size())
//if index<0, count from the end of the result set, but check
//to be sure that it is not beyond the first index
if (index<0)
if (index>=-rows.size())
internalIndex=rows.size()+index;
else {
beforeFirst();
return false;
}
//must be the case that index>0,
//find the correct place, assuming that
//the index is not too large
if (index<=rows.size())
internalIndex = index-1;
else {
afterLast();
return false; return false;
}
this_row = (byte [][])rows.elementAt(index);
current_row=internalIndex;
this_row = (byte [][])rows.elementAt(internalIndex);
return true; return true;
} }
@ -1022,7 +1059,8 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
// Peter: Implemented in 7.0 // Peter: Implemented in 7.0
public boolean relative(int rows) throws SQLException public boolean relative(int rows) throws SQLException
{ {
return absolute(current_row+rows); //have to add 1 since absolute expects a 1-based index
return absolute(current_row+1+rows);
} }
public boolean rowDeleted() throws SQLException public boolean rowDeleted() throws SQLException

View File

@ -417,5 +417,4 @@ public class Statement implements java.sql.Statement
throw org.postgresql.Driver.notImplemented(); throw org.postgresql.Driver.notImplemented();
} }
} }