packages/sql-semantics-mysql/src/Statement/Alter/DropTable.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Alter;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\MySql\Model\Statement\Statement;
12use SqlSemantics\Platform\MySql\Rules\TableChange\Targets;
13use SqlSemantics\Platform\MySql\Rules\TableDefinition\RelationKinds;
14use SqlSemantics\Platform\MySql\Statement\Alter\Problem\RepeatedTable;
15use SqlSemantics\Platform\MySql\Statement\Table\Problem\KindRefusal;
16
17/**
18 * `DROP [TEMPORARY] TABLE [IF EXISTS] t, … [RESTRICT | CASCADE]`: a request to remove tables.
19 *
20 * Mirrors PT_drop_table_stmt. Rule: MYSQL-DROP-TABLE-001. Each table name
21 * resolves by MYSQL-CHANGE-TARGET-001 and its resolution is the relation fact
22 * of its TargetTable; under IF EXISTS a name a complete context does not
23 * declare resolves to AbsentTable. A table named twice is the diagnostic
24 * RepeatedTable. A declared view is not a table DROP TABLE finds: without
25 * IF EXISTS it is refused (MYSQL-RELATION-KIND-001). TEMPORARY restricts the request to temporary tables, which a
26 * declaration context does not distinguish. RESTRICT and CASCADE have no
27 * effect and are kept as written. The statement removes no declaration and
28 * provides none. TABLE and TABLES are synonyms.
29 * Source: https://dev.mysql.com/doc/refman/8.4/en/drop-table.html.
30 * Status: Implemented.
31 *
32 * @visibility public
33 * @example Resolving the dropped table to its declaration
34 * $semantics = new \SqlSemantics\Platform\MySql\Semantics();
35 * $drop = $semantics->analyze('drop tables if exists t, u cascade', []);
36 * [$drop->toString(), $drop->statement->behavior] // => ['DROP TABLE IF EXISTS t, u CASCADE', \SqlSemantics\Platform\MySql\Statement\Alter\DropBehavior::Cascade]
37 */
38final class DropTable implements Statement
39{
40 use Snapshot;
41
42 /**
43 * @var list<TargetTable> The tables in order; at least one
44 */
45 public readonly array $tables;
46
47 /**
48 * @param bool $temporary Whether TEMPORARY is written
49 * @param bool $ifExists Whether IF EXISTS is written
50 * @param list<TargetTable> $tables The tables in order; at least one
51 * @param DropBehavior|null $behavior RESTRICT or CASCADE, when written
52 */
53 public function __construct(public readonly bool $temporary, public readonly bool $ifExists, array $tables, public readonly ?DropBehavior $behavior = null)
54 {
55 Check::input($tables !== [], 'DROP TABLE names at least one table.');
56 $this->tables = Check::listOf($tables, TargetTable::class, 'DROP TABLE names a list of tables.');
57 }
58
59 /**
60 * Records the resolution of each table and reports a table named twice.
61 */
62 public function deriveStatement(Derivation $derivation): void
63 {
64 $targets = new Targets();
65 foreach ($this->tables as $index => $table) {
66 $fact = $derivation->target($table, $targets->optional($derivation, $table->name, $this->ifExists));
67 if (!$this->ifExists) {
68 (new RelationKinds())->refuseView($derivation, $table->name, $fact->table, KindRefusal::UnknownTable);
69 }
70 foreach (array_slice($this->tables, 0, $index) as $earlier) {
71 if ($targets->same($derivation->context, $earlier->name, $table->name)) {
72 $derivation->report(new RepeatedTable($table->name));
73 break;
74 }
75 }
76 }
77 }
78
79 /**
80 * Writes the request.
81 */
82 public function render(Output $out): void
83 {
84 $out->keyword('DROP');
85 if ($this->temporary) {
86 $out->keyword('TEMPORARY');
87 }
88 $out->keyword('TABLE');
89 if ($this->ifExists) {
90 $out->keyword('IF', 'EXISTS');
91 }
92 $out->list($this->tables);
93 if ($this->behavior !== null) {
94 $out->keyword($this->behavior->value);
95 }
96 }
97}
98