File tree Expand file tree Collapse file tree 2 files changed +36
-3
lines changed
main/java/com/clickhouse/jdbc
test/java/com/clickhouse/jdbc Expand file tree Collapse file tree 2 files changed +36
-3
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments