classCaseExpression
final class CaseExpression
implements Scalar
uses Snapshot
@visibility public.A CASE expression in its simple form, CASE operand WHEN value THEN result … [ELSE result] END, or its searched form, CASE WHEN condition THEN result … [ELSE result] END (Item_func_case).
The branches are tried in order and the first that matches gives the result; the order is kept as written.
Rule: MYSQL-CASE-001. Facts: the type is the aggregate of the results (MYSQL-TYPE-AGGREGATION-001); the value is NULL when a result can be NULL or when there is no ELSE. In the simple form the operand and every value must have the same number of columns (MYSQL-OPERAND-COLUMNS-001); conditions and results take single values. Terminates: the parts are strict parts. Source: https://dev.mysql.com/doc/refman/8.4/en/flow-control-functions.html#operator_case. Status: Implemented.
$query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze("SELECT a FROM t WHERE CASE a WHEN 1 THEN 'x' ELSE 'y' END");
[$query->facts->scalar($query->statement->where)->type->descriptor->name(), $query->facts->scalar($query->statement->where)->nullability] // => ['VARCHAR', \SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability::NotNull]Properties§
public non-empty-list<CaseBranch> $branchespublic Scalar|null $operandpublic Scalar|null $else = nullMethods§
public function __construct(
public Scalar|null $operand,
list<CaseBranch> $branches,
public Scalar|null $else = null,
)Parameters
$operand | Scalar|null | The compared value of the simple form; null for the searched form |
$branches | list<CaseBranch> | The branches in order; at least one |
$else | Scalar|null | The result after ELSE, when written |
Calls 2
- static-call Check::listOf() line 54
- class-const CaseBranch::class() line 54
public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFactDerives the operand, every branch and the ELSE result, and aggregates the result types.
Parameters
$derivation | Derivation | |
$environment | Environment |
Returns
Calls 9
- new Operands line 62
- method-call Derivation::scalar() line 63
- class-const Nullability::Nullable() line 65
- class-const Nullability::NotNull() line 65
- method-call Operands::single() line 69
- method-call Operands::comparable() line 77
- new ScalarFact line 84
- method-call TypeAggregation::aggregate() line 84
- new TypeAggregation line 84
public function render(Output $out): voidWrites CASE, the operand, the branches, the ELSE result and END.
Parameters
$out | Output |
Test cases 1
Calls 2
- method-call Output::keyword() line 92
- method-call Output::node() line 94
Test cases 5§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.