Skip to content

Commit 4bb003a

Browse files
demon101graemerocher
authored andcommitted
null value support for dynamic finders (#1220)
* null value support for dynamic finders #1182 * null value support for dynamic finders #1182 * null value support for dynamic finders #1182
1 parent 3c0b69d commit 4bb003a

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package grails.gorm.tests
2+
3+
class NullValueEqualSpec extends GormDatastoreSpec {
4+
5+
void "test null value in equal and not equal"() {
6+
when:
7+
new TestEntity(name:"Fred", age: null).save(failOnError: true)
8+
new TestEntity(name:"Bob", age: 11).save(failOnError: true)
9+
new TestEntity(name:"Jack", age: null).save(flush:true, failOnError: true)
10+
11+
then:
12+
TestEntity.countByAge(11) == 1
13+
TestEntity.findAllByAge(null).size() == 2
14+
TestEntity.countByAge(null) == 2
15+
16+
TestEntity.countByAgeNotEqual(11) == 2
17+
TestEntity.countByAgeNotEqual(null) == 1
18+
}
19+
}

grails-datastore-gorm-tck/src/main/groovy/grails/gorm/tests/TestEntity.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ class TestEntity implements Serializable {
1616

1717
static mapping = {
1818
name index:true
19-
age index:true, nullable:true
20-
child index:true, nullable:true
19+
age index:true
20+
child index:true
2121
}
2222

2323
static constraints = {
2424
name blank:false
2525
child nullable:true
26+
age nullable:true
2627
}
2728
}

grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/finders/DynamicFinder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -765,11 +765,11 @@ private static String calcPropertyName(String queryParameter, String clause) {
765765
* @return the initialized expression
766766
*/
767767
private MethodExpression getInitializedExpression(MethodExpression expression, Object[] arguments) {
768-
if (expression instanceof Equal && arguments.length == 1 && arguments[0] == null) {
769-
expression = new IsNull(expression.propertyName);
770-
} else {
771-
expression.setArguments(arguments);
772-
}
768+
// if (expression instanceof Equal && arguments.length == 1 && arguments[0] == null) { // logic moved directly to Equal.createCriterion
769+
// expression = new IsNull(expression.propertyName);
770+
// } else {
771+
expression.setArguments(arguments);
772+
// }
773773
return expression;
774774
}
775775

grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/finders/MethodExpression.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,12 @@ public Equal(String propertyName) {
422422

423423
@Override
424424
public Query.Criterion createCriterion() {
425-
return Restrictions.eq(propertyName, arguments[0]);
425+
Object argument = arguments[0];
426+
if (argument != null) {
427+
return Restrictions.eq(propertyName, argument);
428+
} else {
429+
return Restrictions.isNull(propertyName);
430+
}
426431
}
427432

428433
}
@@ -437,7 +442,12 @@ public NotEqual(String propertyName) {
437442

438443
@Override
439444
public Query.Criterion createCriterion() {
440-
return Restrictions.ne(propertyName, arguments[0]);
445+
Object argument = arguments[0];
446+
if (argument != null) {
447+
return Restrictions.ne(propertyName, arguments[0]);
448+
} else {
449+
return Restrictions.isNotNull(propertyName);
450+
}
441451
}
442452

443453
}

0 commit comments

Comments
 (0)