packages/sql-semantics-mysql/src/Statement/Server/Database/AlterDatabase.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Server\Database;
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\Diagnostic\InvalidConstruction;
11use SqlSemantics\Platform\MySql\Model\Rendering\Output;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
14use SqlSemantics\Platform\MySql\Model\Statement\Statement;
15
16/**
17 * `ALTER {DATABASE | SCHEMA} [name] option …`: a request to change the options of a database.
18 *
19 * Mirrors SQLCOM_ALTER_DB. Rule: MYSQL-ALTER-DATABASE-001. Without a name
20 * the current database is altered. The options are kept in written order.
21 * The statement changes no declaration.
22 * Source: https://dev.mysql.com/doc/refman/8.4/en/alter-database.html.
23 * Status: Implemented.
24 *
25 * @visibility public
26 * @example Altering the current database
27 * $alter = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('alter schema character set latin1 read only default');
28 * [$alter->toString(), $alter->statement->name] // => ['ALTER DATABASE CHARACTER SET latin1 READ ONLY DEFAULT', null]
29 */
30final class AlterDatabase implements Statement
31{
32 use Snapshot;
33
34 /**
35 * @var list<DatabaseOption> The options in written order; at least one
36 */
37 public readonly array $options;
38
39 /**
40 * @param Name|null $name The database name; null for the current database
41 * @param list<DatabaseOption> $options The options in written order; at least one
42 * @throws InvalidConstruction When there is no option
43 */
44 public function __construct(public readonly ?Name $name, array $options)
45 {
46 $this->options = Check::listOf($options, DatabaseOption::class, 'ALTER DATABASE changes at least one option.', 1);
47 }
48
49 /**
50 * Has nothing to derive: a database is no relation.
51 */
52 public function deriveStatement(Derivation $derivation): void
53 {
54 }
55
56 /**
57 * Writes the request.
58 */
59 public function render(Output $out): void
60 {
61 $out->keyword('ALTER', 'DATABASE');
62 if ($this->name !== null) {
63 $out->name($this->name, NameUse::Qualifier);
64 }
65 foreach ($this->options as $option) {
66 $out->node($option);
67 }
68 }
69}
70