Skip to content

Commit

Permalink
fix(jdbc): PreparedStatement#executeQuery should not return null
Browse files Browse the repository at this point in the history
Closes: xerial#914
  • Loading branch information
gotson committed Oct 18, 2023
1 parent 22b27c6 commit 5845ead
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/org/sqlite/jdbc3/JDBC3PreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public boolean execute() throws SQLException {
checkOpen();
rs.close();
pointer.safeRunConsume(DB::reset);
exhaustedResults = false;

if (this.conn instanceof JDBC3Connection) {
((JDBC3Connection) this.conn).tryEnforceTransactionMode();
Expand Down Expand Up @@ -74,6 +75,7 @@ public ResultSet executeQuery() throws SQLException {

rs.close();
pointer.safeRunConsume(DB::reset);
exhaustedResults = false;

if (this.conn instanceof JDBC3Connection) {
((JDBC3Connection) this.conn).tryEnforceTransactionMode();
Expand Down Expand Up @@ -110,6 +112,7 @@ public long executeLargeUpdate() throws SQLException {

rs.close();
pointer.safeRunConsume(DB::reset);
exhaustedResults = false;

if (this.conn instanceof JDBC3Connection) {
((JDBC3Connection) this.conn).tryEnforceTransactionMode();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/sqlite/jdbc3/JDBC3Statement.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class JDBC3Statement extends CoreStatement {

private int queryTimeout; // in seconds, as per the JDBC spec
protected long updateCount;
private boolean exhaustedResults = false;
protected boolean exhaustedResults = false;

// PUBLIC INTERFACE /////////////////////////////////////////////

Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/sqlite/PrepStmtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -816,4 +816,18 @@ void getParameterTypeTest_when_no_parameter_set() throws SQLException {
.hasMessage("No parameter has been set yet");
}
}

@Test
public void gh914_reuseExecute() throws SQLException {
try (PreparedStatement ps = conn.prepareStatement("SELECT 1")) {
assertThat(ps.execute()).isTrue();
ResultSet rs = ps.getResultSet();
assertThat(rs.next()).isTrue();
assertThat(rs.next()).isFalse();
assertThat(ps.getMoreResults()).isFalse();

ResultSet rs2 = ps.executeQuery();
assertThat(rs2).isNotNull();
}
}
}

0 comments on commit 5845ead

Please sign in to comment.