Browse Source

Fix #22: Failure to set fields to null

tags/release-2.0.0
James Moger 8 years ago
parent
commit
b215ca82bb

+ 3
- 2
releases.moxie View File

html: ~ html: ~
text: ~ text: ~
security: ~ security: ~
fixes: ~
fixes:
- Fixed setting null column values (issue-22)
changes: ~ changes: ~
additions: additions:
- Added groupBy methods to QueryWhere
- Added groupBy methods to QueryWhere (issue-20)
dependencyChanges: ~ dependencyChanges: ~
contributors: ~ contributors: ~
} }

+ 6
- 2
src/main/java/com/iciql/Query.java View File

return stat.executeUpdate(); return stat.executeUpdate();
} }


public <A> Query<T> setNull(A field) {
return set(field).to(null);
}

public <A> UpdateColumnSet<T, A> set(A field) { public <A> UpdateColumnSet<T, A> set(A field) {
from.getAliasDefinition().checkMultipleEnums(field); from.getAliasDefinition().checkMultipleEnums(field);
return new UpdateColumnSet<T, A>(this, field); return new UpdateColumnSet<T, A>(this, field);
token.appendSQL(stat, this); token.appendSQL(stat, this);
return; return;
} }
if (alias != null && value.getClass().isEnum()) {
if (alias != null && value != null && value.getClass().isEnum()) {
// special case: // special case:
// value is first enum constant which is also the alias object. // value is first enum constant which is also the alias object.
// the first enum constant is used as the alias because we can not // the first enum constant is used as the alias because we can not


private void addParameter(SQLStatement stat, Object alias, Object value) { private void addParameter(SQLStatement stat, Object alias, Object value) {
SelectColumn<T> col = getColumnByReference(alias); SelectColumn<T> col = getColumnByReference(alias);
if (col != null && value.getClass().isEnum()) {
if (col != null && value != null && value.getClass().isEnum()) {
// enum // enum
EnumType type = col.getFieldDefinition().enumType; EnumType type = col.getFieldDefinition().enumType;
Enum<?> anEnum = (Enum<?>) value; Enum<?> anEnum = (Enum<?>) value;

+ 6
- 11
src/main/java/com/iciql/TableDefinition.java View File

buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append('?'); buff.append('?');
Object value = getValue(obj, field); 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); Object parameter = db.getDialect().serialize(value, field.typeAdapter);
stat.addParameter(parameter); stat.addParameter(parameter);
// conditionally skip insert of null // conditionally skip insert of null
Object value = getValue(obj, field); Object value = getValue(obj, field);
if (value == null) { 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; return false;

+ 22
- 0
src/test/java/com/iciql/test/UpdateTest.java View File



import static java.sql.Date.valueOf; import static java.sql.Date.valueOf;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;


import org.junit.After; 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);

}

} }

+ 2
- 1
src/test/java/com/iciql/test/models/Customer.java View File

new Customer("ANTON", "CA"), new Customer("ANTON", "CA"),
new Customer("BROWN", "LA"), new Customer("BROWN", "LA"),
new Customer("SMITH", "NY"), new Customer("SMITH", "NY"),
new Customer("JONES", "ME"));
new Customer("JONES", "ME"),
new Customer(null, null));
} }
} }

Loading…
Cancel
Save