final class CaseExpression
    implements Scalar, OutputNaming
    uses Snapshot
Public API: explicitly declared with @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.

Typing a CASE without ELSEdoctest
$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> $branches
public Scalar|null $operand
public Scalar|null $default = null

Methods§

public function __construct(
    public Scalar|null $operand,
    list<CaseBranch> $branches,
    public Scalar|null $default = null,
)

Parameters

$operandScalar|nullThe value the branches compare with, or null for a searched CASE
$brancheslist<CaseBranch>The branches in order; at least one
$defaultScalar|nullThe ELSE result, or null when none is written
Calls 2
public function outputName(): ?Name

Names an unaliased result column after the ELSE result, or case.

Returns

Test cases 1
Calls 2
public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact

Derives the operand, the branches and the ELSE result, and the type of the result.

Parameters

$derivationDerivation
$environmentEnvironment

Returns

Calls 2
public function render(Output $out): void

Writes CASE, the operand, the branches, ELSE and END.

Parameters

$outOutput
Test cases 1
Calls 2

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