]> source.dussan.org Git - iciql.git/commitdiff
Fix #22: Failure to set fields to null
authorJames Moger <james.moger@gitblit.com>
Mon, 4 Apr 2016 16:54:42 +0000 (12:54 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 4 Apr 2016 17:33:33 +0000 (13:33 -0400)
releases.moxie
src/main/java/com/iciql/Query.java
src/main/java/com/iciql/TableDefinition.java
src/test/java/com/iciql/test/UpdateTest.java
src/test/java/com/iciql/test/models/Customer.java

index 44135648ab3c9fbf6875ca5e0717fdca8ca0b4b8..c19c81da7d1a3a5b85865c370e4d56f3fb162f74 100644 (file)
@@ -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: ~
 }
index 694f42e8d3e84b8ccc5cf2ef03322ddfd3d0db28..882fea1df2a85ff6e0865c536812e577cb650212 100644 (file)
@@ -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;
index 4fe860d2a9f38c70155e361c1ad45ae0d8e74d4d..07f1d48d7fe21d9f99cbcbe047b6e98adff795fb 100644 (file)
@@ -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;
index 717c23ade97f3ad32ea66a54479c53081564b362..b0981a92093f63d68c8d733ee280f4b2485ca9e4 100644 (file)
@@ -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);
+
+       }
+
 }
index 4270623173f22e65d8a5a06e339cf66bf4058b43..1537c53fa4efdfd49a956788f5ccf912700a15bb 100644 (file)
@@ -57,7 +57,8 @@ public class Customer {
                                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