Skip to content

Commit

Permalink
Fix constant update set to null value
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjian2664 committed Nov 25, 2024
1 parent ff9b2a0 commit 38a5bbc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,24 +289,30 @@ public PreparedStatement prepareStatement(
.map(jdbcType -> getWriteFunction(client, session, connection, jdbcType, parameter.getType()))
.orElseGet(() -> getWriteFunction(client, session, parameter.getType()));
Class<?> javaType = writeFunction.getJavaType();
Object value = parameter.getValue()
// The value must be present, since DefaultQueryBuilder never creates null parameters. Values coming from Domain's ValueSet are non-null, and
// nullable domains are handled explicitly, with SQL syntax.
.orElseThrow(() -> new VerifyException("Value is missing"));
if (javaType == boolean.class) {
((BooleanWriteFunction) writeFunction).set(statement, parameterIndex, (boolean) value);
}
else if (javaType == long.class) {
((LongWriteFunction) writeFunction).set(statement, parameterIndex, (long) value);
}
else if (javaType == double.class) {
((DoubleWriteFunction) writeFunction).set(statement, parameterIndex, (double) value);
}
else if (javaType == Slice.class) {
((SliceWriteFunction) writeFunction).set(statement, parameterIndex, (Slice) value);

if (parameter.getValue().isEmpty()) {
writeFunction.setNull(statement, parameterIndex);
}
else {
((ObjectWriteFunction) writeFunction).set(statement, parameterIndex, value);
Object value = parameter.getValue()
// The value must be present, since DefaultQueryBuilder never creates null parameters. Values coming from Domain's ValueSet are non-null, and
// nullable domains are handled explicitly, with SQL syntax.
.orElseThrow(() -> new VerifyException("Value is missing"));
if (javaType == boolean.class) {
((BooleanWriteFunction) writeFunction).set(statement, parameterIndex, (boolean) value);
}
else if (javaType == long.class) {
((LongWriteFunction) writeFunction).set(statement, parameterIndex, (long) value);
}
else if (javaType == double.class) {
((DoubleWriteFunction) writeFunction).set(statement, parameterIndex, (double) value);
}
else if (javaType == Slice.class) {
((SliceWriteFunction) writeFunction).set(statement, parameterIndex, (Slice) value);
}
else {
((ObjectWriteFunction) writeFunction).set(statement, parameterIndex, value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,10 @@ public void testConstantUpdateWithVarcharEqualityPredicates()
}
assertUpdate("UPDATE " + table.getName() + " SET col1 = 20 WHERE col2 = 'A'", 1);
assertQuery("SELECT * FROM " + table.getName(), "VALUES (1, 'a'), (20, 'A')");

// test update set to null
assertUpdate("UPDATE " + table.getName() + " SET col1 = null WHERE col2 = 'A'", 1);
assertQuery("SELECT * FROM " + table.getName(), "VALUES (1, 'a'), (null, 'A')");
}
}

Expand All @@ -1883,6 +1887,10 @@ public void testConstantUpdateWithVarcharInequalityPredicates()

assertUpdate("UPDATE " + table.getName() + " SET col1 = 20 WHERE col2 != 'A'", 1);
assertQuery("SELECT * FROM " + table.getName(), "VALUES (20, 'a'), (2, 'A')");

// test update set to null
assertUpdate("UPDATE " + table.getName() + " SET col1 = null WHERE col2 != 'A'", 1);
assertQuery("SELECT * FROM " + table.getName(), "VALUES (null, 'a'), (2, 'A')");
}
}

Expand All @@ -1902,6 +1910,13 @@ public void testConstantUpdateWithVarcharGreaterAndLowerPredicate()

assertUpdate("UPDATE " + table.getName() + " SET col1 = 20 WHERE col2 < 'a'", 1);
assertQuery("SELECT * FROM " + table.getName(), "VALUES (20, 'a'), (20, 'A')");

// test update set to null
assertUpdate("UPDATE " + table.getName() + " SET col1 = null WHERE col2 > 'A'", 1);
assertQuery("SELECT * FROM " + table.getName(), "VALUES (null, 'a'), (2, 'A')");

assertUpdate("UPDATE " + table.getName() + " SET col1 = null WHERE col2 < 'a'", 1);
assertQuery("SELECT * FROM " + table.getName(), "VALUES (null, 'a'), (null, 'A')");
}
}

Expand Down

0 comments on commit 38a5bbc

Please sign in to comment.