Skip to content
Permalink
Browse files

fix: improper relative cursor movement (#1462)

Fix a wrong relative cursor movement when a new calculated index is
negative (beyond the first row). According to the java-doc, in this
case the position leaves before the first row. Current implementation
of the 'relative' method delegates the movement to the 'absolute' method
which treats a negative index with respect to the end of the result set.
  • Loading branch information...
nicktorwald authored and davecramer committed Apr 8, 2019
1 parent 5e48c4d commit b6ab27c170ebeb88728f33207924d664ce7f7d77
@@ -873,7 +873,12 @@ public boolean relative(int rows) throws SQLException {
}

// have to add 1 since absolute expects a 1-based index
return absolute(currentRow + 1 + rows);
int index = currentRow + 1 + rows;
if (index < 0) {
beforeFirst();
return false;
}
return absolute(index);
}


@@ -180,6 +180,49 @@ public void testAbsolute() throws SQLException {
stmt.close();
}

@Test
public void testRelative() throws SQLException {
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("SELECT * FROM testrs");

assertTrue(!rs.relative(0));
assertEquals(0, rs.getRow());
assertTrue(rs.isBeforeFirst());

assertTrue(rs.relative(2));
assertEquals(2, rs.getRow());

assertTrue(rs.relative(1));
assertEquals(3, rs.getRow());

assertTrue(rs.relative(0));
assertEquals(3, rs.getRow());

assertTrue(!rs.relative(-3));
assertEquals(0, rs.getRow());
assertTrue(rs.isBeforeFirst());

assertTrue(rs.relative(4));
assertEquals(4, rs.getRow());

assertTrue(rs.relative(-1));
assertEquals(3, rs.getRow());

assertTrue(!rs.relative(6));
assertEquals(0, rs.getRow());
assertTrue(rs.isAfterLast());

assertTrue(rs.relative(-4));
assertEquals(3, rs.getRow());

assertTrue(!rs.relative(-6));
assertEquals(0, rs.getRow());
assertTrue(rs.isBeforeFirst());

stmt.close();
}

@Test
public void testEmptyResult() throws SQLException {
Statement stmt =

0 comments on commit b6ab27c

Please sign in to comment.
You can’t perform that action at this time.