Skip to content

Commit 2e53d74

Browse files
committed
Infix functions for as with case expressions
1 parent 026ebbc commit 2e53d74

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ package org.mybatis.dynamic.sql.util.kotlin.elements
1717

1818
import org.mybatis.dynamic.sql.DerivedColumn
1919
import org.mybatis.dynamic.sql.SqlColumn
20+
import org.mybatis.dynamic.sql.select.caseexpression.SearchedCaseModel
21+
import org.mybatis.dynamic.sql.select.caseexpression.SimpleCaseModel
2022

2123
infix fun <T> DerivedColumn<T>.`as`(alias: String): DerivedColumn<T> = this.`as`(alias)
2224

2325
infix fun <T> SqlColumn<T>.`as`(alias: String): SqlColumn<T> = this.`as`(alias)
2426

27+
infix fun SearchedCaseModel.`as`(alias: String): SearchedCaseModel = this.`as`(alias)
28+
29+
infix fun <T> SimpleCaseModel<T>.`as`(alias: String): SimpleCaseModel<T> = this.`as`(alias)
30+
2531
/**
2632
* Adds a qualifier to a column for use with table aliases (typically in joins or sub queries).
2733
* This is as close to natural SQL syntax as we can get in Kotlin. Natural SQL would look like

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.junit.jupiter.api.Test
2828
import org.mybatis.dynamic.sql.exception.InvalidSqlException
2929
import org.mybatis.dynamic.sql.util.Messages
3030
import org.mybatis.dynamic.sql.util.kotlin.KInvalidSQLException
31+
import org.mybatis.dynamic.sql.util.kotlin.elements.`as`
3132
import org.mybatis.dynamic.sql.util.kotlin.elements.case
3233
import org.mybatis.dynamic.sql.util.kotlin.elements.cast
3334
import org.mybatis.dynamic.sql.util.kotlin.elements.isEqualTo
@@ -63,7 +64,7 @@ class KCaseExpressionTest {
6364
then("Bat")
6465
}
6566
`else`("Not a Fox or a bat")
66-
}.`as`("AnimalType")
67+
} `as` "AnimalType"
6768
) {
6869
from(animalData, "a")
6970
where { id.isIn(2, 3, 31, 32, 38, 39) }
@@ -140,7 +141,7 @@ class KCaseExpressionTest {
140141
then(2)
141142
}
142143
`else`(3)
143-
}.`as`("AnimalType")
144+
} `as` "AnimalType"
144145
) {
145146
from(animalData, "a")
146147
where { id.isIn(2, 3, 31, 32, 38, 39) }
@@ -217,7 +218,7 @@ class KCaseExpressionTest {
217218
then(value("Bat"))
218219
}
219220
`else`(cast { value("Not a Fox or a bat") `as` "VARCHAR(30)" })
220-
}.`as`("AnimalType")
221+
} `as` "AnimalType"
221222
) {
222223
from(animalData, "a")
223224
where { id.isIn(2, 3, 31, 32, 38, 39) }
@@ -297,7 +298,7 @@ class KCaseExpressionTest {
297298
or { animalName isEqualTo "Big brown bat" }
298299
then("Bat")
299300
}
300-
}.`as`("AnimalType")
301+
} `as` "AnimalType"
301302
) {
302303
from(animalData, "a")
303304
where { id.isIn(2, 3, 31, 32, 38, 39) }
@@ -379,7 +380,7 @@ class KCaseExpressionTest {
379380
then("Fred")
380381
}
381382
`else`("Not a Fox or a bat")
382-
}.`as`("AnimalType")
383+
} `as` "AnimalType"
383384
) {
384385
from(animalData, "a")
385386
where { id.isIn(2, 3, 4, 31, 32, 38, 39) }
@@ -458,7 +459,7 @@ class KCaseExpressionTest {
458459
case(animalName) {
459460
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("yes") }
460461
`else`("no")
461-
}.`as`("IsAFox")
462+
} `as` "IsAFox"
462463
) {
463464
from(animalData)
464465
where { id.isIn(31, 32, 38, 39) }
@@ -512,7 +513,7 @@ class KCaseExpressionTest {
512513
case(animalName) {
513514
`when` ("Artic fox", "Red fox") { then("yes") }
514515
`else`("no")
515-
}.`as`("IsAFox")
516+
} `as` "IsAFox"
516517
) {
517518
from(animalData)
518519
where { id.isIn(31, 32, 38, 39) }
@@ -566,7 +567,7 @@ class KCaseExpressionTest {
566567
case(animalName) {
567568
`when` (isEqualTo("Artic fox"), isEqualTo("Red fox")) { then(true) }
568569
`else`(false)
569-
}.`as`("IsAFox")
570+
} `as` "IsAFox"
570571
) {
571572
from(animalData)
572573
where { id.isIn(31, 32, 38, 39) }
@@ -620,7 +621,7 @@ class KCaseExpressionTest {
620621
case(animalName) {
621622
`when` ("Artic fox", "Red fox") { then(true) }
622623
`else`(false)
623-
}.`as`("IsAFox")
624+
} `as` "IsAFox"
624625
) {
625626
from(animalData)
626627
where { id.isIn(31, 32, 38, 39) }
@@ -673,7 +674,7 @@ class KCaseExpressionTest {
673674
animalName,
674675
case(animalName) {
675676
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then("yes") }
676-
}.`as`("IsAFox")
677+
} `as` "IsAFox"
677678
) {
678679
from(animalData)
679680
where { id.isIn(31, 32, 38, 39) }
@@ -721,7 +722,7 @@ class KCaseExpressionTest {
721722
case(animalName) {
722723
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then(cast { "It's a fox" `as` "VARCHAR(30)" })}
723724
`else`("It's not a fox")
724-
}.`as`("IsAFox")
725+
} `as` "IsAFox"
725726
) {
726727
from(animalData)
727728
where { id.isIn(31, 32, 38, 39) }
@@ -764,7 +765,7 @@ class KCaseExpressionTest {
764765
case(animalName) {
765766
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1L) }
766767
`else`(2L)
767-
}.`as`("IsAFox")
768+
} `as` "IsAFox"
768769
) {
769770
from(animalData)
770771
where { id.isIn(31, 32, 38, 39) }
@@ -807,7 +808,7 @@ class KCaseExpressionTest {
807808
case(animalName) {
808809
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1.1) }
809810
`else`(2.2)
810-
}.`as`("IsAFox")
811+
} `as` "IsAFox"
811812
) {
812813
from(animalData)
813814
where { id.isIn(31, 32, 38, 39) }
@@ -850,7 +851,7 @@ class KCaseExpressionTest {
850851
case(animalName) {
851852
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1.1) }
852853
`else`(cast { 2.2 `as` "DOUBLE" })
853-
}.`as`("IsAFox")
854+
} `as` "IsAFox"
854855
) {
855856
from(animalData)
856857
where { id.isIn(31, 32, 38, 39) }

0 commit comments

Comments
 (0)