classCaseExpression
final class CaseExpression
implements Scalar, OutputNaming
uses Snapshot
@visibility public.A conditional expression: CASE [operand] WHEN … THEN … [ELSE …] END.
Mirrors PostgreSQL's CaseExpr node. The branches are tried in order and only the taken branch's result is evaluated.
Rule: PG-CASE-001. Facts follow PG-CASE-TYPING-001: the result is of the common type of the results (PG-UNIFICATION-001), NULL when a result can be or no ELSE is written. An unaliased result column is named after the ELSE result when it names one firmly, and otherwise case. Source: https://www.postgresql.org/docs/17/functions-conditional.html#FUNCTIONS-CASE, https://www.postgresql.org/docs/17/typeconv-union-case.html. Status: Implemented.
$query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('SELECT CASE 1 WHEN 1 THEN 2 WHEN 2 THEN 3.5 END');
[$query->field(0)->type->descriptor->name(), $query->field(0)->nullability, $query->field(0)->name->value] // => ['numeric', \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::Nullable, 'case']Properties§
public non-empty-list<CaseBranch> $branchespublic Scalar|null $operandpublic Scalar|null $default = nullMethods§
public function __construct(
public Scalar|null $operand,
list<CaseBranch> $branches,
public Scalar|null $default = null,
)Parameters
$operand | Scalar|null | The value the branches compare with, or null for a searched CASE |
$branches | list<CaseBranch> | The branches in order; at least one |
$default | Scalar|null | The ELSE result, or null when none is written |
Calls 2
- static-call Check::listOf() line 53
- class-const CaseBranch::class() line 53
public function outputName(): ?NameNames an unaliased result column after the ELSE result, or case.
Returns
?NameTest cases 1
Calls 2
- method-call ColumnNaming::name() line 61
- new ColumnNaming line 61
public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFactDerives the operand, the branches and the ELSE result, and the type of the result.
Parameters
$derivation | Derivation | |
$environment | Environment |
Returns
Calls 2
- method-call CaseTyping::derive() line 69
- new CaseTyping line 69
public function render(Output $out): voidWrites CASE, the operand, the branches, ELSE and END.
Parameters
$out | Output |
Test cases 1
Calls 2
- method-call Output::keyword() line 77
- method-call Output::node() line 79
Test cases 6§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 4
Other tests reaching this symbol 2
Relations§
Instantiated in 1
Type checks 1
Type declarations 3
- type SqlSemantics\Platform\PostgreSql\Lowering\Expression\ConstructorRule packages/sql-semantics-postgres/src/Lowering/Expression/ConstructorRule.php:92
- type SqlSemantics\Platform\PostgreSql\Lowering\Expression\Expressions packages/sql-semantics-postgres/src/Lowering/Expression/Expressions.php:156
- type SqlSemantics\Platform\PostgreSql\Rules\Expression\Typing\CaseTyping packages/sql-semantics-postgres/src/Rules/Expression/Typing/CaseTyping.php:36