html: ~
text: ~
security: ~
- fixes: ~
+ fixes:
+ - Fixed setting null column values (issue-22)
changes: ~
additions:
- - Added groupBy methods to QueryWhere
+ - Added groupBy methods to QueryWhere (issue-20)
dependencyChanges: ~
contributors: ~
}
return stat.executeUpdate();
}
+ public <A> Query<T> setNull(A field) {
+ return set(field).to(null);
+ }
+
public <A> UpdateColumnSet<T, A> set(A field) {
from.getAliasDefinition().checkMultipleEnums(field);
return new UpdateColumnSet<T, A>(this, field);
token.appendSQL(stat, this);
return;
}
- if (alias != null && value.getClass().isEnum()) {
+ if (alias != null && value != null && value.getClass().isEnum()) {
// special case:
// value is first enum constant which is also the alias object.
// the first enum constant is used as the alias because we can not
private void addParameter(SQLStatement stat, Object alias, Object value) {
SelectColumn<T> col = getColumnByReference(alias);
- if (col != null && value.getClass().isEnum()) {
+ if (col != null && value != null && value.getClass().isEnum()) {
// enum
EnumType type = col.getFieldDefinition().enumType;
Enum<?> anEnum = (Enum<?>) value;
buff.appendExceptFirst(", ");
buff.append('?');
Object value = getValue(obj, field);
- if (value == null && !field.nullable) {
- // try to interpret and instantiate a default value
- value = ModelUtils.getDefaultValue(field, db.getDialect().getDateTimeClass());
+ if (value == null) {
+ if (!field.nullable) {
+ // try to interpret and instantiate a default value
+ value = ModelUtils.getDefaultValue(field, db.getDialect().getDateTimeClass());
+ }
}
Object parameter = db.getDialect().serialize(value, field.typeAdapter);
stat.addParameter(parameter);
// conditionally skip insert of null
Object value = getValue(obj, field);
if (value == null) {
- if (field.nullable) {
- // skip null assignment, field is nullable
- return true;
- } else if (StringUtils.isNullOrEmpty(field.defaultValue)) {
- IciqlLogger.warn("no default value, skipping null insert assignment for {0}.{1}",
- tableName, field.columnName);
- return true;
- }
+ return !StringUtils.isNullOrEmpty(field.defaultValue);
}
}
return false;
import static java.sql.Date.valueOf;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.After;
}
+ @Test
+ public void testSetNull() {
+ Product p = new Product();
+ Product original = db.from(p).where(p.productId).is(1).selectFirst();
+
+ String originalName = original.productName;
+ db.from(p).setNull(p.productName).update();
+
+ // confirm the data was properly updated
+ Product revised = db.from(p).where(p.productId).is(1).selectFirst();
+ assertNull(revised.productName);
+
+ // restore the data
+ db.from(p).set(p.productName).to(originalName).update();
+
+ // confirm the data was properly restored
+ Product restored = db.from(p).where(p.productId).is(1).selectFirst();
+ assertEquals(originalName, restored.productName);
+
+ }
+
}
new Customer("ANTON", "CA"),\r
new Customer("BROWN", "LA"),\r
new Customer("SMITH", "NY"),\r
- new Customer("JONES", "ME"));\r
+ new Customer("JONES", "ME"),\r
+ new Customer(null, null));\r
}\r
\r
}\r