Skip to content

Commit e8f1890

Browse files
authored
Merge pull request #785 from jeffgbutler/improve-kotlin-case-dsl
Improve Kotlin CASE DSL
2 parents ecf4bc3 + 15d258e commit e8f1890

File tree

5 files changed

+52
-79
lines changed

5 files changed

+52
-79
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
This log will detail notable changes to MyBatis Dynamic SQL. Full details are available on the GitHub milestone pages.
44

5-
## Release 1.5.1 - April 30, 2024
5+
## Release 1.5.2 - Unreleased
66

7-
This is a minor release with several enhancements.
7+
This is a small maintenance release with improvements to the Kotlin DSL for CASE expressions.
88

99
**Important:** This is the last release that will be compatible with Java 8.
1010

11+
## Release 1.5.1 - April 30, 2024
12+
13+
This is a minor release with several enhancements.
14+
1115
GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/milestone/13?closed=1](https://github.com/mybatis/mybatis-dynamic-sql/milestone/13?closed=1)
1216

1317
### Case Expressions and Cast Function

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
<groupId>org.mybatis.dynamic-sql</groupId>
2828
<artifactId>mybatis-dynamic-sql</artifactId>
29-
<version>1.6.0-SNAPSHOT</version>
29+
<version>1.5.2-SNAPSHOT</version>
3030

3131
<name>MyBatis Dynamic SQL</name>
3232
<description>MyBatis framework for generating dynamic SQL</description>

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

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ class KSearchedCaseDSL : KElseDSL {
3838
.withSubCriteria(subCriteria)
3939
.withThenValue(thenValue)
4040
.build())
41-
4241
}
4342

44-
override fun `else`(column: BasicColumn) {
43+
override infix fun `else`(column: BasicColumn) {
4544
this.elseValue = column
4645
}
4746
}
@@ -53,7 +52,7 @@ class SearchedCaseCriteriaCollector : GroupingCriteriaCollector(), KThenDSL {
5352
field = value
5453
}
5554

56-
override fun then(column: BasicColumn) {
55+
override infix fun then(column: BasicColumn) {
5756
thenValue = column
5857
}
5958
}
@@ -66,88 +65,71 @@ class KSimpleCaseDSL<T : Any> : KElseDSL {
6665
}
6766
internal val whenConditions = mutableListOf<SimpleCaseWhenCondition<T>>()
6867

