packages/sql-semantics-sqlite/src/Statement/Relation/DerivedQuery.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Statement\Relation;
6
7use SqlSemantics\Platform\Sqlite\Model\Construction\Derivation;
8use SqlSemantics\Platform\Sqlite\Model\Contract\NameUse;
9use SqlSemantics\Platform\Sqlite\Model\Diagnostic\Check;
10use SqlSemantics\Platform\Sqlite\Model\Rendering\Output;
11use SqlSemantics\Platform\Sqlite\Model\Resolution\Environment;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\RelationFact;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Query;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Relation;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Snapshot;
17use SqlSemantics\Platform\Sqlite\Rules\Query\RelationNames;
18
19/**
20 * A query in parentheses used as query input.
21 *
22 * Rule: SQLITE-DERIVED-QUERY-001. The query is derived in the environment
23 * that encloses the FROM clause: it sees the enclosing queries and no
24 * sibling of its own FROM clause. Its columns are the result columns of the
25 * query, named by SQLITE-RELATION-NAME-001.
26 * Source: https://sqlite.org/lang_select.html#the_from_clause. Status: Implemented.
27 *
28 * @visibility public
29 * @example Reading a derived query
30 *     $query = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('SELECT x FROM (SELECT 1 AS x) AS d');
31 *     [$query->statement->from->alias->value, $query->field('x')->type->descriptor] // => ['d', \SqlSemantics\Platform\Sqlite\Statement\Type\Storage::Integer]
32 */
33final class DerivedQuery implements Relation
34{
35    use Snapshot;
36
37    /**
38     * @param Query $query The query
39     * @param Name|null $alias The correlation name
40     * @param bool $as Whether the keyword AS introduces the alias; SQLite names a column after the text of an expression, which includes it
41     * @throws \SqlSemantics\Platform\Sqlite\Model\Diagnostic\InvalidConstruction When AS is left out without an alias
42     */
43    public function __construct(public readonly Query $query, public readonly ?Name $alias = null, public readonly bool $as = true)
44    {
45        Check::input($as || $alias !== null, 'AS is left out only before an alias.');
46    }
47
48    /**
49     * Derives the query and the row shape its result gives the occurrence.
50     */
51    public function deriveRelation(Derivation $derivation, Environment $environment): RelationFact
52    {
53        return new RelationFact((new RelationNames())->shape($derivation->query($this->query, $environment), $this->query, $derivation));
54    }
55
56    /**
57     * Writes the query in parentheses and the correlation name.
58     */
59    public function render(Output $out): void
60    {
61        $out->symbol('(')->node($this->query)->symbol(')');
62        if ($this->alias !== null) {
63            if ($this->as) {
64                $out->keyword('AS');
65            }
66            $out->name($this->alias, NameUse::Alias);
67        }
68    }
69}
70