packages/sql-semantics-mysql/src/Statement/Alter/Problem/TableExists.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Alter\Problem;
6
7use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
8use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
9use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
10
11/**
12 * A new table name that a declared table, or a table an earlier rename of the same statement created, already has.
13 *
14 * The server rejects the rename with ER_TABLE_EXISTS_ERROR.
15 * Source: https://dev.mysql.com/doc/refman/8.4/en/rename-table.html.
16 *
17 * @visibility public
18 * @example Describing the problem
19 *     (new \SqlSemantics\Platform\MySql\Statement\Alter\Problem\TableExists(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name('t'))))->message() // => 'Table t already exists.'
20 */
21final class TableExists implements Diagnostic
22{
23    use Snapshot;
24
25    /**
26     * @param QualifiedName $name The new name as the statement wrote it
27     */
28    public function __construct(public readonly QualifiedName $name)
29    {
30    }
31
32    /**
33     * Describes the problem.
34     */
35    public function message(): string
36    {
37        return 'Table ' . ($this->name->schema === null ? '' : $this->name->schema->value . '.') . $this->name->name->value . ' already exists.';
38    }
39}
40