diff options
-rw-r--r-- | releases.moxie | 5 | ||||
-rw-r--r-- | src/main/java/com/iciql/Query.java | 8 | ||||
-rw-r--r-- | src/main/java/com/iciql/TableDefinition.java | 17 | ||||
-rw-r--r-- | src/test/java/com/iciql/test/UpdateTest.java | 22 | ||||
-rw-r--r-- | src/test/java/com/iciql/test/models/Customer.java | 3 |
5 files changed, 39 insertions, 16 deletions
diff --git a/releases.moxie b/releases.moxie index 4413564..c19c81d 100644 --- a/releases.moxie +++ b/releases.moxie @@ -9,10 +9,11 @@ r30: { 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: ~ } diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java index 694f42e..882fea1 100644 --- a/src/main/java/com/iciql/Query.java +++ b/src/main/java/com/iciql/Query.java @@ -274,6 +274,10 @@ public class Query<T> { 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); @@ -776,7 +780,7 @@ public class Query<T> { 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 @@ -841,7 +845,7 @@ public class Query<T> { 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; diff --git a/src/main/java/com/iciql/TableDefinition.java b/src/main/java/com/iciql/TableDefinition.java index 4fe860d..07f1d48 100644 --- a/src/main/java/com/iciql/TableDefinition.java +++ b/src/main/java/com/iciql/TableDefinition.java @@ -669,9 +669,11 @@ public class TableDefinition<T> { 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); @@ -737,14 +739,7 @@ public class TableDefinition<T> { // 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; diff --git a/src/test/java/com/iciql/test/UpdateTest.java b/src/test/java/com/iciql/test/UpdateTest.java index 717c23a..b0981a9 100644 --- a/src/test/java/com/iciql/test/UpdateTest.java +++ b/src/test/java/com/iciql/test/UpdateTest.java @@ -19,6 +19,7 @@ package com.iciql.test; 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; @@ -157,4 +158,25 @@ public class UpdateTest { } + @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); + + } + } diff --git a/src/test/java/com/iciql/test/models/Customer.java b/src/test/java/com/iciql/test/models/Customer.java index 4270623..1537c53 100644 --- a/src/test/java/com/iciql/test/models/Customer.java +++ b/src/test/java/com/iciql/test/models/Customer.java @@ -57,7 +57,8 @@ public class Customer { new Customer("ANTON", "CA"),
new Customer("BROWN", "LA"),
new Customer("SMITH", "NY"),
- new Customer("JONES", "ME"));
+ new Customer("JONES", "ME"),
+ new Customer(null, null));
}
}
|