packages/sql-semantics-postgres/src/Statement/Routine/CreateFunction.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Routine;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Statement;
12use SqlSemantics\Platform\PostgreSql\Rules\Routine\RoutineFacts;
13use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
14use SqlSemantics\Platform\PostgreSql\Statement\Routine\Option\RoutineDefinition;
15use SqlSemantics\Platform\PostgreSql\Statement\Routine\Option\RoutineLanguage;
16use SqlSemantics\Platform\PostgreSql\Statement\Routine\Option\RoutineOption;
17use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
18
19/**
20 * `CREATE [OR REPLACE] FUNCTION` or `PROCEDURE`: defines a routine.
21 *
22 * Mirrors PostgreSQL's `CreateFunctionStmt`. The result is a type, a table of
23 * columns, or, for a function with output parameters or a procedure, not
24 * written. The body is either an option (`AS 'definition'`) or an
25 * SQL-standard body (`RETURN expression` or `BEGIN ATOMIC ... END`) whose
26 * statements are analyzed with the parameters visible. A definition written
27 * as a string is unanalysed text in the routine's language
28 * (PG-ROUTINE-SOURCE-001): the server parses it only when the routine runs. Defining a routine
29 * declares no relation.
30 * Source: https://www.postgresql.org/docs/17/sql-createfunction.html,
31 * https://www.postgresql.org/docs/17/sql-createprocedure.html.
32 *
33 * @visibility public
34 * @example Reading the parameters of a function
35 * $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('CREATE FUNCTION add(a int4, b int4) RETURNS int4 RETURN a + b');
36 * [$operation->statement->name->last()->value, count($operation->statement->parameters->parameters)] // => ['add', 2]
37 */
38final class CreateFunction implements Statement
39{
40 use Snapshot;
41
42 /**
43 * @var list<RoutineOption> The options in written order
44 */
45 public readonly array $options;
46
47 /**
48 * @param DottedName $name The routine name
49 * @param RoutineParameters $parameters The parameters
50 * @param TypeName|ResultTable|null $returns The written result; a procedure has none
51 * @param list<RoutineOption> $options The options in written order
52 * @param ReturnStatement|AtomicBody|null $body The SQL-standard body, if written
53 * @param bool $procedure Whether a procedure is defined
54 * @param bool $replace Whether OR REPLACE is written
55 */
56 public function __construct(
57 public readonly DottedName $name,
58 public readonly RoutineParameters $parameters,
59 public readonly TypeName|ResultTable|null $returns,
60 array $options = [],
61 public readonly ReturnStatement|AtomicBody|null $body = null,
62 public readonly bool $procedure = false,
63 public readonly bool $replace = false,
64 ) {
65 $this->options = Check::listOf($options, RoutineOption::class, 'Routine options are a list of routine options.');
66 Check::input(!$procedure || $returns === null, 'A procedure has no result.');
67 $language = null;
68 foreach ($this->options as $option) {
69 $language ??= $option instanceof RoutineLanguage ? $option->name() : null;
70 }
71 foreach ($this->options as $option) {
72 Check::input(!$option instanceof RoutineDefinition || $option->source->language?->value === $language, 'A routine definition is written in the language of the first LANGUAGE option, or in none without one.');
73 }
74 }
75
76 /**
77 * Derives the parameters, the result, the options and the body.
78 */
79 public function deriveStatement(Derivation $derivation): void
80 {
81 (new RoutineFacts())->create($this, $derivation);
82 }
83
84 /**
85 * Writes the statement.
86 */
87 public function render(Output $out): void
88 {
89 $out->keyword('CREATE');
90 if ($this->replace) {
91 $out->keyword('OR', 'REPLACE');
92 }
93 $out->keyword($this->procedure ? 'PROCEDURE' : 'FUNCTION')->node($this->name)->node($this->parameters);
94 if ($this->returns !== null) {
95 $out->keyword('RETURNS')->node($this->returns);
96 }
97 foreach ($this->options as $option) {
98 $out->node($option);
99 }
100 $out->node($this->body);
101 }
102}
103