Skip to content

Commit bdc607a

Browse files
committed
Kotlin Polishing
1 parent e5671a7 commit bdc607a

File tree

6 files changed

+102
-76
lines changed

6 files changed

+102
-76
lines changed

src/main/java/org/mybatis/dynamic/sql/where/AbstractWhereSupportingDSL.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.mybatis.dynamic.sql.SqlCriterion;
55
import org.mybatis.dynamic.sql.VisitableCondition;
66

7+
import java.util.Arrays;
8+
import java.util.List;
79
import java.util.function.Consumer;
810

911
/**
@@ -14,6 +16,10 @@
1416
public abstract class AbstractWhereSupportingDSL<W extends AbstractWhereDSL<?>> {
1517

1618
public <T> W where(BindableColumn<T> column, VisitableCondition<T> condition, SqlCriterion<?>...subCriteria) {
19+
return where(column, condition, Arrays.asList(subCriteria));
20+
}
21+
22+
public <T> W where(BindableColumn<T> column, VisitableCondition<T> condition, List<SqlCriterion<?>> subCriteria) {
1723
return apply(w -> w.where(column, condition, subCriteria));
1824
}
1925

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

Lines changed: 90 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import org.mybatis.dynamic.sql.BindableColumn
1919
import org.mybatis.dynamic.sql.SqlTable
2020
import org.mybatis.dynamic.sql.VisitableCondition
2121
import org.mybatis.dynamic.sql.select.AbstractQueryExpressionDSL
22-
import org.mybatis.dynamic.sql.select.SelectModel
2322
import org.mybatis.dynamic.sql.where.AbstractWhereDSL
23+
import org.mybatis.dynamic.sql.where.AbstractWhereSupportingDSL
2424

2525
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
2626
@DslMarker
@@ -30,136 +30,163 @@ typealias WhereApplier = AbstractWhereDSL<*>.() -> Unit
3030

3131
@MyBatisDslMarker
3232
@Suppress("TooManyFunctions")
33-
abstract class KotlinBaseBuilder<W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<W, B>> {
33+
abstract class KotlinBaseBuilder<D: AbstractWhereSupportingDSL<*>, B: KotlinBaseBuilder<D, B>> {
3434
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
35-
applySelf {
36-
getWhere().where(column, condition)
35+
applyToWhere {
36+
where(column, condition)
3737
}
3838

3939
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
40-
applySelf {
41-
getWhere().where(column, condition, CriteriaCollector().apply(subCriteria).criteria)
40+
applyToWhere {
41+
where(column, condition, CriteriaCollector().apply(subCriteria).criteria)
4242
}
4343

4444
fun applyWhere(whereApplier: WhereApplier): B =
45-
applySelf {
46-
getWhere().applyWhere(whereApplier)
45+
applyToWhere {
46+
applyWhere(whereApplier)
4747
}
4848

4949
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
50-
applySelf {
51-
getWhere().and(column, condition)
50+
applyToWhere {
51+
and(column, condition)
5252
}
5353

5454
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
55-
applySelf {
56-
getWhere().and(column, condition, CriteriaCollector().apply(subCriteria).criteria)
55+
applyToWhere {
56+
and(column, condition, CriteriaCollector().apply(subCriteria).criteria)
5757
}
5858

59-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
60-
applySelf {
61-
getWhere().or(column, condition)
59+
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>) =
60+
applyToWhere {
61+
or(column, condition)
6262
}
6363

6464
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
65-
applySelf {
66-
getWhere().or(column, condition, CriteriaCollector().apply(subCriteria).criteria)
65+
applyToWhere {
66+
or(column, condition, CriteriaCollector().apply(subCriteria).criteria)
6767
}
6868

6969
fun allRows() = self()
7070

71-
protected fun applySelf(block: B.() -> Unit): B =
72-
self().apply { block() }
71+
private fun applyToWhere(block: AbstractWhereDSL<*>.() -> Unit): B {
72+
getDsl().where().apply(block)
73+
return self()
74+
}
7375

7476
protected abstract fun self(): B
7577

76-
protected abstract fun getWhere(): W
78+
protected abstract fun getDsl(): D
7779
}
7880

7981
@Suppress("TooManyFunctions")
80-
abstract class KotlinBaseJoiningBuilder<T : AbstractQueryExpressionDSL<W, T, SelectModel>, W : AbstractWhereDSL<W>,
81-
B : KotlinBaseJoiningBuilder<T, W, B>> : KotlinBaseBuilder<W, B>() {
82+
abstract class KotlinBaseJoiningBuilder<D: AbstractQueryExpressionDSL<*, *, *>, B: KotlinBaseJoiningBuilder<D, B>> : KotlinBaseBuilder<D, B>() {
8283

83-
fun join(table: SqlTable, joinCriteria: JoinReceiver) =
84-
applyJoin(joinCriteria) {
85-
getDsl().join(table, it.onJoinCriterion, it.andJoinCriteria)
84+
fun join(table: SqlTable, joinCriteria: JoinReceiver): B =
85+
buildAndApply(joinCriteria) {
86+
applyToDsl {
87+
join(table, it.onJoinCriterion, it.andJoinCriteria)
88+
}
8689
}
8790

88-
fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
89-
applyJoin(joinCriteria) {
90-
getDsl().join(table, alias, it.onJoinCriterion, it.andJoinCriteria)
91+
fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
92+
buildAndApply(joinCriteria) {
93+
applyToDsl {
94+
join(table, alias, it.onJoinCriterion, it.andJoinCriteria)
95+
}
9196
}
9297

9398
fun join(
9499
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
95100
joinCriteria: JoinReceiver
96-
) =
97-
applyJoin(joinCriteria) {
101+
): B =
102+
buildAndApply(joinCriteria) {
98103
val builder = KotlinQualifiedSubQueryBuilder().apply(subQuery)
99-
getDsl().join(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
104+
applyToDsl {
105+
join(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
106+
}
100107
}
101108

102-
fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver) =
103-
applyJoin(joinCriteria) {
104-
getDsl().fullJoin(table, it.onJoinCriterion, it.andJoinCriteria)
109+
fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
110+
buildAndApply(joinCriteria) {
111+
applyToDsl {
112+
fullJoin(table, it.onJoinCriterion, it.andJoinCriteria)
113+
}
105114
}
106115

107-
fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
108-
applyJoin(joinCriteria) {
109-
getDsl().fullJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
116+
fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
117+
buildAndApply(joinCriteria) {
118+
applyToDsl {
119+
fullJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
120+
}
110121
}
111122

112123
fun fullJoin(
113124
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
114125
joinCriteria: JoinReceiver
115-
) =
116-
applyJoin(joinCriteria) {
126+
): B =
127+
buildAndApply(joinCriteria) {
117128
val builder = KotlinQualifiedSubQueryBuilder().apply(subQuery)
118-
getDsl().fullJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
129+
applyToDsl {
130+
fullJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
131+
}
119132
}
120133

121-
fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver) =
122-
applyJoin(joinCriteria) {
123-
getDsl().leftJoin(table, it.onJoinCriterion, it.andJoinCriteria)
134+
fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
135+
buildAndApply(joinCriteria) {
136+
applyToDsl {
137+
leftJoin(table, it.onJoinCriterion, it.andJoinCriteria)
138+
}
124139
}
125140

126-
fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
127-
applyJoin(joinCriteria) {
128-
getDsl().leftJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
141+
fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
142+
buildAndApply(joinCriteria) {
143+
applyToDsl {
144+
leftJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
145+
}
129146
}
130147

131148
fun leftJoin(
132149
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
133150
joinCriteria: JoinReceiver
134-
) =
135-
applyJoin(joinCriteria) {
151+
): B =
152+
buildAndApply(joinCriteria) {
136153
val builder = KotlinQualifiedSubQueryBuilder().apply(subQuery)
137-
getDsl().leftJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
154+
applyToDsl {
155+
leftJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
156+
}
138157
}
139158

140-
fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver) =
141-
applyJoin(joinCriteria) {
142-
getDsl().rightJoin(table, it.onJoinCriterion, it.andJoinCriteria)
159+
fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
160+
buildAndApply(joinCriteria) {
161+
applyToDsl {
162+
rightJoin(table, it.onJoinCriterion, it.andJoinCriteria)
163+
}
143164
}
144165

145-
fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
146-
applyJoin(joinCriteria) {
147-
getDsl().rightJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
166+
fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
167+
buildAndApply(joinCriteria) {
168+
applyToDsl {
169+
rightJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
170+
}
148171
}
149172

150173
fun rightJoin(
151174
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
152175
joinCriteria: JoinReceiver
153-
) =
154-
applyJoin(joinCriteria) {
176+
): B =
177+
buildAndApply(joinCriteria) {
155178
val builder = KotlinQualifiedSubQueryBuilder().apply(subQuery)
156-
getDsl().rightJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
179+
applyToDsl {
180+
rightJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
181+
}
157182
}
158183

159-
private fun applyJoin(joinCriteria: JoinReceiver, block: (JoinCollector) -> Unit) =
160-
applySelf {
161-
JoinCollector().apply(joinCriteria).apply(block)
162-
}
184+
private fun buildAndApply(joinCriteria: JoinReceiver, block: (JoinCollector) -> B): B {
185+
return block(JoinCollector().apply(joinCriteria))
186+
}
163187

164-
protected abstract fun getDsl(): AbstractQueryExpressionDSL<W, T, SelectModel>
188+
private fun applyToDsl(block: D.() -> Unit): B {
189+
getDsl().apply(block)
190+
return self()
191+
}
165192
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +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>,
27-
CountDSL<SelectModel>.CountWhereBuilder, KotlinCountBuilder>(),
26+
KotlinBaseJoiningBuilder<CountDSL<SelectModel>, KotlinCountBuilder>(),
2827
Buildable<SelectModel> {
2928

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

3736
override fun build(): SelectModel = getDsl().build()
3837

39-
override fun getWhere(): CountDSL<SelectModel>.CountWhereBuilder = getDsl().where()
40-
4138
override fun self() = this
4239

4340
override fun getDsl(): CountDSL<SelectModel> {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import org.mybatis.dynamic.sql.util.Buildable
2222
typealias DeleteCompleter = KotlinDeleteBuilder.() -> Unit
2323

2424
class KotlinDeleteBuilder(private val dsl: DeleteDSL<DeleteModel>) :
25-
KotlinBaseBuilder<DeleteDSL<DeleteModel>.DeleteWhereBuilder, KotlinDeleteBuilder>(), Buildable<DeleteModel> {
25+
KotlinBaseBuilder<DeleteDSL<DeleteModel>, KotlinDeleteBuilder>(), Buildable<DeleteModel> {
2626

2727
override fun build(): DeleteModel = dsl.build()
2828

29-
override fun getWhere(): DeleteDSL<DeleteModel>.DeleteWhereBuilder = dsl.where()
29+
override fun getDsl() = dsl
3030

3131
override fun self() = this
3232
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ typealias SelectCompleter = KotlinSelectBuilder.() -> Unit
2626

2727
@Suppress("TooManyFunctions")
2828
class KotlinSelectBuilder(private val fromGatherer: QueryExpressionDSL.FromGatherer<SelectModel>) :
29-
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>,
30-
QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder,
31-
KotlinSelectBuilder>(), Buildable<SelectModel> {
29+
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>, KotlinSelectBuilder>(), Buildable<SelectModel> {
3230

3331
private lateinit var dsl: QueryExpressionDSL<SelectModel>
3432

@@ -87,8 +85,6 @@ class KotlinSelectBuilder(private val fromGatherer: QueryExpressionDSL.FromGathe
8785

8886
override fun self(): KotlinSelectBuilder = this
8987

90-
override fun getWhere(): QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder = getDsl().where()
91-
9288
override fun getDsl(): QueryExpressionDSL<SelectModel> {
9389
try {
9490
return dsl

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ import org.mybatis.dynamic.sql.util.Buildable
2424
typealias UpdateCompleter = KotlinUpdateBuilder.() -> Unit
2525

2626
class KotlinUpdateBuilder(private val dsl: UpdateDSL<UpdateModel>) :
27-
KotlinBaseBuilder<UpdateDSL<UpdateModel>.UpdateWhereBuilder, KotlinUpdateBuilder>(), Buildable<UpdateModel> {
27+
KotlinBaseBuilder<UpdateDSL<UpdateModel>, KotlinUpdateBuilder>(), Buildable<UpdateModel> {
2828

2929
fun <T> set(column: SqlColumn<T>) = KotlinSetClauseFinisher(column)
3030

3131
override fun build(): UpdateModel = dsl.build()
3232

33-
override fun getWhere(): UpdateDSL<UpdateModel>.UpdateWhereBuilder = dsl.where()
33+
override fun getDsl() = dsl
3434

3535
override fun self() = this
3636

0 commit comments

Comments
 (0)