Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4bc8c8d

Browse files
author
Barry Lind
committed
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
1 parent 23b5ca9 commit 4bc8c8d

File tree

14 files changed

+79
-45
lines changed

14 files changed

+79
-45
lines changed

src/interfaces/jdbc/example/basic.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/*
88
*
9-
* $Id: basic.java,v 1.10 2001/11/19 23:16:44 momjian Exp $
9+
* $Id: basic.java,v 1.11 2001/11/25 23:26:56 barry Exp $
1010
*
1111
* This example tests the basic components of the JDBC driver, and shows
1212
* how even the simplest of queries can be implemented.
@@ -89,7 +89,7 @@ public void doexample() throws SQLException
8989
// This shows how to get the oid of a just inserted row
9090
// updated for 7.1
9191
st.executeUpdate("insert into basic values (4,1)");
92-
int insertedOID = ((org.postgresql.Statement)st).getInsertedOID();
92+
long insertedOID = ((org.postgresql.Statement)st).getLastOID();
9393
System.out.println("Inserted row with oid " + insertedOID);
9494

9595
// Now change the value of b from 1 to 8

src/interfaces/jdbc/org/postgresql/Connection.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.postgresql.core.*;
1212

1313
/*
14-
* $Id: Connection.java,v 1.38 2001/11/19 23:19:20 momjian Exp $
14+
* $Id: Connection.java,v 1.39 2001/11/25 23:26:56 barry Exp $
1515
*
1616
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
1717
* JDBC2 versions of the Connection class.
@@ -594,14 +594,26 @@ public Object getObject(String type, String value) throws SQLException
594594
return null;
595595
}
596596

597+
/*
598+
* This stores an object into the database. This method was
599+
* deprecated in 7.2 bacause an OID can be larger than the java signed
600+
* int returned by this method.
601+
* @deprecated Replaced by storeObject() in 7.2
602+
*/
603+
public int putObject(Object o) throws SQLException
604+
{
605+
return (int) storeObject(o);
606+
}
607+
597608
/*
598609
* This stores an object into the database.
599610
* @param o Object to store
600611
* @return OID of the new rectord
601612
* @exception SQLException if value is not correct for this type
602613
* @see org.postgresql.util.Serialize
614+
* @since 7.2
603615
*/
604-
public int putObject(Object o) throws SQLException
616+
public long storeObject(Object o) throws SQLException
605617
{
606618
try
607619
{
@@ -615,13 +627,13 @@ public int putObject(Object o) throws SQLException
615627
{
616628
Serialize ser = new Serialize(this, type);
617629
objectTypes.put(type, ser);
618-
return ser.store(o);
630+
return ser.storeObject(o);
619631
}
620632

621633
// If it's an object, it should be an instance of our Serialize class
622634
// If so, then call it's fetch method.
623635
if (x instanceof Serialize)
624-
return ((Serialize)x).store(o);
636+
return ((Serialize)x).storeObject(o);
625637

626638
// Thow an exception because the type is unknown
627639
throw new PSQLException("postgresql.con.strobj");
@@ -697,7 +709,7 @@ private void initObjectTypes()
697709
* This returns a resultset. It must be overridden, so that the correct
698710
* version (from jdbc1 or jdbc2) are returned.
699711
*/
700-
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;
712+
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;
701713

702714
/*
703715
* In some cases, it is desirable to immediately release a Connection's

src/interfaces/jdbc/org/postgresql/ResultSet.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public abstract class ResultSet
2020
protected String status; // Status of the result
2121
protected boolean binaryCursor = false; // is the data binary or Strings
2222
protected int updateCount; // How many rows did we get back?
23-
protected int insertOID; // The oid of an inserted row
23+
protected long insertOID; // The oid of an inserted row
2424
protected int current_row; // Our pointer to where we are at
2525
protected byte[][] this_row; // the current row result
2626
protected Connection connection; // the connection which we returned from
@@ -42,7 +42,7 @@ public abstract class ResultSet
4242
* @param updateCount the number of rows affected by the operation
4343
* @param cursor the positioned update/delete cursor name
4444
*/
45-
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, int insertOID, boolean binaryCursor)
45+
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor)
4646
{
4747
this.connection = conn;
4848
this.fields = fields;
@@ -170,9 +170,21 @@ public int getColumnOID(int field)
170170
}
171171

172172
/*
173-
* returns the OID of the last inserted row
173+
* returns the OID of the last inserted row. Deprecated in 7.2 because
174+
* range for OID values is greater than java signed int.
175+
* @deprecated Replaced by getLastOID() in 7.2
174176
*/
175177
public int getInsertedOID()
178+
{
179+
return (int) getLastOID();
180+
}
181+
182+
183+
/*
184+
* returns the OID of the last inserted row
185+
* @since 7.2
186+
*/
187+
public long getLastOID()
176188
{
177189
return insertOID;
178190
}

