Skip to content

[Kotlin] General DSL Cleanup and Simplification #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 7, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
but is deprecated. It will be removed in version 1.5.0 of the library. Documentation for the new DSL is here:
https://github.com/mybatis/mybatis-dynamic-sql/blob/master/src/site/markdown/docs/kotlinWhereClauses.md
([#442](https://github.com/mybatis/mybatis-dynamic-sql/pull/442))
5. General cleanup of the Kotlin DSL. The Kotlin DSL functions are now mostly Unit functions. This should have
no impact on most users and is source code compatible with prior versions of the library when the library was used
as described in the documentation. This change greatly simplifies the type hierarchy of the Kotlin builders.
([#446](https://github.com/mybatis/mybatis-dynamic-sql/pull/446))

## Release 1.3.1 - December 18, 2021

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ class CriteriaCollector {
column: BindableColumn<T>,
condition: VisitableCondition<T>,
criteriaReceiver: CriteriaReceiver = {}
): CriteriaCollector =
): Unit =
addCriteriaGroup("and", buildCriterion(column, condition), criteriaReceiver)

fun and(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver = {}): CriteriaCollector =
fun and(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver = {}): Unit =
addCriteriaGroup("and", buildCriterion(existsPredicate), criteriaReceiver)

fun <T> or(
column: BindableColumn<T>,
condition: VisitableCondition<T>,
criteriaReceiver: CriteriaReceiver = {}
): CriteriaCollector =
): Unit =
addCriteriaGroup("or", buildCriterion(column, condition), criteriaReceiver)

fun or(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver = {}): CriteriaCollector =
fun or(existsPredicate: ExistsPredicate, criteriaReceiver: CriteriaReceiver = {}): Unit =
addCriteriaGroup("or", buildCriterion(existsPredicate), criteriaReceiver)

private fun <T> buildCriterion(
Expand All @@ -64,14 +64,13 @@ class CriteriaCollector {
connector: String,
initialCriterion: SqlCriterion,
criteriaReceiver: CriteriaReceiver
) =
apply {
criteria.add(
AndOrCriteriaGroup.Builder()
.withInitialCriterion(initialCriterion)
.withSubCriteria(CriteriaCollector().apply(criteriaReceiver).criteria)
.withConnector(connector)
.build()
)
}
) {
criteria.add(
AndOrCriteriaGroup.Builder()
.withInitialCriterion(initialCriterion)
.withSubCriteria(CriteriaCollector().apply(criteriaReceiver).criteria)
.withConnector(connector)
.build()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2016-2022 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 Down Expand Up @@ -27,23 +27,21 @@ class JoinCollector {
val andJoinCriteria = mutableListOf<JoinCriterion>()
private lateinit var internalOnCriterion: JoinCriterion

fun on(column: BasicColumn, condition: JoinCondition): JoinCollector =
apply {
internalOnCriterion = JoinCriterion.Builder()
.withConnector("on")
fun on(column: BasicColumn, condition: JoinCondition) {
internalOnCriterion = JoinCriterion.Builder()
.withConnector("on")
.withJoinColumn(column)
.withJoinCondition(condition)
.build()
}

fun and(column: BasicColumn, condition: JoinCondition) {
andJoinCriteria.add(
JoinCriterion.Builder()
.withConnector("and")
.withJoinColumn(column)
.withJoinCondition(condition)
.build()
}

fun and(column: BasicColumn, condition: JoinCondition): JoinCollector =
apply {
andJoinCriteria.add(
JoinCriterion.Builder()
.withConnector("and")
.withJoinColumn(column)
.withJoinCondition(condition)
.build()
)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.mybatis.dynamic.sql.where.AbstractWhereSupport
@DslMarker
annotation class MyBatisDslMarker

typealias WhereApplier = KotlinBaseBuilder<*, *>.() -> Unit
typealias WhereApplier = KotlinBaseBuilder<*>.() -> Unit

fun WhereApplier.andThen(after: WhereApplier): WhereApplier = {
invoke(this)
Expand All @@ -37,7 +37,7 @@ fun WhereApplier.andThen(after: WhereApplier): WhereApplier = {

@MyBatisDslMarker
@Suppress("TooManyFunctions")
abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuilder<D, B>> {
abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>> {
fun where(criteria: GroupingCriteriaReceiver): Unit =
with(GroupingCriteriaCollector().apply(criteria)) {
this@KotlinBaseBuilder.getDsl().where(initialCriterion, subCriteria)
Expand All @@ -53,21 +53,18 @@ abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuil
this@KotlinBaseBuilder.getDsl().where().or(initialCriterion, subCriteria)
}

fun applyWhere(whereApplier: WhereApplier): B =
self().apply {
whereApplier.invoke(this)
}
fun applyWhere(whereApplier: WhereApplier) = whereApplier.invoke(this)

@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
"into a lambda and rewriting the condition to use an infix function.")
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): Unit =
applyToWhere {
where(column, condition)
}

@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
"inside the lambda and rewriting the condition to use an infix function.")
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): Unit =
applyToWhere(subCriteria) { sc ->
where(column, condition, sc)
}
Expand All @@ -76,28 +73,28 @@ abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuil
message = "Deprecated in favor of the new where clause DSL.",
replaceWith = ReplaceWith("where { existsPredicate }")
)
fun where(existsPredicate: ExistsPredicate): B =
fun where(existsPredicate: ExistsPredicate): Unit =
applyToWhere {
where(existsPredicate)
}

@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the exists expression " +
"into the lambda.")
fun where(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): B =
fun where(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): Unit =
applyToWhere(subCriteria) { sc ->
where(existsPredicate, sc)
}

@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
"into a lambda and rewriting the condition to use an infix function.")
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>): Unit =
applyToWhere {
and(column, condition)
}

@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
"inside the lambda and rewriting the condition to use an infix function.")
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): Unit =
applyToWhere(subCriteria) { sc ->
and(column, condition, sc)
}
Expand All @@ -106,28 +103,28 @@ abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuil
message = "Deprecated in favor of the new where clause DSL.",
replaceWith = ReplaceWith("and { existsPredicate }")
)
fun and(existsPredicate: ExistsPredicate): B =
fun and(existsPredicate: ExistsPredicate): Unit =
applyToWhere {
and(existsPredicate)
}

@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the exists expression " +
"into the lambda.")
fun and(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): B =
fun and(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): Unit =
applyToWhere(subCriteria) { sc ->
and(existsPredicate, sc)
}

