packages/sql-semantics-postgres/src/Statement/Relation/Json/JsonOrdinalityColumn.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Relation\Json;
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\Snapshot;
11
12/**
13 * name FOR ORDINALITY: a JSON_TABLE column that numbers the rows.
14 *
15 * Source: https://www.postgresql.org/docs/17/functions-json.html#FUNCTIONS-SQLJSON-TABLE.
16 *
17 * @visibility public
18 * @example Reading an ordinality column
19 *     (new \SqlSemantics\Platform\PostgreSql\Statement\Relation\Json\JsonOrdinalityColumn(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('n')))->name->value // => 'n'
20 */
21final class JsonOrdinalityColumn implements JsonTableColumn
22{
23    use Snapshot;
24
25    /**
26     * @param Name $name The column name
27     */
28    public function __construct(public readonly Name $name)
29    {
30    }
31
32    /**
33     * Writes the name and FOR ORDINALITY.
34     */
35    public function render(Output $out): void
36    {
37        $out->name($this->name, NameUse::Column)->keyword('FOR', 'ORDINALITY');
38    }
39}
40