Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ 415] ResultSet.absolute() should not always return true
  • Loading branch information
rusher committed Jan 30, 2017
1 parent 2f99de9 commit 40f2ccf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
Expand Up @@ -785,17 +785,56 @@ public int getRow() throws SQLException {
@Override
public boolean absolute(int row) throws SQLException {
checkClose();

if (resultSetScrollType == TYPE_FORWARD_ONLY) {
throw new SQLException("Invalid operation for result set type TYPE_FORWARD_ONLY");
} else {
if (row >= 0 && row <= resultSetSize) {
}

if (row >= 0 && row <= resultSetSize) {
rowPointer = row - 1;
return true;
}

//if streaming, must read additional results.
if (!isEof) {
ReentrantLock lock = protocol.getLock();
lock.lock();
try {
fetchRemaining();
} catch (SQLException ioe) {
throw new SQLException("Server has closed the connection. If result set contain huge amount of data, Server expects client to"
+ " read off the result set relatively fast. "
+ "In this case, please consider increasing net_wait_timeout session variable."
+ " / processing your result set faster (check Streaming result sets documentation for more information)", ioe);
} finally {
lock.unlock();
}
}

if (row >= 0) {

if (row <= resultSetSize) {
rowPointer = row - 1;
return true;
} else if (row < 0) {
}

rowPointer = resultSetSize; //go to afterLast() position
return false;

} else {

if (resultSetSize + row >= 0) {
//absolute position reverse from ending resultSet
rowPointer = resultSetSize + row;
return true;
}
return true;

rowPointer = -1; // go to before first position
return false;

}


}

@Override
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/mariadb/jdbc/ResultSetTest.java
Expand Up @@ -298,6 +298,36 @@ public void generatedKeyNpe() throws SQLException {
rs.close();
}

@Test
public void testResultSetAbsolute() throws Exception {
insertRows(50);
try (Statement statement = sharedConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
statement.setFetchSize(10);
try (ResultSet rs = statement.executeQuery("SELECT * FROM result_set_test")) {
assertFalse(rs.absolute(52));
assertFalse(rs.absolute(-52));

assertTrue(rs.absolute(42));
assertEquals("row42", rs.getString(2));

assertTrue(rs.absolute(-11));
assertEquals("row40", rs.getString(2));

assertTrue(rs.absolute(0));
assertTrue(rs.isBeforeFirst());

assertFalse(rs.absolute(51));
assertTrue(rs.isAfterLast());

assertTrue(rs.absolute(-1));
assertEquals("row50", rs.getString(2));

assertTrue(rs.absolute(-50));
assertEquals("row1", rs.getString(2));
}
}
}

@Test
public void testResultSetIsAfterLast() throws Exception {
insertRows(2);
Expand Down

0 comments on commit 40f2ccf

Please sign in to comment.