]> granicus.if.org Git - postgresql/commitdiff
Fix some problems with result sets positioned before the start or
authorKris Jurka <books@ejurka.com>
Mon, 21 Jun 2004 03:12:01 +0000 (03:12 +0000)
committerKris Jurka <books@ejurka.com>
Mon, 21 Jun 2004 03:12:01 +0000 (03:12 +0000)
after the end of results.  You could still call a number of methods
on them like getXXX, updateXXX, and updateRow().

src/interfaces/jdbc/org/postgresql/errors.properties
src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java
src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
src/interfaces/jdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java

index a0b7ddee3e2463efabe6372952e5c3e471285fd1..15689894f18939de970f8c7abc6adbfade1b7611 100644 (file)
@@ -92,6 +92,7 @@ postgresql.updateable.oninsertrow:Can not call deleteRow() when on insert row
 postgresql.updateable.emptydelete:Can't deleteRow() on empty result set
 postgresql.updateable.beforestartdelete:Before start of result set. Can not call deleteRow().
 postgresql.updateable.afterlastdelete:After end of result set. Can not call deleteRow().
+postgresql.updateable.badupdateposition:Cannot update the result set because it is either before the start or after the end of the results.
 postgresql.updateable.notoninsertrow:Not on insert row.
 postgresql.updateable.ioerror:Input Stream Error - {0}
 postgresql.call.noreturntype:A CallableStatement Function was declared but no call to 'registerOutParameter (1, <some_type>)' was made.
index 5216ff6df3384e1244ce08a8a4fd6ddf3853ba15..c07a34a4abf88cf45df1b25bd698e7558e7522da 100644 (file)
@@ -9,7 +9,7 @@
  * Copyright (c) 2003, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.22.2.3 2004/03/29 17:47:47 barry Exp $
+ *       $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.22.2.4 2004/06/21 03:11:37 jurka Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -138,6 +138,8 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
                        String cursorName = statement.getFetchingCursorName();
                        if (cursorName == null || lastFetchSize == 0 || rows.size() < lastFetchSize) {
                                current_row = rows.size();
+                               this_row = null;
+                               rowBuffer = null;
                                return false;  // Not doing a cursor-based fetch or the last fetch was the end of the query
                        }
  
@@ -160,8 +162,11 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
   
                        // Test the new rows array.
                        lastFetchSize = fetchSize;
-                       if (rows.size() == 0)
+                       if (rows.size() == 0) {
+                               this_row = null;
+                               rowBuffer = null;
                                return false;
+                       }
 
                        // Otherwise reset the counter and let it go on...
                        current_row = 0;
index b44e301854d77a2ba17d0860f5dc7241dbf2e161..2667a373b43e0055e63e4afdc6d2ce521a510a47 100644 (file)
@@ -9,7 +9,7 @@
  * Copyright (c) 2003, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.25.2.7 2004/06/21 02:01:11 jurka Exp $
+ *       $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.25.2.8 2004/06/21 03:11:49 jurka Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -225,6 +225,8 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
                        current_row = rows_size;
 
                onInsertRow = false;
+               this_row = null;
+               rowBuffer = null;
        }
 
 
@@ -234,6 +236,8 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
                        current_row = -1;
 
                onInsertRow = false;
+               this_row = null;
+               rowBuffer = null;
        }
 
 
@@ -500,6 +504,8 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
        {
                if (current_row-1 < 0) {
                        current_row = -1;
+                       this_row = null;
+                       rowBuffer = null;
                        return false;
                } else {
                        current_row--;
@@ -1073,6 +1079,10 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
                {
                        throw new PSQLException( "postgresql.updateable.notupdateable" );
                }
+               if (isBeforeFirst() || isAfterLast())
+               {
+                       throw new PSQLException("postgresql.updateable.badupdateposition");
+               }
 
                if (doingUpdates)
                {
@@ -1580,6 +1590,10 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
                {
                        throw new PSQLException( "postgresql.updateable.notupdateable" );
                }
+               if (!onInsertRow && (isBeforeFirst() || isAfterLast()))
+               {
+                       throw new PSQLException("postgresql.updateable.badupdateposition");
+               }
                doingUpdates = !onInsertRow;
                if (value == null)
                        updateNull(columnIndex);
index b3284d4df28149e698141a24254fe92b96748ca8..bba9755eb0aa584ce36deaf55abb759b5b37c086 100644 (file)
@@ -118,6 +118,57 @@ public class UpdateableResultTest extends TestCase
                st.close();
        }
 
+       private void checkPositioning(ResultSet rs) throws SQLException
+       {
+               try {
+                       rs.getInt(1);
+                       fail("Can't use an incorrectly positioned result set.");
+               } catch (SQLException sqle) { }
+
+               try {
+                       rs.updateInt(1,2);
+                       fail("Can't use an incorrectly positioned result set.");
+               } catch (SQLException sqle) { }
+
+               try {
+                       rs.updateRow();
+                       fail("Can't use an incorrectly positioned result set.");
+               } catch (SQLException sqle) { }
+
+               try {
+                       rs.deleteRow();
+                       fail("Can't use an incorrectly positioned result set.");
+               } catch (SQLException sqle) { }
+       }
+
+       public void testPositioning() throws SQLException
+       {
+               Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
+               ResultSet rs = stmt.executeQuery("SELECT id1,name1 FROM second");
+
+               checkPositioning(rs);
+
+               assertTrue(rs.next());
+               rs.beforeFirst();
+               checkPositioning(rs);
+
+               rs.afterLast();
+               checkPositioning(rs);
+
+               rs.beforeFirst();
+               assertTrue(rs.next());
+               assertFalse(rs.next());
+               checkPositioning(rs);
+
+               rs.afterLast();
+               assertTrue(rs.previous());
+               assertFalse(rs.previous());
+               checkPositioning(rs);
+
+               rs.close();
+               stmt.close();
+       }
+
        public void testUpdateStreams() throws SQLException, UnsupportedEncodingException
        {
                Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);