File tree Expand file tree Collapse file tree 3 files changed +40
-2
lines changed
main/kotlin/org/mybatis/dynamic/sql/util/kotlin
test/kotlin/examples/kotlin/mybatis3/general Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -52,8 +52,8 @@ class KotlinSelectBuilder(private val fromGatherer: QueryExpressionDSL.FromGathe
52
52
}
53
53
54
54
fun having (criteria : GroupingCriteriaReceiver ): Unit =
55
- with ( GroupingCriteriaCollector ().apply (criteria)) {
56
- this @KotlinSelectBuilder. getDsl().applyHaving(initialCriterion, subCriteria)
55
+ GroupingCriteriaCollector ().apply (criteria). let {
56
+ getDsl().applyHaving(it. initialCriterion, it. subCriteria)
57
57
}
58
58
59
59
fun orderBy (vararg columns : SortSpecification ) {
Original file line number Diff line number Diff line change @@ -93,6 +93,24 @@ import org.mybatis.dynamic.sql.where.condition.IsNull
93
93
*/
94
94
fun where (receiver : GroupingCriteriaReceiver ): GroupingCriteriaReceiver = receiver
95
95
96
+ /* *
97
+ * Simple function for code beautification. This allows creation of an independent having clause
98
+ * that can be reused in different statements. For example:
99
+ *
100
+ * val havingClause = where { count() isGreaterThanTo 1 }
101
+ *
102
+ * val rows = select(id, count()) {
103
+ * from(foo)
104
+ * groupBy(id)
105
+ * having(havingClause)
106
+ * }
107
+ *
108
+ * Use of this function is optional. You can also write code like this:
109
+ *
110
+ * val havingClause: GroupingCriteriaReceiver = { count() isGreaterThanTo 1 }
111
+ */
112
+ fun having (receiver : GroupingCriteriaReceiver ): GroupingCriteriaReceiver = receiver
113
+
96
114
// support for criteria without initial conditions
97
115
fun and (receiver : GroupingCriteriaReceiver ): AndOrCriteriaGroup =
98
116
with (GroupingCriteriaCollector ().apply (receiver)) {
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ import org.mybatis.dynamic.sql.exception.InvalidSqlException
27
27
import org.mybatis.dynamic.sql.util.kotlin.elements.add
28
28
import org.mybatis.dynamic.sql.util.kotlin.elements.column
29
29
import org.mybatis.dynamic.sql.util.kotlin.elements.count
30
+ import org.mybatis.dynamic.sql.util.kotlin.elements.having
30
31
import org.mybatis.dynamic.sql.util.kotlin.elements.isBetween
31
32
import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualTo
32
33
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
@@ -360,6 +361,25 @@ class KGroupingTest {
360
361
assertThat(selectStatement.parameters).containsEntry(" p1" , 6L )
361
362
}
362
363
364
+ @Test
365
+ fun testIndependentHaving () {
366
+ val havingClause = having { count() isGreaterThan 6 }
367
+
368
+ val selectStatement = select(A , count()) {
369
+ from(foo)
370
+ groupBy(A )
371
+ having(havingClause)
372
+ }
373
+
374
+ val expected = " select A, count(*)" +
375
+ " from Foo" +
376
+ " group by A" +
377
+ " having count(*) > #{parameters.p1}"
378
+
379
+ assertThat(selectStatement.selectStatement).isEqualTo(expected)
380
+ assertThat(selectStatement.parameters).containsEntry(" p1" , 6L )
381
+ }
382
+
363
383
@Test
364
384
fun testHavingMultipleConditions () {
365
385
val selectStatement = select(A , count()) {
You can’t perform that action at this time.
0 commit comments