This patch fixes a bug reported by Graham Leggett (minfrin@sharp.fm).

The bug was that any insert or update would fail if the returned oid was
larger than a signed int.  Since OIDs are unsigned int's it was
a bug that the code used a java signed int to deal with the values.  The bug
would result in the error message: "Unable to fathom update count".
While fixing the bug, it became apparent that other code made a similar
assumption about OIDs being signed ints.  Therefore some methods that returned
or took OIDs are arguements also needed to be changed.
Since we are so close to the 7.2 release I have added new methods that
return longs and deprecated the old methods returning ints.  Therefore all
old code should still work without requiring a code change to cast from long to int.  Also note that the methods below are PostgreSQL specific extensions to
the JDBC api are are not part of the spec from Sun, thus it is unlikely that
they are used much or at all.

The deprecated methods are:
  ResultSet.getInsertedOID()
  Statement.getInsertedOID()
  Serialize.store()
  Connection.putObject()
and are replaced by:
  ResultSet.getLastOID()
  Statement.getLastOID()
  Serialize.storeObject()
  Connection.storeObject()
All the deprecated methods returned int, while their replacements return long

This patch also fixes two comments in MD5Digest that the author Jeremy Wohl
submitted.

--Barry
This commit is contained in:
Barry Lind 2001-11-25 23:26:59 +00:00
parent 23b5ca91aa
commit 4bc8c8dd95
14 changed files with 79 additions and 45 deletions

View File

