packages/sql-semantics-mysql/src/Statement/Account/CreateUser.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Account;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\MySql\Model\Statement\Statement;
12use SqlSemantics\Platform\MySql\Rules\Account\AccountChecks;
13use SqlSemantics\Platform\MySql\Statement\Account\Option\AccountOption;
14use SqlSemantics\Platform\MySql\Statement\Account\Option\ResourceLimit;
15use SqlSemantics\Platform\MySql\Statement\Account\Option\TlsRequirement;
16use SqlSemantics\Platform\MySql\Statement\Account\Option\UserComment;
17use SqlSemantics\Platform\MySql\Statement\Account\User\UserSpecification;
18use SqlSemantics\Platform\MySql\Statement\Name\AccountName;
19
20/**
21 * `CREATE USER [IF NOT EXISTS] account [auth], … [DEFAULT ROLE …] [REQUIRE …] [WITH limits] [options] [ATTRIBUTE|COMMENT '…']`: a request to create accounts.
22 *
23 * Mirrors SQLCOM_CREATE_USER (LEX users_list, default_roles, ssl_type, mqh,
24 * alter_password, alter_user_attribute). Rule: MYSQL-CREATE-USER-001. The
25 * accounts, their authentication methods and every clause are kept in
26 * source order; passwords keep their exact values. The problems of the
27 * clauses are reported by MYSQL-ACCOUNT-CHECKS-001. An account is not a
28 * relation: the statement provides no declaration.
29 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-user.html. Status: Implemented.
30 *
31 * @visibility public
32 * @example Creating an account with a password and a lock
33 * (new \SqlSemantics\Platform\MySql\Semantics())->analyze("create user if not exists app@'%' identified by 'pw' account lock")->toString() // => "CREATE USER IF NOT EXISTS app@`%` IDENTIFIED BY 'pw' ACCOUNT LOCK"
34 */
35final class CreateUser implements Statement
36{
37 use Snapshot;
38
39 /**
40 * @var list<UserSpecification> The accounts in order
41 */
42 public readonly array $users;
43
44 /**
45 * @var list<AccountName> The roles of DEFAULT ROLE; none when the clause is absent
46 */
47 public readonly array $defaultRoles;
48
49 /**
50 * @var list<ResourceLimit> The resource limits of the WITH clause in order
51 */
52 public readonly array $resources;
53
54 /**
55 * @var list<AccountOption> The password and locking options in order
56 */
57 public readonly array $options;
58
59 /**
60 * @param bool $ifNotExists Whether IF NOT EXISTS is written
61 * @param list<UserSpecification> $users The accounts in order; at least one
62 * @param list<AccountName> $defaultRoles The roles of DEFAULT ROLE; none when the clause is absent
63 * @param TlsRequirement|null $tls The REQUIRE clause, when written
64 * @param list<ResourceLimit> $resources The resource limits of the WITH clause in order
65 * @param list<AccountOption> $options The password and locking options in order
66 * @param UserComment|null $comment The ATTRIBUTE or COMMENT clause, when written
67 */
68 public function __construct(
69 public readonly bool $ifNotExists,
70 array $users,
71 array $defaultRoles = [],
72 public readonly ?TlsRequirement $tls = null,
73 array $resources = [],
74 array $options = [],
75 public readonly ?UserComment $comment = null,
76 ) {
77 $this->users = Check::listOf($users, UserSpecification::class, 'CREATE USER names at least one account.', 1);
78 $this->defaultRoles = Check::listOf($defaultRoles, AccountName::class, 'DEFAULT ROLE names roles.');
79 $this->resources = Check::listOf($resources, ResourceLimit::class, 'The WITH clause lists resource limits.');
80 $this->options = Check::listOf($options, AccountOption::class, 'The account options are a list.');
81 foreach ($this->users as $user) {
82 Check::input($user->created(), 'CREATE USER names accounts without password management words.');
83 }
84 }
85
86 /**
87 * Reports the problems of the clauses.
88 */
89 public function deriveStatement(Derivation $derivation): void
90 {
91 $checks = new AccountChecks();
92 $checks->tls($derivation, $this->tls);
93 $checks->options($derivation, $this->options);
94 $checks->comment($derivation, $this->comment);
95 }
96
97 /**
98 * Writes the request.
99 */
100 public function render(Output $out): void
101 {
102 $out->keyword('CREATE', 'USER');
103 if ($this->ifNotExists) {
104 $out->keyword('IF', 'NOT', 'EXISTS');
105 }
106 $out->list($this->users);
107 if ($this->defaultRoles !== []) {
108 $out->keyword('DEFAULT', 'ROLE')->list($this->defaultRoles);
109 }
110 $out->node($this->tls);
111 if ($this->resources !== []) {
112 $out->keyword('WITH');
113 foreach ($this->resources as $resource) {
114 $out->node($resource);
115 }
116 }
117 foreach ($this->options as $option) {
118 $out->node($option);
119 }
120 $out->node($this->comment);
121 }
122}
123