Skip to content

Support a Statement.getMoreResults feature #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main/java/org/tarantool/jdbc/SQLStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,21 @@ public int getUpdateCount() throws SQLException {

@Override
public boolean getMoreResults() throws SQLException {
checkNotClosed();
return false;
return getMoreResults(Statement.CLOSE_CURRENT_RESULT);
}

@Override
public boolean getMoreResults(int current) throws SQLException {
checkNotClosed();
JdbcConstants.checkCurrentResultConstant(current);
if (resultSet != null &&
(current == KEEP_CURRENT_RESULT || current == CLOSE_ALL_RESULTS)) {
throw new SQLFeatureNotSupportedException();
}

// the driver doesn't support multiple results
// close current result and return no-more-results flag
discardLastResults();
return false;
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/tarantool/util/JdbcConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public static void checkHoldabilityConstant(int holdability) throws SQLException
}
}

public static void checkCurrentResultConstant(int currentResult) throws SQLException {
if (currentResult != Statement.CLOSE_CURRENT_RESULT &&
currentResult != Statement.CLOSE_ALL_RESULTS &&
currentResult != Statement.KEEP_CURRENT_RESULT) {
throw new SQLNonTransientException("", SQLStates.INVALID_PARAMETER_VALUE.getSqlState());
}
}

