Skip to content

Commit 2473ffa

Browse files
committed
Support a Statement.getMoreResults feature
Add support for two methods getMoreResults and its synonym getMoreResults(CLOSE_CURRENT_RESULT). Tarantool does not support multiple results per one query. It leads both KEEP_CURRENT_RESULT and CLOSE_ALL_RESULTS modes are not supported by the driver. Closes: #182
1 parent 6f620ce commit 2473ffa

File tree

4 files changed

+156
-4
lines changed

4 files changed

+156
-4
lines changed

Diff for: src/main/java/org/tarantool/jdbc/SQLStatement.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,21 @@ public int getUpdateCount() throws SQLException {
218218

219219
@Override
220220
public boolean getMoreResults() throws SQLException {
221-
checkNotClosed();
222-
return false;
221+
return getMoreResults(Statement.CLOSE_CURRENT_RESULT);
223222
}
224223

225224
@Override
226225
public boolean getMoreResults(int current) throws SQLException {
227226
checkNotClosed();
227+
JdbcConstants.checkCurrentResultConstant(current);
228+
if (resultSet != null &&
229+
(current == KEEP_CURRENT_RESULT || current == CLOSE_ALL_RESULTS)) {
230+
throw new SQLFeatureNotSupportedException();
231+
}
232+
233+
// the driver doesn't support multiple results
234+
// close current result and return no-more-results flag
235+
discardLastResults();
228236
return false;
229237
}
230238

Diff for: src/main/java/org/tarantool/util/JdbcConstants.java

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public static void checkHoldabilityConstant(int holdability) throws SQLException
2727
}
2828
}
2929

