Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ If you have written your own set of functions to extend the library, you will no
- Added the ability to write a function that will change the column data type ([#197](https://github.com/mybatis/mybatis-dynamic-sql/issues/197))
- 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))
- Added convenience methods for count(column) and count(distinct column)([#221](https://github.com/mybatis/mybatis-dynamic-sql/issues/221))
- Added support for union queries in Kotlin([#187](https://github.com/mybatis/mybatis-dynamic-sql/issues/187))

## Release 1.1.4 - November 23, 2019

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@ import org.mybatis.dynamic.sql.VisitableCondition

typealias CriteriaReceiver = CriteriaCollector.() -> CriteriaCollector

@MyBatisDslMarker
class CriteriaCollector {
val criteria = mutableListOf<SqlCriterion<*>>()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@ import org.mybatis.dynamic.sql.select.join.JoinCriterion

typealias JoinReceiver = JoinCollector.() -> JoinCollector

@MyBatisDslMarker
class JoinCollector {
val onJoinCriterion: JoinCriterion by lazy { internalOnCriterion }
val andJoinCriteria = mutableListOf<JoinCriterion>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,10 +20,12 @@ import org.mybatis.dynamic.sql.SqlTable
import org.mybatis.dynamic.sql.VisitableCondition
import org.mybatis.dynamic.sql.select.AbstractQueryExpressionDSL
import org.mybatis.dynamic.sql.select.SelectModel
import org.mybatis.dynamic.sql.util.Buildable
import org.mybatis.dynamic.sql.where.AbstractWhereDSL

abstract class KotlinBaseBuilder<M, W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<M, W, B>> : Buildable<M> {
@DslMarker annotation class MyBatisDslMarker

@MyBatisDslMarker
abstract class KotlinBaseBuilder<W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<W, B>> {
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
applySelf {
getWhere().where(column, condition)
Expand Down Expand Up @@ -69,7 +71,7 @@ abstract class KotlinBaseBuilder<M, W : AbstractWhereDSL<W>, B : KotlinBaseBuild

abstract class KotlinBaseJoiningBuilder<T : AbstractQueryExpressionDSL<T, SelectModel>, W : AbstractWhereDSL<W>, B : KotlinBaseJoiningBuilder<T, W, B>>(
private val dsl: AbstractQueryExpressionDSL<T, SelectModel>
) : KotlinBaseBuilder<SelectModel, W, B>() {
) : KotlinBaseBuilder<W, B>() {

fun join(table: SqlTable, receiver: JoinReceiver): B =
applySelf {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@ import org.mybatis.dynamic.sql.util.Buildable
typealias CountCompleter = KotlinCountBuilder.() -> Buildable<SelectModel>

class KotlinCountBuilder(private val dsl: CountDSL<SelectModel>) :
KotlinBaseJoiningBuilder<CountDSL<SelectModel>, CountDSL<SelectModel>.CountWhereBuilder, KotlinCountBuilder>(dsl) {
KotlinBaseJoiningBuilder<CountDSL<SelectModel>, CountDSL<SelectModel>.CountWhereBuilder, KotlinCountBuilder>(dsl), Buildable<SelectModel> {

fun allRows() = this

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@ import org.mybatis.dynamic.sql.util.Buildable
typealias DeleteCompleter = KotlinDeleteBuilder.() -> Buildable<DeleteModel>

class KotlinDeleteBuilder(private val dsl: DeleteDSL<DeleteModel>) :
KotlinBaseBuilder<DeleteModel, DeleteDSL<DeleteModel>.DeleteWhereBuilder, KotlinDeleteBuilder>() {
KotlinBaseBuilder<DeleteDSL<DeleteModel>.DeleteWhereBuilder, KotlinDeleteBuilder>(), Buildable<DeleteModel> {

fun allRows() = this

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@ import org.mybatis.dynamic.sql.util.Buildable
typealias SelectCompleter = KotlinQueryBuilder.() -> Buildable<SelectModel>

class KotlinQueryBuilder(private val dsl: QueryExpressionDSL<SelectModel>) :
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>, QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder, KotlinQueryBuilder>(dsl) {
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>, QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder, KotlinQueryBuilder>(dsl), Buildable<SelectModel> {

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

fun allRows() = this

fun union(union: KotlinUnionBuilder.() -> Unit) =
apply {
union(KotlinUnionBuilder(dsl.union()))
}

fun unionAll(unionAll: KotlinUnionBuilder.() -> Unit) =
apply {
unionAll(KotlinUnionBuilder(dsl.unionAll()))
}

override fun build(): SelectModel = dsl.build()

override fun getWhere(): QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder = dsl.where()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util.kotlin

import org.mybatis.dynamic.sql.BasicColumn
import org.mybatis.dynamic.sql.SqlTable
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
import org.mybatis.dynamic.sql.select.SelectModel

@MyBatisDslMarker
class KotlinUnionBuilder(private val unionBuilder: QueryExpressionDSL<SelectModel>.UnionBuilder) {
fun select(vararg selectList: BasicColumn) =
select(selectList.toList())

fun select(selectList: List<BasicColumn>) =
KotlinUnionFromGatherer(unionBuilder.select(selectList))

fun selectDistinct(vararg selectList: BasicColumn) =
selectDistinct(selectList.toList())

fun selectDistinct(selectList: List<BasicColumn>) =
KotlinUnionFromGatherer(unionBuilder.selectDistinct(selectList))
}

class KotlinUnionFromGatherer(private val fromGatherer: QueryExpressionDSL.FromGatherer<SelectModel>) {
fun from(table: SqlTable, enhance: KotlinUnionQueryBuilder.() -> Unit) =
enhance(KotlinUnionQueryBuilder(fromGatherer.from(table)))

fun from(table: SqlTable, alias: String, enhance: KotlinUnionQueryBuilder.() -> Unit) =
enhance(KotlinUnionQueryBuilder(fromGatherer.from(table, alias)))
}

class KotlinUnionQueryBuilder(private val unionDsl: QueryExpressionDSL<SelectModel>) :
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>, QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder,
KotlinUnionQueryBuilder>(unionDsl) {
fun allRows() = this

override fun self() = this

override fun getWhere(): QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder = unionDsl.where()
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typealias MultiRowInsertCompleter<T> = MultiRowInsertDSL<T>.() -> MultiRowInsert
typealias UpdateCompleter = KotlinUpdateBuilder.() -> Buildable<UpdateModel>

class KotlinUpdateBuilder(private val dsl: UpdateDSL<UpdateModel>) :
KotlinBaseBuilder<UpdateModel, UpdateDSL<UpdateModel>.UpdateWhereBuilder, KotlinUpdateBuilder>() {
KotlinBaseBuilder<UpdateDSL<UpdateModel>.UpdateWhereBuilder, KotlinUpdateBuilder>(), Buildable<UpdateModel> {

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

Expand Down
Loading