Skip to content

Commit d56d07b

Browse files
committed
[Kotlin] Independent Having Clauses
1 parent e01716f commit d56d07b

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class KotlinSelectBuilder(private val fromGatherer: QueryExpressionDSL.FromGathe
5252
}
5353

5454
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)
5757
}
5858

5959
fun orderBy(vararg columns: SortSpecification) {

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlElements.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ import org.mybatis.dynamic.sql.where.condition.IsNull
9393
*/
9494
fun where(receiver: GroupingCriteriaReceiver): GroupingCriteriaReceiver = receiver
9595

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+
96114
// support for criteria without initial conditions
97115
fun and(receiver: GroupingCriteriaReceiver): AndOrCriteriaGroup =
98116
with(GroupingCriteriaCollector().apply(receiver)) {

src/test/kotlin/examples/kotlin/mybatis3/general/KGroupingTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.mybatis.dynamic.sql.exception.InvalidSqlException
2727
import org.mybatis.dynamic.sql.util.kotlin.elements.add
2828
import org.mybatis.dynamic.sql.util.kotlin.elements.column
2929
import org.mybatis.dynamic.sql.util.kotlin.elements.count
30+
import org.mybatis.dynamic.sql.util.kotlin.elements.having
3031
import org.mybatis.dynamic.sql.util.kotlin.elements.isBetween
3132
import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualTo
3233
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
@@ -360,6 +361,25 @@ class KGroupingTest {
360361
assertThat(selectStatement.parameters).containsEntry("p1", 6L)
361362
}
362363

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+
363383
@Test
364384
fun testHavingMultipleConditions() {
365385
val selectStatement = select(A, count()) {

0 commit comments

Comments
 (0)