diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/SqlExceptionUtils.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/SqlExceptionUtils.java index da5b824be..25b3ffe3f 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/SqlExceptionUtils.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/SqlExceptionUtils.java @@ -107,10 +107,6 @@ public static BatchUpdateException batchUpdateError(Throwable e, long[] updateCo return new BatchUpdateException("Unexpected error", SQL_STATE_SQL_ERROR, 0, updateCounts, cause); } - public static SQLException emptyBatchError() { - return clientError("Please call addBatch method at least once before batch execution"); - } - public static BatchUpdateException queryInBatchError(int[] updateCounts) { return new BatchUpdateException("Query is not allow in batch update", SQL_STATE_CLIENT_ERROR, updateCounts); } diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseConnectionImpl.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseConnectionImpl.java index bae6de913..be75aa33f 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseConnectionImpl.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseConnectionImpl.java @@ -220,7 +220,7 @@ public ClickHouseConnectionImpl(ConnectionInfo connInfo) throws SQLException { autoCommit = !jdbcConf.isJdbcCompliant() || jdbcConf.isAutoCommit(); ClickHouseNode node = connInfo.getServer(); - log.debug("Connecting to node: %s", node); + log.debug("Connecting to: %s", node); jvmTimeZone = TimeZone.getDefault(); diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java index 2b64d85c6..94de8e5ec 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java @@ -25,6 +25,7 @@ import com.clickhouse.client.ClickHouseSerializer; import com.clickhouse.client.ClickHouseUtils; import com.clickhouse.client.ClickHouseValue; +import com.clickhouse.client.ClickHouseValues; import com.clickhouse.client.config.ClickHouseClientOption; import com.clickhouse.client.config.ClickHouseConfigChangeListener; import com.clickhouse.client.config.ClickHouseOption; @@ -588,7 +589,7 @@ public int[] executeBatch() throws SQLException { public long[] executeLargeBatch() throws SQLException { ensureOpen(); if (batchStmts.isEmpty()) { - throw SqlExceptionUtils.emptyBatchError(); + return ClickHouseValues.EMPTY_LONG_ARRAY; } boolean continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError(); diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/InputBasedPreparedStatement.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/InputBasedPreparedStatement.java index 28c09b805..fd676de9c 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/InputBasedPreparedStatement.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/InputBasedPreparedStatement.java @@ -94,7 +94,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException { boolean continueOnError = false; if (asBatch) { if (counter < 1) { - throw SqlExceptionUtils.emptyBatchError(); + return ClickHouseValues.EMPTY_LONG_ARRAY; } continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError(); } else { diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java index 8958f95d3..664f999ed 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java @@ -122,7 +122,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException { boolean continueOnError = false; if (asBatch) { if (counter < 1) { - throw SqlExceptionUtils.emptyBatchError(); + return ClickHouseValues.EMPTY_LONG_ARRAY; } continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError(); } else { diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/TableBasedPreparedStatement.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/TableBasedPreparedStatement.java index c389b5890..e86c94e27 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/TableBasedPreparedStatement.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/TableBasedPreparedStatement.java @@ -19,6 +19,7 @@ import com.clickhouse.client.ClickHouseRequest; import com.clickhouse.client.ClickHouseResponse; import com.clickhouse.client.ClickHouseUtils; +import com.clickhouse.client.ClickHouseValues; import com.clickhouse.client.data.ClickHouseExternalTable; import com.clickhouse.client.logging.Logger; import com.clickhouse.client.logging.LoggerFactory; @@ -75,7 +76,7 @@ public long[] executeAny(boolean asBatch) throws SQLException { boolean continueOnError = false; if (asBatch) { if (batch.isEmpty()) { - throw SqlExceptionUtils.emptyBatchError(); + return ClickHouseValues.EMPTY_LONG_ARRAY; } continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError(); } else { diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java index 2a811b792..c131e052b 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java @@ -778,15 +778,22 @@ public void testBatchInput() throws SQLException { public void testBatchQuery() throws SQLException { try (ClickHouseConnection conn = newConnection(new Properties()); PreparedStatement stmt = conn.prepareStatement("select * from numbers(100) where number < ?")) { + Assert.assertEquals(stmt.executeBatch(), new int[0]); + Assert.assertEquals(stmt.executeLargeBatch(), new long[0]); Assert.assertThrows(SQLException.class, () -> stmt.setInt(0, 5)); Assert.assertThrows(SQLException.class, () -> stmt.setInt(2, 5)); Assert.assertThrows(SQLException.class, () -> stmt.addBatch()); stmt.setInt(1, 3); + Assert.assertEquals(stmt.executeBatch(), new int[0]); + Assert.assertEquals(stmt.executeLargeBatch(), new long[0]); stmt.addBatch(); stmt.setInt(1, 2); stmt.addBatch(); Assert.assertThrows(BatchUpdateException.class, () -> stmt.executeBatch()); + + Assert.assertEquals(stmt.executeBatch(), new int[0]); + Assert.assertEquals(stmt.executeLargeBatch(), new long[0]); } } diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java index 9a61a4bf4..f08479186 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java @@ -136,6 +136,9 @@ public void testMaxFloatValues() throws SQLException { public void testMutation() throws SQLException { Properties props = new Properties(); try (ClickHouseConnection conn = newConnection(props); ClickHouseStatement stmt = conn.createStatement()) { + Assert.assertEquals(stmt.executeBatch(), new int[0]); + Assert.assertEquals(stmt.executeLargeBatch(), new long[0]); + Assert.assertFalse(stmt.execute("drop table if exists test_mutation;" + "create table test_mutation(a String, b UInt32) engine=MergeTree() order by tuple()"), "Should not return result set"); @@ -156,6 +159,9 @@ public void testMutation() throws SQLException { stmt.addBatch("drop table non_existing_table"); stmt.addBatch("insert into test_mutation values('2',2)"); Assert.assertThrows(SQLException.class, () -> stmt.executeBatch()); + + Assert.assertEquals(stmt.executeBatch(), new int[0]); + Assert.assertEquals(stmt.executeLargeBatch(), new long[0]); } props.setProperty(JdbcConfig.PROP_CONTINUE_BATCH, "true"); @@ -269,7 +275,12 @@ public void testExecute() throws SQLException { public void testExecuteBatch() throws SQLException { Properties props = new Properties(); try (Connection conn = newConnection(props); Statement stmt = conn.createStatement()) { - Assert.assertThrows(SQLException.class, () -> stmt.executeBatch()); + Assert.assertEquals(stmt.executeBatch(), new int[0]); + Assert.assertEquals(stmt.executeLargeBatch(), new long[0]); + stmt.addBatch("select 1"); + stmt.clearBatch(); + Assert.assertEquals(stmt.executeBatch(), new int[0]); + Assert.assertEquals(stmt.executeLargeBatch(), new long[0]); stmt.addBatch("select 1"); // mixed usage Assert.assertThrows(SQLException.class, () -> stmt.execute("select 2"));