packages/sql-semantics-postgres/src/Statement/Table/Partition/ListBound.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Table\Partition;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
10use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Scalar;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
13
14/**
15 * A list partition bound: FOR VALUES IN (values).
16 *
17 * Mirrors `PartitionBoundSpec` with `listdatums`. The values are derived
18 * where no column is visible.
19 * Source: https://www.postgresql.org/docs/17/sql-createtable.html.
20 *
21 * @visibility public
22 * @example Reading the values of a list bound
23 * $create = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("CREATE TABLE p1 PARTITION OF p FOR VALUES IN ('a', 'b')");
24 * count($create->statement->definition->bound->values) // => 2
25 */
26final class ListBound implements PartitionBound
27{
28 use Snapshot;
29
30 /**
31 * @var non-empty-list<Scalar> The values
32 */
33 public readonly array $values;
34
35 /**
36 * @param list<Scalar> $values The values; at least one
37 */
38 public function __construct(array $values)
39 {
40 $this->values = Check::listOf($values, Scalar::class, 'A list bound has at least one value.', 1);
41 }
42
43 /**
44 * Derives the values where no column is visible.
45 */
46 public function deriveClause(Derivation $derivation, Environment $environment): void
47 {
48 foreach ($this->values as $value) {
49 $derivation->scalar($value, $derivation->environment());
50 }
51 }
52
53 /**
54 * Writes the bound.
55 */
56 public function render(Output $out): void
57 {
58 $out->keyword('FOR', 'VALUES', 'IN')->symbol('(')->list($this->values)->symbol(')');
59 }
60}
61