packages/sql-semantics-postgres/src/Statement/Table/Sequence/SequenceAs.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Table\Sequence;
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\Type\TypeDesignation;
12
13/**
14 * The AS option of a sequence: the data type of its values.
15 *
16 * The server accepts smallint, integer and bigint.
17 * Source: https://www.postgresql.org/docs/17/sql-createsequence.html.
18 *
19 * @visibility public
20 * @example Reading the data type of a sequence
21 * $create = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('CREATE SEQUENCE s AS smallint');
22 * $create->statement->options[0]->type->catalogName()->value // => 'int2'
23 */
24final class SequenceAs implements SequenceOption
25{
26 use Snapshot;
27
28 /**
29 * @param TypeDesignation $type The data type
30 */
31 public function __construct(public readonly TypeDesignation $type)
32 {
33 }
34
35 /**
36 * Derives the type modifiers.
37 */
38 public function deriveClause(Derivation $derivation, Environment $environment): void
39 {
40 $this->type->deriveClause($derivation, $environment);
41 }
42
43 /**
44 * Writes AS and the type.
45 */
46 public function render(Output $out): void
47 {
48 $out->keyword('AS')->node($this->type);
49 }
50}
51