packages/sql-semantics-postgres/src/Lowering/Utility/UtilityCommands.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Lowering\Utility;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
9use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Statement;
11
12/**
13 * The entry point of the utility family: transactions, configuration, EXPLAIN, maintenance, locking, notification, CALL and DO.
14 *
15 * Rule: PG-UTILITY-001. Scope: the statement nonterminals of the family
16 * (`php .agent/pg-families.php Utility`), each handed to the rule of its
17 * group, and `SetResetClause`, `FunctionSetResetClause`. Termination: each
18 * statement is lowered by one rule; lists are flattened iteratively.
19 * Source: https://www.postgresql.org/docs/17/sql-commands.html. Status: Implemented.
20 *
21 * @visibility SqlSemantics
22 */
23final class UtilityCommands
24{
25    /**
26     * The rule group of each statement nonterminal.
27     */
28    private const GROUPS = [
29        'TransactionStmt' => 'transaction', 'TransactionStmtLegacy' => 'transaction',
30        'VariableSetStmt' => 'setting', 'VariableResetStmt' => 'setting', 'VariableShowStmt' => 'setting', 'AlterSystemStmt' => 'setting',
31        'ConstraintsSetStmt' => 'setting', 'DiscardStmt' => 'setting',
32        'VacuumStmt' => 'maintenance', 'AnalyzeStmt' => 'maintenance', 'ClusterStmt' => 'maintenance', 'ReindexStmt' => 'maintenance',
33        'CheckPointStmt' => 'maintenance', 'ExplainStmt' => 'maintenance', 'LockStmt' => 'maintenance',
34        'CallStmt' => 'command', 'DoStmt' => 'command', 'NotifyStmt' => 'command', 'ListenStmt' => 'command', 'UnlistenStmt' => 'command', 'LoadStmt' => 'command',
35    ];
36
37    /**
38     * @param Lowering $lowering The hub
39     */
40    public function __construct(private readonly Lowering $lowering)
41    {
42    }
43
44    /**
45     * Lowers a statement of the family, such as `TransactionStmt`, `TransactionStmtLegacy`, `VariableSetStmt`, `ExplainStmt` or `VacuumStmt`.
46     *
47     * @throws ImplementationGap When the nonterminal is not a statement of the family
48     */
49    public function statement(Node $statement): Statement
50    {
51        $group = self::GROUPS[$statement->name] ?? throw ImplementationGap::production($this->lowering->productions->form($statement));
52
53        return match ($group) {
54            'transaction' => (new TransactionRule($this->lowering))->statement($statement),
55            'setting' => (new SettingRule($this->lowering))->statement($statement),
56            'maintenance' => (new MaintenanceRule($this->lowering))->statement($statement),
57            'command' => (new CommandRule($this->lowering))->statement($statement),
58        };
59    }
60
61    /**
62     * Lowers `SetResetClause` or `FunctionSetResetClause`: a SET or RESET of a configuration parameter attached to a role, a database or a routine.
63     *
64     * The result is the same structure the SET or RESET command has on its
65     * own; it renders as that command, which is how the clause is written.
66     *
67     * @throws ImplementationGap When the production has no rule
68     */
69    public function setReset(Node $clause): Statement
70    {
71        return (new SettingRule($this->lowering))->clause($clause);
72    }
73}
74