Skip to content

Commit ff623a0

Browse files
committed
Add a cast for doubles
1 parent 9ebb2db commit ff623a0

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ static CastFinisher cast(String value) {
535535
return cast(stringConstant(value));
536536
}
537537

538+
static CastFinisher cast(Double value) {
539+
return cast(constant(value.toString()));
540+
}
541+
538542
static CastFinisher cast(BasicColumn column) {
539543
return new CastFinisher(column);
540544
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class CastDSL {
3131
cast = SqlBuilder.cast(this).`as`(targetType)
3232
}
3333

34+
infix fun Double.`as`(targetType: String) {
35+
cast = SqlBuilder.cast(this).`as`(targetType)
36+
}
37+
3438
infix fun BasicColumn.`as`(targetType: String) {
3539
cast = SqlBuilder.cast(this).`as`(targetType)
3640
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +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
62+
ERROR.43=A Kotlin cast expression must have one, and only one, `as` element
6363
INTERNAL.ERROR=Internal Error {0}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,49 @@ class KCaseExpressionTest {
840840
}
841841
}
842842

843+
@Test
844+
fun testCaseCastDoubles() {
845+
newSession().use { session ->
846+
val mapper = session.getMapper(CommonSelectMapper::class.java)
847+
848+
val selectStatement = select(
849+
animalName,
850+
case(animalName) {
851+
`when`(isEqualTo("Artic fox"), isEqualTo("Red fox")) { then( 1.1) }
852+
`else`(cast { 2.2 `as` "DOUBLE" })
853+
}.`as`("IsAFox")
854+
) {
855+
from(animalData)
856+
where { id.isIn(31, 32, 38, 39) }
857+
orderBy(id)
858+
}
859+
860+
val expected = "select animal_name, " +
861+
"case animal_name when = #{parameters.p1,jdbcType=VARCHAR}, = #{parameters.p2,jdbcType=VARCHAR} then 1.1 " +
862+
"else cast(2.2 as DOUBLE) end " +
863+
"as IsAFox from AnimalData where id in " +
864+
"(#{parameters.p3,jdbcType=INTEGER},#{parameters.p4,jdbcType=INTEGER},#{parameters.p5,jdbcType=INTEGER},#{parameters.p6,jdbcType=INTEGER}) " +
865+
"order by id"
866+
assertThat(selectStatement.selectStatement).isEqualTo(expected)
867+
assertThat(selectStatement.parameters)
868+
.containsOnly(
869+
entry("p1", "Artic fox"),
870+
entry("p2", "Red fox"),
871+
entry("p3", 31),
872+
entry("p4", 32),
873+
entry("p5", 38),
874+
entry("p6", 39)
875+
)
876+
877+
val records = mapper.selectManyMappedRows(selectStatement)
878+
assertThat(records).hasSize(4)
879+
assertThat(records[0]).containsOnly(entry("ANIMAL_NAME", "Cat"), entry("ISAFOX", 2.2))
880+
assertThat(records[1]).containsOnly(entry("ANIMAL_NAME", "Artic fox"), entry("ISAFOX", 1.1))
881+
assertThat(records[2]).containsOnly(entry("ANIMAL_NAME", "Red fox"), entry("ISAFOX", 1.1))
882+
assertThat(records[3]).containsOnly(entry("ANIMAL_NAME", "Raccoon"), entry("ISAFOX", 2.2))
883+
}
884+
}
885+
843886
@Test
844887
fun testInvalidDoubleElseSimple() {
845888
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {

0 commit comments

Comments
 (0)