Skip to content

Commit bae9343

Browse files
committed
Initial Kotlin support for Having
1 parent dac18e3 commit bae9343

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util.kotlin
1717

18+
import org.mybatis.dynamic.sql.AndOrCriteriaGroup
1819
import org.mybatis.dynamic.sql.BasicColumn
1920
import org.mybatis.dynamic.sql.SortSpecification
21+
import org.mybatis.dynamic.sql.SqlCriterion
2022
import org.mybatis.dynamic.sql.SqlTable
2123
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
2224
import org.mybatis.dynamic.sql.select.SelectModel
25+
import org.mybatis.dynamic.sql.select.SubQuery
2326
import org.mybatis.dynamic.sql.util.Buildable
2427
import org.mybatis.dynamic.sql.util.Messages
2528

@@ -29,25 +32,30 @@ typealias SelectCompleter = KotlinSelectBuilder.() -> Unit
2932
class KotlinSelectBuilder(private val fromGatherer: QueryExpressionDSL.FromGatherer<SelectModel>) :
3033
KotlinBaseJoiningBuilder<QueryExpressionDSL<SelectModel>>(), Buildable<SelectModel> {
3134

32-
private var dsl: QueryExpressionDSL<SelectModel>? = null
35+
private var dsl: KQueryExpressionDSL? = null
3336

3437
fun from(table: SqlTable) {
35-
dsl = fromGatherer.from(table)
38+
dsl = KQueryExpressionDSL(fromGatherer, table)
3639
}
3740

3841
fun from(table: SqlTable, alias: String) {
39-
dsl = fromGatherer.from(table, alias)
42+
dsl = KQueryExpressionDSL(fromGatherer, table, alias)
4043
}
4144

4245
fun from(subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit) {
4346
val builder = KotlinQualifiedSubQueryBuilder().apply(subQuery)
44-
dsl = fromGatherer.from(builder, builder.correlationName)
47+
dsl = KQueryExpressionDSL(fromGatherer, builder)
4548
}
4649

4750
fun groupBy(vararg columns: BasicColumn) {
4851
getDsl().groupBy(columns.toList())
4952
}
5053

54+
fun having(criteria: GroupingCriteriaReceiver): Unit =
55+
with(GroupingCriteriaCollector().apply(criteria)) {
56+
this@KotlinSelectBuilder.getDsl().applyHaving(initialCriterion, subCriteria)
57+
}
58+
5159
fun orderBy(vararg columns: SortSpecification) {
5260
getDsl().orderBy(columns.toList())
5361
}
@@ -72,7 +80,34 @@ class KotlinSelectBuilder(private val fromGatherer: QueryExpressionDSL.FromGathe
7280

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

75-
override fun getDsl(): QueryExpressionDSL<SelectModel> {
83+
override fun getDsl(): KQueryExpressionDSL {
7684
return dsl?: throw KInvalidSQLException(Messages.getString("ERROR.27")) //$NON-NLS-1$
7785
}
7886
}
87+
88+
/**
89+
* Extension of the QueryExpressionDSL class that provides access to protected methods in that class.
90+
* We do this especially for having support because we don't want to publicly expose a "having" method
91+
* directly in QueryExpressionDSL as it would be in an odd place for the Java DSL.
92+
*/
93+
class KQueryExpressionDSL: QueryExpressionDSL<SelectModel> {
94+
constructor(fromGatherer: FromGatherer<SelectModel>, table: SqlTable) : super(fromGatherer, table)
95+
96+
constructor(fromGatherer: FromGatherer<SelectModel>, table: SqlTable, alias: String) :
97+
super(fromGatherer, table, alias)
98+
99+
constructor(fromGatherer: FromGatherer<SelectModel>, subQuery: KotlinQualifiedSubQueryBuilder) :
100+
super(fromGatherer, buildSubQuery(subQuery))
101+
102+
internal fun applyHaving(initialCriterion: SqlCriterion?, subCriteria: List<AndOrCriteriaGroup>) {
103+
having(initialCriterion, subCriteria)
104+
}
105+
106+
companion object {
107+
fun buildSubQuery(subQuery: KotlinQualifiedSubQueryBuilder): SubQuery {
108+
return SubQuery.Builder().withSelectModel(subQuery.build())
109+
.withAlias(subQuery.correlationName)
110+
.build()
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)