Skip to content

Commit f8ad7be

Browse files
authored
Merge pull request #222 from jeffgbutler/gh-187
Add support for union queries in Kotlin
2 parents aba2e6e + ec66562 commit f8ad7be

File tree

10 files changed

+375
-13
lines changed

10 files changed

+375
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ If you have written your own set of functions to extend the library, you will no
1919
- Added the ability to write a function that will change the column data type ([#197](https://github.com/mybatis/mybatis-dynamic-sql/issues/197))
2020
- Added the `applyOperator` function to make it easy to use non-standard database operators in expressions ([#220](https://github.com/mybatis/mybatis-dynamic-sql/issues/220))
2121
- Added convenience methods for count(column) and count(distinct column)([#221](https://github.com/mybatis/mybatis-dynamic-sql/issues/221))
22+
- Added support for union queries in Kotlin([#187](https://github.com/mybatis/mybatis-dynamic-sql/issues/187))
2223

2324
## Release 1.1.4 - November 23, 2019
2425

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -21,6 +21,7 @@ import org.mybatis.dynamic.sql.VisitableCondition
2121

2222
typealias CriteriaReceiver = CriteriaCollector.() -> CriteriaCollector
2323

24+
@MyBatisDslMarker
2425
class CriteriaCollector {
2526
val criteria = mutableListOf<SqlCriterion<*>>()
2627

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -21,6 +21,7 @@ import org.mybatis.dynamic.sql.select.join.JoinCriterion
2121

2222
typealias JoinReceiver = JoinCollector.() -> JoinCollector
2323

24+
@MyBatisDslMarker
2425
class JoinCollector {
2526
val onJoinCriterion: JoinCriterion by lazy { internalOnCriterion }
2627
val andJoinCriteria = mutableListOf<JoinCriterion>()

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -20,10 +20,12 @@ import org.mybatis.dynamic.sql.SqlTable
2020
import org.mybatis.dynamic.sql.VisitableCondition
2121
import org.mybatis.dynamic.sql.select.AbstractQueryExpressionDSL
2222
import org.mybatis.dynamic.sql.select.SelectModel
23-
import org.mybatis.dynamic.sql.util.Buildable
2423
import org.mybatis.dynamic.sql.where.AbstractWhereDSL
2524

26-
abstract class KotlinBaseBuilder<M, W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<M, W, B>> : Buildable<M> {
25+
@DslMarker annotation class MyBatisDslMarker
26+
27+
@MyBatisDslMarker
28+
abstract class KotlinBaseBuilder<W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<W, B>> {
2729
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
2830
applySelf {
2931
getWhere().where(column, condition)
@@ -69,7 +71,7 @@ abstract class KotlinBaseBuilder<M, W : AbstractWhereDSL<W>, B : KotlinBaseBuild
6971

7072
abstract class KotlinBaseJoiningBuilder<T : AbstractQueryExpressionDSL<T, SelectModel>, W : AbstractWhereDSL<W>, B : KotlinBaseJoiningBuilder<T, W, B>>(
7173
private val dsl: AbstractQueryExpressionDSL<T, SelectModel>
72-
) : KotlinBaseBuilder<SelectModel, W, B>() {
74+
) : KotlinBaseBuilder<W, B>() {
7375

7476
fun join(table: SqlTable, receiver: JoinReceiver): B =
7577
applySelf {

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

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

2424
class KotlinCountBuilder(private val dsl: CountDSL<SelectModel>) :
25-
KotlinBaseJoiningBuilder<CountDSL<SelectModel>, CountDSL<SelectModel>.CountWhereBuilder, KotlinCountBuilder>(dsl) {
25+
KotlinBaseJoiningBuilder<CountDSL<SelectModel>, CountDSL<SelectModel>.CountWhereBuilder, KotlinCountBuilder>(dsl), Buildable<SelectModel> {
2626

2727
fun allRows() = this
2828

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
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -22,7 +22,7 @@ import org.mybatis.dynamic.sql.util.Buildable
2222
typealias DeleteCompleter = KotlinDeleteBuilder.() -> Buildable<DeleteModel>
2323

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

2727
fun allRows() = this
2828

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -24,7 +24,7 @@ import org.mybatis.dynamic.sql.util.Buildable
2424
typealias SelectCompleter = KotlinQueryBuilder.() -> Buildable<SelectModel>
2525

2626
class KotlinQueryBuilder(private val dsl: QueryExpressionDSL<SelectModel>) :
27-
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>, QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder, KotlinQueryBuilder>(dsl) {
27+
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>, QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder, KotlinQueryBuilder>(dsl), Buildable<SelectModel> {
2828

2929
fun groupBy(vararg columns: BasicColumn) =
3030
apply {
@@ -50,6 +50,16 @@ class KotlinQueryBuilder(private val dsl: QueryExpressionDSL<SelectModel>) :
5050

5151
fun allRows() = this
5252

53+
fun union(union: KotlinUnionBuilder.() -> Unit) =
54+
apply {
55+
union(KotlinUnionBuilder(dsl.union()))
56+
}
57+
58+
fun unionAll(unionAll: KotlinUnionBuilder.() -> Unit) =
59+
apply {
60+
unionAll(KotlinUnionBuilder(dsl.unionAll()))
61+
}
62+
5363
override fun build(): SelectModel = dsl.build()
5464

5565
override fun getWhere(): QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder = dsl.where()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.SqlTable
20+
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
21+
import org.mybatis.dynamic.sql.select.SelectModel
22+
23+
@MyBatisDslMarker
24+
class KotlinUnionBuilder(private val unionBuilder: QueryExpressionDSL<SelectModel>.UnionBuilder) {
25+
fun select(vararg selectList: BasicColumn) =
26+
select(selectList.toList())
27+
28+
fun select(selectList: List<BasicColumn>) =
29+
KotlinUnionFromGatherer(unionBuilder.select(selectList))
30+
31+
fun selectDistinct(vararg selectList: BasicColumn) =
32+
selectDistinct(selectList.toList())
33+
34+
fun selectDistinct(selectList: List<BasicColumn>) =
35+
KotlinUnionFromGatherer(unionBuilder.selectDistinct(selectList))
36+
}
37+
38+
class KotlinUnionFromGatherer(private val fromGatherer: QueryExpressionDSL.FromGatherer<SelectModel>) {
39+
fun from(table: SqlTable, enhance: KotlinUnionQueryBuilder.() -> Unit) =
40+
enhance(KotlinUnionQueryBuilder(fromGatherer.from(table)))
41+
42+
fun from(table: SqlTable, alias: String, enhance: KotlinUnionQueryBuilder.() -> Unit) =
43+
enhance(KotlinUnionQueryBuilder(fromGatherer.from(table, alias)))
44+
}
45+
46+
class KotlinUnionQueryBuilder(private val unionDsl: QueryExpressionDSL<SelectModel>) :
47+
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>, QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder,
48+
KotlinUnionQueryBuilder>(unionDsl) {
49+
fun allRows() = this
50+
51+
override fun self() = this
52+
53+
override fun getWhere(): QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder = unionDsl.where()
54+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typealias MultiRowInsertCompleter<T> = MultiRowInsertDSL<T>.() -> MultiRowInsert
3333
typealias UpdateCompleter = KotlinUpdateBuilder.() -> Buildable<UpdateModel>
3434

3535
class KotlinUpdateBuilder(private val dsl: UpdateDSL<UpdateModel>) :
36-
KotlinBaseBuilder<UpdateModel, UpdateDSL<UpdateModel>.UpdateWhereBuilder, KotlinUpdateBuilder>() {
36+
KotlinBaseBuilder<UpdateDSL<UpdateModel>.UpdateWhereBuilder, KotlinUpdateBuilder>(), Buildable<UpdateModel> {
3737

3838
fun <T> set(column: SqlColumn<T>): UpdateDSL<UpdateModel>.SetClauseFinisher<T> = dsl.set(column)
3939

0 commit comments

Comments
 (0)