packages/sql-semantics-postgres/src/Statement/Catalog/ForeignData/CreateFdw.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\GenericOption;
17
18/**
19 * A request to define a foreign-data wrapper.
20 *
21 * Rule: PG-FDW-001. Mirrors `CreateFdwStmt`: name, handler and validator
22 * clauses in the order written, and options. A handler or validator named
23 * twice is a diagnostic (PG-FOREIGN-OPTION-001).
24 * Source: https://www.postgresql.org/docs/17/sql-createforeigndatawrapper.html. Status: Implemented.
25 *
26 * @visibility public
27 * @example Reading the handler of a wrapper
28 *     $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("CREATE FOREIGN DATA WRAPPER w HANDLER h NO VALIDATOR OPTIONS (debug 'true')");
29 *     [$operation->statement->functions[0]->function?->last()->value, $operation->toString()] // => ['h', "CREATE FOREIGN DATA WRAPPER w HANDLER h NO VALIDATOR OPTIONS (debug 'true')"]
30 */
31final class CreateFdw implements Statement
32{
33    use Snapshot;
34
35    /**
36     * @var list<FunctionClause> The handler and validator clauses in the order written
37     */
38    public readonly array $functions;
39
40    /**
41     * @var list<GenericOption> The options
42     */
43    public readonly array $options;
44
45    /**
46     * @param Name $name The wrapper name
47     * @param list<FunctionClause> $functions The handler and validator clauses in the order written
48     * @param list<GenericOption> $options The options
49     */
50    public function __construct(public readonly Name $name, array $functions = [], array $options = [])
51    {
52        $this->functions = Check::listOf($functions, FunctionClause::class, 'Wrapper functions are a list of function clauses.');
53        $this->options = Check::listOf($options, GenericOption::class, 'Wrapper options are a list of options.');
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('CREATE', '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