packages/sql-semantics-mysql/src/Statement/Partition/Bound/PartitionRow.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Partition\Bound;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Statement\Node;
11use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
12use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
13
14/**
15 * A parenthesized row of partition values: expressions and MAXVALUE.
16 *
17 * Mirrors PT_part_value_item_list_paren. Each expression is a constant the
18 * server evaluates when the table is defined; it is derived in a scope with
19 * no visible relation, so a column name in it is a diagnostic.
20 * Source: https://dev.mysql.com/doc/refman/8.4/en/partitioning-columns.html.
21 *
22 * @visibility public
23 * @example Holding a row of partition values
24 *     $row = new \SqlSemantics\Platform\MySql\Statement\Partition\Bound\PartitionRow([new \SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral('10'), new \SqlSemantics\Platform\MySql\Statement\Partition\Bound\PartitionMaximum()]);
25 *     count($row->items) // => 2
26 */
27final class PartitionRow implements Node
28{
29    use Snapshot;
30
31    /**
32     * @var list<Scalar|PartitionMaximum> The values in order; at least one
33     */
34    public readonly array $items;
35
36    /**
37     * @param list<Scalar|PartitionMaximum> $items The values in order; at least one
38     */
39    public function __construct(array $items)
40    {
41        $list = [];
42        foreach (Check::listOf($items, Node::class, 'A row of partition values holds at least one value.', 1) as $item) {
43            Check::input($item instanceof Scalar || $item instanceof PartitionMaximum, 'A partition value is an expression or MAXVALUE.');
44            $list[] = $item;
45        }
46        $this->items = $list;
47    }
48
49    /**
50     * Derives the expressions as constants.
51     */
52    public function deriveRow(Derivation $derivation): void
53    {
54        foreach ($this->items as $item) {
55            if ($item instanceof Scalar) {
56                $derivation->scalar($item, $derivation->environment());
57            }
58        }
59    }
60
61    /**
62     * Writes the parenthesized values.
63     */
64    public function render(Output $out): void
65    {
66        $out->symbol('(')->list($this->items)->symbol(')');
67    }
68}
69