@ -6,7 +6,7 @@ import java.text.*;
/*
*
* $Id: basic.java,v 1.10 2001/11/19 23:16:44 momjian Exp $
* $Id: basic.java,v 1.11 2001/11/25 23:26:56 barry Exp $
*
* This example tests the basic components of the JDBC driver, and shows
* how even the simplest of queries can be implemented.
@ -89,7 +89,7 @@ public class basic
// This shows how to get the oid of a just inserted row
// updated for 7.1
st.executeUpdate("insert into basic values (4,1)");
int insertedOID = ((org.postgresql.Statement)st).getInsertedOID();
long insertedOID = ((org.postgresql.Statement)st).getLastOID();
System.out.println("Inserted row with oid " + insertedOID);
// Now change the value of b from 1 to 8

View File

@ -11,7 +11,7 @@ import org.postgresql.util.*;
import org.postgresql.core.*;
/*
* $Id: Connection.java,v 1.38 2001/11/19 23:19:20 momjian Exp $
* $Id: Connection.java,v 1.39 2001/11/25 23:26:56 barry Exp $
*
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
* JDBC2 versions of the Connection class.
@ -594,14 +594,26 @@ public abstract class Connection
return null;
}
/*
* This stores an object into the database. This method was
* deprecated in 7.2 bacause an OID can be larger than the java signed
* int returned by this method.
* @deprecated Replaced by storeObject() in 7.2
*/
public int putObject(Object o) throws SQLException
{
return (int) storeObject(o);
}
/*
* This stores an object into the database.
* @param o Object to store
* @return OID of the new rectord
* @exception SQLException if value is not correct for this type
* @see org.postgresql.util.Serialize
* @since 7.2
*/
public int putObject(Object o) throws SQLException
public long storeObject(Object o) throws SQLException
{
try
{
@ -615,13 +627,13 @@ public abstract class Connection
{
Serialize ser = new Serialize(this, type);
objectTypes.put(type, ser);
return ser.store(o);
return ser.storeObject(o);
}
// If it's an object, it should be an instance of our Serialize class
// If so, then call it's fetch method.
if (x instanceof Serialize)
return ((Serialize)x).store(o);
return ((Serialize)x).storeObject(o);
// Thow an exception because the type is unknown
throw new PSQLException("postgresql.con.strobj");
@ -697,7 +709,7 @@ public abstract class Connection
* This returns a resultset. It must be overridden, so that the correct
* version (from jdbc1 or jdbc2) are returned.
*/
public abstract java.sql.ResultSet getResultSet(org.postgresql.Connection conn, java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount, int insertOID, boolean binaryCursor) throws SQLException;
public abstract java.sql.ResultSet getResultSet(org.postgresql.Connection conn, java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor) throws SQLException;
/*
* In some cases, it is desirable to immediately release a Connection's

View File

@ -20,7 +20,7 @@ public abstract class ResultSet
protected String status; // Status of the result
protected boolean binaryCursor = false; // is the data binary or Strings
protected int updateCount; // How many rows did we get back?
protected int insertOID; // The oid of an inserted row
protected long insertOID; // The oid of an inserted row
protected int current_row; // Our pointer to where we are at
protected byte[][] this_row; // the current row result
protected Connection connection; // the connection which we returned from
@ -42,7 +42,7 @@ public abstract class ResultSet
* @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, boolean binaryCursor)
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor)
{
this.connection = conn;
this.fields = fields;
@ -170,9 +170,21 @@ public abstract class ResultSet
}
/*
* returns the OID of the last inserted row
* returns the OID of the last inserted row. Deprecated in 7.2 because
* range for OID values is greater than java signed int.
* @deprecated Replaced by getLastOID() in 7.2
*/
public int getInsertedOID()
{
return (int) getLastOID();
}
/*
* returns the OID of the last inserted row
* @since 7.2
*/
public long getLastOID()
{
return insertOID;
}

View File

@ -8,19 +8,6 @@ import org.postgresql.util.PSQLException;
* org.postgresql.jdbc1.Statement and org.postgresql.jdbc2.Statement that are
* unique to PostgreSQL's JDBC driver.
*
* <p>They are defined so that client code can cast to org.postgresql.Statement
* without having to predetermine the jdbc driver type.
*
* <p>ie: Before this class existed, you had to use:
*
* <p>((org.postgresql.jdbc2.Statement)stat).getInsertedOID();
*
* <p>now you use:
*
* <p>((org.postgresql.Statement)stat).getInsertedOID();
*
* <p>As you can see, this is independent of JDBC1.2, JDBC2.0 or the upcoming
* JDBC3.
*/
public abstract class Statement
@ -196,16 +183,27 @@ public abstract class Statement
}
/*
* New in 7.1: Returns the Last inserted oid. This should be used, rather
* than the old method using getResultSet, which for executeUpdate returns
* null.
* @return OID of last insert
* Returns the Last inserted/updated oid. Deprecated in 7.2 because
* range of OID values is greater than a java signed int.
* @deprecated Replaced by getLastOID in 7.2
*/
public int getInsertedOID() throws SQLException
{
if (result == null)
return 0;
return ((org.postgresql.ResultSet) result).getInsertedOID();
return (int)((org.postgresql.ResultSet) result).getLastOID();
}
/*
* Returns the Last inserted/updated oid.
* @return OID of last insert
* @since 7.2
*/
public long getLastOID() throws SQLException
{
if (result == null)
return 0;
return ((org.postgresql.ResultSet) result).getLastOID();
}
/*

View File

@ -13,7 +13,7 @@ import org.postgresql.util.PSQLException;
* <p>The lifetime of a QueryExecutor object is from sending the query
* until the response has been received from the backend.
*
* $Id: QueryExecutor.java,v 1.5 2001/11/19 23:16:45 momjian Exp $
* $Id: QueryExecutor.java,v 1.6 2001/11/25 23:26:56 barry Exp $
*/
public class QueryExecutor
@ -46,7 +46,7 @@ public class QueryExecutor
private boolean binaryCursor = false;
private String status = null;
private int update_count = 1;
private int insert_oid = 0;
private long insert_oid = 0;
private int maxRows;
/*
@ -173,7 +173,7 @@ public class QueryExecutor
}
if (status.startsWith("INSERT"))
{
insert_oid = Integer.parseInt(status.substring(1 + status.indexOf(' '),
insert_oid = Long.parseLong(status.substring(1 + status.indexOf(' '),
status.lastIndexOf(' ')));
}
}

View File

@ -17,7 +17,7 @@ import org.postgresql.largeobject.*;
import org.postgresql.util.*;
/*
* $Id: Connection.java,v 1.13 2001/11/19 22:33:38 momjian Exp $
* $Id: Connection.java,v 1.14 2001/11/25 23:26:58 barry Exp $
*
* A Connection represents a session with a specific database. Within the
* context of a Connection, SQL statements are executed and results are
@ -131,7 +131,7 @@ public class Connection extends org.postgresql.Connection implements java.sql.Co
* This overides the method in org.postgresql.Connection and returns a
* ResultSet.
*/
public java.sql.ResultSet getResultSet(org.postgresql.Connection conn, java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount, int insertOID, boolean binaryCursor) throws SQLException
public java.sql.ResultSet getResultSet(org.postgresql.Connection conn, java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor) throws SQLException
{
// in jdbc1 stat is ignored.
return new org.postgresql.jdbc1.ResultSet((org.postgresql.jdbc1.Connection)conn, fields, tuples, status, updateCount, insertOID, binaryCursor);

View File

@ -712,7 +712,7 @@ public class PreparedStatement extends Statement implements java.sql.PreparedSta
else if (x instanceof PGobject)
setString(parameterIndex, ((PGobject)x).getValue());
else
setLong(parameterIndex, connection.putObject(x));
setLong(parameterIndex, connection.storeObject(x));
}
/*

View File

@ -70,7 +70,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
* @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, boolean binaryCursor)
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor)
{
super(conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
}

View File

@ -17,7 +17,7 @@ import org.postgresql.largeobject.*;
import org.postgresql.util.*;
/*
* $Id: Connection.java,v 1.15 2001/11/19 22:33:38 momjian Exp $
* $Id: Connection.java,v 1.16 2001/11/25 23:26:59 barry Exp $
*
* A Connection represents a session with a specific database. Within the
* context of a Connection, SQL statements are executed and results are
@ -207,7 +207,7 @@ public class Connection extends org.postgresql.Connection implements java.sql.Co
* This overides the method in org.postgresql.Connection and returns a
* ResultSet.
*/
public java.sql.ResultSet getResultSet(org.postgresql.Connection conn, java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount, int insertOID, boolean binaryCursor) throws SQLException
public java.sql.ResultSet getResultSet(org.postgresql.Connection conn, java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor) throws SQLException
{
// In 7.1 we now test concurrency to see which class to return. If we are not working with a
// Statement then default to a normal ResultSet object.

View File

@ -748,7 +748,7 @@ public class PreparedStatement extends Statement implements java.sql.PreparedSta
setString(parameterIndex, ((PGobject)x).getValue());
else
// Try to store java object in database
setSerialize(parameterIndex, connection.putObject(x), x.getClass().getName() );
setSerialize(parameterIndex, connection.storeObject(x), x.getClass().getName() );
}
/*

View File

@ -74,7 +74,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
* @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, boolean binaryCursor)
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor)
{
super(conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
}

View File

@ -40,7 +40,7 @@ public class UpdateableResultSet extends org.postgresql.jdbc2.ResultSet
* @param updateCount the number of rows affected by the operation
* @param cursor the positioned update/delete cursor name
*/
public UpdateableResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, int insertOID, boolean binaryCursor)
public UpdateableResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor)
{
super(conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
}

View File

@ -4,7 +4,7 @@ package org.postgresql.util;
* MD5-based utility function to obfuscate passwords before network transmission
*
* @author Jeremy Wohl
*
* $Id: MD5Digest.java,v 1.3 2001/11/25 23:26:59 barry Exp $
*/
import java.security.*;
@ -23,7 +23,7 @@ public class MD5Digest
* @param password The connecting user's password.
* @param salt A four-character string sent by the server.
*
* @return A 35-byte array, comprising the string "md5", followed by an MD5 digest.
* @return A 35-byte array, comprising the string "md5" and an MD5 digest.
*/
public static byte[] encode(String user, String password, String salt)
{

View File

@ -267,6 +267,17 @@ public class Serialize
}
}
/*
* This stores an object into a table, returning it's OID.<p>
* This method was deprecated in 7.2 because the value of an OID
* can be larger than a java signed int.
* @deprecated Replaced by storeObject() in 7.2
*/
public int store(Object o) throws SQLException
{
return (int) storeObject(o);
}
/*
* This stores an object into a table, returning it's OID.<p>
*
@ -284,8 +295,9 @@ public class Serialize
* @param o Object to store (must implement Serializable)
* @return oid of stored object
* @exception SQLException on error
* @since 7.2
*/
public int store(Object o) throws SQLException
public long storeObject(Object o) throws SQLException
{
try
{
@ -390,11 +402,11 @@ public class Serialize
else
{
// new record inserted has new oid; rs should be not null
int newOID = ((org.postgresql.ResultSet)rs).getInsertedOID();
long newOID = ((org.postgresql.ResultSet)rs).getLastOID();
rs.close();
// update the java object's oid field if it has the oid field
if (hasOID)
f[oidFIELD].setInt(o, newOID);
f[oidFIELD].setLong(o, newOID);
// new object stored, return newly inserted oid
return newOID;
}