packages/sql-semantics-postgres/src/Statement/Catalog/ForeignData/AlterFdw.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Catalog\ForeignData;
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\Statement\Identifier\Name;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Statement;
14use SqlSemantics\Platform\PostgreSql\Rules\Catalog\ForeignOptions;
15use SqlSemantics\Platform\PostgreSql\Statement\Catalog\Language\FunctionClause;
16use SqlSemantics\Platform\PostgreSql\Statement\Option\AlteredOption;
17
18/**
19 * A request to change the functions or options of a foreign-data wrapper.
20 *
21 * Rule: PG-FDW-002. Mirrors `AlterFdwStmt`. At least one function clause or
22 * option change is written. NO HANDLER and NO VALIDATOR remove the function.
23 * Source: https://www.postgresql.org/docs/17/sql-alterforeigndatawrapper.html. Status: Implemented.
24 *
25 * @visibility public
26 * @example Removing the validator
27 *     $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('ALTER FOREIGN DATA WRAPPER w NO VALIDATOR');
28 *     $operation->statement->functions[0]->function // => null
29 */
30final class AlterFdw implements Statement
31{
32    use Snapshot;
33
34    /**
35     * @var list<FunctionClause> The handler and validator clauses in the order written
36     */
37    public readonly array $functions;
38
39    /**
40     * @var list<AlteredOption> The option changes
41     */
42    public readonly array $options;
43
44    /**
45     * @param Name $name The wrapper name
46     * @param list<FunctionClause> $functions The handler and validator clauses in the order written
47     * @param list<AlteredOption> $options The option changes
48     */
49    public function __construct(public readonly Name $name, array $functions = [], array $options = [])
50    {
51        $this->functions = Check::listOf($functions, FunctionClause::class, 'Wrapper functions are a list of function clauses.');
52        $this->options = Check::listOf($options, AlteredOption::class, 'Wrapper option changes are a list of changes.');
53        Check::input($this->functions !== [] || $this->options !== [], 'ALTER FOREIGN DATA WRAPPER changes a function or an option.');
54    }
55
56    /**
57     * Reports a function role named twice.
58     */
59    public function deriveStatement(Derivation $derivation): void
60    {
61        (new ForeignOptions())->functions($derivation, $this->functions);
62    }
63
64    /**
65     * Writes the request.
66     */
67    public function render(Output $out): void
68    {
69        $out->keyword('ALTER', 'FOREIGN', 'DATA', 'WRAPPER')->name($this->name, NameUse::Column);
70        foreach ($this->functions as $function) {
71            $out->node($function);
72        }
73        (new ForeignOptions())->write($out, $this->options);
74    }
75}
76