69-
fun `when`(firstCondition: VisitableCondition<T>, vararg subsequentConditions: VisitableCondition<T>,
70-
completer: SimpleCaseThenGatherer.() -> Unit) =
71-
SimpleCaseThenGatherer().apply(completer).run {
72-
val allConditions = buildList {
73-
add(firstCondition)
74-
addAll(subsequentConditions)
75-
}
76-
77-
whenConditions.add(ConditionBasedWhenCondition(allConditions, thenValue))
68+
fun `when`(vararg conditions: VisitableCondition<T>) =
69+
SimpleCaseThenGatherer { thenValue ->
70+
whenConditions.add(ConditionBasedWhenCondition(conditions.asList(), thenValue))
7871
}
7972

80-
fun `when`(firstValue: T, vararg subsequentValues: T, completer: SimpleCaseThenGatherer.() -> Unit) =
81-
SimpleCaseThenGatherer().apply(completer).run {
82-
val allConditions = buildList {
83-
add(firstValue)
84-
addAll(subsequentValues)
85-
}
86-
87-
whenConditions.add(BasicWhenCondition(allConditions, thenValue))
73+
fun `when`(vararg values: T) =
74+
SimpleCaseThenGatherer { thenValue ->
75+
whenConditions.add(BasicWhenCondition(values.asList(), thenValue))
8876
}
8977

90-
override fun `else`(column: BasicColumn) {
78+
override infix fun `else`(column: BasicColumn) {
9179
this.elseValue = column
9280
}
9381
}
9482

95-
class SimpleCaseThenGatherer: KThenDSL {
96-
internal var thenValue: BasicColumn? = null
97-
private set(value) {
98-
assertNull(field, "ERROR.41") //$NON-NLS-1$
99-
field = value
100-
}
101-
102-
override fun then(column: BasicColumn) {
103-
thenValue = column
83+
class SimpleCaseThenGatherer(private val consumer: (BasicColumn) -> Unit): KThenDSL {
84+
override infix fun then(column: BasicColumn) {
85+
consumer.invoke(column)
10486
}
10587
}
10688

10789
interface KThenDSL {
108-
fun then(value: String) {
90+
infix fun then(value: String) {
10991
then(stringConstant(value))
11092
}
11193

112-
fun then(value: Boolean) {
94+
infix fun then(value: Boolean) {
11395
then(constant<String>(value.toString()))
11496
}
11597

116-
fun then(value: Int) {
98+
infix fun then(value: Int) {
11799
then(constant<String>(value.toString()))
118100
}
119101

120-
fun then(value: Long) {
102+
infix fun then(value: Long) {
121103
then(constant<String>(value.toString()))
122104
}
123105

124-
fun then(value: Double) {
106+
infix fun then(value: Double) {
125107
then(constant<String>(value.toString()))
126108
}
127109

128-
fun then(column: BasicColumn)
110+
infix fun then(column: BasicColumn)
129111
}
130112

131113
interface KElseDSL {
132-
fun `else`(value: String) {
114+
infix fun `else`(value: String) {
133115
`else`(stringConstant(value))
134116
}
135117

136-
fun `else`(value: Boolean) {
118+
infix fun `else`(value: Boolean) {
137119
`else`(constant<String>(value.toString()))
138120
}
139121

140-
fun `else`(value: Int) {
122+
infix fun `else`(value: Int) {
141123
`else`(constant<String>(value.toString()))
142124
}
143125

144-
fun `else`(value: Long) {
126+
infix fun `else`(value: Long) {
145127
`else`(constant<String>(value.toString()))
146128
}
147129

148-
fun `else`(value: Double) {
130+
infix fun `else`(value: Double) {
149131
`else`(constant<String>(value.toString()))
150132
}
151133

152-
fun `else`(column: BasicColumn)
134+
infix fun `else`(column: BasicColumn)
153135
}

src/site/markdown/docs/kotlinCaseExpressions.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ A simple case expression can be coded like the following in the Kotlin DSL:
7272

7373
```kotlin
7474
select(case(id) {
75-
`when`(1, 2, 3) { then(true) }
75+
`when`(1, 2, 3) then true
7676
`else`(false)
7777
} `as` "small_id"
7878
) {
@@ -91,7 +91,7 @@ you can write the query as follows:
9191

9292
```kotlin
9393
select(case(id) {
94-
`when`(1, 2, 3) { then(value(true)) }
94+
`when`(1, 2, 3) then value(true)
9595
`else`(value(false))
9696
} `as` "small_id"
9797
) {
@@ -111,7 +111,7 @@ expected data type. Here's an example of using the `cast` function:
111111

112112
```kotlin
113113
select(case(id) {
114-
`when`(1, 2, 3) { then(value(true)) }
114+
`when`(1, 2, 3) then value(true)
115115
`else`(cast { value(false) `as` "BOOLEAN" })
116116
} `as` "small_id"
117117
) {
@@ -134,8 +134,8 @@ A simple case expression can be coded like the following in the Kotlin DSL:
134134

135135
```kotlin
136136
select(case(total_length) {
137-
`when`(isLessThan(10)) { then("small") }
138-
`when`(isGreaterThan(20)) { then("large") }
137+
`when`(isLessThan(10)) then "small"
138+
`when`(isGreaterThan(20)) then "large"
139139
`else`("medium")
140140
} `as` "tshirt_size"
141141
) {
@@ -158,8 +158,8 @@ VARCHAR, you can use the `cast` function as follows:
158158

159159
```kotlin
160160
select(case(total_length) {
161-
`when`(isLessThan(10)) { then("small") }
162-
`when`(isGreaterThan(20)) { then("large") }
161+
`when`(isLessThan(10)) then "small"
162+
`when`(isGreaterThan(20)) then "large"
163163
`else`(cast { "medium" `as` "VARCHAR(6)" })
164164
} `as` "tshirt_size"
165165
) {

src/test/kotlin/examples/kotlin/animal/data/KCaseExpressionTest.kt

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ class KCaseExpressionTest {
457457
val selectStatement = select(
458458
animalName,
459459
case(animalName) {
460-
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("yes") }
460+
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) then "yes"
461461
`else`("no")
462462
} `as` "IsAFox"
463463
) {
@@ -511,7 +511,7 @@ class KCaseExpressionTest {
511511
val selectStatement = select(
512512
animalName,
513513
case(animalName) {
514-
`when` ("Artic fox", "Red fox") { then("yes") }
514+
`when` ("Artic fox", "Red fox") then "yes"
515515
`else`("no")
516516
} `as` "IsAFox"
517517
) {
@@ -565,7 +565,7 @@ class KCaseExpressionTest {
565565
val selectStatement = select(
566566
animalName,
567567
case(animalName) {
568-
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) { then(true) }
568+
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) then true
569569
`else`(false)
570570
} `as` "IsAFox"
571571
) {
@@ -619,7 +619,7 @@ class KCaseExpressionTest {
619619
val selectStatement = select(
620620
animalName,
621621
case(animalName) {
622-
`when` ("Artic fox", "Red fox") { then(true) }
622+
`when` ("Artic fox", "Red fox") then true
623623
`else`(false)
624624
} `as` "IsAFox"
625625
) {
@@ -673,7 +673,7 @@ class KCaseExpressionTest {
673673
val selectStatement = select(
674674
animalName,
675675
case(animalName) {
676-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("yes") }
676+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then "yes"
677677
} `as` "IsAFox"
678678
) {
679679
from(animalData)
@@ -720,7 +720,7 @@ class KCaseExpressionTest {
720720
val selectStatement = select(
721721
animalName,
722722
case(animalName) {
723-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then(cast { "It's a fox" `as` "VARCHAR(30)" })}
723+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then cast { "It's a fox" `as` "VARCHAR(30)" }
724724
`else`("It's not a fox")
725725
} `as` "IsAFox"
726726
) {
@@ -763,7 +763,7 @@ class KCaseExpressionTest {
763763
val selectStatement = select(
764764
animalName,
765765
case(animalName) {
766-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1L) }
766+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then 1L
767767
`else`(2L)
768768
} `as` "IsAFox"
769769
) {
@@ -806,7 +806,7 @@ class KCaseExpressionTest {
806806
val selectStatement = select(
807807
animalName,
808808
case(animalName) {
809-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1.1) }
809+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then 1.1
810810
`else`(2.2)
811811
} `as` "IsAFox"
812812
) {
@@ -849,7 +849,7 @@ class KCaseExpressionTest {
849849
val selectStatement = select(
850850
animalName,
851851
case(animalName) {
852-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1.1) }
852+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then 1.1
853853
`else`(cast { 2.2 `as` "DOUBLE" })
854854
} `as` "IsAFox"
855855
) {
@@ -888,35 +888,22 @@ class KCaseExpressionTest {
888888
fun testInvalidDoubleElseSimple() {
889889
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
890890
case(animalName) {
891-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("'yes'") }
891+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) then "yes"
892892
`else`("Fred")
893893
`else`("Wilma")
894894
}
895895
}.withMessage(Messages.getString("ERROR.42"))
896896
}
897897

898-
@Test
899-
fun testInvalidDoubleThenSimple() {
900-
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
901-
case(animalName) {
902-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) {
903-
then("'yes'")
904-
then("no")
905-
}
906-
`else`("Fred")
907-
}
908-
}.withMessage(Messages.getString("ERROR.41"))
909-
}
910-
911898
@Test
912899
fun testInvalidDoubleElseSearched() {
913900
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
914901
case {
915902
`when` {
916903
id isEqualTo 22
917-
then("'yes'")
904+
this then "yes"
918905
}
919-
`else`("Fred")
906+
this `else` "Fred"
920907
`else`("Wilma")
921908
}
922909
}.withMessage(Messages.getString("ERROR.42"))
@@ -928,8 +915,8 @@ class KCaseExpressionTest {
928915
case {
929916
`when` {
930917
id isEqualTo 22
931-
then("'yes'")
932-
then("'no'")
918+
then("yes")
919+
then("no")
933920
}
934921
}
935922
}.withMessage(Messages.getString("ERROR.41"))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy