packages/sql-semantics-mysql/src/Lowering/Routine/RoutineRules.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\Lowering\Routine\Condition\DiagnosticsRule;
10use SqlSemantics\Platform\MySql\Lowering\Routine\Condition\SignalRule;
11use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
12use SqlSemantics\Platform\MySql\Model\Lowering\Form;
13use SqlSemantics\Platform\MySql\Model\Statement\Statement;
14use SqlSemantics\Platform\MySql\Statement\Name\Account;
15
16/**
17 * The entry rules of the routine family: the methods other families and the statement dispatcher call.
18 *
19 * Rule: MYSQL-ROUTINE-ENTRY-001. Scope: stored procedures, functions, triggers, events, loadable functions and
20 * compound statements.
21 * The method names, parameters and return types are fixed by the family
22 * plan. A method delegates to the rule classes of this family.
23 * Terminates: constant dispatch.
24 * Source: https://dev.mysql.com/doc/refman/8.4/en/sql-compound-statements.html.
25 * Status: Implemented.
26 *
27 * @visibility SqlSemantics\Platform\MySql
28 */
29final class RoutineRules
30{
31    /**
32     * @param Lowering $lowering The lowering this rule belongs to
33     */
34    public function __construct(public readonly Lowering $lowering)
35    {
36    }
37
38    /**
39     * Lowers a stored program statement: a node of one of the statement rules this family owns.
40     *
41     * @throws ImplementationGap When a production has no rule
42     */
43    public function statement(Node $statement): Statement
44    {
45        $form = $this->lowering->form($statement);
46
47        return match ($statement->name) {
48            'signal_stmt', 'resignal_stmt' => (new SignalRule($this->lowering))->statement($form),
49            'get_diagnostics' => (new DiagnosticsRule($this->lowering))->statement($form),
50            'alter_procedure_stmt', 'alter_function_stmt', 'alter_event_stmt', 'drop_procedure_stmt', 'drop_function_stmt', 'drop_trigger_stmt', 'drop_event_stmt' => $this->definition($form),
51            default => throw ImplementationGap::production($form),
52        };
53    }
54
55    /**
56     * Lowers a production of `alter` or `drop` that MYSQL-DEFINITION-ROUTES-001 routes to this family.
57     *
58     * The form may also be that of one of the 8.0 statement rules `alter_procedure_stmt`,
59     * `alter_function_stmt`, `alter_event_stmt` and `drop_*_stmt` of stored programs.
60     *
61     * @throws ImplementationGap When a production has no rule
62     */
63    public function definition(Form $form): Statement
64    {
65        $symbols = explode(' ', $form->signature);
66
67        return match (true) {
68            in_array('EVENT_SYM', $symbols, true) && $symbols[1] === 'ALTER' => (new EventRule($this->lowering))->alter($form),
69            $symbols[1] === 'ALTER' => (new DefinitionRule($this->lowering))->alter($form),
70            $symbols[1] === 'DROP' => (new DefinitionRule($this->lowering))->drop($form),
71            default => throw ImplementationGap::production($form),
72        };
73    }
74
75    /**
76     * Lowers CREATE of a stored program: a node of `trigger_tail`, `sp_tail`, `sf_tail`, `event_tail` or
77     * `udf_tail`, and the definer.
78     *
79     * @throws ImplementationGap When a production has no rule
80     */
81    public function create(Node $tail, ?Account $definer): Statement
82    {
83        return match ($tail->name) {
84            'sp_tail', 'sf_tail' => (new DefinitionRule($this->lowering))->routine($tail, $definer),
85            'udf_tail' => (new DefinitionRule($this->lowering))->loadable($tail),
86            'trigger_tail' => (new TriggerRule($this->lowering))->create($tail, $definer),
87            'event_tail' => (new EventRule($this->lowering))->create($tail, $definer),
88            default => throw ImplementationGap::production($this->lowering->form($tail)),
89        };
90    }
91}
92