packages/sql-semantics-mysql/src/Statement/Server/Database/DatabaseCharset.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Server\Database;
6
7use SqlSemantics\Platform\MySql\Model\Rendering\Output;
8use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
9use SqlSemantics\Platform\MySql\Statement\Name\CharsetName;
10
11/**
12 * `[DEFAULT] CHARACTER SET [=] charset`: the default character set of the database's tables.
13 *
14 * DEFAULT before the option and the equals sign are optional and not
15 * written; CHARSET and CHARACTER SET are synonyms and CHARACTER SET is
16 * written, because CHARSET right after ALTER DATABASE would be read as the
17 * database name.
18 * The name DEFAULT, accepted by MySQL 5.x, takes the server's character
19 * set.
20 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-database.html.
21 *
22 * @visibility public
23 * @example Reading the character set
24 * $create = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('CREATE DATABASE d DEFAULT CHARACTER SET = latin1');
25 * [$create->toString(), $create->statement->options[0]->charset->name?->value] // => ['CREATE DATABASE d CHARACTER SET latin1', 'latin1']
26 */
27final class DatabaseCharset implements DatabaseOption
28{
29 use Snapshot;
30
31 /**
32 * @param CharsetName $charset The character set
33 */
34 public function __construct(public readonly CharsetName $charset)
35 {
36 }
37
38 /**
39 * Writes the option.
40 */
41 public function render(Output $out): void
42 {
43 $out->keyword('CHARACTER', 'SET')->node($this->charset);
44 }
45}
46