30+
public static void checkCurrentResultConstant(int currentResult) throws SQLException {
31+
if (currentResult != Statement.CLOSE_CURRENT_RESULT &&
32+
currentResult != Statement.CLOSE_ALL_RESULTS &&
33+
currentResult != Statement.KEEP_CURRENT_RESULT) {
34+
throw new SQLNonTransientException("", SQLStates.INVALID_PARAMETER_VALUE.getSqlState());
35+
}
36+
}
37+
3038
public static class DatabaseMetadataTable {
3139

3240
private DatabaseMetadataTable() {

Diff for: src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java

+79
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.sql.PreparedStatement;
2727
import java.sql.ResultSet;
2828
import java.sql.SQLException;
29+
import java.sql.SQLFeatureNotSupportedException;
2930
import java.sql.Statement;
3031
import java.util.Collections;
3132
import java.util.List;
@@ -243,6 +244,84 @@ void testStatementConnection() throws SQLException {
243244
assertEquals(conn, statement.getConnection());
244245
}
245246

247+
@Test
248+
public void testMoreResultsWithResultSet() throws SQLException {
249+
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
250+
prep.setInt(1, 1);
251+
252+
prep.execute();
253+
ResultSet resultSet = prep.getResultSet();
254+
255+
assertFalse(resultSet.isClosed());
256+
assertFalse(prep.getMoreResults());
257+
assertEquals(-1, prep.getUpdateCount());
258+
assertTrue(resultSet.isClosed());
259+
}
260+
261+
@Test
262+
public void testMoreResultsWithUpdateCount() throws SQLException {
263+
prep = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
264+
prep.setInt(1, 9);
265+
prep.setString(2, "nine");
266+
267+
prep.execute();
268+
int updateCount = prep.getUpdateCount();
269+
270+
assertEquals(1, prep.getUpdateCount());
271+
assertFalse(prep.getMoreResults());
272+
assertEquals(-1, prep.getUpdateCount());
273+
}
274+
275+
@Test
276+
public void testMoreResultsButCloseCurrent() throws SQLException {
277+
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
278+
prep.setInt(1, 2);
279+
280+
prep.execute();
281+
ResultSet resultSet = prep.getResultSet();
282+
283+
assertFalse(resultSet.isClosed());
284+
assertFalse(prep.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
285+
assertEquals(-1, prep.getUpdateCount());
286+
assertTrue(resultSet.isClosed());
287+
}
288+
289+
@Test
290+
public void testMoreResultsButCloseAll() throws SQLException {
291+
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
292+
prep.setInt(1, 2);
293+
prep.execute();
294+
295+
assertThrows(SQLFeatureNotSupportedException.class, () -> prep.getMoreResults(Statement.CLOSE_ALL_RESULTS));
296+
297+
prep = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
298+
prep.setInt(1, 21);
299+
prep.setString(2, "twenty one");
300+
prep.execute();
301+
302+
assertEquals(1, prep.getUpdateCount());
303+
assertFalse(prep.getMoreResults(Statement.CLOSE_ALL_RESULTS));
304+
assertEquals(-1, prep.getUpdateCount());
305+
}
306+
307+
@Test
308+
public void testMoreResultsButKeepCurrent() throws SQLException {
309+
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
310+
prep.setInt(1, 3);
311+
prep.execute();
312+
313+
assertThrows(SQLFeatureNotSupportedException.class, () -> prep.getMoreResults(Statement.KEEP_CURRENT_RESULT));
314+
315+
prep = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
316+
prep.setInt(1, 22);
317+
prep.setString(2, "twenty two");
318+
prep.execute();
319+
320+
assertEquals(1, prep.getUpdateCount());
321+
assertFalse(prep.getMoreResults(Statement.KEEP_CURRENT_RESULT));
322+
assertEquals(-1, prep.getUpdateCount());
323+
}
324+
246325
private List<?> consoleSelect(Object key) {
247326
List<?> list = testHelper.evaluate(TestUtils.toLuaSelect("TEST", key));
248327
return list == null ? Collections.emptyList() : (List<?>) list.get(0);

Diff for: src/test/java/org/tarantool/jdbc/JdbcStatementIT.java

+59-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.junit.jupiter.api.Assertions.assertThrows;
77
import static org.junit.jupiter.api.Assertions.assertTrue;
88
import static org.tarantool.TestAssumptions.assumeMinimalServerVersion;
9+
import static org.tarantool.jdbc.SqlAssertions.assertSqlExceptionHasStatus;
910

1011
import org.tarantool.ServerVersion;
1112
import org.tarantool.TarantoolTestHelper;
@@ -22,6 +23,7 @@
2223
import java.sql.DriverManager;
2324
import java.sql.ResultSet;
2425
import java.sql.SQLException;
26+
import java.sql.SQLFeatureNotSupportedException;
2527
import java.sql.Statement;
2628
import java.util.Collections;
2729
import java.util.List;
@@ -93,7 +95,7 @@ public void testExecuteWrongQuery() throws SQLException {
9395
String wrongResultQuery = "INSERT INTO test(id, val) VALUES (40, 'forty')";
9496

9597
SQLException exception = assertThrows(SQLException.class, () -> stmt.executeQuery(wrongResultQuery));
96-
SqlAssertions.assertSqlExceptionHasStatus(exception, SQLStates.NO_DATA);
98+
assertSqlExceptionHasStatus(exception, SQLStates.NO_DATA);
9799
}
98100

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

110112
SQLException exception = assertThrows(SQLException.class, () -> stmt.executeUpdate(wrongUpdateQuery));
111-
SqlAssertions.assertSqlExceptionHasStatus(exception, SQLStates.TOO_MANY_RESULTS);
113+
assertSqlExceptionHasStatus(exception, SQLStates.TOO_MANY_RESULTS);
112114
}
113115

114116
@Test
@@ -364,6 +366,61 @@ void testCloseOnCompletionMixedQueries() throws SQLException {
364366
assertTrue(stmt.isClosed());
365367
}
366368

369+
@Test
370+
public void testMoreResultsWithResultSet() throws SQLException {
371+
stmt.execute("SELECT val FROM test WHERE id = 1");
372+
373+
ResultSet rs = stmt.getResultSet();
374+
375+
assertFalse(rs.isClosed());
376+
assertFalse(stmt.getMoreResults());
377+
assertEquals(-1, stmt.getUpdateCount());
378+
assertTrue(rs.isClosed());
379+
}
380+
381+
@Test
382+
public void testMoreResultsWithUpdateCount() throws SQLException {
383+
stmt.execute("INSERT INTO test(id, val) VALUES (9, 'nine')");
384+
385+
assertEquals(1, stmt.getUpdateCount());
386+
assertFalse(stmt.getMoreResults());
387+
assertEquals(-1, stmt.getUpdateCount());
388+
}
389+
390+
@Test
391+
public void testMoreResultsButCloseCurrent() throws SQLException {
392+
stmt.execute("SELECT val FROM test WHERE id = 1");
393+
394+
ResultSet resultSet = stmt.getResultSet();
395+
396+
assertFalse(resultSet.isClosed());
397+
assertFalse(stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
398+
assertEquals(-1, stmt.getUpdateCount());
399+
assertTrue(resultSet.isClosed());
400+
}
401+
402+
@Test
403+
public void testMoreResultsButCloseAll() throws SQLException {
404+
stmt.execute("SELECT val FROM test WHERE id = 3");
405+
assertThrows(SQLFeatureNotSupportedException.class, () -> stmt.getMoreResults(Statement.CLOSE_ALL_RESULTS));
406+
407+
stmt.execute("INSERT INTO test(id, val) VALUES (21, 'twenty one')");
408+
assertEquals(1, stmt.getUpdateCount());
409+
assertFalse(stmt.getMoreResults(Statement.CLOSE_ALL_RESULTS));
410+
assertEquals(-1, stmt.getUpdateCount());
411+
}
412+
413+
@Test
414+
public void testMoreResultsButKeepCurrent() throws SQLException {
415+
stmt.execute("SELECT val FROM test WHERE id = 2");
416+
assertThrows(SQLFeatureNotSupportedException.class, () -> stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
417+
418+
stmt.execute("INSERT INTO test(id, val) VALUES (22, 'twenty two')");
419+
assertEquals(1, stmt.getUpdateCount());
420+
assertFalse(stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
421+
assertEquals(-1, stmt.getUpdateCount());
422+
}
423+
367424
private List<?> consoleSelect(Object key) {
368425
List<?> list = testHelper.evaluate(TestUtils.toLuaSelect("TEST", key));
369426
return list == null ? Collections.emptyList() : (List<?>) list.get(0);

0 commit comments

Comments
 (0)