src/interfaces/jdbc/org/postgresql/Statement.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@
88
* org.postgresql.jdbc1.Statement and org.postgresql.jdbc2.Statement that are
99
* unique to PostgreSQL's JDBC driver.
1010
*
11-
* <p>They are defined so that client code can cast to org.postgresql.Statement
12-
* without having to predetermine the jdbc driver type.
13-
*
14-
* <p>ie: Before this class existed, you had to use:
15-
*
16-
* <p>((org.postgresql.jdbc2.Statement)stat).getInsertedOID();
17-
*
18-
* <p>now you use:
19-
*
20-
* <p>((org.postgresql.Statement)stat).getInsertedOID();
21-
*
22-
* <p>As you can see, this is independent of JDBC1.2, JDBC2.0 or the upcoming
23-
* JDBC3.
2411
*/
2512

2613
public abstract class Statement
@@ -196,16 +183,27 @@ public void cancel() throws SQLException
196183
}
197184

198185
/*
199-
* New in 7.1: Returns the Last inserted oid. This should be used, rather
200-
* than the old method using getResultSet, which for executeUpdate returns
201-
* null.
202-
* @return OID of last insert
186+
* Returns the Last inserted/updated oid. Deprecated in 7.2 because
187+
* range of OID values is greater than a java signed int.
188+
* @deprecated Replaced by getLastOID in 7.2
203189
*/
204190
public int getInsertedOID() throws SQLException
205191
{
206192
if (result == null)
207193
return 0;
208-
return ((org.postgresql.ResultSet) result).getInsertedOID();
194+
return (int)((org.postgresql.ResultSet) result).getLastOID();
195+
}
196+
197+
/*
198+
* Returns the Last inserted/updated oid.
199+
* @return OID of last insert
200+
* @since 7.2
201+
*/
202+
public long getLastOID() throws SQLException
203+
{
204+
if (result == null)
205+
return 0;
206+
return ((org.postgresql.ResultSet) result).getLastOID();
209207
}
210208

211209
/*

src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* <p>The lifetime of a QueryExecutor object is from sending the query
1414
* until the response has been received from the backend.
1515
*
16-
* $Id: QueryExecutor.java,v 1.5 2001/11/19 23:16:45 momjian Exp $
16+
* $Id: QueryExecutor.java,v 1.6 2001/11/25 23:26:56 barry Exp $
1717
*/
1818

