packages/sql-semantics-postgres/src/Statement/Table/Policy/CreatePolicy.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Table\Policy;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Contract\NameUse;
9use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
10use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
11use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\RelationFact;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Relation;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Scalar;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Statement;
19use SqlSemantics\Platform\PostgreSql\Rendering\Spelling;
20use SqlSemantics\Platform\PostgreSql\Rules\Table\Command\Policies;
21use SqlSemantics\Platform\PostgreSql\Rules\Table\Targets;
22use SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpec;
23
24/**
25 * A request to create a row security policy.
26 *
27 * Mirrors PostgreSQL's `CreatePolicyStmt` (`policy_name`, `table`, `cmd_name`, `permissive`, `roles`, `qual`,
28 * `with_check`). Rule PG-POLICY-001: the table is resolved and is the relation fact of the statement; the
29 * USING and WITH CHECK expressions are conditions where the table is visible. AS takes PERMISSIVE or
30 * RESTRICTIVE; the grammar rejects another word.
31 * Source: https://www.postgresql.org/docs/17/sql-createpolicy.html.
32 *
33 * @visibility public
34 * @example Reading a policy
35 *     $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('CREATE POLICY p ON t AS RESTRICTIVE FOR UPDATE TO CURRENT_USER USING (a > 0) WITH CHECK (a < 10)');
36 *     $statement->toString() // => 'CREATE POLICY p ON t AS restrictive FOR UPDATE TO CURRENT_USER USING (a > 0) WITH CHECK (a < 10)'
37 */
38final class CreatePolicy implements Statement, Relation
39{
40    use Snapshot;
41
42    /**
43     * @var list<RoleSpec> The roles after TO; none means PUBLIC
44     */
45    public readonly array $roles;
46
47    /**
48     * @param Name $name The policy name
49     * @param QualifiedName $table The table
50     * @param Name|null $mode The word after AS: permissive or restrictive
51     * @param PolicyCommand|null $command The command after FOR
52     * @param list<RoleSpec> $roles The roles after TO; none means PUBLIC
53     * @param Scalar|null $using The USING condition
54     * @param Scalar|null $check The WITH CHECK condition
55     */
56    public function __construct(
57        public readonly Name $name,
58        public readonly QualifiedName $table,
59        public readonly ?Name $mode = null,
60        public readonly ?PolicyCommand $command = null,
61        array $roles = [],
62        public readonly ?Scalar $using = null,
63        public readonly ?Scalar $check = null,
64    ) {
65        Check::input($mode === null || in_array($mode->value, ['permissive', 'restrictive'], true), 'A policy is permissive or restrictive.');
66        $this->roles = Check::listOf($roles, RoleSpec::class, 'Policy roles are roles.');
67    }
68
69    /**
70     * Resolves the table and derives the conditions.
71     */
72    public function deriveStatement(Derivation $derivation): void
73    {
74        (new Policies())->derive($this, $this->table, $this->using, $this->check, $derivation);
75    }
76
77    /**
78     * Resolves the table and answers its facts.
79     */
80    public function deriveRelation(Derivation $derivation, Environment $environment): RelationFact
81    {
82        return (new Targets())->resolve($derivation, $this->table);
83    }
84
85    /**
86     * Writes the statement.
87     */
88    public function render(Output $out): void
89    {
90        $out->keyword('CREATE', 'POLICY')->name($this->name)->keyword('ON');
91        (new Spelling())->qualified($out, $this->table);
92        if ($this->mode !== null) {
93            $out->keyword('AS')->name($this->mode, NameUse::Identifier);
94        }
95        if ($this->command !== null) {
96            $out->keyword('FOR', $this->command->value);
97        }
98        (new Policies())->write($out, $this->roles, $this->using, $this->check);
99    }
100}
101