packages/sql-semantics-postgres/src/Statement/Routine/Option/RoutineSource.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Routine\Option;
6
7use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
8use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
9use SqlSemantics\Platform\PostgreSql\Model\Statement\Node;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
12
13/**
14 * The definition of a routine written as a string constant: unanalysed text in the routine's language.
15 *
16 * Rule: PG-ROUTINE-SOURCE-001. Scope: `func_as` in `createfunc_opt_item: AS
17 * func_as` of `CreateFunctionStmt`. Constructor: `RoutineSource`, held by
18 * `RoutineDefinition`. Decision: the text is kept as the exact decoded
19 * string, together with the name of the language it is written in, and is
20 * not parsed, for every language including `sql`.
21 * Rationale: the grammar gives the definition as a string constant whose
22 * meaning depends on the language: an internal function name for `internal`,
23 * an object file for `c`, SQL commands for `sql`, or text in a procedural
24 * language (sql-createfunction.html, "definition"). Reading it is the job of
25 * the language's handler and validator, not of the SQL grammar; the
26 * procedural languages have no grammar shipped here. For `sql` the manual
27 * states that the string form "is parsed at execution time", unlike an
28 * SQL-standard body, which "is parsed at function definition time"
29 * (sql-createfunction.html, `sql_body`): names in the text are resolved
30 * against the catalog and search path of each call, and the text is
31 * validated at definition time only when `check_function_bodies` is on
32 * (runtime-config-client.html#GUC-CHECK-FUNCTION-BODIES), a session setting
33 * that is not part of the language profile. Parsing the text here would
34 * attach name resolutions and types to a definition for which PostgreSQL
35 * fixes none, so the model states only what the server fixes: the language
36 * and the text. An SQL-standard body (`RETURN` or `BEGIN ATOMIC`) is
37 * structured and derived instead (PG-ROUTINE-BODY-001). The language is the
38 * name of the routine's LANGUAGE option, decoded as the server looks it up;
39 * it is null when no LANGUAGE is written, which the server rejects for a
40 * string definition (PG-ROUTINE-CHECK-001). With several LANGUAGE options,
41 * which the server rejects as conflicting, it is the first one.
42 * Facts: none; the text holds no expression. Diagnostics: none of its own.
43 * Termination: no recursion.
44 * Source: https://www.postgresql.org/docs/17/sql-createfunction.html,
45 * https://www.postgresql.org/docs/17/runtime-config-client.html#GUC-CHECK-FUNCTION-BODIES,
46 * `interpret_AS_clause` in `src/backend/commands/functioncmds.c` of PostgreSQL 17.
47 * Status: Implemented.
48 *
49 * @visibility public
50 * @example Reading the language and the text of a routine definition
51 *     $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("CREATE FUNCTION one() RETURNS int4 LANGUAGE sql AS \$\$ SELECT 1 \$\$");
52 *     $source = $operation->statement->options[1]->source;
53 *     [$source->language?->value, $source->text->value] // => ['sql', ' SELECT 1 ']
54 */
55final class RoutineSource implements Node
56{
57    use Snapshot;
58
59    /**
60     * @param Name|null $language The language the text is written in, as the routine's LANGUAGE option names it; null when none is written
61     * @param StringConstant $text The text, decoded
62     */
63    public function __construct(public readonly ?Name $language, public readonly StringConstant $text)
64    {
65    }
66
67    /**
68     * Writes the text as a string constant.
69     */
70    public function render(Output $out): void
71    {
72        $out->node($this->text);
73    }
74}
75