From 7db110ff8cf708c59121adcbb8a7684fd74f87ec Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Fri, 20 May 2022 22:22:15 +0800 Subject: [PATCH] Fix #920 --- .../internal/ClickHouseConnectionImpl.java | 19 ++++---- .../jdbc/ClickHousePreparedStatementTest.java | 44 +++++++++++++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) 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 ebef724bc..3df1afb69 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 @@ -615,14 +615,17 @@ public PreparedStatement prepareStatement(String sql, int resultSetType, int res !parsedStmt.containsKeyword("SELECT") && parsedStmt.hasValues() && (!parsedStmt.hasFormat() || clientRequest.getFormat().name().equals(parsedStmt.getFormat()))) { String query = parsedStmt.getSQL(); - int startIndex = parsedStmt.getPositions().get(ClickHouseSqlStatement.KEYWORD_VALUES_START); - int endIndex = parsedStmt.getPositions().get(ClickHouseSqlStatement.KEYWORD_VALUES_END); - boolean useStream = true; - for (int i = startIndex + 1; i < endIndex; i++) { - char ch = query.charAt(i); - if (ch != '?' && ch != ',' && !Character.isWhitespace(ch)) { - useStream = false; - break; + boolean useStream = false; + Integer startIndex = parsedStmt.getPositions().get(ClickHouseSqlStatement.KEYWORD_VALUES_START); + if (startIndex != null) { + useStream = true; + int endIndex = parsedStmt.getPositions().get(ClickHouseSqlStatement.KEYWORD_VALUES_END); + for (int i = startIndex + 1; i < endIndex; i++) { + char ch = query.charAt(i); + if (ch != '?' && ch != ',' && !Character.isWhitespace(ch)) { + useStream = false; + break; + } } } 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 025b67adf..2f496ff91 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java @@ -1,8 +1,10 @@ package com.clickhouse.jdbc; import java.io.ByteArrayInputStream; +import java.math.BigDecimal; import java.net.Inet4Address; import java.net.Inet6Address; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.sql.BatchUpdateException; import java.sql.Connection; @@ -1257,4 +1259,46 @@ public void testInsertWithAndSelect() throws Exception { Assert.assertFalse(rs.next()); } } + + @Test(groups = "integration") + public void testInsertWithMultipleValues() throws Exception { + try (ClickHouseConnection conn = newConnection(new Properties()); + Statement s = conn.createStatement()) { + s.execute("drop table if exists test_insert_with_multiple_values; " + + "CREATE TABLE test_insert_with_multiple_values(a Int32, b Nullable(String)) ENGINE=Memory"); + try (PreparedStatement ps = conn.prepareStatement( + "INSERT INTO test_insert_with_multiple_values values(?, ?), (2 , ? ), ( ? , '') , (?,?) ,( ? ,? )")) { + ps.setInt(1, 1); + ps.setNull(2, Types.VARCHAR); + ps.setObject(3, "er"); + ps.setInt(4, 3); + ps.setInt(5, 4); + ps.setURL(6, new URL("http://some.host")); + ps.setInt(7, 5); + ps.setString(8, null); + ps.executeUpdate(); + } + + try (ResultSet rs = s.executeQuery("select * from test_insert_with_multiple_values order by a")) { + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getByte(1), (byte) 1); + Assert.assertEquals(rs.getObject(2), null); + Assert.assertTrue(rs.wasNull()); + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getBigDecimal(1), BigDecimal.valueOf(2L)); + Assert.assertEquals(rs.getString(2), "er"); + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getString(1), "3"); + Assert.assertEquals(rs.getObject(2), ""); + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getShort(1), (short) 4); + Assert.assertEquals(rs.getURL(2), new URL("http://some.host")); + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getObject(1), Integer.valueOf(5)); + Assert.assertEquals(rs.getString(2), null); + Assert.assertTrue(rs.wasNull()); + Assert.assertFalse(rs.next()); + } + } + } }