Skip to content

Commit 8edd8d3

Browse files
authored
Merge pull request #541 from enqueue/prepared_static_null
Static null parameters for non-batch PreparedStatement
2 parents b8d0e78 + 04ffd1c commit 8edd8d3

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/main/java/ru/yandex/clickhouse/ClickHousePreparedStatementImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public class ClickHousePreparedStatementImpl extends ClickHouseStatementImpl implements ClickHousePreparedStatement {
2626

2727
static final String PARAM_MARKER = "?";
28+
static final String NULL_MARKER = "\\N";
2829

2930
private static final Pattern VALUES = Pattern.compile("(?i)VALUES[\\s]*\\(");
3031

@@ -82,6 +83,8 @@ private String buildSql() throws SQLException {
8283
String pValue = getParameter(i - 1);
8384
if (PARAM_MARKER.equals(pValue)) {
8485
sb.append(binds[p++].getRegularValue());
86+
} else if (NULL_MARKER.equals(pValue)) {
87+
sb.append("NULL");
8588
} else {
8689
sb.append(pValue);
8790
}

src/main/java/ru/yandex/clickhouse/PreparedStatementParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private static String typeTransformParameterValue(String paramValue) {
173173
return "0";
174174
}
175175
if ("NULL".equalsIgnoreCase(paramValue)) {
176-
return "\\N";
176+
return ClickHousePreparedStatementImpl.NULL_MARKER;
177177
}
178178
return paramValue;
179179
}

src/test/java/ru/yandex/clickhouse/integration/ClickHousePreparedStatementTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,25 @@ public void testArrayDateTime() throws Exception {
640640
Assert.assertEquals(result[1].getTime(), 1560698526598L);
641641
}
642642

643+
@Test
644+
public void testStaticNullValue() throws Exception {
645+
connection.createStatement().execute(
646+
"DROP TABLE IF EXISTS test.static_null_value");
647+
connection.createStatement().execute(
648+
"CREATE TABLE IF NOT EXISTS test.static_null_value"
649+
+ "(foo Nullable(String), bar Nullable(String)) "
650+
+ "ENGINE = TinyLog"
651+
);
652+
PreparedStatement ps0 = connection.prepareStatement(
653+
"INSERT INTO test.static_null_value(foo) VALUES (null)");
654+
ps0.executeUpdate();
655+
656+
ps0 = connection.prepareStatement(
657+
"INSERT INTO test.static_null_value(foo, bar) VALUES (null, ?)");
658+
ps0.setNull(1, Types.VARCHAR);
659+
ps0.executeUpdate();
660+
}
661+
643662
private static byte[] randomEncodedUUID() {
644663
UUID uuid = UUID.randomUUID();
645664
return ByteBuffer.allocate(16)

0 commit comments

Comments
 (0)