Pārlūkot izejas kodu

Fix #22: Failure to set fields to null

tags/release-2.0.0
James Moger pirms 8 gadiem
vecāks
revīzija
b215ca82bb

+ 3
- 2
releases.moxie Parādīt failu

@@ -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: ~
}

+ 6
- 2
src/main/java/com/iciql/Query.java Parādīt failu

@@ -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;

+ 6
- 11
src/main/java/com/iciql/TableDefinition.java Parādīt failu

@@ -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;

+ 22
- 0
src/test/java/com/iciql/test/UpdateTest.java Parādīt failu

@@ -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);

}

}

+ 2
- 1
src/test/java/com/iciql/test/models/Customer.java Parādīt failu

@@ -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));
}
}

Notiek ielāde…
Atcelt
Saglabāt