Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue of failed to create PreparedStatement for insert query with multiple values expression #940

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
}
}
}