packages/sql-semantics-sqlite/src/Rules/Definition/TableShapes.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Rules\Definition;
6
7use SqlSemantics\Platform\Sqlite\Model\Construction\Derivation;
8use SqlSemantics\Platform\Sqlite\Model\Resolution\Environment;
9use SqlSemantics\Platform\Sqlite\Model\Resolution\ImplicitSlot;
10use SqlSemantics\Platform\Sqlite\Model\Resolution\VisibleRelation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\RelationFact;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\IncompleteMembers;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\ConditionalTable;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\DeclaredTable;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\UndeclaredTable;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Relation;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OutputSlot;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\RowShape;
21use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
22
23/**
24 * Derives the row shape of the table a definition or administration command names, and the scope of its expressions.
25 *
26 * Rule: SQLITE-DEFINITION-TARGET-001. The name resolves by
27 * CORE-TABLE-LOOKUP-001. A declared table contributes one slot per declared
28 * column, in order, each referring to the declaration, and an open shape
29 * when its column list is incomplete; an undeclared or conditionally
30 * resolved name contributes an open shape that names the missing
31 * declaration; a missing or conflicting name contributes an empty complete
32 * shape and is a diagnostic. The implicit columns of a declared table (the
33 * row identifier names) are found by name only and refer to the slot of the
34 * declared column they alias, when they alias one. Precision: every slot of a
35 * declared table is a known type. Terminates: one pass over the columns.
36 * Source: https://sqlite.org/lang_naming.html, https://sqlite.org/lang_createtable.html#rowid.
37 * Status: Implemented.
38 *
39 * @visibility SqlSemantics\Platform\Sqlite
40 */
41final class TableShapes
42{
43    /**
44     * Answers the row shape of a declaration.
45     */
46    public function shape(Table $table): RowShape
47    {
48        $slots = [];
49        foreach ($table->columns as $column) {
50            $slots[] = new OutputSlot($column->name, new Known($column->type), $column->nullability, $column);
51        }
52
53        return new RowShape($slots, $table->complete ? [] : [new IncompleteMembers($table)]);
54    }
55
56    /**
57     * Answers the slots found by the implicit column names of a declaration; `$shape` must be its row shape.
58     *
59     * @return list<ImplicitSlot>
60     */
61    public function implicit(Table $table, RowShape $shape): array
62    {
63        $slots = [];
64        foreach ($table->implicit as $implicit) {
65            $position = array_search($implicit->column, $table->columns, true);
66            $slots[] = new ImplicitSlot(
67                $implicit->names,
68                is_int($position) ? $shape->slots[$position] : new OutputSlot($implicit->names[0], new Known($implicit->column->type), $implicit->column->nullability, $implicit->column),
69            );
70        }
71
72        return $slots;
73    }
74
75    /**
76     * Resolves the name of an existing table and answers its facts.
77     */
78    public function target(Derivation $derivation, QualifiedName $name): RelationFact
79    {
80        $resolution = $derivation->table($name, $derivation->environment());
81        if ($resolution instanceof DeclaredTable) {
82            return new RelationFact($this->shape($resolution->table), $resolution);
83        }
84        if ($resolution instanceof UndeclaredTable || $resolution instanceof ConditionalTable) {
85            return new RelationFact(new RowShape([], [$resolution->missing]), $resolution);
86        }
87
88        return new RelationFact(new RowShape([]), $resolution);
89    }
90
91    /**
92     * Answers the implicit slots of the table a relation fact resolved to, if it resolved to one declaration.
93     *
94     * @return list<ImplicitSlot>
95     */
96    public function implicitOf(RelationFact $fact): array
97    {
98        return $fact->table instanceof DeclaredTable ? $this->implicit($fact->table->table, new RowShape(array_slice($fact->shape->slots, 0, count($fact->table->table->columns)))) : [];
99    }
100
101    /**
102     * Answers the scope of the expressions of a definition whose only visible relation is the given table.
103     *
104     * @param Relation $relation The statement node that stands for the table
105     * @param QualifiedName $name The name that qualifies its columns
106     * @param RowShape $shape The row shape of the table
107     * @param list<ImplicitSlot> $implicit The slots found by implicit column names
108     */
109    public function scope(Derivation $derivation, Relation $relation, QualifiedName $name, RowShape $shape, array $implicit): ConstraintScope
110    {
111        $names = [];
112        foreach ($shape->slots as $slot) {
113            if ($slot->name !== null) {
114                $names[] = $slot->name;
115            }
116        }
117
118        return new ConstraintScope(
119            new Environment($derivation->context, null, [new VisibleRelation($relation, $shape, null, $name, [], $implicit)]),
120            new Environment($derivation->context, null, [new VisibleRelation($relation, $shape, null, $name)]),
121            $derivation->environment(),
122            $shape->complete() ? $names : null,
123            $derivation->context->columnNames,
124        );
125    }
126}
127