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
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 ) {
Original file line number Diff line number Diff line change @@ -93,6 +93,24 @@ import org.mybatis.dynamic.sql.where.condition.IsNull
9393 */
9494fun 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
97115fun and (receiver : GroupingCriteriaReceiver ): AndOrCriteriaGroup =
98116 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
2727import org.mybatis.dynamic.sql.util.kotlin.elements.add
2828import org.mybatis.dynamic.sql.util.kotlin.elements.column
2929import org.mybatis.dynamic.sql.util.kotlin.elements.count
30+ import org.mybatis.dynamic.sql.util.kotlin.elements.having
3031import org.mybatis.dynamic.sql.util.kotlin.elements.isBetween
3132import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualTo
3233import 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()) {
You can’t perform that action at this time.
0 commit comments