packages/sql-semantics-postgres/src/Lowering/Leaf/Roles.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Lowering\Leaf;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
9use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\AnalysisException;
10use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\ImplementationGap;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpec;
13use SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpecKind;
14
15/**
16 * Lowers role specifications.
17 *
18 * Rule: PG-ROLE-001. Scope: `RoleSpec`, `RoleId`, `role_list`. Constructor:
19 * `RoleSpec`. The word `public`, quoted or not, designates every role; the
20 * word `none` is reserved and rejected by the grammar action. `RoleId` is a
21 * position that takes a role name only, so every designation there is
22 * rejected by the grammar action as well. Termination: the list is flattened
23 * iteratively. Source: https://www.postgresql.org/docs/17/sql-grant.html,
24 * https://www.postgresql.org/docs/17/sql-createrole.html. Status: Implemented.
25 *
26 * @visibility SqlSemantics
27 */
28final class Roles
29{
30    /**
31     * @param Lowering $lowering The hub
32     */
33    public function __construct(private readonly Lowering $lowering)
34    {
35    }
36
37    /**
38     * Lowers `RoleSpec`.
39     *
40     * @throws AnalysisException When the role is named none, which the action of `RoleSpec: NonReservedWord` in `gram.y` rejects
41     * @throws ImplementationGap When the production has no rule
42     */
43    public function role(Node $role): RoleSpec
44    {
45        $form = $this->lowering->productions->form($role);
46        if ($form->signature === 'RoleSpec: NonReservedWord') {
47            $word = $this->lowering->names->text($form->node(0));
48            if ($word === 'none') {
49                throw new AnalysisException('role name "none" is reserved');
50            }
51
52            return $word === 'public' ? new RoleSpec(RoleSpecKind::Everyone) : new RoleSpec(RoleSpecKind::Named, $this->lowering->names->name($form->node(0)));
53        }
54
55        return new RoleSpec(match ($form->signature) {
56            'RoleSpec: CURRENT_ROLE' => RoleSpecKind::CurrentRole,
57            'RoleSpec: CURRENT_USER' => RoleSpecKind::CurrentUser,
58            'RoleSpec: SESSION_USER' => RoleSpecKind::SessionUser,
59            default => throw ImplementationGap::production($form),
60        });
61    }
62
63    /**
64     * Lowers `RoleId`: a position that names a role and accepts no designation.
65     *
66     * @throws AnalysisException When a designation is written, which the action of `RoleId: RoleSpec` in `gram.y` rejects
67     * @throws ImplementationGap When the production has no rule
68     */
69    public function name(Node $role): Name
70    {
71        $form = $this->lowering->productions->form($role);
72        if ($form->signature !== 'RoleId: RoleSpec') {
73            throw ImplementationGap::production($form);
74        }
75
76        return $this->role($form->node(0))->name ?? throw new AnalysisException('a role designation cannot be used as a role name here');
77    }
78
79    /**
80     * Lowers `role_list`.
81     *
82     * @return list<RoleSpec>
83     */
84    public function roles(Node $list): array
85    {
86        $roles = [];
87        foreach ($this->lowering->items($list, 'role_list: RoleSpec', 'role_list: role_list , RoleSpec') as $role) {
88            $roles[] = $this->role($role);
89        }
90
91        return $roles;
92    }
93}
94