packages/sql-semantics-postgres/src/Statement/Table/Trigger/TriggerArgument.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Table\Trigger;
6
7use SqlSemantics\Platform\PostgreSql\Model\Contract\NameUse;
8use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
9use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Node;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
12use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
13use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
14use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
15
16/**
17 * One argument passed to a trigger function.
18 *
19 * The server passes every argument as a string: an integer constant as its
20 * decimal value, a numeric constant as its written text, a string constant
21 * as its value and a word as its name (`TriggerFuncArg` in gram.y).
22 * Source: https://www.postgresql.org/docs/17/sql-createtrigger.html.
23 *
24 * @visibility public
25 * @example Reading trigger arguments
26 *     $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("CREATE TRIGGER g BEFORE INSERT ON t FOR EACH ROW EXECUTE FUNCTION f(1, 2.5, 'x', word)");
27 *     $statement->toString() // => "CREATE TRIGGER g BEFORE INSERT ON t FOR EACH ROW EXECUTE FUNCTION f(1, 2.5, 'x', word)"
28 */
29final class TriggerArgument implements Node
30{
31    use Snapshot;
32
33    /**
34     * @param IntegerConstant|NumericConstant|StringConstant|Name $value The argument
35     */
36    public function __construct(public readonly IntegerConstant|NumericConstant|StringConstant|Name $value)
37    {
38    }
39
40    /**
41     * Writes the argument.
42     */
43    public function render(Output $out): void
44    {
45        if ($this->value instanceof Name) {
46            $out->name($this->value, NameUse::Label);
47        } else {
48            $out->node($this->value);
49        }
50    }
51}
52