Skip to content

Commit 2790aeb

Browse files
committed
Added tests and more safe codding
1 parent 2d8783f commit 2790aeb

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,18 @@ protected ResultSetImpl executeQueryImpl(String sql, QuerySettings settings) thr
190190
// Best effort: leave writtenRows as 0 if we can't obtain it.
191191
}
192192
this.currentUpdateCount = (int) Math.min(writtenRows, Integer.MAX_VALUE);
193-
reader.close();
194-
response.close();
195-
onResultSetClosed(null); // no more result sets left - close statement.
193+
try {
194+
reader.close();
195+
} catch (Exception closeEx) {
196+
LOG.warn("Failed to close reader when schema is null", closeEx);
197+
} finally {
198+
try {
199+
response.close();
200+
} catch (Exception closeRespEx) {
201+
LOG.warn("Failed to close response when schema is null", closeRespEx);
202+
}
203+
}
204+
onResultSetClosed(null);
196205
return null;
197206
}
198207
return new ResultSetImpl(this, response, reader, this::handleSocketTimeoutException);

jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,26 @@ public void testUnknownStatementTest() throws Exception {
18101810
}
18111811
}
18121812
}
1813+
1814+
// No-ResultSet path: DDL should not produce a ResultSet
1815+
String tmpTable = "tmp_no_result_" + RandomStringUtils.randomAlphanumeric(8);
1816+
// PreparedStatement: execute() should return false, executeQuery() should throw
1817+
try (PreparedStatement stmt = conn.prepareStatement(
1818+
"CREATE TEMPORARY TABLE " + tmpTable + " (x Int32)")) {
1819+
Assert.assertFalse(stmt.execute(), "DDL should not produce a ResultSet");
1820+
Assert.assertNull(stmt.getResultSet(), "ResultSet should be null for DDL");
1821+
assertThrows(SQLException.class, stmt::executeQuery);
1822+
}
1823+
// Statement: execute() should return false, executeQuery() should throw
1824+
String tmpTable2 = "tmp_no_result_" + RandomStringUtils.randomAlphanumeric(8);
1825+
try (Statement stmt = conn.createStatement()) {
1826+
Assert.assertFalse(
1827+
stmt.execute("CREATE TEMPORARY TABLE " + tmpTable2 + " (x Int32)"),
1828+
"DDL should not produce a ResultSet");
1829+
Assert.assertNull(stmt.getResultSet(), "ResultSet should be null for DDL");
1830+
assertThrows(SQLException.class,
1831+
() -> stmt.executeQuery("CREATE TEMPORARY TABLE " + tmpTable2 + " (x Int32)"));
1832+
}
18131833
}
18141834
}
18151835
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1342,12 +1342,17 @@ public void testUnknownStatementTest(String parserName) throws Exception {
13421342
stmt.execute("DROP TABLE IF EXISTS test_unknown_statement_test");
13431343
stmt.execute("CREATE TABLE test_unknown_statement_test (v Int32) Engine MergeTree ORDER BY ()");
13441344

1345-
stmt.execute("INSERT INTO test_unknown_statement_test VALUES (1);");
1345+
// INSERT via execute(...) must not produce a ResultSet and should return false
1346+
boolean hasResultSet = stmt.execute("INSERT INTO test_unknown_statement_test VALUES (1);");
1347+
assertFalse(hasResultSet);
13461348
assertEquals(stmt.getUpdateCount(), 1);
13471349

13481350
stmt.executeUpdate("INSERT INTO test_unknown_statement_test VALUES (2);");
13491351
assertEquals(stmt.getUpdateCount(), 1);
13501352

1353+
assertThrows(SQLException.class,
1354+
() -> stmt.executeQuery("INSERT INTO test_unknown_statement_test VALUES (3);"));
1355+
13511356
}
13521357
}
13531358
}

0 commit comments

Comments
 (0)