Skip to content

Commit ac3f693

Browse files
committed
fixed case when parameter is not set and added test
1 parent 4ccc7ab commit ac3f693

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ private String buildSQL() {
118118
int[] positions = parsedPreparedStatement.getParamPositions();
119119
for (int i = 0; i < argCount; i++) {
120120
int p = positions[i] + posOffset;
121-
String val = values[i].toString();
122-
compiledSql.replace(p, p+1, val);
123-
posOffset += val.length() - 1;
121+
String val = values[i];
122+
compiledSql.replace(p, p+1, val == null ? "NULL" : val);
123+
posOffset += val == null ? 0 : val.length() - 1;
124124
}
125125
return compiledSql.toString();
126126
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,4 +904,37 @@ public void testStatementsWithDatabaseInTableIdentifier() throws Exception {
904904
}
905905
}
906906
}
907+
908+
@Test(groups = {"integration "})
909+
public void testNullValues() throws Exception {
910+
try (Connection conn = getJdbcConnection()) {
911+
final String table = "test_null_values";
912+
try (Statement stmt = conn.createStatement()) {
913+
stmt.execute("DROP TABLE IF EXISTS " + table);
914+
stmt.execute("CREATE TABLE " + table +
915+
"(v1 Int32, v2 Nullable(Int32)) Engine MergeTree ORDER BY ()");
916+
}
917+
918+
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO " + table + " VALUES (?, ?)")) {
919+
stmt.setInt(1, 10);
920+
// do not set second value
921+
assertEquals(stmt.executeUpdate(), 1);
922+
stmt.setInt(1, 20);
923+
stmt.setObject(2, null);
924+
assertEquals(stmt.executeUpdate(), 1);
925+
}
926+
927+
try (Statement stmt = conn.createStatement();
928+
ResultSet rs = stmt.executeQuery("SELECT * FROM " + table)) {
929+
930+
int count = 0;
931+
while(rs.next()) {
932+
count++;
933+
assertNull(rs.getObject(2));
934+
}
935+
936+
assertEquals(count, 2);
937+
}
938+
}
939+
}
907940
}

0 commit comments

Comments
 (0)