Skip to content

Commit 62a7bda

Browse files
authored
Merge pull request #446 from jeffgbutler/kotlin-dsl-cleanup
[Kotlin] General DSL Cleanup and Simplification
2 parents 8495687 + a1eee89 commit 62a7bda

21 files changed

+146
-192
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
2727
but is deprecated. It will be removed in version 1.5.0 of the library. Documentation for the new DSL is here:
2828
https://github.com/mybatis/mybatis-dynamic-sql/blob/master/src/site/markdown/docs/kotlinWhereClauses.md
2929
([#442](https://github.com/mybatis/mybatis-dynamic-sql/pull/442))
30+
5. General cleanup of the Kotlin DSL. The Kotlin DSL functions are now mostly Unit functions. This should have
31+
no impact on most users and is source code compatible with prior versions of the library when the library was used
32+
as described in the documentation. This change greatly simplifies the type hierarchy of the Kotlin builders.
33+
([#446](https://github.com/mybatis/mybatis-dynamic-sql/pull/446))
3034

3135
## Release 1.3.1 - December 18, 2021
3236

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ class CriteriaCollector {
3535
column: BindableColumn<T>,
3636
condition: VisitableCondition<T>,
3737
criteriaReceiver: CriteriaReceiver = {}
38-
): CriteriaCollector =
38+
): Unit =
3939
addCriteriaGroup("and", buildCriterion(column, condition), criteriaReceiver)
4040

41-
fun and(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver = {}): CriteriaCollector =
41+
fun and(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver = {}): Unit =
4242
addCriteriaGroup("and", buildCriterion(existsPredicate), criteriaReceiver)
4343

4444
fun <T> or(
4545
column: BindableColumn<T>,
4646
condition: VisitableCondition<T>,
4747
criteriaReceiver: CriteriaReceiver = {}
48-
): CriteriaCollector =
48+
): Unit =
4949
addCriteriaGroup("or", buildCriterion(column, condition), criteriaReceiver)
5050

51-
fun or(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver = {}): CriteriaCollector =
51+
fun or(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver = {}): Unit =
5252
addCriteriaGroup("or", buildCriterion(existsPredicate), criteriaReceiver)
5353

5454
private fun <T> buildCriterion(
@@ -64,14 +64,13 @@ class CriteriaCollector {
6464
connector: String,
6565
initialCriterion: SqlCriterion,
6666
criteriaReceiver: CriteriaReceiver
67-
) =
68-
apply {
69-
criteria.add(
70-
AndOrCriteriaGroup.Builder()
71-
.withInitialCriterion(initialCriterion)
72-
.withSubCriteria(CriteriaCollector().apply(criteriaReceiver).criteria)
73-
.withConnector(connector)
74-
.build()
75-
)
76-
}
67+
) {
68+
criteria.add(
69+
AndOrCriteriaGroup.Builder()
70+
.withInitialCriterion(initialCriterion)
71+
.withSubCriteria(CriteriaCollector().apply(criteriaReceiver).criteria)
72+
.withConnector(connector)
73+
.build()
74+
)
75+
}
7776
}

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 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.
@@ -27,23 +27,21 @@ class JoinCollector {
2727
val andJoinCriteria = mutableListOf<JoinCriterion>()
2828
private lateinit var internalOnCriterion: JoinCriterion
2929

30-
fun on(column: BasicColumn, condition: JoinCondition): JoinCollector =
31-
apply {
32-
internalOnCriterion = JoinCriterion.Builder()
33-
.withConnector("on")
30+
fun on(column: BasicColumn, condition: JoinCondition) {
31+
internalOnCriterion = JoinCriterion.Builder()
32+
.withConnector("on")
33+
.withJoinColumn(column)
34+
.withJoinCondition(condition)
35+
.build()
36+
}
37+
38+
fun and(column: BasicColumn, condition: JoinCondition) {
39+
andJoinCriteria.add(
40+
JoinCriterion.Builder()
41+
.withConnector("and")
3442
.withJoinColumn(column)
3543
.withJoinCondition(condition)
3644
.build()
37-
}
38-
39-
fun and(column: BasicColumn, condition: JoinCondition): JoinCollector =
40-
apply {
41-
andJoinCriteria.add(
42-
JoinCriterion.Builder()
43-
.withConnector("and")
44-
.withJoinColumn(column)
45-
.withJoinCondition(condition)
46-
.build()
47-
)
48-
}
45+
)
46+
}
4947
}

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

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.mybatis.dynamic.sql.where.AbstractWhereSupport
2828
@DslMarker
2929
annotation class MyBatisDslMarker
3030

31-
typealias WhereApplier = KotlinBaseBuilder<*, *>.() -> Unit
31+
typealias WhereApplier = KotlinBaseBuilder<*>.() -> Unit
3232

3333
fun WhereApplier.andThen(after: WhereApplier): WhereApplier = {
3434
invoke(this)
@@ -37,7 +37,7 @@ fun WhereApplier.andThen(after: WhereApplier): WhereApplier = {
3737

3838
@MyBatisDslMarker
3939
@Suppress("TooManyFunctions")
40-
abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuilder<D, B>> {
40+
abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>> {
4141
fun where(criteria: GroupingCriteriaReceiver): Unit =
4242
with(GroupingCriteriaCollector().apply(criteria)) {
4343
this@KotlinBaseBuilder.getDsl().where(initialCriterion, subCriteria)
@@ -53,21 +53,18 @@ abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuil
5353
this@KotlinBaseBuilder.getDsl().where().or(initialCriterion, subCriteria)
5454
}
5555

56-
fun applyWhere(whereApplier: WhereApplier): B =
57-
self().apply {
58-
whereApplier.invoke(this)
59-
}
56+
fun applyWhere(whereApplier: WhereApplier) = whereApplier.invoke(this)
6057

6158
@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
6259
"into a lambda and rewriting the condition to use an infix function.")
63-
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
60+
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): Unit =
6461
applyToWhere {
6562
where(column, condition)
6663
}
6764

6865
@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
6966
"inside the lambda and rewriting the condition to use an infix function.")
70-
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
67+
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): Unit =
7168
applyToWhere(subCriteria) { sc ->
7269
where(column, condition, sc)
7370
}
@@ -76,28 +73,28 @@ abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuil
7673
message = "Deprecated in favor of the new where clause DSL.",
7774
replaceWith = ReplaceWith("where { existsPredicate }")
7875
)
79-
fun where(existsPredicate: ExistsPredicate): B =
76+
fun where(existsPredicate: ExistsPredicate): Unit =
8077
applyToWhere {
8178
where(existsPredicate)
8279
}
8380

