packages/sql-semantics-mysql/src/Statement/Utility/Set/PasswordAssignment.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\Account\Password;
11use SqlSemantics\Platform\MySql\Statement\Name\Account;
12
13/**
14 * A password assignment among other assignments of a MySQL 5.6 SET: `SET @a = 1, PASSWORD FOR u = PASSWORD('x')`.
15 *
16 * Rule: MYSQL-SET-PASSWORD-001. MySQL 5.6 reads `PASSWORD [FOR user] = …`
17 * as one item of the SET list; it assigns the password of the account, or
18 * of the current user without FOR, in order with the other items. A SET
19 * whose only item is a password is the account statement SET PASSWORD of
20 * the account family. Facts: none; the password is a literal or a hashing
21 * call the account family models. Terminates: the parts are leaves.
22 * Source: https://dev.mysql.com/doc/refman/5.6/en/set-password.html,
23 * https://dev.mysql.com/doc/refman/5.6/en/set-variable.html.
24 * Status: Implemented.
25 *
26 * @visibility public
27 * @example Reading a password item of a list
28 * $set = (new \SqlSemantics\Platform\MySql\Semantics('mysql-5.6.51'))->analyze("SET @a = 1, PASSWORD FOR u = 'x'");
29 * [$set->statement->items[1]->user?->user->value, $set->toString()] // => ['u', "SET @a = 1, PASSWORD FOR u = 'x'"]
30 */
31final class PasswordAssignment implements SetItem
32{
33 use Snapshot;
34
35 /**
36 * @param Password $password The new password
37 * @param Account|null $user The account, or null for the current user
38 */
39 public function __construct(public readonly Password $password, public readonly ?Account $user = null)
40 {
41 }
42
43 /**
44 * Derives nothing: the parts are literals and account names.
45 */
46 public function deriveItem(Derivation $derivation): void
47 {
48 }
49
50 /**
51 * Writes PASSWORD, the account, an equals sign and the password.
52 */
53 public function render(Output $out): void
54 {
55 $out->keyword('PASSWORD');
56 if ($this->user !== null) {
57 $out->keyword('FOR')->node($this->user);
58 }
59 $out->symbol('=')->node($this->password);
60 }
61}
62