]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
ResultSet.next() and previous() incremented or decremented the
authorKris Jurka <books@ejurka.com>
Tue, 3 Feb 2004 05:25:38 +0000 (05:25 +0000)
committerKris Jurka <books@ejurka.com>
Tue, 3 Feb 2004 05:25:38 +0000 (05:25 +0000)
internal current_row variable regardless of wether they succeeded or
not.  This generated some ArrayIndexOutOfBoundsExceptions when the
errorneous adjustment current_row led to out of range values.

Per report from Fischer Krisztian.

src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java
src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
src/interfaces/jdbc/org/postgresql/test/jdbc2/ResultSetTest.java

index b75f0106a00deae41c86093b6bce327ff932430c..5dbb80a6921f5eee196f0567a292c03e566e400a 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.1 2003/12/12 17:59:08 davec Exp $
+ *       $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.22.2.2 2004/02/03 05:25:36 jurka Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -137,11 +137,13 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
                if (rows == null)
                        throw new PSQLException("postgresql.con.closed", PSQLState.CONNECTION_DOES_NOT_EXIST);
 
-               if (++current_row >= rows.size())
+               if (current_row+1 >= rows.size())
                {
                        String cursorName = statement.getFetchingCursorName();
-                       if (cursorName == null || lastFetchSize == 0 || rows.size() < lastFetchSize)
+                       if (cursorName == null || lastFetchSize == 0 || rows.size() < lastFetchSize) {
+                               current_row = rows.size();
                                return false;  // Not doing a cursor-based fetch or the last fetch was the end of the query
+                       }
  
                        // Use the ref to the statement to get
                        // the details we need to do another cursor
@@ -167,6 +169,8 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
 
                        // Otherwise reset the counter and let it go on...
                        current_row = 0;
+               } else {
+                       current_row++;
                }
 
                this_row = (byte [][])rows.elementAt(current_row);
index 10c6ad136ab979a2279f445fde109ada8a045c67..a10578cfe92f7384d5a8adbb07b166ede9657c53 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.2 2003/12/18 03:35:55 davec Exp $
+ *       $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.25.2.3 2004/02/03 05:25:37 jurka Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -490,8 +490,12 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
 
        public boolean previous() throws SQLException
        {
-               if (--current_row < 0)
+               if (current_row-1 < 0) {
+                       current_row = -1;
                        return false;
+               } else {
+                       current_row--;
+               }
                this_row = (byte[][]) rows.elementAt(current_row);
                rowBuffer = new byte[this_row.length][];
                System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length);
index c09ac49a9825bb42011bf558b28d08a7e63eaf4c..aec118504674158dd0fb3bd4985fde9f67d09065 100644 (file)
@@ -4,6 +4,7 @@ import org.postgresql.test.TestUtil;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.Statement;
+import java.sql.SQLException;
 
 import junit.framework.TestCase;
 
@@ -285,5 +286,48 @@ public class ResultSetTest extends TestCase
                                fail("Exception expected.");
                }
        }
-       
+
+       public void testZeroRowResultPositioning() throws SQLException
+       {
+               Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
+               ResultSet rs = stmt.executeQuery("SELECT * FROM pg_database WHERE datname='nonexistantdatabase'");
+               assertEquals(rs.previous(),false);
+               assertEquals(rs.previous(),false);
+               assertEquals(rs.next(),false);
+               assertEquals(rs.next(),false);
+               assertEquals(rs.next(),false);
+               assertEquals(rs.next(),false);
+               assertEquals(rs.next(),false);
+               assertEquals(rs.previous(),false);
+               assertEquals(rs.first(),false);
+               assertEquals(rs.last(),false);
+               assertEquals(rs.getRow(),0);
+               assertEquals(rs.absolute(1),false);
+               assertEquals(rs.relative(1),false);
+               assertEquals(rs.isBeforeFirst(),false);
+               assertEquals(rs.isAfterLast(),false);
+               assertEquals(rs.isFirst(),false);
+               assertEquals(rs.isLast(),false);
+               rs.close();
+               stmt.close();
+       }
+
+       public void testRowResultPositioning() throws SQLException
+       {
+               Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
+               // Create a one row result set.
+               ResultSet rs = stmt.executeQuery("SELECT * FROM pg_database WHERE datname='template1'");
+               assertTrue(rs.isBeforeFirst());
+               assertTrue(rs.next());
+               assertTrue(rs.isFirst());
+               assertTrue(rs.isLast());
+               assertTrue(!rs.next());
+               assertTrue(rs.isAfterLast());
+               assertTrue(rs.previous());
+               assertTrue(rs.absolute(1));
+               rs.close();
+               stmt.close();
+       }
+
 }
+