8481
@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the exists expression " +
8582
"into the lambda.")
86-
fun where(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): B =
83+
fun where(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): Unit =
8784
applyToWhere(subCriteria) { sc ->
8885
where(existsPredicate, sc)
8986
}
9087

9188
@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
9289
"into a lambda and rewriting the condition to use an infix function.")
93-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
90+
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>): Unit =
9491
applyToWhere {
9592
and(column, condition)
9693
}
9794

9895
@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
9996
"inside the lambda and rewriting the condition to use an infix function.")
100-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
97+
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): Unit =
10198
applyToWhere(subCriteria) { sc ->
10299
and(column, condition, sc)
103100
}
@@ -106,28 +103,28 @@ abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuil
106103
message = "Deprecated in favor of the new where clause DSL.",
107104
replaceWith = ReplaceWith("and { existsPredicate }")
108105
)
109-
fun and(existsPredicate: ExistsPredicate): B =
106+
fun and(existsPredicate: ExistsPredicate): Unit =
110107
applyToWhere {
111108
and(existsPredicate)
112109
}
113110

114111
@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the exists expression " +
115112
"into the lambda.")
116-
fun and(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): B =
113+
fun and(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): Unit =
117114
applyToWhere(subCriteria) { sc ->
118115
and(existsPredicate, sc)
119116
}
120117

121118
@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
122119
"into a lambda and rewriting the condition to use an infix function.")
123-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
120+
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>): Unit =
124121
applyToWhere {
125122
or(column, condition)
126123
}
127124

128125
@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
129126
"inside the lambda and rewriting the condition to use an infix function.")
130-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
127+
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): Unit =
131128
applyToWhere(subCriteria) { sc ->
132129
or(column, condition, sc)
133130
}
@@ -136,125 +133,118 @@ abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuil
136133
message = "Deprecated in favor of the new where clause DSL.",
137134
replaceWith = ReplaceWith("or { existsPredicate }")
138135
)
139-
fun or(existsPredicate: ExistsPredicate): B =
136+
fun or(existsPredicate: ExistsPredicate): Unit =
140137
applyToWhere {
141138
or(existsPredicate)
142139
}
143140

144141
@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the exists expression " +
145142
"into the lambda.")
146-
fun or(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): B =
143+
fun or(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): Unit =
147144
applyToWhere(subCriteria) { sc ->
148145
or(existsPredicate, sc)
149146
}
150147

151-
fun allRows(): B = self()
148+
fun allRows() {}
152149

