packages/sql-semantics-postgres/src/Rules/Catalog/PublishedTables.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Rules\Catalog;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
9use SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\RelationFact;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\IncompleteMembers;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\ConditionalTable;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\UndeclaredTable;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape;
17use SqlSemantics\Platform\PostgreSql\Rules\Typing\DeclaredTyping;
18use SqlSemantics\Platform\PostgreSql\Statement\Catalog\Problem\CatalogMisuse;
19use SqlSemantics\Platform\PostgreSql\Statement\Catalog\Problem\CatalogMisuseRule;
20use SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\PublicationTable;
21
22/**
23 * Derives the facts of a table published by CREATE or ALTER PUBLICATION.
24 *
25 * Rule: PG-PUBLICATION-TABLE-001. The table name is resolved along the
26 * schema search path (CORE-TABLE-LOOKUP-001); a publication sees no common
27 * table. A declared table contributes one slot per declared column and is
28 * open when its column list is incomplete; an undeclared or conditionally
29 * resolved name contributes an open shape naming the missing declaration; a
30 * missing or conflicting name is a diagnostic. The column list names
31 * columns of the table: against a declared table, a column it does not have
32 * (when its columns are complete), a system column and a repeated column are
33 * diagnostics. The row filter is evaluated with the table visible under its
34 * name, as the server adds it to the filter's namespace.
35 * Termination: one pass over the finite column list.
36 * Source: https://www.postgresql.org/docs/17/sql-createpublication.html,
37 * https://www.postgresql.org/docs/17/logical-replication-row-filter.html,
38 * https://www.postgresql.org/docs/17/logical-replication-col-lists.html. Status: Implemented.
39 *
40 * @visibility SqlSemantics\Platform\PostgreSql
41 */
42final class PublishedTables
43{
44 /**
45 * Resolves the table, checks the column list, derives the row filter and answers the facts of the occurrence.
46 */
47 public function derive(PublicationTable $table, Derivation $derivation, Environment $environment): RelationFact
48 {
49 $resolution = $derivation->table($table->table->name, new Environment($environment->context));
50 $shape = new RowShape([]);
51 if ($resolution instanceof DeclaredTable) {
52 $slots = [];
53 foreach ($resolution->table->columns as $column) {
54 $slots[] = new OutputSlot($column->name, (new DeclaredTyping())->fact($column->type), $column->nullability, $column);
55 }
56 $shape = new RowShape($slots, $resolution->table->complete ? [] : [new IncompleteMembers($resolution->table)]);
57 $this->columns($table, $resolution, $derivation);
58 } elseif ($resolution instanceof UndeclaredTable || $resolution instanceof ConditionalTable) {
59 $shape = new RowShape([], [$resolution->missing]);
60 }
61 if ($table->where !== null) {
62 $derivation->scalar($table->where, new Environment($environment->context, null, [new VisibleRelation($table, $shape, null, $table->table->name)]));
63 }
64
65 return new RelationFact($shape, $resolution);
66 }
67
68 /**
69 * Reports the names of the column list a declared table does not offer, and repeated names.
70 */
71 public function columns(PublicationTable $table, DeclaredTable $resolution, Derivation $derivation): void
72 {
73 $seen = [];
74 $comparison = $derivation->context->columnNames;
75 foreach ($table->columns as $column) {
76 $matches = $resolution->table->matchingColumns($column->value, $comparison);
77 if ($matches !== [] && !in_array($matches[0], $resolution->table->columns, true)) {
78 $derivation->report(new CatalogMisuse(CatalogMisuseRule::PublicationSystemColumn, [$column->value]));
79 } elseif ($matches === [] && $resolution->table->complete) {
80 $derivation->report(new CatalogMisuse(CatalogMisuseRule::PublicationMissingColumn, [$column->value, $resolution->table->name->name->value]));
81 }
82 foreach ($seen as $earlier) {
83 if ($comparison->equal($earlier, $column->value)) {
84 $derivation->report(new CatalogMisuse(CatalogMisuseRule::PublicationDuplicateColumn, [$column->value]));
85 }
86 }
87 $seen[] = $column->value;
88 }
89 }
90}
91