packages/sql-semantics-postgres/src/Rules/Table/Targets.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Rules\Table;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
9use SqlSemantics\Platform\PostgreSql\Model\Resolution\ImplicitSlot;
10use SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\RelationFact;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\IncompleteMembers;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRelation;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\ConditionalTable;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\MissingTable;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\UndeclaredTable;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Relation;
22use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
23use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape;
24use SqlSemantics\Platform\PostgreSql\Rules\Typing\DeclaredTyping;
25
26/**
27 * Resolves the relation a definition or command names, and builds the scope its expressions see.
28 *
29 * Rule: PG-TABLE-TARGET-001. The name resolves by CORE-TABLE-LOOKUP-001
30 * along the search path (the temporary schema and `pg_catalog` first). A
31 * declared relation contributes one slot per declared column, in order, each
32 * referring to the declaration, and an open shape when its column list is
33 * incomplete; an undeclared or conditionally resolved name contributes an
34 * open shape that names the missing declaration; a missing or conflicting
35 * name contributes an empty shape and is a diagnostic, except that a missing
36 * relation of a statement written with IF EXISTS is no error ("a notice is
37 * issued instead"). The implicit columns
38 * of a declaration (the system columns) are found by name only. The scope of
39 * a definition's expressions has that relation as its only visible relation,
40 * named by the relation name.
41 * Source: https://www.postgresql.org/docs/17/ddl-schemas.html#DDL-SCHEMAS-PATH,
42 * https://www.postgresql.org/docs/17/ddl-system-columns.html. Termination: one
43 * pass over the columns. Status: Implemented.
44 *
45 * @visibility SqlSemantics\Platform\PostgreSql
46 */
47final class Targets
48{
49 /**
50 * Resolves the name of an existing relation and answers its facts.
51 */
52 public function resolve(Derivation $derivation, QualifiedName $name): RelationFact
53 {
54 $resolution = $derivation->table($name, $derivation->environment());
55 if ($resolution instanceof DeclaredTable) {
56 return new RelationFact($this->shape($resolution->table), $resolution);
57 }
58 if ($resolution instanceof UndeclaredTable || $resolution instanceof ConditionalTable) {
59 return new RelationFact(new RowShape([], [$resolution->missing]), $resolution);
60 }
61
62 return new RelationFact(new RowShape([]), $resolution);
63 }
64
65 /**
66 * Resolves the relation of a statement that may be written with IF EXISTS.
67 *
68 * With IF EXISTS a relation that does not exist is not an error: the
69 * server issues a notice and skips the statement. The actions then have
70 * no relation to be checked against, so the shape is open on the
71 * declaration of the named relation and nothing is reported.
72 */
73 public function existing(Derivation $derivation, QualifiedName $name, bool $ifExists): RelationFact
74 {
75 $fact = $this->resolve($derivation, $name);
76
77 return $ifExists && $fact->table instanceof MissingTable ? new RelationFact(new RowShape([], [new UndeclaredRelation($name)])) : $fact;
78 }
79
80 /**
81 * Answers the row shape of a declaration.
82 */
83 public function shape(Table $table): RowShape
84 {
85 $slots = [];
86 foreach ($table->columns as $column) {
87 $slots[] = new OutputSlot($column->name, (new DeclaredTyping())->fact($column->type), $column->nullability, $column);
88 }
89
90 return new RowShape($slots, $table->complete ? [] : [new IncompleteMembers($table)]);
91 }
92
93 /**
94 * Answers the slots found by the implicit column names of the declaration a fact resolved to.
95 *
96 * @return list<ImplicitSlot>
97 */
98 public function implicit(RelationFact $fact): array
99 {
100 $slots = [];
101 foreach ($fact->table instanceof DeclaredTable ? $fact->table->table->implicit : [] as $implicit) {
102 $column = $implicit->column;
103 $slots[] = new ImplicitSlot($implicit->names, new OutputSlot($column->name, (new DeclaredTyping())->fact($column->type), $column->nullability, $column));
104 }
105
106 return $slots;
107 }
108
109 /**
110 * Answers an environment whose only visible relation is the given one.
111 *
112 * @param Relation $relation The node that stands for the relation
113 * @param QualifiedName $name The name that qualifies its columns
114 * @param RowShape $shape The row shape of the relation
115 * @param list<ImplicitSlot> $implicit The slots found by implicit column names
116 * @param Name|null $alias The correlation name that replaces the relation name, as OLD and NEW do
117 */
118 public function scope(Derivation $derivation, Relation $relation, QualifiedName $name, RowShape $shape, array $implicit = [], ?Name $alias = null): Environment
119 {
120 return new Environment($derivation->context, null, [new VisibleRelation($relation, $shape, $alias, $alias === null ? $name : null, [], $implicit)]);
121 }
122}
123