Skip to content

Commit 6683f49

Browse files
authored
Merge pull request #291 from jeffgbutler/remove-extensions
Kotlin DSL Improvements
2 parents 87f5e94 + 899a9cd commit 6683f49

File tree

17 files changed

+463
-273
lines changed

17 files changed

+463
-273
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ For example, a typical search can be coded with a query like this (the following
2020

2121
```kotlin
2222
fun search(id: String?, firstName: String?, lastName: String?) =
23-
select(Customer.id, Customer.firstName, Customer.lastName).from(Customer) {
23+
select(Customer.id, Customer.firstName, Customer.lastName) {
24+
from(Customer)
2425
where(Customer.active, isEqualTo(true))
2526
and(Customer.id, isEqualToWhenPresent(id).then{ it?.padStart(5, '0') })
2627
and(Customer.firstName, isLikeCaseInsensitiveWhenPresent(firstName)

src/main/java/org/mybatis/dynamic/sql/insert/InsertSelectDSL.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
import java.util.List;
2020
import java.util.Objects;
2121

22+
import org.jetbrains.annotations.NotNull;
2223
import org.mybatis.dynamic.sql.SqlColumn;
2324
import org.mybatis.dynamic.sql.SqlTable;
2425
import org.mybatis.dynamic.sql.select.SelectModel;
2526
import org.mybatis.dynamic.sql.util.Buildable;
2627

27-
public class InsertSelectDSL {
28+
public class InsertSelectDSL implements Buildable<InsertSelectModel> {
2829

2930
private final SqlTable table;
3031
private final InsertColumnListModel columnList;
@@ -42,6 +43,8 @@ private InsertSelectDSL(SqlTable table, SelectModel selectModel) {
4243
this.columnList = null;
4344
}
4445

46+
@NotNull
47+
@Override
4548
public InsertSelectModel build() {
4649
return InsertSelectModel.withTable(table)
4750
.withColumnList(columnList)

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

Lines changed: 0 additions & 68 deletions
This file was deleted.

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import org.mybatis.dynamic.sql.where.AbstractWhereDSL
2626
@DslMarker
2727
annotation class MyBatisDslMarker
2828

29+
typealias WhereApplier = AbstractWhereDSL<*>.() -> Unit
30+
2931
@MyBatisDslMarker
3032
@Suppress("TooManyFunctions")
3133
abstract class KotlinBaseBuilder<W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<W, B>> {
@@ -34,9 +36,9 @@ abstract class KotlinBaseBuilder<W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<
3436
getWhere().where(column, condition)
3537
}
3638

37-
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): B =
39+
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
3840
applySelf {
39-
getWhere().where(column, condition, collect)
41+
getWhere().where(column, condition, subCriteria(CriteriaCollector()).criteria)
4042
}
4143

4244
fun applyWhere(whereApplier: WhereApplier): B =
@@ -49,19 +51,19 @@ abstract class KotlinBaseBuilder<W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<
4951
getWhere().and(column, condition)
5052
}
5153

52-
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): B =
54+
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
5355
applySelf {
54-
getWhere().and(column, condition, collect)
56+
getWhere().and(column, condition, subCriteria(CriteriaCollector()).criteria)
5557
}
5658

5759
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
5860
applySelf {
5961
getWhere().or(column, condition)
6062
}
6163

62-
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): B =
64+
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
6365
applySelf {
64-
getWhere().or(column, condition, collect)
66+
getWhere().or(column, condition, subCriteria(CriteriaCollector()).criteria)
6567
}
6668

6769
fun allRows() = self()
@@ -77,44 +79,49 @@ abstract class KotlinBaseBuilder<W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<
7779
abstract class KotlinBaseJoiningBuilder<T : AbstractQueryExpressionDSL<T, SelectModel>, W : AbstractWhereDSL<W>,
7880
B : KotlinBaseJoiningBuilder<T, W, B>> : KotlinBaseBuilder<W, B>() {
7981

80-
fun join(table: SqlTable, receiver: JoinReceiver): B =
81-
applySelf {
82-
getDsl().join(table, receiver)
82+
fun join(table: SqlTable, joinCriteria: JoinReceiver) =
83+
applyJoin(joinCriteria) {
84+
getDsl().join(table, it.onJoinCriterion, it.andJoinCriteria)
8385
}
8486

85-
fun join(table: SqlTable, alias: String, receiver: JoinReceiver): B =
86-
applySelf {
87-
getDsl().join(table, alias, receiver)
87+
fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
88+
applyJoin(joinCriteria) {
89+
getDsl().join(table, alias, it.onJoinCriterion, it.andJoinCriteria)
8890
}
8991

90-
fun fullJoin(table: SqlTable, receiver: JoinReceiver): B =
91-
applySelf {
92-
getDsl().fullJoin(table, receiver)
92+
fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver) =
93+
applyJoin(joinCriteria) {
94+
getDsl().fullJoin(table, it.onJoinCriterion, it.andJoinCriteria)
9395
}
9496

95-
fun fullJoin(table: SqlTable, alias: String, receiver: JoinReceiver): B =
96-
applySelf {
97-
getDsl().fullJoin(table, alias, receiver)
97+
fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
98+
applyJoin(joinCriteria) {
99+
getDsl().fullJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
98100
}
99101

100-
fun leftJoin(table: SqlTable, receiver: JoinReceiver): B =
101-
applySelf {
102-
getDsl().leftJoin(table, receiver)
102+
fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver) =
103+
applyJoin(joinCriteria) {
104+
getDsl().leftJoin(table, it.onJoinCriterion, it.andJoinCriteria)
103105
}
104106

105-
fun leftJoin(table: SqlTable, alias: String, receiver: JoinReceiver): B =
106-
applySelf {
107-
getDsl().leftJoin(table, alias, receiver)
107+
fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
108+
applyJoin(joinCriteria) {
109+
getDsl().leftJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
108110
}
109111

110-
fun rightJoin(table: SqlTable, receiver: JoinReceiver): B =
111-
applySelf {
112-
getDsl().rightJoin(table, receiver)
112+
fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver) =
113+
applyJoin(joinCriteria) {
114+
getDsl().rightJoin(table, it.onJoinCriterion, it.andJoinCriteria)
115+
}
116+
117+
fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver) =
118+
applyJoin(joinCriteria) {
119+
getDsl().rightJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
113120
}
114121

115-
fun rightJoin(table: SqlTable, alias: String, receiver: JoinReceiver): B =
122+
private fun applyJoin(joinCriteria: JoinReceiver, block: (JoinCollector) -> Unit) =
116123
applySelf {
117-
getDsl().rightJoin(table, alias, receiver)
124+
joinCriteria(JoinCollector()).also(block)
118125
}
119126

120127
protected abstract fun getDsl(): AbstractQueryExpressionDSL<T, SelectModel>

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,11 @@ import org.mybatis.dynamic.sql.select.CountDSL
2020
import org.mybatis.dynamic.sql.select.SelectModel
2121
import org.mybatis.dynamic.sql.util.Buildable
2222

23-
typealias CountCompleter = KotlinCountBuilder.() -> Buildable<SelectModel>
23+
typealias CountCompleter = KotlinCountBuilder.() -> KotlinCountBuilder
2424

25-
typealias CountColumnCompleter = KotlinCountColumnBuilder.() -> Buildable<SelectModel>
26-
27-
class KotlinCountBuilder(private val dsl: CountDSL<SelectModel>) :
28-
KotlinBaseJoiningBuilder<CountDSL<SelectModel>, CountDSL<SelectModel>.CountWhereBuilder, KotlinCountBuilder>(),
29-
Buildable<SelectModel> {
30-
31-
override fun build(): SelectModel = getDsl().build()
32-
33-
override fun getWhere(): CountDSL<SelectModel>.CountWhereBuilder = getDsl().where()
34-
35-
override fun self() = this
36-
37-
override fun getDsl() = dsl
38-
}
39-
40-
class KotlinCountColumnBuilder(private val fromGatherer: CountDSL.FromGatherer<SelectModel>) :
25+
class KotlinCountBuilder(private val fromGatherer: CountDSL.FromGatherer<SelectModel>) :
4126
KotlinBaseJoiningBuilder<CountDSL<SelectModel>,
42-
CountDSL<SelectModel>.CountWhereBuilder, KotlinCountColumnBuilder>(),
27+
CountDSL<SelectModel>.CountWhereBuilder, KotlinCountBuilder>(),
4328
Buildable<SelectModel> {
4429

4530
private lateinit var dsl: CountDSL<SelectModel>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import org.mybatis.dynamic.sql.delete.DeleteDSL
1919
import org.mybatis.dynamic.sql.delete.DeleteModel
2020
import org.mybatis.dynamic.sql.util.Buildable
2121

22-
typealias DeleteCompleter = KotlinDeleteBuilder.() -> Buildable<DeleteModel>
22+
typealias DeleteCompleter = KotlinDeleteBuilder.() -> KotlinDeleteBuilder
2323

2424
class KotlinDeleteBuilder(private val dsl: DeleteDSL<DeleteModel>) :
2525
KotlinBaseBuilder<DeleteDSL<DeleteModel>.DeleteWhereBuilder, KotlinDeleteBuilder>(), Buildable<DeleteModel> {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2016-2020 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.util.kotlin
17+
18+
import org.mybatis.dynamic.sql.BasicColumn
19+
import org.mybatis.dynamic.sql.SqlBuilder
20+
import org.mybatis.dynamic.sql.SqlTable
21+
import org.mybatis.dynamic.sql.insert.BatchInsertDSL
22+
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL
23+
import org.mybatis.dynamic.sql.insert.InsertDSL
24+
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
25+
26+
/**
27+
* Collection of functions that will create various DSL models in a Kotlin native way.
28+
* They are wrapped in an object as a namespacing technique to avoid collision
29+
* with the similar functions that build providers for the different rendering
30+
* strategies.
31+
*/
32+
object KotlinModelBuilderFunctions {
33+
fun count(column: BasicColumn, completer: CountCompleter) =
34+
completer(KotlinCountBuilder(SqlBuilder.countColumn(column))).build()
35+
36+
fun countDistinct(column: BasicColumn, completer: CountCompleter) =
37+
completer(KotlinCountBuilder(SqlBuilder.countDistinctColumn(column))).build()
38+
39+
fun countFrom(table: SqlTable, completer: CountCompleter) =
40+
with(KotlinCountBuilder(SqlBuilder.countColumn(SqlBuilder.constant<Long>("*")))) {
41+
completer(from(table)).build()
42+
}
43+
44+
fun deleteFrom(table: SqlTable, completer: DeleteCompleter) =
45+
completer(KotlinDeleteBuilder(SqlBuilder.deleteFrom(table))).build()
46+
47+
fun insertInto(table: SqlTable, completer: GeneralInsertCompleter) =
48+
completer(GeneralInsertDSL.insertInto(table)).build()
49+
50+
fun insertSelect(table: SqlTable, completer: InsertSelectCompleter) =
51+
with(completer(KotlinInsertSelectSubQueryBuilder())) {
52+
SqlBuilder.insertInto(table)
53+
.withColumnList(columnList)
54+
.withSelectStatement(selectBuilder)
55+
.build()
56+
}
57+
58+
fun <T> BatchInsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: BatchInsertCompleter<T>) =
59+
completer(into(table)).build()
60+
61+
fun <T> InsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: InsertCompleter<T>) =
62+
completer(into(table)).build()
63+
64+
fun <T> MultiRowInsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: MultiRowInsertCompleter<T>) =
65+
completer(into(table)).build()
66+
67+
fun select(vararg columns: BasicColumn, completer: SelectCompleter) =
68+
select(columns.asList(), completer)
69+
70+
fun select(columns: List<BasicColumn>, completer: SelectCompleter) =
71+
completer(KotlinSelectBuilder(SqlBuilder.select(columns))).build()
72+
73+
fun selectDistinct(vararg columns: BasicColumn, completer: SelectCompleter) =
74+
selectDistinct(columns.asList(), completer)
75+
76+
fun selectDistinct(columns: List<BasicColumn>, completer: SelectCompleter) =
77+
completer(KotlinSelectBuilder(SqlBuilder.selectDistinct(columns))).build()
78+
79+
fun update(table: SqlTable, completer: UpdateCompleter) =
80+
completer(KotlinUpdateBuilder(SqlBuilder.update(table))).build()
81+
}

0 commit comments

Comments
 (0)