Skip to content

Commit 2c477e2

Browse files
committed
Add a utility interface and more idiomatic Kotlin
1 parent 36d4144 commit 2c477e2

File tree

8 files changed

+51
-29
lines changed

8 files changed

+51
-29
lines changed

src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Objects;
1919
import java.util.function.Function;
2020
import java.util.function.ToIntFunction;
21-
import java.util.function.UnaryOperator;
2221

2322
import org.mybatis.dynamic.sql.BindableColumn;
2423
import org.mybatis.dynamic.sql.SqlCriterion;
@@ -28,6 +27,7 @@
2827
import org.mybatis.dynamic.sql.util.Buildable;
2928
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
3029
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
30+
import org.mybatis.dynamic.sql.where.WhereApplier;
3131
import org.mybatis.dynamic.sql.where.WhereModel;
3232

3333
public class DeleteDSL<R> implements Buildable<R> {
@@ -52,8 +52,8 @@ public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition
5252
}
5353

5454
@SuppressWarnings("unchecked")
55-
public DeleteWhereBuilder applyWhere(UnaryOperator<AbstractWhereDSL<?>> whereApplyer) {
56-
return (DeleteWhereBuilder) whereApplyer.apply(whereBuilder);
55+
public DeleteWhereBuilder applyWhere(WhereApplier whereApplier) {
56+
return (DeleteWhereBuilder) whereApplier.apply(whereBuilder);
5757
}
5858

5959
/**

src/main/java/org/mybatis/dynamic/sql/select/CountDSL.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.Objects;
1919
import java.util.function.Function;
20-
import java.util.function.UnaryOperator;
2120

2221
import org.mybatis.dynamic.sql.BindableColumn;
2322
import org.mybatis.dynamic.sql.SqlBuilder;
@@ -26,6 +25,7 @@
2625
import org.mybatis.dynamic.sql.VisitableCondition;
2726
import org.mybatis.dynamic.sql.util.Buildable;
2827
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
28+
import org.mybatis.dynamic.sql.where.WhereApplier;
2929
import org.mybatis.dynamic.sql.where.WhereModel;
3030

3131
/**
@@ -58,8 +58,8 @@ public <T> CountWhereBuilder where(BindableColumn<T> column, VisitableCondition<
5858
}
5959

6060
@SuppressWarnings("unchecked")
61-
public CountWhereBuilder applyWhere(UnaryOperator<AbstractWhereDSL<?>> whereApplyer) {
62-
return (CountWhereBuilder) whereApplyer.apply(whereBuilder);
61+
public CountWhereBuilder applyWhere(WhereApplier whereApplier) {
62+
return (CountWhereBuilder) whereApplier.apply(whereBuilder);
6363
}
6464

6565
@Override

src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Collection;
2121
import java.util.List;
2222
import java.util.Objects;
23-
import java.util.function.UnaryOperator;
2423

2524
import org.mybatis.dynamic.sql.BasicColumn;
2625
import org.mybatis.dynamic.sql.BindableColumn;
@@ -34,6 +33,7 @@
3433
import org.mybatis.dynamic.sql.select.join.JoinType;
3534
import org.mybatis.dynamic.sql.util.Buildable;
3635
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
36+
import org.mybatis.dynamic.sql.where.WhereApplier;
3737
import org.mybatis.dynamic.sql.where.WhereModel;
3838

3939
public class QueryExpressionDSL<R> extends AbstractQueryExpressionDSL<QueryExpressionDSL<R>, R>
@@ -70,8 +70,8 @@ public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, Visitable
7070
}
7171

7272
@SuppressWarnings("unchecked")
73-
public QueryExpressionWhereBuilder applyWhere(UnaryOperator<AbstractWhereDSL<?>> whereApplyer) {
74-
return (QueryExpressionWhereBuilder) whereApplyer.apply(whereBuilder);
73+
public QueryExpressionWhereBuilder applyWhere(WhereApplier whereApplier) {
74+
return (QueryExpressionWhereBuilder) whereApplier.apply(whereBuilder);
7575
}
7676

7777
@Override
@@ -334,8 +334,8 @@ public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, Visitable
334334
return QueryExpressionDSL.this.where(column, condition, subCriteria);
335335
}
336336

337-
public QueryExpressionWhereBuilder applyWhere(UnaryOperator<AbstractWhereDSL<?>> whereApplyer) {
338-
return QueryExpressionDSL.this.applyWhere(whereApplyer);
337+
public QueryExpressionWhereBuilder applyWhere(WhereApplier whereApplier) {
338+
return QueryExpressionDSL.this.applyWhere(whereApplier);
339339
}
340340

341341
public JoinSpecificationFinisher and(BasicColumn joinColumn, JoinCondition joinCondition) {

src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.function.Function;
2222
import java.util.function.Supplier;
2323
import java.util.function.ToIntFunction;
24-
import java.util.function.UnaryOperator;
2524

2625
import org.mybatis.dynamic.sql.BasicColumn;
2726
import org.mybatis.dynamic.sql.BindableColumn;
@@ -41,6 +40,7 @@
4140
import org.mybatis.dynamic.sql.util.ValueMapping;
4241
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
4342
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
43+
import org.mybatis.dynamic.sql.where.WhereApplier;
4444
import org.mybatis.dynamic.sql.where.WhereModel;
4545

4646
public class UpdateDSL<R> implements Buildable<R> {
@@ -70,8 +70,8 @@ public <T> UpdateWhereBuilder where(BindableColumn<T> column, VisitableCondition
7070
}
7171

7272
@SuppressWarnings("unchecked")
73-
public UpdateWhereBuilder applyWhere(UnaryOperator<AbstractWhereDSL<?>> whereApplyer) {
74-
return (UpdateWhereBuilder) whereApplyer.apply(whereBuilder);
73+
public UpdateWhereBuilder applyWhere(WhereApplier whereApplier) {
74+
return (UpdateWhereBuilder) whereApplier.apply(whereBuilder);
7575
}
7676

7777
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 org.mybatis.dynamic.sql.where;
17+
18+
import java.util.function.UnaryOperator;
19+
20+
@FunctionalInterface
21+
public interface WhereApplier extends UnaryOperator<AbstractWhereDSL<?>> {}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import org.mybatis.dynamic.sql.BindableColumn
1919
import org.mybatis.dynamic.sql.VisitableCondition
2020
import org.mybatis.dynamic.sql.where.AbstractWhereDSL
2121

22+
typealias WhereApplier = AbstractWhereDSL<*>.() -> AbstractWhereDSL<*>
23+
2224
fun <T, M : AbstractWhereDSL<M>> AbstractWhereDSL<M>.where(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): M {
2325
val collector = CriteriaCollector()
2426
collect(collector)

src/test/java/examples/simple/ReusableWhereTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
3838
import org.junit.jupiter.api.BeforeEach;
3939
import org.junit.jupiter.api.Test;
40-
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
40+
import org.mybatis.dynamic.sql.where.WhereApplier;
4141

4242
public class ReusableWhereTest {
4343

@@ -69,7 +69,7 @@ public void testCount() {
6969
try (SqlSession session = sqlSessionFactory.openSession()) {
7070
PersonMapper mapper = session.getMapper(PersonMapper.class);
7171

72-
long rows = mapper.count(c -> c.applyWhere(this::commonWhere));
72+
long rows = mapper.count(c -> c.applyWhere(commonWhere));
7373

7474
assertThat(rows).isEqualTo(3);
7575
}
@@ -80,7 +80,7 @@ public void testDelete() {
8080
try (SqlSession session = sqlSessionFactory.openSession()) {
8181
PersonMapper mapper = session.getMapper(PersonMapper.class);
8282

83-
int rows = mapper.delete(c -> c.applyWhere(this::commonWhere));
83+
int rows = mapper.delete(c -> c.applyWhere(commonWhere));
8484

8585
assertThat(rows).isEqualTo(3);
8686
}
@@ -92,7 +92,7 @@ public void testSelect() {
9292
PersonMapper mapper = session.getMapper(PersonMapper.class);
9393

9494
List<PersonRecord> rows = mapper.select(c ->
95-
c.applyWhere(this::commonWhere)
95+
c.applyWhere(commonWhere)
9696
.orderBy(id));
9797

9898
assertThat(rows.size()).isEqualTo(3);
@@ -106,13 +106,11 @@ public void testUpdate() {
106106

107107
int rows = mapper.update(c ->
108108
c.set(occupation).equalToStringConstant("worker")
109-
.applyWhere(this::commonWhere));
109+
.applyWhere(commonWhere));
110110

111111
assertThat(rows).isEqualTo(3);
112112
}
113113
}
114114

115-
private AbstractWhereDSL<?> commonWhere(AbstractWhereDSL<?> dsl) {
116-
return dsl.where(id, isEqualTo(1)).or(occupation, isNull());
117-
}
115+
private WhereApplier commonWhere = d -> d.where(id, isEqualTo(1)).or(occupation, isNull());
118116
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.assertj.core.api.Assertions.assertThat
2828
import org.junit.jupiter.api.Test
2929
import org.mybatis.dynamic.sql.SqlBuilder.isEqualTo
3030
import org.mybatis.dynamic.sql.SqlBuilder.isNull
31-
import org.mybatis.dynamic.sql.where.AbstractWhereDSL
31+
import org.mybatis.dynamic.sql.util.kotlin.WhereApplier
3232
import java.io.InputStreamReader
3333
import java.sql.DriverManager
3434

@@ -57,7 +57,7 @@ class ReusableWhereTest {
5757
val mapper = session.getMapper(PersonMapper::class.java)
5858

5959
val rows = mapper.count {
60-
applyWhere (::commonWhere)
60+
applyWhere (commonWhere)
6161
}
6262

6363
assertThat(rows).isEqualTo(3)
@@ -70,7 +70,7 @@ class ReusableWhereTest {
7070
val mapper = session.getMapper(PersonMapper::class.java)
7171

7272
val rows = mapper.delete {
73-
applyWhere (::commonWhere)
73+
applyWhere (commonWhere)
7474
}
7575

7676
assertThat(rows).isEqualTo(3)
@@ -83,7 +83,7 @@ class ReusableWhereTest {
8383
val mapper = session.getMapper(PersonMapper::class.java)
8484

8585
val rows = mapper.select {
86-
applyWhere (::commonWhere)
86+
applyWhere (commonWhere)
8787
orderBy(id)
8888
}
8989

@@ -98,15 +98,16 @@ class ReusableWhereTest {
9898

9999
val rows = mapper.update {
100100
set(occupation).equalToStringConstant("worker")
101-
applyWhere (::commonWhere)
101+
applyWhere (commonWhere)
102102
}
103103

104104
assertThat(rows).isEqualTo(3)
105105
}
106106
}
107107

108-
private fun commonWhere(dsl: AbstractWhereDSL<*>): AbstractWhereDSL<*> =
109-
dsl.where(id, isEqualTo(1)).or(occupation, isNull());
108+
private val commonWhere: WhereApplier = {
109+
where(id, isEqualTo(1)).or(occupation, isNull<String>())
110+
}
110111

111112
companion object {
112113
const val JDBC_URL = "jdbc:hsqldb:mem:aname"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy