packages/sql-semantics-sqlite/src/Model/Statement/Reference/Column/ConditionalColumn.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column;
6
7use SqlSemantics\Platform\Sqlite\Model\Diagnostic\Check;
8use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
9use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\MissingInput;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Snapshot;
11
12/**
13 * A name whose resolution depends on declarations missing from the context.
14 *
15 * The candidates are the places the name can resolve to once the missing
16 * inputs are known. A known candidate in a farther scope is not chosen while a
17 * nearer scope is incompletely known.
18 *
19 * @visibility public
20 * @example Keeping the candidate occurrence of an undeclared table
21 *     $query = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('SELECT a FROM t');
22 *     $resolution = $query->field('a')->resolution;
23 *     [$resolution->relations[0] === $query->inputRelation(), $resolution->missing[0]->describe()] // => [true, 'the declaration of relation t']
24 */
25final class ConditionalColumn implements Resolution
26{
27    use Snapshot;
28
29    /**
30     * @var list<ResolvedColumn> The known slots the name can resolve to
31     */
32    public readonly array $candidates;
33
34    /**
35     * @var list<\SqlSemantics\Platform\Sqlite\Model\Statement\Relation> The incompletely known occurrences that can own the name
36     */
37    public readonly array $relations;
38
39    /**
40     * @var non-empty-list<MissingInput> The inputs needed to decide the resolution
41     */
42    public readonly array $missing;
43
44    /**
45     * @param Name $name The column name
46     * @param list<ResolvedColumn> $candidates The known slots the name can resolve to
47     * @param list<\SqlSemantics\Platform\Sqlite\Model\Statement\Relation> $relations The incompletely known occurrences that can own the name
48     * @param list<MissingInput> $missing The inputs needed to decide the resolution; at least one
49     */
50    public function __construct(public readonly Name $name, array $candidates, array $relations, array $missing)
51    {
52        $this->candidates = Check::listOf($candidates, ResolvedColumn::class, 'Conditional candidates are resolved columns.');
53        $this->relations = Check::listOf($relations, \SqlSemantics\Platform\Sqlite\Model\Statement\Relation::class, 'Conditional owners are relation occurrences.');
54        $this->missing = Check::listOf($missing, MissingInput::class, 'A conditional column names its missing inputs.', 1);
55    }
56}
57