Skip to content

Commit

Permalink
SQL: improve ResultSet behavior when no rows are available (#46753)
Browse files Browse the repository at this point in the history
Improve the defensive behavior of ResultSet when dealing with incorrect
API usage. In particular handle the case of dealing with no row
available (either because the cursor is before the first entry or after
the last).

Fix #46750

(cherry picked from commit 58fa38e)
  • Loading branch information
costin committed Sep 17, 2019
1 parent 9d6632d commit 90f4c23
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import static org.elasticsearch.xpack.sql.jdbc.EsType.DATETIME;
import static org.elasticsearch.xpack.sql.jdbc.EsType.TIME;
import static org.elasticsearch.xpack.sql.jdbc.JdbcDateUtils.asDateTimeField;
import static org.elasticsearch.xpack.sql.jdbc.JdbcDateUtils.dateTimeAsMillisSinceEpoch;
import static org.elasticsearch.xpack.sql.jdbc.JdbcDateUtils.asTimestamp;
import static org.elasticsearch.xpack.sql.jdbc.JdbcDateUtils.dateTimeAsMillisSinceEpoch;
import static org.elasticsearch.xpack.sql.jdbc.JdbcDateUtils.timeAsMillisSinceEpoch;
import static org.elasticsearch.xpack.sql.jdbc.JdbcDateUtils.timeAsTime;
import static org.elasticsearch.xpack.sql.jdbc.JdbcDateUtils.timeAsTimestamp;
Expand All @@ -57,6 +57,7 @@ class JdbcResultSet implements ResultSet, JdbcWrapper {

private boolean closed = false;
private boolean wasNull = false;
private boolean wasLast = false;

private int rowNumber;

Expand All @@ -78,10 +79,13 @@ private Object column(int columnIndex) throws SQLException {
if (columnIndex < 1 || columnIndex > cursor.columnSize()) {
throw new SQLException("Invalid column index [" + columnIndex + "]");
}
if (wasLast == true || rowNumber < 1) {
throw new SQLException("No row available");
}
Object object = null;
try {
object = cursor.column(columnIndex - 1);
} catch (IllegalArgumentException iae) {
} catch (Exception iae) {
throw new SQLException(iae.getMessage());
}
wasNull = (object == null);
Expand Down Expand Up @@ -114,6 +118,7 @@ public boolean next() throws SQLException {
rowNumber++;
return true;
}
wasLast = true;
return false;
}

Expand Down Expand Up @@ -461,7 +466,7 @@ public boolean isBeforeFirst() throws SQLException {

@Override
public boolean isAfterLast() throws SQLException {
throw new SQLFeatureNotSupportedException("isAfterLast not supported");
return rowNumber > 0 && wasLast;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,34 @@ public void testUnsupportedUpdateMethods() throws IOException, SQLException {
assertThrowsWritesUnsupportedForUpdate(() -> r.rowDeleted());
}

public void testResultSetNotInitialized() throws Exception {
createTestDataForNumericValueTypes(() -> randomInt());

SQLException sqle = expectThrows(SQLException.class, () -> {
doWithQuery(SELECT_WILDCARD, rs -> {
assertFalse(rs.isAfterLast());
rs.getObject(1);
});
});
assertEquals("No row available", sqle.getMessage());
}

public void testResultSetConsumed() throws Exception {
createTestDataForNumericValueTypes(() -> randomInt());

SQLException sqle = expectThrows(SQLException.class, () -> {
doWithQuery("SELECT * FROM test LIMIT 1", rs -> {
assertFalse(rs.isAfterLast());
assertTrue(rs.next());
assertFalse(rs.isAfterLast());
assertFalse(rs.next());
assertTrue(rs.isAfterLast());
rs.getObject(1);
});
});
assertEquals("No row available", sqle.getMessage());
}

private void doWithQuery(String query, CheckedConsumer<ResultSet, SQLException> consumer) throws SQLException {
doWithQuery(() -> esJdbc(timeZoneId), query, consumer);
}
Expand Down

0 comments on commit 90f4c23

Please sign in to comment.