packages/sql-semantics-mysql/src/Lowering/Routine/ParameterRule.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Routine;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\MySql\Statement\Name\CollationName;
11use SqlSemantics\Platform\MySql\Statement\Routine\Parameter;
12use SqlSemantics\Platform\MySql\Statement\Routine\ParameterList;
13use SqlSemantics\Platform\MySql\Statement\Routine\ParameterMode;
14use SqlSemantics\Platform\MySql\Statement\Type\TypeName;
15
16/**
17 * Lowers the parameter lists of stored routines and the data types of parameters, variables and return values.
18 *
19 * Rule: MYSQL-ROUTINE-PARAMETER-LOWERING-001. Scope: sp_fdparam_list,
20 * sp_fdparams, sp_fdparam, sp_pdparam_list, sp_pdparams, sp_pdparam,
21 * sp_opt_inout, type_with_opt_collate. The type comes from the type rules,
22 * the collation from the leaf rules. Constructs: ParameterList, Parameter.
23 * Terminates: the lists are flattened iteratively.
24 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-procedure.html.
25 * Status: Implemented.
26 *
27 * @visibility SqlSemantics\Platform\MySql
28 */
29final class ParameterRule
30{
31 /**
32 * The direction keywords.
33 */
34 private const MODES = [
35 'sp_opt_inout:' => null, 'sp_opt_inout: IN_SYM' => ParameterMode::In, 'sp_opt_inout: OUT_SYM' => ParameterMode::Out, 'sp_opt_inout: INOUT_SYM' => ParameterMode::InOut,
36 ];
37
38 /**
39 * The parameter list productions.
40 */
41 private const LISTS = ['sp_fdparams: sp_fdparams , sp_fdparam', 'sp_fdparams: sp_fdparam', 'sp_pdparams: sp_pdparams , sp_pdparam', 'sp_pdparams: sp_pdparam'];
42
43 /**
44 * @param Lowering $lowering The lowering this rule belongs to
45 */
46 public function __construct(private readonly Lowering $lowering)
47 {
48 }
49
50 /**
51 * Lowers a parameter list: a node of `sp_fdparam_list` or `sp_pdparam_list`.
52 *
53 * @throws ImplementationGap When a production has no rule
54 */
55 public function parameters(Node $list): ParameterList
56 {
57 $form = $this->lowering->form($list);
58 $parameters = [];
59 $items = match ($form->signature) {
60 'sp_fdparam_list:', 'sp_pdparam_list:' => [],
61 'sp_fdparam_list: sp_fdparams', 'sp_pdparam_list: sp_pdparams' => (new Sequence($this->lowering))->items($form->node(0), self::LISTS),
62 default => throw ImplementationGap::production($form),
63 };
64 foreach ($items as $item) {
65 $parameters[] = $this->parameter($item);
66 }
67
68 return new ParameterList($parameters);
69 }
70
71 /**
72 * Lowers one parameter: a node of `sp_fdparam` or `sp_pdparam`.
73 *
74 * @throws ImplementationGap When a production has no rule
75 */
76 public function parameter(Node $parameter): Parameter
77 {
78 $form = $this->lowering->form($parameter);
79 [$mode, $name, $type, $collation] = match ($form->signature) {
80 'sp_fdparam: ident sp_init_param type_with_opt_collate' => [null, 0, 2, null],
81 'sp_fdparam: ident type opt_collate' => [null, 0, 1, 2],
82 'sp_pdparam: sp_opt_inout sp_init_param ident type_with_opt_collate' => [0, 2, 3, null],
83 'sp_pdparam: sp_opt_inout ident type opt_collate' => [0, 1, 2, 3],
84 default => throw ImplementationGap::production($form),
85 };
86 if ($collation === null) {
87 $this->lowering->options->skip($form->node(1));
88 }
89 $direction = null;
90 if ($mode !== null) {
91 $inout = $this->lowering->form($form->node($mode));
92 if (!array_key_exists($inout->signature, self::MODES)) {
93 throw ImplementationGap::production($inout);
94 }
95 $direction = self::MODES[$inout->signature];
96 }
97 [$declared, $collate] = $this->declared($form->node($type), $collation === null ? null : $form->node($collation));
98
99 return new Parameter($this->lowering->names->identifier($form->node($name)), $declared, $collate, $direction);
100 }
101
102 /**
103 * Lowers a data type with its optional COLLATE clause: a node of `type_with_opt_collate`, or a node
104 * of `type` and the node of `opt_collate` that follows it.
105 *
106 * @return array{TypeName, CollationName|null}
107 * @throws ImplementationGap When a production has no rule
108 */
109 public function declared(Node $type, ?Node $collation): array
110 {
111 if ($collation === null) {
112 $form = $this->lowering->form($type);
113 if ($form->signature !== 'type_with_opt_collate: type opt_collate') {
114 throw ImplementationGap::production($form);
115 }
116 [$type, $collation] = [$form->node(0), $form->node(1)];
117 }
118
119 return [$this->lowering->types->type($type), $this->lowering->charsets->collation($collation)];
120 }
121}
122