public static class DatabaseMetadataTable {

private DatabaseMetadataTable() {
Expand Down
79 changes: 79 additions & 0 deletions src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -243,6 +244,84 @@ void testStatementConnection() throws SQLException {
assertEquals(conn, statement.getConnection());
}

@Test
public void testMoreResultsWithResultSet() throws SQLException {
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
prep.setInt(1, 1);

prep.execute();
ResultSet resultSet = prep.getResultSet();

assertFalse(resultSet.isClosed());
assertFalse(prep.getMoreResults());
assertEquals(-1, prep.getUpdateCount());
assertTrue(resultSet.isClosed());
}

@Test
public void testMoreResultsWithUpdateCount() throws SQLException {
prep = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
prep.setInt(1, 9);
prep.setString(2, "nine");

prep.execute();
int updateCount = prep.getUpdateCount();

assertEquals(1, prep.getUpdateCount());
assertFalse(prep.getMoreResults());
assertEquals(-1, prep.getUpdateCount());
}

@Test
public void testMoreResultsButCloseCurrent() throws SQLException {
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
prep.setInt(1, 2);

prep.execute();
ResultSet resultSet = prep.getResultSet();

assertFalse(resultSet.isClosed());
assertFalse(prep.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
assertEquals(-1, prep.getUpdateCount());
assertTrue(resultSet.isClosed());
}

@Test
public void testMoreResultsButCloseAll() throws SQLException {
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
prep.setInt(1, 2);
prep.execute();

assertThrows(SQLFeatureNotSupportedException.class, () -> prep.getMoreResults(Statement.CLOSE_ALL_RESULTS));

prep = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
prep.setInt(1, 21);
prep.setString(2, "twenty one");
prep.execute();

assertEquals(1, prep.getUpdateCount());
assertFalse(prep.getMoreResults(Statement.CLOSE_ALL_RESULTS));
assertEquals(-1, prep.getUpdateCount());
}

@Test
public void testMoreResultsButKeepCurrent() throws SQLException {
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
prep.setInt(1, 3);
prep.execute();

assertThrows(SQLFeatureNotSupportedException.class, () -> prep.getMoreResults(Statement.KEEP_CURRENT_RESULT));

prep = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
prep.setInt(1, 22);
prep.setString(2, "twenty two");
prep.execute();

assertEquals(1, prep.getUpdateCount());
assertFalse(prep.getMoreResults(Statement.KEEP_CURRENT_RESULT));
assertEquals(-1, prep.getUpdateCount());
}

private List<?> consoleSelect(Object key) {
List<?> list = testHelper.evaluate(TestUtils.toLuaSelect("TEST", key));
return list == null ? Collections.emptyList() : (List<?>) list.get(0);
Expand Down
61 changes: 59 additions & 2 deletions src/test/java/org/tarantool/jdbc/JdbcStatementIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.tarantool.TestAssumptions.assumeMinimalServerVersion;
import static org.tarantool.jdbc.SqlAssertions.assertSqlExceptionHasStatus;

import org.tarantool.ServerVersion;
import org.tarantool.TarantoolTestHelper;
Expand All @@ -22,6 +23,7 @@
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -93,7 +95,7 @@ public void testExecuteWrongQuery() throws SQLException {
String wrongResultQuery = "INSERT INTO test(id, val) VALUES (40, 'forty')";

SQLException exception = assertThrows(SQLException.class, () -> stmt.executeQuery(wrongResultQuery));
SqlAssertions.assertSqlExceptionHasStatus(exception, SQLStates.NO_DATA);
assertSqlExceptionHasStatus(exception, SQLStates.NO_DATA);
}

@Test
Expand All @@ -108,7 +110,7 @@ public void testExecuteWrongUpdate() throws SQLException {
String wrongUpdateQuery = "SELECT val FROM test";

SQLException exception = assertThrows(SQLException.class, () -> stmt.executeUpdate(wrongUpdateQuery));
SqlAssertions.assertSqlExceptionHasStatus(exception, SQLStates.TOO_MANY_RESULTS);
assertSqlExceptionHasStatus(exception, SQLStates.TOO_MANY_RESULTS);
}

@Test
Expand Down Expand Up @@ -364,6 +366,61 @@ void testCloseOnCompletionMixedQueries() throws SQLException {
assertTrue(stmt.isClosed());
}

@Test
public void testMoreResultsWithResultSet() throws SQLException {
stmt.execute("SELECT val FROM test WHERE id = 1");

ResultSet rs = stmt.getResultSet();

assertFalse(rs.isClosed());
assertFalse(stmt.getMoreResults());
assertEquals(-1, stmt.getUpdateCount());
assertTrue(rs.isClosed());
}

@Test
public void testMoreResultsWithUpdateCount() throws SQLException {
stmt.execute("INSERT INTO test(id, val) VALUES (9, 'nine')");

assertEquals(1, stmt.getUpdateCount());
assertFalse(stmt.getMoreResults());
assertEquals(-1, stmt.getUpdateCount());
}

@Test
public void testMoreResultsButCloseCurrent() throws SQLException {
stmt.execute("SELECT val FROM test WHERE id = 1");

ResultSet resultSet = stmt.getResultSet();

assertFalse(resultSet.isClosed());
assertFalse(stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
assertEquals(-1, stmt.getUpdateCount());
assertTrue(resultSet.isClosed());
}

@Test
public void testMoreResultsButCloseAll() throws SQLException {
stmt.execute("SELECT val FROM test WHERE id = 3");
assertThrows(SQLFeatureNotSupportedException.class, () -> stmt.getMoreResults(Statement.CLOSE_ALL_RESULTS));

stmt.execute("INSERT INTO test(id, val) VALUES (21, 'twenty one')");
assertEquals(1, stmt.getUpdateCount());
assertFalse(stmt.getMoreResults(Statement.CLOSE_ALL_RESULTS));
assertEquals(-1, stmt.getUpdateCount());
}

@Test
public void testMoreResultsButKeepCurrent() throws SQLException {
stmt.execute("SELECT val FROM test WHERE id = 2");
assertThrows(SQLFeatureNotSupportedException.class, () -> stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));

stmt.execute("INSERT INTO test(id, val) VALUES (22, 'twenty two')");
assertEquals(1, stmt.getUpdateCount());
assertFalse(stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
assertEquals(-1, stmt.getUpdateCount());
}

private List<?> consoleSelect(Object key) {
List<?> list = testHelper.evaluate(TestUtils.toLuaSelect("TEST", key));
return list == null ? Collections.emptyList() : (List<?>) list.get(0);
Expand Down