packages/sql-semantics-mysql/src/Rules/TableDefinition/ElementFacts.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Rules\TableDefinition;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
10use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
13use SqlSemantics\Platform\MySql\Statement\Table\Column\ColumnDefinition;
14use SqlSemantics\Platform\MySql\Statement\Table\Column\GeneratedColumn;
15use SqlSemantics\Platform\MySql\Statement\Table\Column\OrdinaryColumn;
16use SqlSemantics\Platform\MySql\Statement\Table\ColumnSpecification;
17use SqlSemantics\Platform\MySql\Statement\Table\Key\ForeignKey;
18use SqlSemantics\Platform\MySql\Statement\Table\Key\References;
19use SqlSemantics\Platform\MySql\Statement\Table\TableElement;
20use SqlSemantics\Platform\MySql\Statement\Table\TableOption;
21
22/**
23 * Derives the facts inside table elements, column specifications and table options, for CREATE TABLE and ALTER TABLE.
24 *
25 * Rule: MYSQL-ELEMENT-FACTS-001. The expressions of an element are derived
26 * at the position the caller gives, whose only visible relation is the
27 * table being defined or changed (MYSQL-DEFINITION-SCOPE-001). The parent
28 * table of a REFERENCES clause is resolved and recorded as a table use
29 * (MYSQL-REFERENCES-001). Table options hold no expression and no
30 * resolvable name: the tables of a MERGE UNION option are named, not read,
31 * when the table is defined. Terminates: one pass over the element.
32 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-table.html,
33 * https://dev.mysql.com/doc/refman/8.4/en/alter-table.html. Status: Implemented.
34 *
35 * @visibility SqlSemantics\Platform\MySql
36 */
37final class ElementFacts
38{
39    /**
40     * Derives an element; a reference to the table being defined resolves to `$defined` when it is given.
41     *
42     * @param Table|null $defined The declaration the statement provides for the table being defined
43     * @param QualifiedName|null $definedName The name the statement writes for that table
44     */
45    public function element(TableElement $element, Derivation $derivation, Environment $scope, ?Table $defined = null, ?QualifiedName $definedName = null): void
46    {
47        $element->deriveElement($derivation, $scope);
48        $references = $element instanceof ForeignKey ? $element->references : ($element instanceof ColumnDefinition ? $this->references($element->specification) : null);
49        $references?->deriveReferences($derivation, $defined, $definedName);
50    }
51
52    /**
53     * Derives a column specification held by an ALTER TABLE.
54     */
55    public function specification(ColumnSpecification $specification, Derivation $derivation, Environment $scope): void
56    {
57        $specification->deriveSpecification($derivation, $scope);
58        $this->references($specification)?->deriveReferences($derivation);
59    }
60
61    /**
62     * Confirms a list of table options; they hold nothing to derive.
63     *
64     * @param list<TableOption> $options
65     */
66    public function options(array $options, Derivation $derivation): void
67    {
68        Check::listOf($options, TableOption::class, 'Table options are an ordered list of table options.');
69    }
70
71    /**
72     * Answers the column declaration a column definition requests, as CREATE TABLE declares it without a table-level primary key.
73     */
74    public function declaration(ColumnDefinition $column): Column
75    {
76        return (new TableDeclaration())->column($column);
77    }
78
79    /**
80     * Answers the inline REFERENCES clause of a column specification.
81     */
82    public function references(ColumnSpecification $specification): ?References
83    {
84        return $specification instanceof OrdinaryColumn || $specification instanceof GeneratedColumn ? $specification->references : null;
85    }
86}
87