packages/sql-semantics-sqlite/src/Statement/Schema/AlterDropColumn.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Statement\Schema;
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\Statement\Identifier\Name;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Snapshot;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Statement;
15use SqlSemantics\Platform\Sqlite\Rules\Definition\AlterationProblems;
16use SqlSemantics\Platform\Sqlite\Rules\Definition\ObjectNames;
17use SqlSemantics\Platform\Sqlite\Rules\Definition\RelationKinds;
18use SqlSemantics\Platform\Sqlite\Rules\Definition\TableShapes;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\Refusal\KindRefusal;
20
21/**
22 * A request to remove a column from a table.
23 *
24 * Rule: SQLITE-ALTER-DROP-COLUMN-001. The resolution of the table is the
25 * relation fact of the statement node. When the column list of the table is
26 * completely known, a name that is no column of it and the removal of its
27 * only column are diagnostics. The other reasons SQLite refuses a removal (a
28 * primary key, unique, indexed or referenced column) need constraints and
29 * indexes a declaration context does not hold. The optional COLUMN keyword
30 * is not written. The statement changes no declaration and provides none.
31 * Source: https://sqlite.org/lang_altertable.html#alter_table_drop_column.
32 * Status: Implemented.
33 *
34 * @visibility public
35 * @example Reading a column removal
36 *     $alter = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('ALTER TABLE t DROP COLUMN b');
37 *     [$alter->statement->column->value, $alter->toString()] // => ['b', 'ALTER TABLE t DROP b']
38 */
39final class AlterDropColumn implements Statement
40{
41    use Snapshot;
42
43    /**
44     * @param QualifiedName $table The table to change
45     * @param Name $column The column to remove
46     */
47    public function __construct(public readonly QualifiedName $table, public readonly Name $column)
48    {
49        Check::input($table->catalog === null, 'A table name has at most a schema qualifier.');
50    }
51
52    /**
53     * Records the resolution of the table and reports a removal the declared columns rule out.
54     */
55    public function deriveStatement(Derivation $derivation): void
56    {
57        $fact = $derivation->target($this, (new TableShapes())->target($derivation, $this->table));
58        if (!(new RelationKinds())->refuse($fact, KindRefusal::DropColumn, $derivation)) {
59            (new AlterationProblems())->dropped($fact, $this->column, $derivation);
60        }
61    }
62
63    /**
64     * Writes the request.
65     */
66    public function render(Output $out): void
67    {
68        $out->keyword('ALTER', 'TABLE');
69        (new ObjectNames())->write($out, $this->table);
70        $out->keyword('DROP')->name($this->column, NameUse::Column);
71    }
72}
73