packages/sql-semantics-mysql/src/Lowering/Utility/Explain/OptionRule.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Utility\Explain;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
12
13/**
14 * Lowers the options of EXPLAIN in MySQL 8.0 and later.
15 *
16 * Rule: MYSQL-EXPLAIN-OPTION-LOWERING-001. Scope: opt_explain_options,
17 * opt_explain_format, opt_explain_into, opt_explain_for_schema. The format
18 * is a name the server compares without regard to case; INTO names a user
19 * variable; FOR DATABASE names a database. Terminates: unit productions
20 * over strict subtrees.
21 * Source: https://dev.mysql.com/doc/refman/8.4/en/explain.html. Status: Implemented.
22 *
23 * @visibility SqlSemantics\Platform\MySql\Lowering\Utility
24 */
25final class OptionRule
26{
27 /**
28 * @param Lowering $lowering The lowering this rule belongs to
29 */
30 public function __construct(private readonly Lowering $lowering)
31 {
32 }
33
34 /**
35 * Lowers a node of `opt_explain_options` into the format, ANALYZE and the variable of INTO.
36 *
37 * @return array{Name|null, bool, UserVariable|null}
38 * @throws ImplementationGap When a production has no rule
39 */
40 public function options(Node $options): array
41 {
42 $form = $this->lowering->form($options);
43
44 return match ($form->signature) {
45 'opt_explain_options: opt_explain_format' => [$this->format($form->node(0)), false, null],
46 'opt_explain_options: ANALYZE_SYM opt_explain_format' => [$this->format($form->node(1)), true, null],
47 'opt_explain_options: opt_explain_format opt_explain_into' => [$this->format($form->node(0)), false, $this->into($form->node(1))],
48 'opt_explain_options: ANALYZE_SYM opt_explain_format opt_explain_into' => [$this->format($form->node(1)), true, $this->into($form->node(2))],
49 default => throw ImplementationGap::production($form),
50 };
51 }
52
53 /**
54 * Lowers a node of `opt_explain_format`.
55 *
56 * @throws ImplementationGap When a production has no rule
57 */
58 public function format(Node $format): ?Name
59 {
60 $form = $this->lowering->form($format);
61
62 return match ($form->signature) {
63 'opt_explain_format:' => null,
64 'opt_explain_format: FORMAT_SYM EQ ident_or_text' => $this->lowering->names->identifier($form->node(2)),
65 default => throw ImplementationGap::production($form),
66 };
67 }
68
69 /**
70 * Lowers a node of `opt_explain_into`.
71 *
72 * @throws ImplementationGap When a production has no rule
73 */
74 public function into(Node $into): ?UserVariable
75 {
76 $form = $this->lowering->form($into);
77
78 return match ($form->signature) {
79 'opt_explain_into:' => null,
80 'opt_explain_into: INTO @ ident_or_text' => $this->lowering->variables->user($form->node(2)),
81 default => throw ImplementationGap::production($form),
82 };
83 }
84
85 /**
86 * Lowers a node of `opt_explain_for_schema`.
87 *
88 * @throws ImplementationGap When a production has no rule
89 */
90 public function database(Node $database): ?Name
91 {
92 $form = $this->lowering->form($database);
93
94 return match ($form->signature) {
95 'opt_explain_for_schema:' => null,
96 'opt_explain_for_schema: FOR_SYM DATABASE ident_or_text' => $this->lowering->names->identifier($form->node(2)),
97 default => throw ImplementationGap::production($form),
98 };
99 }
100}
101