packages/sql-semantics-postgres/src/Statement/Relation/TableInput.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Relation;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
10use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\RelationFact;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\NamedRelation;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
16use SqlSemantics\Platform\PostgreSql\Rules\Query\AliasSpelling;
17use SqlSemantics\Platform\PostgreSql\Rules\Resolution\TableShapes;
18use SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference;
19
20/**
21 * One occurrence of a named table, view or common table as query input.
22 *
23 * Mirrors PostgreSQL's `RangeVar` with its `Alias` and an optional
24 * `RangeTableSample`. The facts follow PG-TABLE-SHAPE-001.
25 * Source: https://www.postgresql.org/docs/17/sql-select.html#SQL-FROM. Status: Implemented.
26 *
27 * @visibility public
28 * @example Reading a named input
29 *     $input = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('SELECT a FROM app.t AS x')->singleNamedInput();
30 *     [$input->name()->schema?->value, $input->name()->name->value, $input->alias()?->value] // => ['app', 't', 'x']
31 * @example Refusing column names without a correlation name
32 *     new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'))), null, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a')]) // throws \SqlSemantics\Platform\PostgreSql\Model\Diagnostic\InvalidConstruction
33 */
34final class TableInput implements NamedRelation
35{
36    use Snapshot;
37
38    /**
39     * @var list<Name> The column names written after the correlation name
40     */
41    public readonly array $columns;
42
43    /**
44     * @param RelationReference $table The relation named
45     * @param Name|null $alias The correlation name
46     * @param list<Name> $columns The column names written after the correlation name
47     * @param TableSample|null $sample The TABLESAMPLE clause
48     */
49    public function __construct(public readonly RelationReference $table, public readonly ?Name $alias = null, array $columns = [], public readonly ?TableSample $sample = null)
50    {
51        $this->columns = Check::listOf($columns, Name::class, 'Column aliases are names.');
52        Check::input($alias !== null || $this->columns === [], 'Column aliases are written after a correlation name.');
53    }
54
55    /**
56     * Answers the relation name.
57     */
58    public function name(): QualifiedName
59    {
60        return $this->table->name;
61    }
62
63    /**
64     * Answers the correlation name.
65     */
66    public function alias(): ?Name
67    {
68        return $this->alias;
69    }
70
71    /**
72     * Resolves the name and derives the row shape of the occurrence.
73     */
74    public function deriveRelation(Derivation $derivation, Environment $environment): RelationFact
75    {
76        return (new TableShapes())->input($this, $derivation, $environment);
77    }
78
79    /**
80     * Writes the relation, the alias and the sample.
81     */
82    public function render(Output $out): void
83    {
84        $out->node($this->table);
85        (new AliasSpelling())->write($out, $this->alias, $this->columns);
86        $out->node($this->sample);
87    }
88}
89