zstan commented on code in PR #2906:
URL: https://github.com/apache/ignite-3/pull/2906#discussion_r1427946533
##########
modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcMultiStatementSelfTest.java:
##########
@@ -52,48 +56,321 @@ public void setupTables() throws Exception {
+ "(4, 19, 'Nick');");
}
- /**
- * Execute sql script using thin driver.
- */
- private void execute(String sql) throws Exception {
- stmt.executeUpdate(sql);
+ @AfterEach
+ void tearDown() throws Exception {
+ // only connection context or 0 if already closed.
+ assertTrue(openCursorsRegistered() <= 1);
+ }
+
+ @Test
+ public void testSimpleQueryExecute() throws Exception {
+ boolean res = stmt.execute("INSERT INTO TEST_TX VALUES (5, 5, '5');");
+ assertFalse(res);
+ assertNull(stmt.getResultSet());
+ assertFalse(stmt.getMoreResults());
+ assertEquals(-1, getResultSetSize());
+
+ stmt.execute("INSERT INTO TEST_TX VALUES (6, 5, '5');");
+ assertEquals(-1, getResultSetSize());
+
+ // empty result
+ res = stmt.execute("SELECT ID FROM TEST_TX WHERE ID=1000;");
+ assertTrue(res);
+ assertNotNull(stmt.getResultSet());
+ }
+
+ @Test
+ public void testSimpleQueryError() throws Exception {
+ boolean res = stmt.execute("SELECT 1; SELECT 1/0");
+ assertTrue(res);
+ assertThrows(SQLException.class, () -> stmt.getMoreResults());
+ }
+
+ @Test
+ public void testCloseOnCompletion() throws Exception {
+ stmt.execute("SELECT 1; SELECT 2");
+ ResultSet rs1 = stmt.getResultSet();
+
+ stmt.getMoreResults();
+ ResultSet rs2 = stmt.getResultSet();
+
+ assertFalse(stmt.isCloseOnCompletion());
+ stmt.closeOnCompletion();
+ assertTrue(stmt.isCloseOnCompletion());
+
+ assertFalse(stmt.isClosed());
+ rs1.close();
+ rs2.close();
+ assertTrue(stmt.isClosed());
+ }
+
+ @Test
+ public void testMixedDmlQueryExecute() throws Exception {
+ boolean res = stmt.execute("INSERT INTO TEST_TX VALUES (6, 5, '5');
DELETE FROM TEST_TX WHERE ID=6; SELECT 1;");
+ assertFalse(res);
+ assertEquals(1, getResultSetSize());
+
+ res = stmt.execute("SELECT 1; INSERT INTO TEST_TX VALUES (7, 5, '5');
DELETE FROM TEST_TX WHERE ID=6;");
+ assertEquals(true, res);
+ assertEquals(1, getResultSetSize());
+
+ // empty results set in the middle
+ res = stmt.execute("SELECT * FROM TEST_TX; INSERT INTO TEST_TX VALUES
(6, 6, '6'); SELECT * FROM TEST_TX;");
+ assertEquals(true, res);
+ assertEquals(11, getResultSetSize());
+ }
+
+ @Test
+ public void testMiscDmlExecute() throws Exception {
+ boolean res = stmt.execute("DROP TABLE IF EXISTS TEST_TX; DROP TABLE
IF EXISTS SOME_UNEXISTING_TBL;");
+ assertFalse(res);
+ assertEquals(-1, getResultSetSize());
+
+ res = stmt.execute("CREATE TABLE TEST_TX (ID INT PRIMARY KEY, AGE INT,
NAME VARCHAR) ");
+ assertFalse(res);
+ assertEquals(-1, getResultSetSize());
+
+ res = stmt.execute("INSERT INTO TEST_TX VALUES (1, 17, 'James'), (2,
43, 'Valery');");
+ assertFalse(res);
+ assertEquals(-1, getResultSetSize());
+
+ res = stmt.execute("DROP TABLE IF EXISTS PUBLIC.TRANSACTIONS; INSERT
INTO TEST_TX VALUES (3, 25, 'Michel');");
+ assertFalse(res);
+ assertEquals(-1, getResultSetSize());
+ }
+
+ @Test
+ public void testPureTransaction() throws Exception {
+ boolean res = stmt.execute("START TRANSACTION; COMMIT");
+ assertFalse(res);
+ assertNull(stmt.getResultSet());
+ assertEquals(0, stmt.getUpdateCount());
+ assertFalse(stmt.getMoreResults());
+ assertEquals(0, stmt.getUpdateCount());
+ assertFalse(stmt.getMoreResults());
+ assertEquals(-1, stmt.getUpdateCount());
+ }
+
+ @Test
+ public void testBrokenTransaction() throws Exception {
+ boolean res = stmt.execute("START TRANSACTION;");
+ assertFalse(res);
+ assertNull(stmt.getResultSet());
+ assertEquals(0, stmt.getUpdateCount());
+ assertFalse(stmt.getMoreResults());
+ assertEquals(-1, stmt.getUpdateCount());
+
+ res = stmt.execute("COMMIT;");
+ assertFalse(res);
+ assertNull(stmt.getResultSet());
+ assertEquals(0, stmt.getUpdateCount());
+ assertFalse(stmt.getMoreResults());
+ assertEquals(-1, stmt.getUpdateCount());
+ }
+
+ @Test
+ public void testTransactionQueryInside() throws Exception {
+ stmt.execute("START TRANSACTION; SELECT 1; COMMIT");
+ ResultSet resultSet = stmt.getResultSet();
+ assertNull(resultSet);
+ assertEquals(0, stmt.getUpdateCount());
+
+ // SELECT 1
+ assertTrue(stmt.getMoreResults());
+ resultSet = stmt.getResultSet();
+ assertNotNull(resultSet);
+
+ // COMMIT
+ assertFalse(stmt.getMoreResults());
+ resultSet = stmt.getResultSet();
+ assertNull(resultSet);
+ assertEquals(0, stmt.getUpdateCount());
+
+ // after commit
+ assertFalse(stmt.getMoreResults());
+ assertEquals(-1, stmt.getUpdateCount());
+ }
+
+ @Test
+ public void testTransactionQueryInsideOutside() throws Exception {
+ stmt.execute("START TRANSACTION; SELECT 1; COMMIT; SELECT 2;");
+ ResultSet resultSet = stmt.getResultSet();
+ assertNull(resultSet);
+ assertEquals(0, stmt.getUpdateCount());
+
+ // SELECT 1;
+ assertTrue(stmt.getMoreResults());
+ resultSet = stmt.getResultSet();
+ assertNotNull(resultSet);
+
+ // COMMIT;
+ assertFalse(stmt.getMoreResults());
+ resultSet = stmt.getResultSet();
+ assertNull(resultSet);
+ assertEquals(0, stmt.getUpdateCount());
+
+ // SELECT 2;
+ assertTrue(stmt.getMoreResults());
+ resultSet = stmt.getResultSet();
+ assertNotNull(resultSet);
+ assertEquals(-1, stmt.getUpdateCount());
+
+ // after
+ assertFalse(stmt.getMoreResults());
+ assertEquals(-1, stmt.getUpdateCount());
}
- /**
- * Assert that script containing both h2 and non h2 (native) sql
statements is handled correctly.
- */
@Test
- public void testMixedCommands() throws Exception {
- execute("CREATE TABLE public.transactions (pk INT, id INT, k VARCHAR,
v VARCHAR, PRIMARY KEY (pk, id)); "
- + "CREATE INDEX transactions_id_k_v ON public.transactions
(id, k, v) INLINE_SIZE 150; "
- + "INSERT INTO public.transactions VALUES (1,2,'some', 'word')
; "
- + "CREATE INDEX transactions_k_v_id ON public.transactions (k,
v, id) INLINE_SIZE 150; "
- + "CREATE INDEX transactions_pk_id ON public.transactions (pk,
id) INLINE_SIZE 20;");
+ public void testDmlInsideTransaction() throws Exception {
+ stmt.execute("START TRANSACTION; INSERT INTO TEST_TX VALUES (5, 19,
'Nick'); COMMIT");
+ assertEquals(-1, getResultSetSize());
+ }
+
+ @Test
+ public void testAutoCommitFalse() throws Exception {
+ conn.setAutoCommit(false);
+ assertThrows(SQLException.class, () -> stmt.execute("COMMIT"));
+
+ boolean res = stmt.execute("SELECT 1;COMMIT");
+ assertTrue(res);
+ assertNotNull(stmt.getResultSet());
+ assertThrows(SQLException.class, () -> stmt.getMoreResults());
+ }
+
+ @Test
+ public void testPreviousResultSetIsClosedExecute() throws Exception {
+ boolean res = stmt.execute("SELECT ID FROM TEST_TX; SELECT 1;");
+ assertEquals(true, res);
+ stmt.getResultSet();
+ ResultSet rs = stmt.getResultSet();
+
+ res = stmt.getMoreResults();
+ assertEquals(true, res);
+
+ assertTrue(rs.isClosed());
+ assertEquals(1, getResultSetSize());
+
+ stmt.execute("SELECT 1; SELECT 2; SELECT 3;");
Review Comment:
tx scenario is fixed, appropriate test appended.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]