packages/sql-semantics-postgres/src/Statement/Routine/ResultColumn.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\Contract\NameUse;
9use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
10use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
13use SqlSemantics\Platform\PostgreSql\Statement\Clause;
14use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
15
16/**
17 * One column of the result of a function declared `RETURNS TABLE ( ... )`.
18 *
19 * Mirrors PostgreSQL's `FunctionParameter` with mode `TABLE`: an output
20 * parameter that is a column of each result row.
21 * Source: https://www.postgresql.org/docs/17/sql-createfunction.html.
22 *
23 * @visibility public
24 * @example Reading the column name
25 * $type = new \SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName(new \SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation(new \SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('int4')])));
26 * (new \SqlSemantics\Platform\PostgreSql\Statement\Routine\ResultColumn(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('id'), $type))->name->value // => 'id'
27 */
28final class ResultColumn implements Clause
29{
30 use Snapshot;
31
32 /**
33 * @param Name $name The column name
34 * @param TypeName $type The column type
35 */
36 public function __construct(public readonly Name $name, public readonly TypeName $type)
37 {
38 }
39
40 /**
41 * Derives the type modifiers.
42 */
43 public function deriveClause(Derivation $derivation, Environment $environment): void
44 {
45 $this->type->deriveClause($derivation, $environment);
46 }
47
48 /**
49 * Writes the name and the type.
50 */
51 public function render(Output $out): void
52 {
53 $out->name($this->name, NameUse::Routine)->node($this->type);
54 }
55}
56