Skip to content

Commit c5e3951

Browse files
authored
Merge pull request #4 from jeffgbutler/make-like-conditions-generic
IsLike and IsNotLike should be generic
2 parents 7643d21 + 581b4d5 commit c5e3951

File tree

9 files changed

+169
-39
lines changed

9 files changed

+169
-39
lines changed

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -327,30 +327,31 @@ static <T> IsNotBetween.Builder<T> isNotBetween(T value1) {
327327
static <T> IsNotBetween.Builder<T> isNotBetween(Supplier<T> valueSupplier1) {
328328
return IsNotBetween.isNotBetween(valueSupplier1);
329329
}
330-
331-
// conditions for strings only
332-
static IsLike isLike(String value) {
330+
331+
// for string columns, but generic for columns with type handlers
332+
static <T> IsLike<T> isLike(T value) {
333333
return isLike(() -> value);
334334
}
335335

336-
static IsLike isLike(Supplier<String> valueSupplier) {
336+
static <T> IsLike<T> isLike(Supplier<T> valueSupplier) {
337337
return IsLike.of(valueSupplier);
338338
}
339339

340-
static IsLikeCaseInsensitive isLikeCaseInsensitive(String value) {
341-
return isLikeCaseInsensitive(() -> value);
340+
static <T> IsNotLike<T> isNotLike(T value) {
341+
return isNotLike(() -> value);
342342
}
343343

344-
static IsLikeCaseInsensitive isLikeCaseInsensitive(Supplier<String> valueSupplier) {
345-
return IsLikeCaseInsensitive.of(valueSupplier);
344+
static <T> IsNotLike<T> isNotLike(Supplier<T> valueSupplier) {
345+
return IsNotLike.of(valueSupplier);
346346
}
347347

348-
static IsNotLike isNotLike(String value) {
349-
return isNotLike(() -> value);
348+
// conditions for strings only
349+
static IsLikeCaseInsensitive isLikeCaseInsensitive(String value) {
350+
return isLikeCaseInsensitive(() -> value);
350351
}
351352

352-
static IsNotLike isNotLike(Supplier<String> valueSupplier) {
353-
return IsNotLike.of(valueSupplier);
353+
static IsLikeCaseInsensitive isLikeCaseInsensitive(Supplier<String> valueSupplier) {
354+
return IsLikeCaseInsensitive.of(valueSupplier);
354355
}
355356

356357
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitive(String value) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -19,9 +19,9 @@
1919

2020
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2121

22-
public class IsLike extends AbstractSingleValueCondition<String> {
22+
public class IsLike<T> extends AbstractSingleValueCondition<T> {
2323

24-
protected IsLike(Supplier<String> valueSupplier) {
24+
protected IsLike(Supplier<T> valueSupplier) {
2525
super(valueSupplier);
2626
}
2727

@@ -30,7 +30,7 @@ public String renderCondition(String columnName, String placeholder) {
3030
return columnName + " like " + placeholder; //$NON-NLS-1$
3131
}
3232

33-
public static IsLike of(Supplier<String> valueSupplier) {
34-
return new IsLike(valueSupplier);
33+
public static <T> IsLike<T> of(Supplier<T> valueSupplier) {
34+
return new IsLike<>(valueSupplier);
3535
}
3636
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -19,9 +19,9 @@
1919

2020
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2121

22-
public class IsNotLike extends AbstractSingleValueCondition<String> {
22+
public class IsNotLike<T> extends AbstractSingleValueCondition<T> {
2323

24-
protected IsNotLike(Supplier<String> valueSupplier) {
24+
protected IsNotLike(Supplier<T> valueSupplier) {
2525
super(valueSupplier);
2626
}
2727

@@ -30,7 +30,7 @@ public String renderCondition(String columnName, String placeholder) {
3030
return columnName + " not like " + placeholder; //$NON-NLS-1$
3131
}
3232

33-
public static IsNotLike of(Supplier<String> valueSupplier) {
34-
return new IsNotLike(valueSupplier);
33+
public static <T> IsNotLike<T> of(Supplier<T> valueSupplier) {
34+
return new IsNotLike<>(valueSupplier);
3535
}
3636
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2016-2018 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 examples.simple;
17+
18+
public class LastName {
19+
private String name;
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
public static LastName of(String name) {
30+
LastName lastName = new LastName();
31+
lastName.setName(name);
32+
return lastName;
33+
}
34+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2016-2018 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 examples.simple;
17+
18+
import java.sql.CallableStatement;
19+
import java.sql.PreparedStatement;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
23+
import org.apache.ibatis.type.JdbcType;
24+
import org.apache.ibatis.type.TypeHandler;
25+
26+
public class LastNameTypeHandler implements TypeHandler<LastName> {
27+
28+
@Override
29+
public void setParameter(PreparedStatement ps, int i, LastName parameter, JdbcType jdbcType) throws SQLException {
30+
ps.setString(i, parameter == null ? null : parameter.getName());
31+
}
32+
33+
@Override
34+
public LastName getResult(ResultSet rs, String columnName) throws SQLException {
35+
return toLastName(rs.getString(columnName));
36+
}
37+
38+
@Override
39+
public LastName getResult(ResultSet rs, int columnIndex) throws SQLException {
40+
return toLastName(rs.getString(columnIndex));
41+
}
42+
43+
@Override
44+
public LastName getResult(CallableStatement cs, int columnIndex) throws SQLException {
45+
return toLastName(cs.getString(columnIndex));
46+
}
47+
48+
private LastName toLastName(String s) {
49+
LastName lastName = null;
50+
51+
if (s != null) {
52+
return null;
53+
}
54+
55+
return lastName;
56+
}
57+
}

src/test/java/examples/simple/SimpleTableAnnotatedMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -57,7 +57,7 @@ public interface SimpleTableAnnotatedMapper {
5757
@Results(id="SimpleTableResult", value= {
5858
@Result(column="A_ID", property="id", jdbcType=JdbcType.INTEGER, id=true),
5959
@Result(column="first_name", property="firstName", jdbcType=JdbcType.VARCHAR),
60-
@Result(column="last_name", property="lastName", jdbcType=JdbcType.VARCHAR),
60+
@Result(column="last_name", property="lastName", jdbcType=JdbcType.VARCHAR, typeHandler=LastNameTypeHandler.class),
6161
@Result(column="birth_date", property="birthDate", jdbcType=JdbcType.DATE),
6262
@Result(column="employed", property="employed", jdbcType=JdbcType.VARCHAR, typeHandler=YesNoTypeHandler.class),
6363
@Result(column="occupation", property="occupation", jdbcType=JdbcType.VARCHAR)

src/test/java/examples/simple/SimpleTableAnnotatedMapperTest.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -178,7 +178,7 @@ public void testInsert() {
178178
SimpleTableRecord record = new SimpleTableRecord();
179179
record.setId(100);
180180
record.setFirstName("Joe");
181-
record.setLastName("Jones");
181+
record.setLastName(LastName.of("Jones"));
182182
record.setBirthDate(new Date());
183183
record.setEmployed(true);
184184
record.setOccupation("Developer");
@@ -198,7 +198,7 @@ public void testInsertSelective() {
198198
SimpleTableRecord record = new SimpleTableRecord();
199199
record.setId(100);
200200
record.setFirstName("Joe");
201-
record.setLastName("Jones");
201+
record.setLastName(LastName.of("Jones"));
202202
record.setBirthDate(new Date());
203203
record.setEmployed(false);
204204

@@ -217,7 +217,7 @@ public void testUpdateByPrimaryKey() {
217217
SimpleTableRecord record = new SimpleTableRecord();
218218
record.setId(100);
219219
record.setFirstName("Joe");
220-
record.setLastName("Jones");
220+
record.setLastName(LastName.of("Jones"));
221221
record.setBirthDate(new Date());
222222
record.setEmployed(true);
223223
record.setOccupation("Developer");
@@ -246,7 +246,7 @@ public void testUpdateByPrimaryKeySelective() {
246246
SimpleTableRecord record = new SimpleTableRecord();
247247
record.setId(100);
248248
record.setFirstName("Joe");
249-
record.setLastName("Jones");
249+
record.setLastName(LastName.of("Jones"));
250250
record.setBirthDate(new Date());
251251
record.setEmployed(true);
252252
record.setOccupation("Developer");
@@ -279,7 +279,7 @@ public void testUpdateWithNulls() {
279279
SimpleTableRecord record = new SimpleTableRecord();
280280
record.setId(100);
281281
record.setFirstName("Joe");
282-
record.setLastName("Jones");
282+
record.setLastName(LastName.of("Jones"));
283283
record.setBirthDate(new Date());
284284
record.setEmployed(true);
285285
record.setOccupation("Developer");
@@ -316,7 +316,7 @@ public void testUpdateByExample() {
316316
SimpleTableRecord record = new SimpleTableRecord();
317317
record.setId(100);
318318
record.setFirstName("Joe");
319-
record.setLastName("Jones");
319+
record.setLastName(LastName.of("Jones"));
320320
record.setBirthDate(new Date());
321321
record.setEmployed(true);
322322
record.setOccupation("Developer");
@@ -357,4 +357,42 @@ public void testCountByExample() {
357357
session.close();
358358
}
359359
}
360+
361+
@Test
362+
public void testTypeHandledLike() {
363+
SqlSession session = sqlSessionFactory.openSession();
364+
try {
365+
SimpleTableAnnotatedMapper mapper = session.getMapper(SimpleTableAnnotatedMapper.class);
366+
367+
List<SimpleTableRecord> rows = mapper.selectByExample()
368+
.where(lastName, isLike(LastName.of("Fl%")))
369+
.orderBy(id)
370+
.build()
371+
.execute();
372+
373+
assertThat(rows.size()).isEqualTo(3);
374+
assertThat(rows.get(0).getFirstName()).isEqualTo("Fred");
375+
} finally {
376+
session.close();
377+
}
378+
}
379+
380+
@Test
381+
public void testTypeHandledNotLike() {
382+
SqlSession session = sqlSessionFactory.openSession();
383+
try {
384+
SimpleTableAnnotatedMapper mapper = session.getMapper(SimpleTableAnnotatedMapper.class);
385+
386+
List<SimpleTableRecord> rows = mapper.selectByExample()
387+
.where(lastName, isNotLike(LastName.of("Fl%")))
388+
.orderBy(id)
389+
.build()
390+
.execute();
391+
392+
assertThat(rows.size()).isEqualTo(3);
393+
assertThat(rows.get(0).getFirstName()).isEqualTo("Barney");
394+
} finally {
395+
session.close();
396+
}
397+
}
360398
}

src/test/java/examples/simple/SimpleTableDynamicSqlSupport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -25,15 +25,15 @@ public final class SimpleTableDynamicSqlSupport {
2525
public static final SimpleTable simpleTable = new SimpleTable();
2626
public static final SqlColumn<Integer> id = simpleTable.id;
2727
public static final SqlColumn<String> firstName = simpleTable.firstName;
28-
public static final SqlColumn<String> lastName = simpleTable.lastName;
28+
public static final SqlColumn<LastName> lastName = simpleTable.lastName;
2929
public static final SqlColumn<Date> birthDate = simpleTable.birthDate;
3030
public static final SqlColumn<Boolean> employed = simpleTable.employed;
3131
public static final SqlColumn<String> occupation = simpleTable.occupation;
3232

3333
public static final class SimpleTable extends SqlTable {
3434
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
3535
public final SqlColumn<String> firstName = column("first_name", JDBCType.VARCHAR);
36-
public final SqlColumn<String> lastName = column("last_name", JDBCType.VARCHAR);
36+
public final SqlColumn<LastName> lastName = column("last_name", JDBCType.VARCHAR, "examples.simple.LastNameTypeHandler");
3737
public final SqlColumn<Date> birthDate = column("birth_date", JDBCType.DATE);
3838
public final SqlColumn<Boolean> employed = column("employed", JDBCType.VARCHAR, "examples.simple.YesNoTypeHandler");
3939
public final SqlColumn<String> occupation = column("occupation", JDBCType.VARCHAR);

src/test/java/examples/simple/SimpleTableRecord.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -20,7 +20,7 @@
2020
public class SimpleTableRecord {
2121
private Integer id;
2222
private String firstName;
23-
private String lastName;
23+
private LastName lastName;
2424
private Date birthDate;
2525
private Boolean employed;
2626
private String occupation;
@@ -41,11 +41,11 @@ public void setFirstName(String firstName) {
4141
this.firstName = firstName;
4242
}
4343

44-
public String getLastName() {
44+
public LastName getLastName() {
4545
return lastName;
4646
}
4747

48-
public void setLastName(String lastName) {
48+
public void setLastName(LastName lastName) {
4949
this.lastName = lastName;
5050
}
5151

0 commit comments

Comments
 (0)