Skip to content

Commit 7effea6

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 762ce2f commit 7effea6

File tree

4 files changed

+156
-4
lines changed

4 files changed

+156
-4
lines changed

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

src/main/java/org/tarantool/util/JdbcConstants.java

+8
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ public static void checkHoldabilityConstant(int holdability) throws SQLException
2121
}
2222
}
2323

24+
public static void checkCurrentResultConstant(int currentResult) throws SQLException {
25+
if (currentResult != Statement.CLOSE_CURRENT_RESULT &&
26+
currentResult != Statement.CLOSE_ALL_RESULTS &&
27+
currentResult != Statement.KEEP_CURRENT_RESULT) {
28+
throw new SQLNonTransientException("", SQLStates.INVALID_PARAMETER_VALUE.getSqlState());
29+
}
30+
}
31+
2432
}

src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java

+79
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.sql.PreparedStatement;
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
22+
import java.sql.SQLFeatureNotSupportedException;
2223
import java.sql.Statement;
2324

2425
public class JdbcPreparedStatementIT extends JdbcTypesIT {
@@ -228,6 +229,84 @@ void testStatementConnection() throws SQLException {
228229
assertEquals(conn, statement.getConnection());
229230
}
230231

232+
@Test
233+
public void testMoreResultsWithResultSet() throws SQLException {
234+
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
235+
prep.setInt(1, 1);
236+
237+
prep.execute();
238+
ResultSet resultSet = prep.getResultSet();
239+
240+
assertFalse(resultSet.isClosed());
241+
assertFalse(prep.getMoreResults());
242+
assertEquals(-1, prep.getUpdateCount());
243+
assertTrue(resultSet.isClosed());
244+
}
245+
246+
@Test
247+
public void testMoreResultsWithUpdateCount() throws SQLException {
248+
prep = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
249+
prep.setInt(1, 9);
250+
prep.setString(2, "nine");
251+
252+
prep.execute();
253+
int updateCount = prep.getUpdateCount();
254+
255+
assertEquals(1, prep.getUpdateCount());
256+
assertFalse(prep.getMoreResults());
257+
assertEquals(-1, prep.getUpdateCount());
258+
}
259+
260+
@Test
261+
public void testMoreResultsButCloseCurrent() throws SQLException {
262+
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
263+
prep.setInt(1, 2);
264+
265+
prep.execute();
266+
ResultSet resultSet = prep.getResultSet();
267+
268+
assertFalse(resultSet.isClosed());
269+
assertFalse(prep.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
270+
assertEquals(-1, prep.getUpdateCount());
271+
assertTrue(resultSet.isClosed());
272+
}
273+
274+
@Test
275+
public void testMoreResultsButCloseAll() throws SQLException {
276+
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
277+
prep.setInt(1, 2);
278+
prep.execute();
279+
280+
assertThrows(SQLFeatureNotSupportedException.class, () -> prep.getMoreResults(Statement.CLOSE_ALL_RESULTS));
281+
282+
prep = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
283+
prep.setInt(1, 21);
284+
prep.setString(2, "twenty one");
285+
prep.execute();
286+
287+
assertEquals(1, prep.getUpdateCount());
288+
assertFalse(prep.getMoreResults(Statement.CLOSE_ALL_RESULTS));
289+
assertEquals(-1, prep.getUpdateCount());
290+
}
291+
292+
@Test
293+
public void testMoreResultsButKeepCurrent() throws SQLException {
294+
prep = conn.prepareStatement("SELECT val FROM test WHERE id = ?");
295+
prep.setInt(1, 3);
296+
prep.execute();
297+
298+
assertThrows(SQLFeatureNotSupportedException.class, () -> prep.getMoreResults(Statement.KEEP_CURRENT_RESULT));
299+
300+
prep = conn.prepareStatement("INSERT INTO test VALUES (?, ?)");
301+
prep.setInt(1, 22);
302+
prep.setString(2, "twenty two");
303+
prep.execute();
304+
305+
assertEquals(1, prep.getUpdateCount());
306+
assertFalse(prep.getMoreResults(Statement.KEEP_CURRENT_RESULT));
307+
assertEquals(-1, prep.getUpdateCount());
308+
}
309+
231310
@Test
232311
public void testSetByte() throws SQLException {
233312
makeHelper(Byte.class)

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.junit.jupiter.api.Assertions.fail;
9+
import static org.tarantool.jdbc.SqlAssertions.assertSqlExceptionHasStatus;
910

1011
import org.tarantool.util.SQLStates;
1112

@@ -16,6 +17,7 @@
1617

1718
import java.sql.ResultSet;
1819
import java.sql.SQLException;
20+
import java.sql.SQLFeatureNotSupportedException;
1921
import java.sql.Statement;
2022

2123
public class JdbcStatementIT extends AbstractJdbcIT {
@@ -49,7 +51,7 @@ public void testExecuteWrongQuery() throws SQLException {
4951
String wrongResultQuery = "INSERT INTO test(id, val) VALUES (40, 'forty')";
5052

5153
SQLException exception = assertThrows(SQLException.class, () -> stmt.executeQuery(wrongResultQuery));
52-
SqlAssertions.assertSqlExceptionHasStatus(exception, SQLStates.NO_DATA);
54+
assertSqlExceptionHasStatus(exception, SQLStates.NO_DATA);
5355
}
5456

5557
@Test
@@ -64,7 +66,7 @@ public void testExecuteWrongUpdate() throws SQLException {
6466
String wrongUpdateQuery = "SELECT val FROM test";
6567

6668
SQLException exception = assertThrows(SQLException.class, () -> stmt.executeUpdate(wrongUpdateQuery));
67-
SqlAssertions.assertSqlExceptionHasStatus(exception, SQLStates.TOO_MANY_RESULTS);
69+
assertSqlExceptionHasStatus(exception, SQLStates.TOO_MANY_RESULTS);
6870
}
6971

7072
@Test
@@ -348,4 +350,59 @@ void testCloseOnCompletionMixedQueries() throws SQLException {
348350
assertTrue(stmt.isClosed());
349351
}
350352

353+
@Test
354+
public void testMoreResultsWithResultSet() throws SQLException {
355+
stmt.execute("SELECT val FROM test WHERE id = 1");
356+
357+
ResultSet rs = stmt.getResultSet();
358+
359+
assertFalse(rs.isClosed());
360+
assertFalse(stmt.getMoreResults());
361+
assertEquals(-1, stmt.getUpdateCount());
362+
assertTrue(rs.isClosed());
363+
}
364+
365+
@Test
366+
public void testMoreResultsWithUpdateCount() throws SQLException {
367+
stmt.execute("INSERT INTO test(id, val) VALUES (9, 'nine')");
368+
369+
assertEquals(1, stmt.getUpdateCount());
370+
assertFalse(stmt.getMoreResults());
371+
assertEquals(-1, stmt.getUpdateCount());
372+
}
373+
374+
@Test
375+
public void testMoreResultsButCloseCurrent() throws SQLException {
376+
stmt.execute("SELECT val FROM test WHERE id = 1");
377+
378+
ResultSet resultSet = stmt.getResultSet();
379+
380+
assertFalse(resultSet.isClosed());
381+
assertFalse(stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
382+
assertEquals(-1, stmt.getUpdateCount());
383+
assertTrue(resultSet.isClosed());
384+
}
385+
386+
@Test
387+
public void testMoreResultsButCloseAll() throws SQLException {
388+
stmt.execute("SELECT val FROM test WHERE id = 3");
389+
assertThrows(SQLFeatureNotSupportedException.class, () -> stmt.getMoreResults(Statement.CLOSE_ALL_RESULTS));
390+
391+
stmt.execute("INSERT INTO test(id, val) VALUES (21, 'twenty one')");
392+
assertEquals(1, stmt.getUpdateCount());
393+
assertFalse(stmt.getMoreResults(Statement.CLOSE_ALL_RESULTS));
394+
assertEquals(-1, stmt.getUpdateCount());
395+
}
396+
397+
@Test
398+
public void testMoreResultsButKeepCurrent() throws SQLException {
399+
stmt.execute("SELECT val FROM test WHERE id = 2");
400+
assertThrows(SQLFeatureNotSupportedException.class, () -> stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
401+
402+
stmt.execute("INSERT INTO test(id, val) VALUES (22, 'twenty two')");
403+
assertEquals(1, stmt.getUpdateCount());
404+
assertFalse(stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
405+
assertEquals(-1, stmt.getUpdateCount());
406+
}
407+
351408
}

0 commit comments

Comments
 (0)