Skip to content

Commit d468f1f

Browse files
authored
Merge pull request #335 from jeffgbutler/master
Add composition functions for WhereApplier
2 parents 0fb1b4f + bb30719 commit d468f1f

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Kotlin DSL.
9494
- Added subquery support for "join" clauses in a select statement ([#293](https://github.com/mybatis/mybatis-dynamic-sql/pull/293))
9595
- Added support for the "exists" and "not exists" operator in where clauses ([#296](https://github.com/mybatis/mybatis-dynamic-sql/pull/296))
9696
- Refactored the built-in conditions ([#331](https://github.com/mybatis/mybatis-dynamic-sql/pull/331))
97+
- Added composition functions for WhereApplier ([#335](https://github.com/mybatis/mybatis-dynamic-sql/pull/335))
9798

9899
## Release 1.2.1 - September 29, 2020
99100

src/main/java/org/mybatis/dynamic/sql/where/WhereApplier.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,4 +18,19 @@
1818
import java.util.function.Consumer;
1919

2020
@FunctionalInterface
21-
public interface WhereApplier extends Consumer<AbstractWhereDSL<?>> {}
21+
public interface WhereApplier extends Consumer<AbstractWhereDSL<?>> {
22+
/**
23+
* Return a composed where applier that performs this operation followed
24+
* by the after operation.
25+
*
26+
* @param after the operation to perform after this operation
27+
* @return a composed where applier that performs this operation followed
28+
* by the after operation.
29+
*/
30+
default WhereApplier andThen(WhereApplier after) {
31+
return t -> {
32+
accept(t);
33+
after.accept(t);
34+
};
35+
}
36+
}

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBaseBuilders.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,11 @@ annotation class MyBatisDslMarker
3030

3131
typealias WhereApplier = AbstractWhereDSL<*>.() -> Unit
3232

33+
fun WhereApplier.andThen(after: WhereApplier): WhereApplier = {
34+
invoke(this)
35+
after(this)
36+
}
37+
3338
@MyBatisDslMarker
3439
@Suppress("TooManyFunctions")
3540
abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuilder<D, B>> {

src/test/java/examples/spring/ReusableWhereTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,9 @@
2424
import org.junit.jupiter.api.BeforeEach;
2525
import org.junit.jupiter.api.Test;
2626
import org.mybatis.dynamic.sql.delete.DeleteModel;
27+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
2728
import org.mybatis.dynamic.sql.select.SelectModel;
29+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2830
import org.mybatis.dynamic.sql.update.UpdateModel;
2931
import org.mybatis.dynamic.sql.util.Buildable;
3032
import org.mybatis.dynamic.sql.util.spring.NamedParameterJdbcTemplateExtensions;
@@ -39,7 +41,7 @@ class ReusableWhereTest {
3941
private NamedParameterJdbcTemplateExtensions template;
4042

4143
@BeforeEach
42-
void setup() throws Exception {
44+
void setup() {
4345
EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
4446
.setType(EmbeddedDatabaseType.HSQL)
4547
.generateUniqueName(true)
@@ -89,5 +91,22 @@ void testUpdate() {
8991
assertThat(rows).isEqualTo(3);
9092
}
9193

92-
private WhereApplier commonWhere = d -> d.where(id, isEqualTo(1)).or(occupation, isNull());
94+
@Test
95+
void testComposition() {
96+
WhereApplier whereApplier = commonWhere.andThen(wa -> wa.and(birthDate, isNotNull()));
97+
whereApplier = whereApplier.andThen(wa -> wa.or(addressId, isLessThan(3)));
98+
99+
SelectStatementProvider selectStatement = select(person.allColumns())
100+
.from(person)
101+
.applyWhere(whereApplier)
102+
.build()
103+
.render(RenderingStrategies.SPRING_NAMED_PARAMETER);
104+
105+
assertThat(selectStatement.getSelectStatement()).isEqualTo(
106+
"select * from Person " +
107+
"where id = :p1 or occupation is null and birth_date is not null or address_id < :p2");
108+
109+
}
110+
111+
private final WhereApplier commonWhere = d -> d.where(id, isEqualTo(1)).or(occupation, isNull());
93112
}

src/test/kotlin/examples/kotlin/mybatis3/canonical/ReusableWhereTest.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,8 +15,11 @@
1515
*/
1616
package examples.kotlin.mybatis3.canonical
1717

18+
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.Person.addressId
19+
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.Person.birthDate
1820
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.Person.id
1921
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.Person.occupation
22+
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.Person
2023
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource
2124
import org.apache.ibatis.jdbc.ScriptRunner
2225
import org.apache.ibatis.mapping.Environment
@@ -26,9 +29,13 @@ import org.apache.ibatis.session.SqlSessionFactoryBuilder
2629
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory
2730
import org.assertj.core.api.Assertions.assertThat
2831
import org.junit.jupiter.api.Test
32+
import org.mybatis.dynamic.sql.SqlBuilder.isLessThan
2933
import org.mybatis.dynamic.sql.SqlBuilder.isEqualTo
34+
import org.mybatis.dynamic.sql.SqlBuilder.isNotNull
3035
import org.mybatis.dynamic.sql.SqlBuilder.isNull
3136
import org.mybatis.dynamic.sql.util.kotlin.WhereApplier
37+
import org.mybatis.dynamic.sql.util.kotlin.andThen
38+
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
3239
import java.io.InputStreamReader
3340
import java.sql.DriverManager
3441

@@ -105,8 +112,31 @@ class ReusableWhereTest {
105112
}
106113
}
107114

115+
@Test
116+
fun testComposition() {
117+
var whereApplier = commonWhere.andThen {
118+
and(birthDate, isNotNull())
119+
}
120+
121+
whereApplier = whereApplier.andThen {
122+
or(addressId, isLessThan(3))
123+
}
124+
125+
val selectStatement = select(Person.allColumns()) {
126+
from(Person)
127+
applyWhere(whereApplier)
128+
}
129+
130+
assertThat(selectStatement.selectStatement).isEqualTo(
131+
"select * from Person " +
132+
"where id = #{parameters.p1,jdbcType=INTEGER} or occupation is null " +
133+
"and birth_date is not null " +
134+
"or address_id < #{parameters.p2,jdbcType=INTEGER}"
135+
)
136+
}
137+
108138
private val commonWhere: WhereApplier = {
109-
where(id, isEqualTo(1)).or(occupation, isNull<String>())
139+
where(id, isEqualTo(1)).or(occupation, isNull())
110140
}
111141

112142
companion object {

0 commit comments

Comments
 (0)