Skip to content

Commit 554d4ab

Browse files
committed
Better Kotlin Pattern
1 parent bdc607a commit 554d4ab

File tree

1 file changed

+68
-81
lines changed

1 file changed

+68
-81
lines changed

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

Lines changed: 68 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.mybatis.dynamic.sql.util.kotlin
1717

1818
import org.mybatis.dynamic.sql.BindableColumn
19+
import org.mybatis.dynamic.sql.SqlCriterion
1920
import org.mybatis.dynamic.sql.SqlTable
2021
import org.mybatis.dynamic.sql.VisitableCondition
2122
import org.mybatis.dynamic.sql.select.AbstractQueryExpressionDSL
@@ -31,39 +32,39 @@ typealias WhereApplier = AbstractWhereDSL<*>.() -> Unit
3132
@MyBatisDslMarker
3233
@Suppress("TooManyFunctions")
3334
abstract class KotlinBaseBuilder<D: AbstractWhereSupportingDSL<*>, B: KotlinBaseBuilder<D, B>> {
34-
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
35+
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>) =
3536
applyToWhere {
3637
where(column, condition)
3738
}
3839

39-
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
40-
applyToWhere {
41-
where(column, condition, CriteriaCollector().apply(subCriteria).criteria)
40+
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver) =
41+
applyToWhere(subCriteria) { sq ->
42+
where(column, condition, sq)
4243
}
4344

44-
fun applyWhere(whereApplier: WhereApplier): B =
45+
fun applyWhere(whereApplier: WhereApplier) =
4546
applyToWhere {
4647
applyWhere(whereApplier)
4748
}
4849

49-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
50+
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>) =
5051
applyToWhere {
5152
and(column, condition)
5253
}
5354

54-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
55-
applyToWhere {
56-
and(column, condition, CriteriaCollector().apply(subCriteria).criteria)
55+
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver) =
56+
applyToWhere(subCriteria) { sq ->
57+
and(column, condition, sq)
5758
}
5859

5960
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>) =
6061
applyToWhere {
6162
or(column, condition)
6263
}
6364

64-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
65-
applyToWhere {
66-
or(column, condition, CriteriaCollector().apply(subCriteria).criteria)
65+
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver) =
66+
applyToWhere(subCriteria) { sq ->
67+
or(column, condition, sq)
6768
}
6869

6970
fun allRows() = self()
@@ -73,120 +74,106 @@ abstract class KotlinBaseBuilder<D: AbstractWhereSupportingDSL<*>, B: KotlinBase
7374
return self()
7475
}
7576

77+
private fun applyToWhere(
78+
subCriteria: CriteriaReceiver,
79+
block: AbstractWhereDSL<*>.(List<SqlCriterion<*>>) -> Unit
80+
): B {
81+
getDsl().where().block(CriteriaCollector().apply(subCriteria).criteria)
82+
return self()
83+
}
84+
7685
protected abstract fun self(): B
7786

7887
protected abstract fun getDsl(): D
7988
}
8089

