Skip to content

Commit c545e3e

Browse files
committed
Add a "when" method to the IsLike condition
Need to add this to all other conditions as applicable.
1 parent 319396c commit c545e3e

File tree

4 files changed

+175
-1
lines changed

4 files changed

+175
-1
lines changed

src/main/java/org/mybatis/dynamic/sql/where/condition/IsLike.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.function.Predicate;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

@@ -42,4 +43,8 @@ public static <T> IsLike<T> of(Supplier<T> valueSupplier) {
4243
public IsLike<T> when(Predicate<T> predicate) {
4344
return new IsLike<>(valueSupplier, predicate);
4445
}
46+
47+
public IsLike<T> then(UnaryOperator<T> transformer) {
48+
return shouldRender() ? new IsLike<>(() -> transformer.apply(value())) : this;
49+
}
4550
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright 2016-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package issue.gh105;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.mybatis.dynamic.sql.SqlBuilder.*;
20+
21+
import java.util.Objects;
22+
23+
import static issue.gh105.PersonDynamicSqlSupport.*;
24+
25+
import org.junit.jupiter.api.Test;
26+
import org.mybatis.dynamic.sql.render.RenderingStrategy;
27+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
28+
29+
public class Issue105Test {
30+
31+
@Test
32+
public void testFuzzySearchBothPresent() {
33+
String fName = "Fred";
34+
String lName = "Flintstone";
35+
36+
SelectStatementProvider selectStatement = select(id, firstName, lastName)
37+
.from(person)
38+
.where(firstName, isLike(fName).when(Objects::nonNull).then(s -> "%" + s + "%"))
39+
.and(lastName, isLike(lName).when(Objects::nonNull).then(s -> "%" + s + "%"))
40+
.build()
41+
.render(RenderingStrategy.MYBATIS3);
42+
43+
String expected = "select person_id, first_name, last_name"
44+
+ " from Person"
45+
+ " where first_name like #{parameters.p1}"
46+
+ " and last_name like #{parameters.p2}";
47+
48+
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
49+
assertThat(selectStatement.getParameters().get("p1")).isEqualTo("%Fred%");
50+
assertThat(selectStatement.getParameters().get("p2")).isEqualTo("%Flintstone%");
51+
}
52+
53+
@Test
54+
public void testFuzzySearchFirstNameNull() {
55+
String fName = null;
56+
String lName = "Flintstone";
57+
58+
SelectStatementProvider selectStatement = select(id, firstName, lastName)
59+
.from(person)
60+
.where(firstName, isLike(fName).when(Objects::nonNull).then(SearchUtils::addWildcards))
61+
.and(lastName, isLike(lName).when(Objects::nonNull).then(SearchUtils::addWildcards))
62+
.build()
63+
.render(RenderingStrategy.MYBATIS3);
64+
65+
String expected = "select person_id, first_name, last_name"
66+
+ " from Person"
67+
+ " where last_name like #{parameters.p1}";
68+
69+
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
70+
assertThat(selectStatement.getParameters().get("p1")).isEqualTo("%Flintstone%");
71+
}
72+
73+
@Test
74+
public void testFuzzySearchLastNameNull() {
75+
String fName = "Fred";
76+
String lName = null;
77+
78+
SelectStatementProvider selectStatement = select(id, firstName, lastName)
79+
.from(person)
80+
.where(firstName, isLike(fName).when(Objects::nonNull).then(SearchUtils::addWildcards))
81+
.and(lastName, isLike(lName).when(Objects::nonNull).then(SearchUtils::addWildcards))
82+
.build()
83+
.render(RenderingStrategy.MYBATIS3);
84+
85+
String expected = "select person_id, first_name, last_name"
86+
+ " from Person"
87+
+ " where first_name like #{parameters.p1}";
88+
89+
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
90+
assertThat(selectStatement.getParameters().get("p1")).isEqualTo("%Fred%");
91+
}
92+
93+
@Test
94+
public void testFuzzySearchBothNull() {
95+
String fName = null;
96+
String lName = null;
97+
98+
SelectStatementProvider selectStatement = select(id, firstName, lastName)
99+
.from(person)
100+
.where(firstName, isLike(fName).when(Objects::nonNull).then(SearchUtils::addWildcards))
101+
.and(lastName, isLike(lName).when(Objects::nonNull).then(SearchUtils::addWildcards))
102+
.build()
103+
.render(RenderingStrategy.MYBATIS3);
104+
105+
String expected = "select person_id, first_name, last_name"
106+
+ " from Person";
107+
108+
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
109+
assertThat(selectStatement.getParameters().size()).isEqualTo(0);
110+
}
111+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2016-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package issue.gh105;
17+
18+
import org.mybatis.dynamic.sql.SqlColumn;
19+
import org.mybatis.dynamic.sql.SqlTable;
20+
21+
public final class PersonDynamicSqlSupport {
22+
public static final Person person = new Person();
23+
public static final SqlColumn<Integer> id = person.id;
24+
public static final SqlColumn<String> firstName = person.firstName;
25+
public static final SqlColumn<String> lastName = person.lastName;
26+
27+
public static final class Person extends SqlTable {
28+
public final SqlColumn<Integer> id = column("person_id");
29+
public final SqlColumn<String> firstName = column("first_name");
30+
public final SqlColumn<String> lastName = column("last_name");
31+
32+
public Person() {
33+
super("Person");
34+
}
35+
}
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright 2016-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package issue.gh105;
17+
18+
public class SearchUtils {
19+
public static String addWildcards(String s) {
20+
return "%" + s + "%";
21+
}
22+
}

0 commit comments

Comments
 (0)