packages/sql-semantics-mysql/src/Statement/Name/ColumnUse.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Name;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Contract\NameUse;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
10use SqlSemantics\Platform\MySql\Model\Rendering\Output;
11use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
12use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
16use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
17use SqlSemantics\Platform\MySql\Rendering\LeadingDot;
18use SqlSemantics\Platform\MySql\Rules\ColumnResolver;
19use SqlSemantics\Platform\MySql\Statement\Expression\OptionalWords;
20
21/**
22 * A name used as a value: a request to resolve it as a column at its position.
23 *
24 * The node holds the decoded column name and its optional table and database
25 * qualifiers. Which occurrence and declaration it denotes is a fact of the
26 * operation that contains it.
27 *
28 * Rule: MYSQL-COLUMN-USE-001. Facts: the resolution of MYSQL-COLUMN-LOOKUP-001
29 * over the relation occurrences of the position and, after them, the select
30 * list aliases the position may use; a use resolved to a slot or to an
31 * aliased item has the type and NULL fact of that slot or item; a
32 * conditional use depends on its missing inputs; a missing or ambiguous use
33 * is invalid and is a diagnostic. Terminates: the lookup is finite.
34 * Source: https://dev.mysql.com/doc/refman/8.4/en/identifier-qualifiers.html,
35 * https://dev.mysql.com/doc/refman/8.4/en/problems-with-alias.html.
36 * Status: Implemented.
37 *
38 * @visibility public
39 * @example Reading a qualified column use
40 *     $query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SELECT shop.t.a FROM shop.t');
41 *     $use = $query->statement->items[0]->expression;
42 *     [$use->qualifier?->schema?->value, $use->qualifier?->name->value, $use->name->value] // => ['shop', 't', 'a']
43 */
44final class ColumnUse implements Scalar
45{
46    use Snapshot;
47
48    /**
49     * @param Name $name The column name
50     * @param QualifiedName|null $qualifier The table the use is qualified with, and the database of that table when written
51     * @param OptionalWords $dot Whether the table qualifier is written after a leading dot, `.t.a` (MySQL 5.6 and 5.7)
52     */
53    public function __construct(public readonly Name $name, public readonly ?QualifiedName $qualifier = null, public readonly OptionalWords $dot = OptionalWords::Omitted)
54    {
55        Check::input($qualifier === null || $qualifier->catalog === null, 'A column is qualified by a table and at most a database.');
56        Check::input($dot === OptionalWords::Omitted || ($qualifier !== null && $qualifier->schema === null), 'Only a table qualifier without database is written after a leading dot.');
57    }
58
59    /**
60     * Resolves the name in the environment and derives its facts from the slot found.
61     */
62    public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
63    {
64        return (new ColumnResolver())->fact($environment, $this->name, $this->qualifier);
65    }
66
67    /**
68     * Writes the qualifier parts and the name.
69     */
70    public function render(Output $out): void
71    {
72        if ($this->dot === OptionalWords::Written) {
73            (new LeadingDot())->write($out);
74        }
75        if ($this->qualifier?->schema !== null) {
76            $out->name($this->qualifier->schema, NameUse::Qualifier)->symbol('.');
77        }
78        if ($this->qualifier !== null) {
79            $out->name($this->qualifier->name, NameUse::Qualifier)->symbol('.');
80        }
81        $out->name($this->name, NameUse::Column);
82    }
83}
84