packages/sql-semantics-mysql/src/Lowering/Utility/Set/ItemRule.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Utility\Set;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\MySql\Model\Lowering\Form;
11use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\MySql\Statement\Name\CharsetName;
13use SqlSemantics\Platform\MySql\Statement\Utility\Set\NameAssignment;
14use SqlSemantics\Platform\MySql\Statement\Utility\Set\NamesExpression;
15use SqlSemantics\Platform\MySql\Statement\Utility\Set\PasswordAssignment;
16use SqlSemantics\Platform\MySql\Statement\Utility\Set\SetCharacterSet;
17use SqlSemantics\Platform\MySql\Statement\Utility\Set\SetItem;
18use SqlSemantics\Platform\MySql\Statement\Utility\Set\SetNames;
19use SqlSemantics\Platform\MySql\Statement\Utility\Set\SystemAssignment;
20use SqlSemantics\Platform\MySql\Statement\Utility\Set\UserAssignment;
21use SqlSemantics\Platform\MySql\Statement\Variable\SystemVariable;
22use SqlSemantics\Platform\MySql\Statement\Variable\VariableScope;
23
24/**
25 * Lowers one item of a SET list.
26 *
27 * Rule: MYSQL-SET-ITEM-LOWERING-001. Scope: option_value_no_option_type,
28 * option_value_following_option_type, internal_variable_name (5.6, 5.7),
29 * and the variable names of lvalue_variable (8.0 and later, also lowered by
30 * the leaf rules). Constructs: NameAssignment, SystemAssignment,
31 * UserAssignment, SetNames, SetCharacterSet, NamesExpression and, in MySQL
32 * 5.6, PasswordAssignment. The values are lowered by
33 * MYSQL-SET-VALUE-LOWERING-001. `DEFAULT.x` names the instance `default`
34 * (UtilityNoise). Terminates: every part is a strict subtree.
35 * Source: https://dev.mysql.com/doc/refman/8.4/en/set-variable.html,
36 * https://dev.mysql.com/doc/refman/8.4/en/set-names.html,
37 * https://dev.mysql.com/doc/refman/8.4/en/set-character-set.html. Status: Implemented.
38 *
39 * @visibility SqlSemantics\Platform\MySql\Lowering\Utility
40 */
41final class ItemRule
42{
43    /**
44     * The MySQL 5.6 password items, by the position of the account (null for none) and of the password.
45     */
46    private const PASSWORDS = [
47        'option_value_no_option_type: PASSWORD equal text_or_password' => [null, 2],
48        'option_value_no_option_type: PASSWORD FOR_SYM user equal text_or_password' => [2, 4],
49    ];
50
51    /**
52     * @param Lowering $lowering The lowering this rule belongs to
53     */
54    public function __construct(private readonly Lowering $lowering)
55    {
56    }
57
58    /**
59     * Tells whether an item is a MySQL 5.6 password item.
60     */
61    public function password(Form $form): bool
62    {
63        return isset(self::PASSWORDS[$form->signature]);
64    }
65
66    /**
67     * Lowers an item written without a leading scope keyword: a form of `option_value_no_option_type`.
68     *
69     * @throws ImplementationGap When a production has no rule
70     */
71    public function unscoped(Form $form): SetItem
72    {
73        $values = new ValueRule($this->lowering);
74        switch ($form->signature) {
75            case 'option_value_no_option_type: internal_variable_name equal set_expr_or_default':
76            case 'option_value_no_option_type: lvalue_variable equal set_expr_or_default':
77                return $this->named(null, $form);
78            case 'option_value_no_option_type: @ ident_or_text equal expr':
79                return new UserAssignment($this->lowering->variables->user($form->node(1)), $this->lowering->expressions->expression($form->node(3)));
80            case 'option_value_no_option_type: @ @ opt_var_ident_type internal_variable_name equal set_expr_or_default':
81                $this->lowering->options->skip($form->node(4));
82                [$name, $instance] = $this->variable($form->node(3));
83
84                return new SystemAssignment($this->lowering->leaves->record(new SystemVariable($name, $this->lowering->variables->scope($form->node(2)), $instance)), $values->value($form->node(5), true));
85            case 'option_value_no_option_type: @ @ opt_set_var_ident_type lvalue_variable equal set_expr_or_default':
86                $this->lowering->options->skip($form->node(4));
87
88                return new SystemAssignment($this->lowering->variables->system($form->node(3), $this->lowering->variables->scope($form->node(2))), $values->value($form->node(5), true));
89            default:
90                return $this->connection($form);
91        }
92    }
93
94    /**
95     * Lowers an item that sets the connection character sets, or a MySQL 5.6 password item.
96     *
97     * @throws ImplementationGap When a production has no rule
98     */
99    public function connection(Form $form): SetItem
100    {
101        $charsets = $this->lowering->charsets;
102        switch ($form->signature) {
103            case 'option_value_no_option_type: charset old_or_new_charset_name_or_default':
104            case 'option_value_no_option_type: character_set old_or_new_charset_name_or_default':
105                $this->lowering->options->skip($form->node(0));
106
107                return new SetCharacterSet($charsets->charset($form->node(1)));
108            case 'option_value_no_option_type: NAMES_SYM charset_name_or_default opt_collate':
109            case 'option_value_no_option_type: NAMES_SYM charset_name opt_collate':
110                return new SetNames($charsets->charset($form->node(1)), $charsets->collation($form->node(2)));
111            case 'option_value_no_option_type: NAMES_SYM DEFAULT_SYM':
112                return new SetNames(new CharsetName(null));
113            case 'option_value_no_option_type: NAMES_SYM equal expr':
114                $this->lowering->options->skip($form->node(1));
115
116                return new NamesExpression($this->lowering->expressions->expression($form->node(2)));
117        }
118        [$user, $password] = self::PASSWORDS[$form->signature] ?? throw ImplementationGap::production($form);
119        $this->lowering->options->skip($form->node($password - 1));
120
121        return new PasswordAssignment($this->lowering->accounts->password($form->node($password)), $user === null ? null : $this->lowering->users->account($form->node($user)));
122    }
123
124    /**
125     * Lowers an item written after a scope keyword: a form of `option_value_following_option_type`.
126     *
127     * @throws ImplementationGap When a production has no rule
128     */
129    public function scoped(VariableScope $scope, Form $form): NameAssignment
130    {
131        if ($form->signature !== 'option_value_following_option_type: internal_variable_name equal set_expr_or_default'
132            && $form->signature !== 'option_value_following_option_type: lvalue_variable equal set_expr_or_default') {
133            throw ImplementationGap::production($form);
134        }
135
136        return $this->named($scope, $form);
137    }
138
139    /**
140     * Lowers a name assignment whose name, equals sign and value are the first three children.
141     *
142     * @throws ImplementationGap When a production has no rule
143     */
144    public function named(?VariableScope $scope, Form $form): NameAssignment
145    {
146        [$name, $qualifier] = $this->variable($form->node(0));
147        $this->lowering->options->skip($form->node(1));
148
149        return new NameAssignment($name, (new ValueRule($this->lowering))->value($form->node(2), $scope !== null), $scope, $qualifier);
150    }
151
152    /**
153     * Lowers a variable name and the qualifier written before it: a node of `internal_variable_name` or `lvalue_variable`.
154     *
155     * @return array{Name, Name|null}
156     * @throws ImplementationGap When a production has no rule
157     */
158    public function variable(Node $variable): array
159    {
160        $form = $this->lowering->form($variable);
161        $names = $this->lowering->names;
162
163        return match ($form->signature) {
164            'internal_variable_name: ident', 'lvalue_variable: lvalue_ident' => [$names->identifier($form->node(0)), null],
165            'internal_variable_name: ident . ident', 'lvalue_variable: lvalue_ident . ident' => [$names->identifier($form->node(2)), $names->identifier($form->node(0))],
166            'internal_variable_name: DEFAULT . ident', 'lvalue_variable: DEFAULT_SYM . ident' => [$names->identifier($form->node(2)), $this->lowering->leaves->record(new Name('default'))],
167            default => throw ImplementationGap::production($form),
168        };
169    }
170}
171