Skip to content

Commit 7db110f

Browse files
committed
Fix #920
1 parent 92e075d commit 7db110f

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

Diff for: clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseConnectionImpl.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -615,14 +615,17 @@ public PreparedStatement prepareStatement(String sql, int resultSetType, int res
615615
!parsedStmt.containsKeyword("SELECT") && parsedStmt.hasValues() &&
616616
(!parsedStmt.hasFormat() || clientRequest.getFormat().name().equals(parsedStmt.getFormat()))) {
617617
String query = parsedStmt.getSQL();
618-
int startIndex = parsedStmt.getPositions().get(ClickHouseSqlStatement.KEYWORD_VALUES_START);
619-
int endIndex = parsedStmt.getPositions().get(ClickHouseSqlStatement.KEYWORD_VALUES_END);
620-
boolean useStream = true;
621-
for (int i = startIndex + 1; i < endIndex; i++) {
622-
char ch = query.charAt(i);
623-
if (ch != '?' && ch != ',' && !Character.isWhitespace(ch)) {
624-
useStream = false;
625-
break;
618+
boolean useStream = false;
619+
Integer startIndex = parsedStmt.getPositions().get(ClickHouseSqlStatement.KEYWORD_VALUES_START);
620+
if (startIndex != null) {
621+
useStream = true;
622+
int endIndex = parsedStmt.getPositions().get(ClickHouseSqlStatement.KEYWORD_VALUES_END);
623+
for (int i = startIndex + 1; i < endIndex; i++) {
624+
char ch = query.charAt(i);
625+
if (ch != '?' && ch != ',' && !Character.isWhitespace(ch)) {
626+
useStream = false;
627+
break;
628+
}
626629
}
627630
}
628631

Diff for: clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java

+44
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.clickhouse.jdbc;
22

33
import java.io.ByteArrayInputStream;
4+
import java.math.BigDecimal;
45
import java.net.Inet4Address;
56
import java.net.Inet6Address;
7+
import java.net.URL;
68
import java.nio.charset.StandardCharsets;
79
import java.sql.BatchUpdateException;
810
import java.sql.Connection;
@@ -1257,4 +1259,46 @@ public void testInsertWithAndSelect() throws Exception {
12571259
Assert.assertFalse(rs.next());
12581260
}
12591261
}
1262+
1263+
@Test(groups = "integration")
1264+
public void testInsertWithMultipleValues() throws Exception {
1265+
try (ClickHouseConnection conn = newConnection(new Properties());
1266+
Statement s = conn.createStatement()) {
1267+
s.execute("drop table if exists test_insert_with_multiple_values; "
1268+
+ "CREATE TABLE test_insert_with_multiple_values(a Int32, b Nullable(String)) ENGINE=Memory");
1269+
try (PreparedStatement ps = conn.prepareStatement(
1270+
"INSERT INTO test_insert_with_multiple_values values(?, ?), (2 , ? ), ( ? , '') , (?,?) ,( ? ,? )")) {
1271+
ps.setInt(1, 1);
1272+
ps.setNull(2, Types.VARCHAR);
1273+
ps.setObject(3, "er");
1274+
ps.setInt(4, 3);
1275+
ps.setInt(5, 4);
1276+
ps.setURL(6, new URL("http://some.host"));
1277+
ps.setInt(7, 5);
1278+
ps.setString(8, null);
1279+
ps.executeUpdate();
1280+
}
1281+
1282+
try (ResultSet rs = s.executeQuery("select * from test_insert_with_multiple_values order by a")) {
1283+
Assert.assertTrue(rs.next());
1284+
Assert.assertEquals(rs.getByte(1), (byte) 1);
1285+
Assert.assertEquals(rs.getObject(2), null);
1286+
Assert.assertTrue(rs.wasNull());
1287+
Assert.assertTrue(rs.next());
1288+
Assert.assertEquals(rs.getBigDecimal(1), BigDecimal.valueOf(2L));
1289+
Assert.assertEquals(rs.getString(2), "er");
1290+
Assert.assertTrue(rs.next());
1291+
Assert.assertEquals(rs.getString(1), "3");
1292+
Assert.assertEquals(rs.getObject(2), "");
1293+
Assert.assertTrue(rs.next());
1294+
Assert.assertEquals(rs.getShort(1), (short) 4);
1295+
Assert.assertEquals(rs.getURL(2), new URL("http://some.host"));
1296+
Assert.assertTrue(rs.next());
1297+
Assert.assertEquals(rs.getObject(1), Integer.valueOf(5));
1298+
Assert.assertEquals(rs.getString(2), null);
1299+
Assert.assertTrue(rs.wasNull());
1300+
Assert.assertFalse(rs.next());
1301+
}
1302+
}
1303+
}
12601304
}

0 commit comments

Comments
 (0)