OSDN Git Service

This patch fixes a bug reported by Graham Leggett (minfrin@sharp.fm).
authorBarry Lind <barry@xythos.com>
Sun, 25 Nov 2001 23:26:59 +0000 (23:26 +0000)
committerBarry Lind <barry@xythos.com>
Sun, 25 Nov 2001 23:26:59 +0000 (23:26 +0000)
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

14 files changed:
src/interfaces/jdbc/example/basic.java
src/interfaces/jdbc/org/postgresql/Connection.java
src/interfaces/jdbc/org/postgresql/ResultSet.java
src/interfaces/jdbc/org/postgresql/Statement.java
src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java
src/interfaces/jdbc/org/postgresql/jdbc1/Connection.java
src/interfaces/jdbc/org/postgresql/jdbc1/PreparedStatement.java
src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java
src/interfaces/jdbc/org/postgresql/jdbc2/Connection.java
src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java
src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java
src/interfaces/jdbc/org/postgresql/jdbc2/UpdateableResultSet.java
src/interfaces/jdbc/org/postgresql/util/MD5Digest.java
src/interfaces/jdbc/org/postgresql/util/Serialize.java

index f01cb9c..9a4469d 100644 (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
index e767ac0..6bbdd1f 100644 (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.
@@ -595,13 +595,25 @@ public abstract class Connection
        }
 
        /*
+        * 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
index 22a49fa..a9da22d 100644 (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,10 +170,22 @@ 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;
        }
 
index 95fd62d..932b93a 100644 (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();
        }
 
        /*
index 7e85dcd..aad95f1 100644 (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(' ')));
                        }
                }
index 71a8fb4..4507a27 100644 (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);
index 7116d0b..1da1da6 100644 (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));
        }
 
        /*
index 33c7de5..7fea1da 100644 (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);
        }
index 1979584..355c240 100644 (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.
index 3c3146e..d5418bd 100644 (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() );
        }
 
        /*
index 765fb46..66e7b4d 100644 (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);
        }
index 2bd2023..108125a 100644 (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);
        }
index 9d7a7d3..4090289 100644 (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)
        {
index f49b757..59d9c03 100644 (file)
@@ -269,6 +269,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>
         *
         * If the object has an int called OID, and it is > 0, then
         * that value is used for the OID, and the table will be updated.
@@ -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;
                        }