packages/sql-semantics-postgres/src/Statement/Catalog/Cast/TransformFunction.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Catalog\Cast;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
9use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\PostgreSql\Statement\Clause;
12use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectReference;
13
14/**
15 * `FROM SQL WITH FUNCTION function` or `TO SQL WITH FUNCTION function` of a transform.
16 *
17 * Source: https://www.postgresql.org/docs/17/sql-createtransform.html.
18 *
19 * @visibility public
20 * @example Reading the direction of a transform function
21 * $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('CREATE TRANSFORM FOR hstore LANGUAGE plpython3u (TO SQL WITH FUNCTION f(internal))');
22 * $operation->statement->functions[0]->direction // => \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Cast\TransformDirection::ToSql
23 */
24final class TransformFunction implements Clause
25{
26 use Snapshot;
27
28 /**
29 * @param TransformDirection $direction The direction
30 * @param ObjectReference $function The function signature
31 */
32 public function __construct(public readonly TransformDirection $direction, public readonly ObjectReference $function)
33 {
34 }
35
36 /**
37 * Derives the function signature.
38 */
39 public function deriveClause(Derivation $derivation, Environment $environment): void
40 {
41 $this->function->deriveClause($derivation, $environment);
42 }
43
44 /**
45 * Writes the clause.
46 */
47 public function render(Output $out): void
48 {
49 $out->keyword(...explode(' ', $this->direction->value))->keyword('WITH', 'FUNCTION')->node($this->function);
50 }
51}
52