@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
"into a lambda and rewriting the condition to use an infix function.")
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>): B =
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>): Unit =
applyToWhere {
or(column, condition)
}

@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the column and condition " +
"inside the lambda and rewriting the condition to use an infix function.")
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): B =
fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, subCriteria: CriteriaReceiver): Unit =
applyToWhere(subCriteria) { sc ->
or(column, condition, sc)
}
Expand All @@ -136,125 +133,118 @@ abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>, B : KotlinBaseBuil
message = "Deprecated in favor of the new where clause DSL.",
replaceWith = ReplaceWith("or { existsPredicate }")
)
fun or(existsPredicate: ExistsPredicate): B =
fun or(existsPredicate: ExistsPredicate): Unit =
applyToWhere {
or(existsPredicate)
}

@Deprecated("Deprecated in favor of the new where clause DSL. Update by moving the exists expression " +
"into the lambda.")
fun or(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): B =
fun or(existsPredicate: ExistsPredicate, subCriteria: CriteriaReceiver): Unit =
applyToWhere(subCriteria) { sc ->
or(existsPredicate, sc)
}

fun allRows(): B = self()
fun allRows() {}

private fun applyToWhere(block: AbstractWhereDSL<*>.() -> Unit): B {
private fun applyToWhere(block: AbstractWhereDSL<*>.() -> Unit) {
getDsl().where().apply(block)
return self()
}

private fun applyToWhere(
subCriteria: CriteriaReceiver,
block: AbstractWhereDSL<*>.(List<AndOrCriteriaGroup>) -> Unit
): B {
) {
getDsl().where().block(CriteriaCollector().apply(subCriteria).criteria)
return self()
}

protected abstract fun self(): B

protected abstract fun getDsl(): D
}

@Suppress("TooManyFunctions")
abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>, B : KotlinBaseJoiningBuilder<D, B>> :
KotlinBaseBuilder<D, B>() {
abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>> : KotlinBaseBuilder<D>() {

fun join(table: SqlTable, joinCriteria: JoinReceiver): B =
fun join(table: SqlTable, joinCriteria: JoinReceiver): Unit =
applyToDsl(joinCriteria) { jc ->
join(table, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
applyToDsl(joinCriteria) { jc ->
join(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun join(
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
joinCriteria: JoinReceiver
): B =
): Unit =
applyToDsl(subQuery, joinCriteria) { sq, jc ->
join(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver): Unit =
applyToDsl(joinCriteria) { jc ->
fullJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
applyToDsl(joinCriteria) { jc ->
fullJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun fullJoin(
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
joinCriteria: JoinReceiver
): B =
): Unit =
applyToDsl(subQuery, joinCriteria) { sq, jc ->
fullJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver): Unit =
applyToDsl(joinCriteria) { jc ->
leftJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
applyToDsl(joinCriteria) { jc ->
leftJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun leftJoin(
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
joinCriteria: JoinReceiver
): B =
): Unit =
applyToDsl(subQuery, joinCriteria) { sq, jc ->
leftJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver): B =
fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver): Unit =
applyToDsl(joinCriteria) { jc ->
rightJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): B =
fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
applyToDsl(joinCriteria) { jc ->
rightJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
}

fun rightJoin(
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
joinCriteria: JoinReceiver
): B =
): Unit =
applyToDsl(subQuery, joinCriteria) { sq, jc ->
rightJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
}

private fun applyToDsl(joinCriteria: JoinReceiver, applyJoin: D.(JoinCollector) -> Unit): B {
private fun applyToDsl(joinCriteria: JoinReceiver, applyJoin: D.(JoinCollector) -> Unit) {
getDsl().applyJoin(JoinCollector().apply(joinCriteria))
return self()
}

private fun applyToDsl(
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
joinCriteria: JoinReceiver,
applyJoin: D.(KotlinQualifiedSubQueryBuilder, JoinCollector) -> Unit
): B {
) {
getDsl().applyJoin(KotlinQualifiedSubQueryBuilder().apply(subQuery), JoinCollector().apply(joinCriteria))
return self()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2016-2022 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 @@ -23,7 +23,7 @@ import org.mybatis.dynamic.sql.util.Buildable
typealias CountCompleter = KotlinCountBuilder.() -> Unit

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

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

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

override fun self(): KotlinCountBuilder = this

override fun getDsl(): CountDSL<SelectModel> {
try {
return dsl
Expand Down
Loading