Skip to content

Commit 9ebb2db

Browse files
committed
More idiomatic DSL for Kotlin casts
1 parent 777cef9 commit 9ebb2db

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util.kotlin.elements
17+
18+
import org.mybatis.dynamic.sql.BasicColumn
19+
import org.mybatis.dynamic.sql.SqlBuilder
20+
import org.mybatis.dynamic.sql.select.function.Cast
21+
import org.mybatis.dynamic.sql.util.kotlin.assertNull
22+
23+
class CastDSL {
24+
internal var cast: Cast? = null
25+
private set(value) {
26+
assertNull(field, "ERROR.43") //$NON-NLS-1$
27+
field = value
28+
}
29+
30+
infix fun String.`as`(targetType: String) {
31+
cast = SqlBuilder.cast(this).`as`(targetType)
32+
}
33+
34+
infix fun BasicColumn.`as`(targetType: String) {
35+
cast = SqlBuilder.cast(this).`as`(targetType)
36+
}
37+
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import org.mybatis.dynamic.sql.BoundValue
2323
import org.mybatis.dynamic.sql.Constant
2424
import org.mybatis.dynamic.sql.SortSpecification
2525
import org.mybatis.dynamic.sql.SqlBuilder
26-
import org.mybatis.dynamic.sql.SqlBuilder.CastFinisher
2726
import org.mybatis.dynamic.sql.SqlColumn
2827
import org.mybatis.dynamic.sql.StringConstant
2928
import org.mybatis.dynamic.sql.VisitableCondition
@@ -50,6 +49,7 @@ import org.mybatis.dynamic.sql.select.function.Upper
5049
import org.mybatis.dynamic.sql.util.kotlin.GroupingCriteriaCollector
5150
import org.mybatis.dynamic.sql.util.kotlin.GroupingCriteriaReceiver
5251
import org.mybatis.dynamic.sql.util.kotlin.KotlinSubQueryBuilder
52+
import org.mybatis.dynamic.sql.util.kotlin.invalidIfNull
5353
import org.mybatis.dynamic.sql.where.condition.IsBetween
5454
import org.mybatis.dynamic.sql.where.condition.IsEqualTo
5555
import org.mybatis.dynamic.sql.where.condition.IsEqualToColumn
@@ -167,11 +167,8 @@ fun <T> subtract(
167167
vararg subsequentColumns: BasicColumn
168168
): Subtract<T> = Subtract.of(firstColumn, secondColumn, subsequentColumns.asList())
169169

170-
fun cast(value: String): CastFinisher = SqlBuilder.cast(value)
171-
172-
fun cast(column: BasicColumn): CastFinisher = SqlBuilder.cast(column)
173-
174-
infix fun CastFinisher.`as`(targetType: String): Cast = this.`as`(targetType)
170+
fun cast(receiver: CastDSL.() -> Unit): Cast =
171+
invalidIfNull(CastDSL().apply(receiver).cast, "ERROR.43")
175172

176173
fun <T> concat(
177174
firstColumn: BindableColumn<T>,

src/main/resources/org/mybatis/dynamic/sql/util/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ ERROR.39=When clauses in case expressions must render (optional conditions are n
5959
ERROR.40=Case expressions must have at least one "when" clause
6060
ERROR.41=You cannot call "then" in a Kotlin case expression more than once
6161
ERROR.42=You cannot call `else` in a Kotlin case expression more than once
62+
ERROR.43=A Kotlin cast expression must have one, and only one, cast
6263
INTERNAL.ERROR=Internal Error {0}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ 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`
3231
import org.mybatis.dynamic.sql.util.kotlin.elements.case
3332
import org.mybatis.dynamic.sql.util.kotlin.elements.cast
3433
import org.mybatis.dynamic.sql.util.kotlin.elements.isEqualTo
@@ -217,7 +216,7 @@ class KCaseExpressionTest {
217216
or { animalName isEqualTo "Big brown bat" }
218217
then(value("Bat"))
219218
}
220-
`else`(cast(value("Not a Fox or a bat")) `as` "VARCHAR(30)")
219+
`else`(cast { value("Not a Fox or a bat") `as` "VARCHAR(30)" })
221220
}.`as`("AnimalType")
222221
) {
223222
from(animalData, "a")
@@ -720,7 +719,7 @@ class KCaseExpressionTest {
720719
val selectStatement = select(
721720
animalName,
722721
case(animalName) {
723-
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then(cast("It's a fox") `as` "VARCHAR(30)") }
722+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then(cast { "It's a fox" `as` "VARCHAR(30)" })}
724723
`else`("It's not a fox")
725724
}.`as`("IsAFox")
726725
) {

0 commit comments

Comments
 (0)