15
15
*/
16
16
package org.mybatis.dynamic.sql.util.kotlin
17
17
18
+ import org.mybatis.dynamic.sql.AndOrCriteriaGroup
18
19
import org.mybatis.dynamic.sql.BasicColumn
19
20
import org.mybatis.dynamic.sql.SortSpecification
21
+ import org.mybatis.dynamic.sql.SqlCriterion
20
22
import org.mybatis.dynamic.sql.SqlTable
21
23
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
22
24
import org.mybatis.dynamic.sql.select.SelectModel
25
+ import org.mybatis.dynamic.sql.select.SubQuery
23
26
import org.mybatis.dynamic.sql.util.Buildable
24
27
import org.mybatis.dynamic.sql.util.Messages
25
28
@@ -29,25 +32,30 @@ typealias SelectCompleter = KotlinSelectBuilder.() -> Unit
29
32
class KotlinSelectBuilder (private val fromGatherer : QueryExpressionDSL .FromGatherer <SelectModel >) :
30
33
KotlinBaseJoiningBuilder <QueryExpressionDSL <SelectModel >>(), Buildable <SelectModel > {
31
34
32
- private var dsl: QueryExpressionDSL < SelectModel > ? = null
35
+ private var dsl: KQueryExpressionDSL ? = null
33
36
34
37
fun from (table : SqlTable ) {
35
- dsl = fromGatherer.from( table)
38
+ dsl = KQueryExpressionDSL (fromGatherer, table)
36
39
}
37
40
38
41
fun from (table : SqlTable , alias : String ) {
39
- dsl = fromGatherer.from( table, alias)
42
+ dsl = KQueryExpressionDSL (fromGatherer, table, alias)
40
43
}
41
44
42
45
fun from (subQuery : KotlinQualifiedSubQueryBuilder .() -> Unit ) {
43
46
val builder = KotlinQualifiedSubQueryBuilder ().apply (subQuery)
44
- dsl = fromGatherer.from(builder , builder.correlationName )
47
+ dsl = KQueryExpressionDSL (fromGatherer , builder)
45
48
}
46
49
47
50
fun groupBy (vararg columns : BasicColumn ) {
48
51
getDsl().groupBy(columns.toList())
49
52
}
50
53
54
+ fun having (criteria : GroupingCriteriaReceiver ): Unit =
55
+ with (GroupingCriteriaCollector ().apply (criteria)) {
56
+ this @KotlinSelectBuilder.getDsl().applyHaving(initialCriterion, subCriteria)
57
+ }
58
+
51
59
fun orderBy (vararg columns : SortSpecification ) {
52
60
getDsl().orderBy(columns.toList())
53
61
}
@@ -72,7 +80,34 @@ class KotlinSelectBuilder(private val fromGatherer: QueryExpressionDSL.FromGathe
72
80
73
81
override fun build (): SelectModel = getDsl().build()
74
82
75
- override fun getDsl (): QueryExpressionDSL < SelectModel > {
83
+ override fun getDsl (): KQueryExpressionDSL {
76
84
return dsl? : throw KInvalidSQLException (Messages .getString(" ERROR.27" )) // $NON-NLS-1$
77
85
}
78
86
}
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