Skip to content

Commit f4ca50a

Browse files
committed
Polishing
1 parent aa58bc9 commit f4ca50a

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

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

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class SearchedCaseCriteriaCollector : GroupingCriteriaCollector() {
5353
}
5454

5555
fun then(value: String) {
56-
this.thenValue = "'$value'"
56+
thenValue = "'$value'"
5757
}
5858

5959
fun then(value: Any) {
60-
this.thenValue = value
60+
thenValue = value
6161
}
6262
}
6363

@@ -69,58 +69,48 @@ class KSimpleCaseDSL<T : Any> {
6969
}
7070
internal val whenConditions = mutableListOf<SimpleCaseWhenCondition<T>>()
7171

72-
fun `when`(firstCondition: VisitableCondition<T>, vararg subsequentConditions: VisitableCondition<T>) =
73-
ConditionBasedThenGatherer(firstCondition, subsequentConditions.asList())
74-
75-
fun `when`(firstValue: T, vararg subsequentValues: T) =
76-
BasicThenGatherer(firstValue, subsequentValues.asList())
77-
78-
fun `else`(value: String) {
79-
this.elseValue = "'$value'"
80-
}
81-
82-
fun `else`(value: Any) {
83-
this.elseValue = value
84-
}
85-
86-
inner class ConditionBasedThenGatherer(private val firstCondition: VisitableCondition<T>,
87-
private val subsequentConditions: List<VisitableCondition<T>>) {
88-
fun then(value: String) {
72+
fun `when`(firstCondition: VisitableCondition<T>, vararg subsequentConditions: VisitableCondition<T>,
73+
completer: SimpleCaseThenGatherer.() -> Unit) =
74+
SimpleCaseThenGatherer().apply(completer).run {
8975
val allConditions = buildList {
9076
add(firstCondition)
9177
addAll(subsequentConditions)
9278
}
9379

94-
whenConditions.add(ConditionBasedWhenCondition(allConditions, "'$value'"))
80+
whenConditions.add(ConditionBasedWhenCondition(allConditions, thenValue))
9581
}
9682

97-
fun then(value: Any) {
83+
fun `when`(firstValue: T, vararg subsequentValues: T, completer: SimpleCaseThenGatherer.() -> Unit) =
84+
SimpleCaseThenGatherer().apply(completer).run {
9885
val allConditions = buildList {
99-
add(firstCondition)
100-
addAll(subsequentConditions)
86+
add(firstValue)
87+
addAll(subsequentValues)
10188
}
10289

103-
whenConditions.add(ConditionBasedWhenCondition(allConditions, value))
90+
whenConditions.add(BasicWhenCondition(allConditions, thenValue))
10491
}
92+
93+
fun `else`(value: String) {
94+
this.elseValue = "'$value'"
10595
}
10696

107-
inner class BasicThenGatherer(private val firstValue: T, private val subsequentValues: List<T>) {
108-
fun then(value: String) {
109-
val allValues = buildList {
110-
add(firstValue)
111-
addAll(subsequentValues)
112-
}
97+
fun `else`(value: Any) {
98+
this.elseValue = value
99+
}
100+
}
113101

114-
whenConditions.add(BasicWhenCondition(allValues, "'$value'"))
102+
class SimpleCaseThenGatherer {
103+
internal var thenValue: Any? = null
104+
private set(value) {
105+
assertNull(field, "ERROR.41") //$NON-NLS-1$
106+
field = value
115107
}
116108

117-
fun then(value: Any) {
118-
val allValues = buildList {
119-
add(firstValue)
120-
addAll(subsequentValues)
121-
}
109+
fun then(value: String) {
110+
thenValue = "'$value'"
111+
}
122112

123-
whenConditions.add(BasicWhenCondition(allValues, value))
124-
}
113+
fun then(value: Any) {
114+
thenValue = value
125115
}
126116
}

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ class KCaseExpressionTest {
373373
val selectStatement = select(
374374
animalName,
375375
case(animalName) {
376-
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")).then("yes")
376+
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("yes") }
377377
`else`("no")
378378
}.`as`("IsAFox")
379379
) {
@@ -427,7 +427,7 @@ class KCaseExpressionTest {
427427
val selectStatement = select(
428428
animalName,
429429
case(animalName) {
430-
`when` ("Artic fox", "Red fox").then("yes")
430+
`when` ("Artic fox", "Red fox") { then("yes") }
431431
`else`("no")
432432
}.`as`("IsAFox")
433433
) {
@@ -481,7 +481,7 @@ class KCaseExpressionTest {
481481
val selectStatement = select(
482482
animalName,
483483
case(animalName) {
484-
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")).then(true)
484+
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) { then(true) }
485485
`else`(false)
486486
}.`as`("IsAFox")
487487
) {
@@ -535,7 +535,7 @@ class KCaseExpressionTest {
535535
val selectStatement = select(
536536
animalName,
537537
case(animalName) {
538-
`when` ("Artic fox", "Red fox").then(true)
538+
`when` ("Artic fox", "Red fox") { then(true) }
539539
`else`(false)
540540
}.`as`("IsAFox")
541541
) {
@@ -589,7 +589,7 @@ class KCaseExpressionTest {
589589
val selectStatement = select(
590590
animalName,
591591
case(animalName) {
592-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")).then("yes")
592+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("yes") }
593593
}.`as`("IsAFox")
594594
) {
595595
from(animalData)
@@ -632,13 +632,26 @@ class KCaseExpressionTest {
632632
fun testInvalidDoubleElseSimple() {
633633
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
634634
case(animalName) {
635-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")).then("'yes'")
635+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("'yes'") }
636636
`else`("Fred")
637637
`else`("Wilma")
638638
}
639639
}.withMessage(Messages.getString("ERROR.42"))
640640
}
641641

642+
@Test
643+
fun testInvalidDoubleThenSimple() {
644+
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
645+
case(animalName) {
646+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) {
647+
then("'yes'")
648+
then("no")
649+
}
650+
`else`("Fred")
651+
}
652+
}.withMessage(Messages.getString("ERROR.41"))
653+
}
654+
642655
@Test
643656
fun testInvalidDoubleElseSearched() {
644657
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
@@ -669,14 +682,14 @@ class KCaseExpressionTest {
669682
@Test
670683
fun testInvalidSearchedMissingWhen() {
671684
assertThatExceptionOfType(InvalidSqlException::class.java).isThrownBy {
672-
select(case { `else`("Fred") }){ from(animalData) }
685+
select(case { `else`("Fred") }) { from(animalData) }
673686
}.withMessage(Messages.getString("ERROR.40"))
674687
}
675688

676689
@Test
677690
fun testInvalidSimpleMissingWhen() {
678691
assertThatExceptionOfType(InvalidSqlException::class.java).isThrownBy {
679-
select(case (id) { `else`("Fred") }){ from (animalData) }
692+
select(case (id) { `else`("Fred") }) { from (animalData) }
680693
}.withMessage(Messages.getString("ERROR.40"))
681694
}
682695
}

0 commit comments

Comments
 (0)