classCreateFunction
final class CreateFunction
implements Statement
uses Snapshot
@visibility public.CREATE [OR REPLACE] FUNCTION or PROCEDURE: defines a routine.
Mirrors PostgreSQL's CreateFunctionStmt. The result is a type, a table of columns, or, for a function with output parameters or a procedure, not written. The body is either an option (AS 'definition') or an SQL-standard body (RETURN expression or BEGIN ATOMIC ... END) whose statements are analyzed with the parameters visible. A definition written as a string is unanalysed text in the routine's language (PG-ROUTINE-SOURCE-001): the server parses it only when the routine runs. Defining a routine declares no relation. Source: https://www.postgresql.org/docs/17/sql-createfunction.html, https://www.postgresql.org/docs/17/sql-createprocedure.html.
$operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('CREATE FUNCTION add(a int4, b int4) RETURNS int4 RETURN a + b');
[$operation->statement->name->last()->value, count($operation->statement->parameters->parameters)] // => ['add', 2]Properties§
public list<RoutineOption> $optionspublic DottedName $namepublic RoutineParameters $parameterspublic TypeName|ResultTable|null $returnspublic ReturnStatement|AtomicBody|null $body = nullpublic bool $procedure = falsepublic bool $replace = falseMethods§
public function __construct(
public DottedName $name,
public RoutineParameters $parameters,
public TypeName|ResultTable|null $returns,
list<RoutineOption> $options = [],
public ReturnStatement|AtomicBody|null $body = null,
public bool $procedure = false,
public bool $replace = false,
)Parameters
$name | DottedName | The routine name |
$parameters | RoutineParameters | The parameters |
$returns | TypeName|ResultTable|null | The written result; a procedure has none |
$options | list<RoutineOption> | The options in written order |
$body | ReturnStatement|AtomicBody|null | The SQL-standard body, if written |
$procedure | bool | Whether a procedure is defined |
$replace | bool | Whether OR REPLACE is written |
Calls 3
- static-call Check::listOf() line 65
- class-const RoutineOption::class() line 65
- static-call Check::input() line 66
public function deriveStatement(Derivation $derivation): voidDerives the parameters, the result, the options and the body.
Parameters
$derivation | Derivation |
Calls 2
- method-call RoutineFacts::create() line 81
- new RoutineFacts line 81
public function render(Output $out): voidWrites the statement.
Parameters
$out | Output |
Calls 2
- method-call Output::keyword() line 89
- method-call Output::node() line 98
Test cases 10§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 4
Other tests reaching this symbol 6
RoutinesTest::testStatementDispatchesEveryStatementOfTheFamilycallsBodyFactsTest::testFirstFindsTheSelectionOfANestedQuerycallsRoutineChecksTest::testParameterReportsAnInputAfterVariadiccallsParameterDeclarationsTest::testInfersUndeclaredInPrepareOnlycallsRoutineSourceTest::testAnalyzeKeepsAnSqlDefinitionUnanalysedWithItsLanguagecallsRoutineSourceTest::testAnalyzeNamesNoLanguageWithoutALanguageOptioncalls
Relations§
Instantiated in 4
- new SqlSemantics\Platform\PostgreSql\Lowering\Routine\FunctionRule::create() packages/sql-semantics-postgres/src/Lowering/Routine/FunctionRule.php:61
- new SqlSemantics\Platform\PostgreSql\Lowering\Routine\FunctionRule::create() packages/sql-semantics-postgres/src/Lowering/Routine/FunctionRule.php:62
- new SqlSemantics\Platform\PostgreSql\Lowering\Routine\FunctionRule::create() packages/sql-semantics-postgres/src/Lowering/Routine/FunctionRule.php:63
- new SqlSemantics\Platform\PostgreSql\Lowering\Routine\FunctionRule::create() packages/sql-semantics-postgres/src/Lowering/Routine/FunctionRule.php:64
Type declarations 6
- type SqlSemantics\Platform\PostgreSql\Lowering\Routine\FunctionRule packages/sql-semantics-postgres/src/Lowering/Routine/FunctionRule.php:52
- type SqlSemantics\Platform\PostgreSql\Rules\Routine\RoutineChecks packages/sql-semantics-postgres/src/Rules/Routine/RoutineChecks.php:77
- type SqlSemantics\Platform\PostgreSql\Rules\Routine\RoutineChecks packages/sql-semantics-postgres/src/Rules/Routine/RoutineChecks.php:105
- type SqlSemantics\Platform\PostgreSql\Rules\Routine\RoutineChecks packages/sql-semantics-postgres/src/Rules/Routine/RoutineChecks.php:129
- type SqlSemantics\Platform\PostgreSql\Rules\Routine\RoutineChecks packages/sql-semantics-postgres/src/Rules/Routine/RoutineChecks.php:144
- type SqlSemantics\Platform\PostgreSql\Rules\Routine\RoutineFacts packages/sql-semantics-postgres/src/Rules/Routine/RoutineFacts.php:34