CaseBuilder
CaseBuilder provides a type-safe way to build SQL CASE expressions and statements. It’s created by calling eb.case() or eb.case(value) on an ExpressionBuilder.
Type Parameters
The database schema type
The table references available in the current query context
The type being compared in when clauses (when using
case(value) form)The accumulated output type from all then clauses
Methods
when
Adds awhen clause to the case statement.
Left-hand side column reference (only for
case() without value)Comparison operator (only for
case() without value)Right-hand side value or expression (only for
case() without value)Expression to evaluate
Value to compare against (only for
case(value) form)when call must be followed by a then call.
Example with case():
case(value):
CaseThenBuilder
Returned bywhen() calls. Provides the then() method to specify the result for the when condition.
then
Adds athen clause to the case statement.
Expression to return when the when condition matches
Value to return when the when condition matches
then call can be followed by when, else, end, or endCase calls.
CaseWhenBuilder
Returned bythen() calls. Allows chaining additional when clauses or finishing with else/end.
when
Adds anotherwhen clause to the case statement.
else
Adds anelse clause to the case statement.
Expression to return when no when conditions match
Value to return when no when conditions match
else call must be followed by an end or endCase call.
Example:
end
Adds anend keyword to the case operator.
case operators can only be used as part of a query. For a case statement used as part of a stored program, use endCase instead.
If no else clause is provided, the result type will be nullable (O | null).
Example:
endCase
Addsend case keywords to the case statement.
case statements can only be used for flow control in stored programs. For a case operator used as part of a query, use end instead.
CaseEndBuilder
Returned byelse() calls. Only provides end and endCase methods.
end
Adds anend keyword to the case operator.
endCase
Addsend case keywords to the case statement.
Complete Example
Kitchen sink example with 2 flavors ofcase operator:
Type Safety
TheCaseBuilder maintains type safety throughout the chain:
- When using
case(value), thewhenclauses must match the value type - The output type
Oaccumulates all possible return types fromthenclauses - Without an
elseclause, the final type isO | null - With an
elseclause, the final type includes the else value type