153-
private fun applyToWhere(block: AbstractWhereDSL<*>.() -> Unit): B {
150+
private fun applyToWhere(block: AbstractWhereDSL<*>.() -> Unit) {
154151
getDsl().where().apply(block)
155-
return self()
156152
}
157153

158154
private fun applyToWhere(
159155
subCriteria: CriteriaReceiver,
160156
block: AbstractWhereDSL<*>.(List<AndOrCriteriaGroup>) -> Unit
161-
): B {
157+
) {
162158
getDsl().where().block(CriteriaCollector().apply(subCriteria).criteria)
163-
return self()
164159
}
165160

166-
protected abstract fun self(): B
167-
168161
protected abstract fun getDsl(): D
169162
}
170163

171164
@Suppress("TooManyFunctions")
172-
abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>, B : KotlinBaseJoiningBuilder<D, B>> :
173-
KotlinBaseBuilder<D, B>() {
165+
abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>> : KotlinBaseBuilder<D>() {
174166

175-
fun join(table: SqlTable, joinCriteria: JoinReceiver): B =
167+
fun join(table: SqlTable, joinCriteria: JoinReceiver): Unit =
176168
applyToDsl(joinCriteria) { jc ->
177169
join(table, jc.onJoinCriterion, jc.andJoinCriteria)
178170
}
179171

180-
fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
172+
fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
181173
applyToDsl(joinCriteria) { jc ->
182174
join(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
183175
}
184176

185177
fun join(
186178
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
187179
joinCriteria: JoinReceiver
188-
): B =
180+
): Unit =
189181
applyToDsl(subQuery, joinCriteria) { sq, jc ->
190182
join(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
191183
}
192184

193-
fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
185+
fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver): Unit =
194186
applyToDsl(joinCriteria) { jc ->
195187
fullJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
196188
}
197189

198-
fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
190+
fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
199191
applyToDsl(joinCriteria) { jc ->
200192
fullJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
201193
}
202194

203195
fun fullJoin(
204196
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
205197
joinCriteria: JoinReceiver
206-
): B =
198+
): Unit =
207199
applyToDsl(subQuery, joinCriteria) { sq, jc ->
208200
fullJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
209201
}
210202

211-
fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
203+
fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver): Unit =
212204
applyToDsl(joinCriteria) { jc ->
213205
leftJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
214206
}
215207

216-
fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
208+
fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
217209
applyToDsl(joinCriteria) { jc ->
218210
leftJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
219211
}
220212

221213
fun leftJoin(
222214
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
223215
joinCriteria: JoinReceiver
224-
): B =
216+
): Unit =
225217
applyToDsl(subQuery, joinCriteria) { sq, jc ->
226218
leftJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
227219
}
228220

229-
fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
221+
fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver): Unit =
230222
applyToDsl(joinCriteria) { jc ->
231223
rightJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
232224
}
233225

234-
fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
226+
fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
235227
applyToDsl(joinCriteria) { jc ->
236228
rightJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
237229
}
238230

239231
fun rightJoin(
240232
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
241233
joinCriteria: JoinReceiver
242-
): B =
234+
): Unit =
243235
applyToDsl(subQuery, joinCriteria) { sq, jc ->
244236
rightJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
245237
}
246238

247-
private fun applyToDsl(joinCriteria: JoinReceiver, applyJoin: D.(JoinCollector) -> Unit): B {
239+
private fun applyToDsl(joinCriteria: JoinReceiver, applyJoin: D.(JoinCollector) -> Unit) {
248240
getDsl().applyJoin(JoinCollector().apply(joinCriteria))
249-
return self()
250241
}
251242

252243
private fun applyToDsl(
253244
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
254245
joinCriteria: JoinReceiver,
255246
applyJoin: D.(KotlinQualifiedSubQueryBuilder, JoinCollector) -> Unit
256-
): B {
247+
) {
257248
getDsl().applyJoin(KotlinQualifiedSubQueryBuilder().apply(subQuery), JoinCollector().apply(joinCriteria))
258-
return self()
259249
}
260250
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 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.
@@ -23,7 +23,7 @@ import org.mybatis.dynamic.sql.util.Buildable
2323
typealias CountCompleter = KotlinCountBuilder.() -> Unit
2424

2525
class KotlinCountBuilder(private val fromGatherer: CountDSL.FromGatherer<SelectModel>) :
26-
KotlinBaseJoiningBuilder<CountDSL<SelectModel>, KotlinCountBuilder>(),
26+
KotlinBaseJoiningBuilder<CountDSL<SelectModel>>(),
2727
Buildable<SelectModel> {
2828

2929
private lateinit var dsl: CountDSL<SelectModel>
@@ -35,8 +35,6 @@ class KotlinCountBuilder(private val fromGatherer: CountDSL.FromGatherer<SelectM
3535

3636
override fun build(): SelectModel = getDsl().build()
3737

38-
override fun self(): KotlinCountBuilder = this
39-
4038
override fun getDsl(): CountDSL<SelectModel> {
4139
try {
4240
return dsl

0 commit comments

Comments
 (0)