Skip to content

Commit 09649e5

Browse files
authored
Support isNull and isNotNull predicates in ON clause of Criteria API (#729)
1 parent 3274805 commit 09649e5

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/declaration/JoinDeclaration.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,28 @@ public <PROPERTY> void lt(PropertyMetamodel<PROPERTY> left, PropertyMetamodel<PR
5353
Objects.requireNonNull(right);
5454
add(new Criterion.Lt(new Operand.Prop(left), new Operand.Prop(right)));
5555
}
56+
57+
/**
58+
* Adds a {@code IS NULL} operator.
59+
*
60+
* @param propertyMetamodel the operand
61+
* @param <PROPERTY> the property type
62+
* @throws NullPointerException if {@code propertyMetamodel} is null
63+
*/
64+
public <PROPERTY> void isNull(PropertyMetamodel<PROPERTY> propertyMetamodel) {
65+
Objects.requireNonNull(propertyMetamodel);
66+
add(new Criterion.IsNull(new Operand.Prop(propertyMetamodel)));
67+
}
68+
69+
/**
70+
* Adds a {@code IS NOT NULL} operator.
71+
*
72+
* @param propertyMetamodel the operand
73+
* @param <PROPERTY> the property type
74+
* @throws NullPointerException if {@code propertyMetamodel} is null
75+
*/
76+
public <PROPERTY> void isNotNull(PropertyMetamodel<PROPERTY> propertyMetamodel) {
77+
Objects.requireNonNull(propertyMetamodel);
78+
add(new Criterion.IsNotNull(new Operand.Prop(propertyMetamodel)));
79+
}
5680
}

doma-core/src/test/java/org/seasar/doma/jdbc/criteria/NativeSqlSelectTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,12 +704,14 @@ void join_on() {
704704
on.gt(e.id, d.id);
705705
on.le(e.id, d.id);
706706
on.lt(e.id, d.id);
707+
on.isNull(e.name);
708+
on.isNotNull(e.name);
707709
})
708710
.select(e.id);
709711

710712
Sql<?> sql = stmt.asSql();
711713
assertEquals(
712-
"select t0_.ID from EMP t0_ inner join CATA.DEPT t1_ on (t0_.ID = t1_.ID and t0_.ID <> t1_.ID and t0_.ID >= t1_.ID and t0_.ID > t1_.ID and t0_.ID <= t1_.ID and t0_.ID < t1_.ID)",
714+
"select t0_.ID from EMP t0_ inner join CATA.DEPT t1_ on (t0_.ID = t1_.ID and t0_.ID <> t1_.ID and t0_.ID >= t1_.ID and t0_.ID > t1_.ID and t0_.ID <= t1_.ID and t0_.ID < t1_.ID and t0_.NAME is null and t0_.NAME is not null)",
713715
sql.getFormattedSql());
714716
}
715717

doma-kotlin/src/main/kotlin/org/seasar/doma/jdbc/criteria/declaration/KJoinDeclaration.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ class KJoinDeclaration(private val declaration: JoinDeclaration) {
2727
fun <PROPERTY> lt(left: PropertyMetamodel<PROPERTY>, right: PropertyMetamodel<PROPERTY>) {
2828
declaration.lt(left, right)
2929
}
30+
31+
fun <PROPERTY> isNull(propertyMetamodel: PropertyMetamodel<PROPERTY>) {
32+
declaration.isNull(propertyMetamodel)
33+
}
34+
35+
fun <PROPERTY> isNotNull(propertyMetamodel: PropertyMetamodel<PROPERTY>) {
36+
declaration.isNotNull(propertyMetamodel)
37+
}
3038
}

doma-kotlin/src/test/kotlin/org/seasar/doma/jdbc/criteria/KNativeSqlSelectTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,13 @@ internal class KNativeSqlSelectTest {
646646
gt(e.id, d.id)
647647
le(e.id, d.id)
648648
lt(e.id, d.id)
649+
isNull(e.name)
650+
isNotNull(e.name)
649651
}
650652
.select(e.id)
651653
val sql = stmt.asSql()
652654
assertEquals(
653-
"select t0_.ID from EMP t0_ inner join CATA.DEPT t1_ on (t0_.ID = t1_.ID and t0_.ID <> t1_.ID and t0_.ID >= t1_.ID and t0_.ID > t1_.ID and t0_.ID <= t1_.ID and t0_.ID < t1_.ID)",
655+
"select t0_.ID from EMP t0_ inner join CATA.DEPT t1_ on (t0_.ID = t1_.ID and t0_.ID <> t1_.ID and t0_.ID >= t1_.ID and t0_.ID > t1_.ID and t0_.ID <= t1_.ID and t0_.ID < t1_.ID and t0_.NAME is null and t0_.NAME is not null)",
654656
sql.formattedSql
655657
)
656658
}

0 commit comments

Comments
 (0)