1919
public class QueryExecutor
@@ -46,7 +46,7 @@ public QueryExecutor(String sql,
4646
private boolean binaryCursor = false;
4747
private String status = null;
4848
private int update_count = 1;
49-
private int insert_oid = 0;
49+
private long insert_oid = 0;
5050
private int maxRows;
5151

5252
/*
@@ -173,7 +173,7 @@ private void receiveCommandStatus() throws SQLException
173173
}
174174
if (status.startsWith("INSERT"))
175175
{
176-
insert_oid = Integer.parseInt(status.substring(1 + status.indexOf(' '),
176+
insert_oid = Long.parseLong(status.substring(1 + status.indexOf(' '),
177177
status.lastIndexOf(' ')));
178178
}
179179
}

src/interfaces/jdbc/org/postgresql/jdbc1/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.postgresql.util.*;
1818

1919
/*
20-
* $Id: Connection.java,v 1.13 2001/11/19 22:33:38 momjian Exp $
20+
* $Id: Connection.java,v 1.14 2001/11/25 23:26:58 barry Exp $
2121
*
2222
* A Connection represents a session with a specific database. Within the
2323
* context of a Connection, SQL statements are executed and results are
@@ -131,7 +131,7 @@ public java.sql.DatabaseMetaData getMetaData() throws SQLException
131131
* This overides the method in org.postgresql.Connection and returns a
132132
* ResultSet.
133133
*/
134-
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
134+
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
135135
{
136136
// in jdbc1 stat is ignored.
137137
return new org.postgresql.jdbc1.ResultSet((org.postgresql.jdbc1.Connection)conn, fields, tuples, status, updateCount, insertOID, binaryCursor);

src/interfaces/jdbc/org/postgresql/jdbc1/PreparedStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ else if (x instanceof Boolean)
712712
else if (x instanceof PGobject)
713713
setString(parameterIndex, ((PGobject)x).getValue());
714714
else
715-
setLong(parameterIndex, connection.putObject(x));
715+
setLong(parameterIndex, connection.storeObject(x));
716716
}
717717

718718
/*

src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
7070
* @param updateCount the number of rows affected by the operation
7171
* @param cursor the positioned update/delete cursor name
7272
*/
73-
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, int insertOID, boolean binaryCursor)
73+
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor)
7474
{
7575
super(conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
7676
}

src/interfaces/jdbc/org/postgresql/jdbc2/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.postgresql.util.*;
1818

1919
/*
20-
* $Id: Connection.java,v 1.15 2001/11/19 22:33:38 momjian Exp $
20+
* $Id: Connection.java,v 1.16 2001/11/25 23:26:59 barry Exp $
2121
*
2222
* A Connection represents a session with a specific database. Within the
2323
* context of a Connection, SQL statements are executed and results are
@@ -207,7 +207,7 @@ public java.sql.DatabaseMetaData getMetaData() throws SQLException
207207
* This overides the method in org.postgresql.Connection and returns a
208208
* ResultSet.
209209
*/
210-
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
210+
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
211211
{
212212
// In 7.1 we now test concurrency to see which class to return. If we are not working with a
213213
// Statement then default to a normal ResultSet object.

src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ else if (x instanceof PGobject)
748748
setString(parameterIndex, ((PGobject)x).getValue());
749749
else
750750
// Try to store java object in database
751-
setSerialize(parameterIndex, connection.putObject(x), x.getClass().getName() );
751+
setSerialize(parameterIndex, connection.storeObject(x), x.getClass().getName() );
752752
}
753753

754754
/*

src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
7474
* @param updateCount the number of rows affected by the operation
7575
* @param cursor the positioned update/delete cursor name
7676
*/
77-
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, int insertOID, boolean binaryCursor)
77+
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor)
7878
{
7979
super(conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
8080
}

src/interfaces/jdbc/org/postgresql/jdbc2/UpdateableResultSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class UpdateableResultSet extends org.postgresql.jdbc2.ResultSet
4040
* @param updateCount the number of rows affected by the operation
4141
* @param cursor the positioned update/delete cursor name
4242
*/
43-
public UpdateableResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, int insertOID, boolean binaryCursor)
43+
public UpdateableResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor)
4444
{
4545
super(conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
4646
}

src/interfaces/jdbc/org/postgresql/util/MD5Digest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* MD5-based utility function to obfuscate passwords before network transmission
55
*
66
* @author Jeremy Wohl
7-
*
7+
* $Id: MD5Digest.java,v 1.3 2001/11/25 23:26:59 barry Exp $
88
*/
99

1010
import java.security.*;
@@ -23,7 +23,7 @@ private MD5Digest()
2323
* @param password The connecting user's password.
2424
* @param salt A four-character string sent by the server.
2525
*
26-
* @return A 35-byte array, comprising the string "md5", followed by an MD5 digest.
26+
* @return A 35-byte array, comprising the string "md5" and an MD5 digest.
2727
*/
2828
public static byte[] encode(String user, String password, String salt)
2929
{

src/interfaces/jdbc/org/postgresql/util/Serialize.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ else if ( f[i].getType().getName().equals("boolean") )
267267
}
268268
}
269269

270+
/*
271+
* This stores an object into a table, returning it's OID.<p>
272+
* This method was deprecated in 7.2 because the value of an OID
273+
* can be larger than a java signed int.
274+
* @deprecated Replaced by storeObject() in 7.2
275+
*/
276+
public int store(Object o) throws SQLException
277+
{
278+
return (int) storeObject(o);
279+
}
280+
270281
/*
271282
* This stores an object into a table, returning it's OID.<p>
272283
*
@@ -284,8 +295,9 @@ else if ( f[i].getType().getName().equals("boolean") )
284295
* @param o Object to store (must implement Serializable)
285296
* @return oid of stored object
286297
* @exception SQLException on error
298+
* @since 7.2
287299
*/
288-
public int store(Object o) throws SQLException
300+
public long storeObject(Object o) throws SQLException
289301
{
290302
try
291303
{
@@ -390,11 +402,11 @@ else if (
390402
else
391403
{
392404
// new record inserted has new oid; rs should be not null
393-
int newOID = ((org.postgresql.ResultSet)rs).getInsertedOID();
405+
long newOID = ((org.postgresql.ResultSet)rs).getLastOID();
394406
rs.close();
395407
// update the java object's oid field if it has the oid field
396408
if (hasOID)
397-
f[oidFIELD].setInt(o, newOID);
409+
f[oidFIELD].setLong(o, newOID);
398410
// new object stored, return newly inserted oid
399411
return newOID;
400412
}

0 commit comments

Comments
 (0)