8190
@Suppress("TooManyFunctions")
82-
abstract class KotlinBaseJoiningBuilder<D: AbstractQueryExpressionDSL<*, *, *>, B: KotlinBaseJoiningBuilder<D, B>> : KotlinBaseBuilder<D, B>() {
91+
abstract class KotlinBaseJoiningBuilder<D: AbstractQueryExpressionDSL<*, *, *>, B: KotlinBaseJoiningBuilder<D, B>>
92+
: KotlinBaseBuilder<D, B>() {
8393

84-
fun join(table: SqlTable, joinCriteria: JoinReceiver): B =
85-
buildAndApply(joinCriteria) {
86-
applyToDsl {
87-
join(table, it.onJoinCriterion, it.andJoinCriteria)
88-
}
94+
fun join(table: SqlTable, joinCriteria: JoinReceiver) =
95+
applyToDsl(joinCriteria) { jc ->
96+
join(table, jc.onJoinCriterion, jc.andJoinCriteria)
8997
}
9098

91-
fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
92-
buildAndApply(joinCriteria) {
93-
applyToDsl {
94-
join(table, alias, it.onJoinCriterion, it.andJoinCriteria)
95-
}
99+
fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
100+
applyToDsl(joinCriteria) { jc ->
101+
join(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
96102
}
97103

98104
fun join(
99105
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
100106
joinCriteria: JoinReceiver
101-
): B =
102-
buildAndApply(joinCriteria) {
103-
val builder = KotlinQualifiedSubQueryBuilder().apply(subQuery)
104-
applyToDsl {
105-
join(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
106-
}
107+
) =
108+
applyToDsl(subQuery, joinCriteria) { sq, jc ->
109+
join(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
107110
}
108111

109-
fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
110-
buildAndApply(joinCriteria) {
111-
applyToDsl {
112-
fullJoin(table, it.onJoinCriterion, it.andJoinCriteria)
113-
}
112+
fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver) =
113+
applyToDsl(joinCriteria) { jc ->
114+
fullJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
114115
}
115116

116-
fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
117-
buildAndApply(joinCriteria) {
118-
applyToDsl {
119-
fullJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
120-
}
117+
fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
118+
applyToDsl(joinCriteria) { jc ->
119+
fullJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
121120
}
122121

123122
fun fullJoin(
124123
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
125124
joinCriteria: JoinReceiver
126-
): B =
127-
buildAndApply(joinCriteria) {
128-
val builder = KotlinQualifiedSubQueryBuilder().apply(subQuery)
129-
applyToDsl {
130-
fullJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
131-
}
125+
) =
126+
applyToDsl(subQuery, joinCriteria) { sq, jc ->
127+
fullJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
132128
}
133129

134-
fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
135-
buildAndApply(joinCriteria) {
136-
applyToDsl {
137-
leftJoin(table, it.onJoinCriterion, it.andJoinCriteria)
138-
}
130+
fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver) =
131+
applyToDsl(joinCriteria) { jc ->
132+
leftJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
139133
}
140134

141-
fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
142-
buildAndApply(joinCriteria) {
143-
applyToDsl {
144-
leftJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
145-
}
135+
fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
136+
applyToDsl(joinCriteria) { jc ->
137+
leftJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
146138
}
147139

148140
fun leftJoin(
149141
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
150142
joinCriteria: JoinReceiver
151-
): B =
152-
buildAndApply(joinCriteria) {
153-
val builder = KotlinQualifiedSubQueryBuilder().apply(subQuery)
154-
applyToDsl {
155-
leftJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
156-
}
143+
) =
144+
applyToDsl(subQuery, joinCriteria) { sq, jc ->
145+
leftJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
157146
}
158147

159-
fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
160-
buildAndApply(joinCriteria) {
161-
applyToDsl {
162-
rightJoin(table, it.onJoinCriterion, it.andJoinCriteria)
163-
}
148+
fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver) =
149+
applyToDsl(joinCriteria) { jc ->
150+
rightJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
164151
}
165152

166-
fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
167-
buildAndApply(joinCriteria) {
168-
applyToDsl {
169-
rightJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
170-
}
153+
fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
154+
applyToDsl(joinCriteria) { jc ->
155+
rightJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
171156
}
172157

173158
fun rightJoin(
174159
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
175160
joinCriteria: JoinReceiver
176-
): B =
177-
buildAndApply(joinCriteria) {
178-
val builder = KotlinQualifiedSubQueryBuilder().apply(subQuery)
179-
applyToDsl {
180-
rightJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
181-
}
161+
) =
162+
applyToDsl(subQuery, joinCriteria) { sq, jc ->
163+
rightJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
182164
}
183165

184-
private fun buildAndApply(joinCriteria: JoinReceiver, block: (JoinCollector) -> B): B {
185-
return block(JoinCollector().apply(joinCriteria))
166+
private fun applyToDsl(joinCriteria: JoinReceiver, applyJoin: D.(JoinCollector) -> Unit): B {
167+
getDsl().applyJoin(JoinCollector().apply(joinCriteria))
168+
return self()
186169
}
187170

188-
private fun applyToDsl(block: D.() -> Unit): B {
189-
getDsl().apply(block)
171+
private fun applyToDsl(
172+
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
173+
joinCriteria: JoinReceiver,
174+
applyJoin: D.(KotlinQualifiedSubQueryBuilder, JoinCollector) -> Unit
175+
): B {
176+
getDsl().applyJoin(KotlinQualifiedSubQueryBuilder().apply(subQuery), JoinCollector().apply(joinCriteria))
190177
return self()
191178
}
192179
}

0 commit comments

Comments
 (0)