diff options
author | James Moger <james.moger@gitblit.com> | 2016-04-04 12:54:42 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2016-04-04 13:33:33 -0400 |
commit | b215ca82bb90f4cd8c0b1e84f91c737da229e6a6 (patch) | |
tree | 924349916cc5c575ba48da6d0543ae7d80b34bf9 /src/main/java/com/iciql | |
parent | 5ede276035ef01d5e3901fe36c45d1bd05efbf8b (diff) | |
download | iciql-b215ca82bb90f4cd8c0b1e84f91c737da229e6a6.tar.gz iciql-b215ca82bb90f4cd8c0b1e84f91c737da229e6a6.zip |
Fix #22: Failure to set fields to null
Diffstat (limited to 'src/main/java/com/iciql')
-rw-r--r-- | src/main/java/com/iciql/Query.java | 8 | ||||
-rw-r--r-- | src/main/java/com/iciql/TableDefinition.java | 17 |
2 files changed, 12 insertions, 13 deletions
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; |