packages/sql-semantics-mysql/src/Statement/Utility/Set/SetCharacterSet.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Utility\Set;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Rendering\Output;
9use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
10use SqlSemantics\Platform\MySql\Statement\Name\CharsetName;
11
12/**
13 * SET CHARACTER SET: sets the client and result character sets and makes the connection use the database character set.
14 *
15 * Rule: MYSQL-SET-CHARSET-001. `SET CHARACTER SET cs` sets
16 * character_set_client and character_set_results to `cs` and
17 * character_set_connection to the value of character_set_database;
18 * `DEFAULT` restores the defaults. `CHARACTER SET`, `CHAR SET` and
19 * `CHARSET` are the same keyword (LeafNoise); the writer emits CHARSET.
20 * Facts: none. Terminates: the part is a leaf.
21 * Source: https://dev.mysql.com/doc/refman/8.4/en/set-character-set.html.
22 * Status: Implemented.
23 *
24 * @visibility public
25 * @example Reading SET CHARACTER SET
26 * $set = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SET CHARACTER SET DEFAULT');
27 * [$set->statement->items[0]->charset->name, $set->toString()] // => [null, 'SET CHARSET DEFAULT']
28 */
29final class SetCharacterSet implements SetItem
30{
31 use Snapshot;
32
33 /**
34 * @param CharsetName $charset The character set, or DEFAULT
35 */
36 public function __construct(public readonly CharsetName $charset)
37 {
38 }
39
40 /**
41 * Derives nothing: the name is resolved by the server.
42 */
43 public function deriveItem(Derivation $derivation): void
44 {
45 }
46
47 /**
48 * Writes CHARSET and the character set.
49 */
50 public function render(Output $out): void
51 {
52 $out->keyword('CHARSET')->node($this->charset);
53 }
54}
55