Skip to content

Conversation

@obabichevjb
Copy link
Collaborator

We've got a list of example where case() could be working, but it's not:

Count(case(CheckIns.checkInMethod).When(CheckInMethod.CODE, 1))  // CE: 1 is type-mismatching
Count(case(CheckIns.checkInMethod).When(CheckInMethod.CODE, intLiteral(1)))  // CE: CheckInMethod.CODE isn't a Boolean expression
Count(case().When(CheckIns.checkInMethod eq CheckInMethod.CODE, intLiteral(1)))  // CE: CaseWhen is not an expression
Count(case().When(CheckIns.checkInMethod eq CheckInMethod.CODE, intLiteral(1)).Else(null))  // CE: null is type-mismatching
Sum(case().When(CheckIns.checkInMethod eq CheckInMethod.CODE, intLiteral(1)).Else(intLiteral(0)))  // CE: no column type
Sum(case().When(CheckIns.checkInMethod eq CheckInMethod.CODE, intLiteral(1)).Else(intLiteral(0)), IntegerColumnType())  // success

With this PR Case was spit to Case and ValueCase classes. With this change it's getting possible to allow pass kotlin values instead of expressions into When/Else parts of the case() statement.

Also new variant allows to create statement without Else() part, what was not possible before.


Type of Change

Please mark the relevant options with an "X":

  • Bug fix

Updates/remove existing public API methods:

  • Is breaking change

Affected databases:

  • All

Related Issues

EXPOSED-825 Case().When(...).Else(...) with QueryParameter(..., EnumerationColumnType) throws “No column type has been found”

@obabichevjb obabichevjb force-pushed the obabichev/exposed-825-refactor-case-when branch 3 times, most recently from 1981119 to e36db22 Compare July 22, 2025 07:48
@obabichevjb obabichevjb requested review from bog-walk and e5l July 22, 2025 08:02
Copy link
Member

@e5l e5l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check if the breaking change document should be adjusted as well

@obabichevjb obabichevjb force-pushed the obabichev/exposed-825-refactor-case-when branch from e36db22 to df29b8f Compare July 28, 2025 12:24
Copy link
Member

@bog-walk bog-walk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check comments, particularly about When(literal, literal) and breaking changes.

Comment on lines 465 to 476
/**
* Adds a WHEN clause that compares the case value against a literal condition with a literal result.
*
* @param R The return type of the result value
* @param cond The literal value to compare against
* @param result The literal value to return if the condition matches
* @param resultType Optional column type for the result value
* @return A ValueCaseWhen instance for method chaining
*/
fun <R> When(cond: T, result: R, resultType: IColumnType<R & Any>? = null): ValueCaseWhen<T, R> {
return ValueCaseWhen<T, R>(value).When(cond, result, resultType)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only new method that gives me pause, as I thought making resultType optional would always throw at runtime. But now I assume it must be for chained WHEN clauses, but it seems to be order-based?

Is the following expected?

// successful
case(tester.key)
    .When(0, 100, IntegerColumnType())
    .When(1, 200)
    .Else(10)
    .let { expr ->
        tester.select(expr).where { tester.key eq 0 }.first()
    }

// fails with -> No column type has been found
case(tester.key)
    .When(0, 100)
    .When(1, 200, IntegerColumnType())
    .Else(10)
    .let { expr ->
        tester.select(expr).where { tester.key eq 0 }.first()
    }

Seems a little odd.
But if it is expected (or unavoidable), can it at least be mentioned in the KDocs? That the first chained use of this method must always have a provided type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the KDoc to mention that column type should be provided in the beginning of the chain.

But I also imrpoved it a bit with resolveColumnType(), so primitives could be used in When without explicit providing of column type.

… EnumerationColumnType) throws “No column type has been found”
@obabichevjb obabichevjb force-pushed the obabichev/exposed-825-refactor-case-when branch 2 times, most recently from fa184ce to 91ad46c Compare July 29, 2025 08:12
@obabichevjb obabichevjb force-pushed the obabichev/exposed-825-refactor-case-when branch from 91ad46c to d6df146 Compare July 29, 2025 08:23
@obabichevjb obabichevjb merged commit aae0f7c into main Jul 29, 2025
6 checks passed
@obabichevjb obabichevjb deleted the obabichev/exposed-825-refactor-case-when branch July 29, 2025 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants