Skip to content

Commit

Permalink
fix: EXPOSED-158 avoid SQL syntax error of CASE WHEN using nested CASE (
Browse files Browse the repository at this point in the history
JetBrains#1847)

* fix: EXPOSED-158 insert space before WHEN

to avoid SQL syntax error ENDWHEN using nested CASE

* fix: EXPOSED-158 insert space before WHEN

added the test code
and removed unnecessary code for this problem
  • Loading branch information
ymotchi authored Aug 30, 2023
1 parent cb721fe commit fe4c41a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,14 @@ class CaseWhenElse<T, R : T>(

override fun toQueryBuilder(queryBuilder: QueryBuilder) {
queryBuilder {
append("CASE ")
append("CASE")
if (caseWhen.value != null) {
+caseWhen.value
+" "
+caseWhen.value
}

for ((first, second) in caseWhen.cases) {
append("WHEN ", first, " THEN ", second)
append(" WHEN ", first, " THEN ", second)
}

append(" ELSE ", elseResult, " END")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,30 @@ class ConditionsTests : DatabaseTestsBase() {
}
}
}

@Test
fun testChainedAndNestedCaseWhenElseSyntax() {
withCitiesAndUsers { cities, _, _ ->
val nestedCondition = Case()
.When(Op.build { cities.id eq 1 }, intLiteral(1))
.Else(intLiteral(-1))
val chainedCondition = Case()
.When(Op.build { cities.name like "M%" }, intLiteral(0))
.When(Op.build { cities.name like "St. %" }, nestedCondition)
.When(Op.build { cities.name like "P%" }, intLiteral(2))
.Else(intLiteral(-1))

val results = cities.slice(cities.name, chainedCondition).selectAll()
results.forEach {
val cityName = it[cities.name]
val expectedNumber = when {
cityName.startsWith("M") -> 0
cityName.startsWith("St. ") -> 1
cityName.startsWith("P") -> 2
else -> -1
}
assertEquals(expectedNumber, it[chainedCondition])
}
}
}
}

0 comments on commit fe4c41a

Please sign in to comment.