packages/sql-semantics-mysql/src/Statement/Alter/Partition/PartitionBy.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Alter\Partition;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Rendering\Output;
9use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\MySql\Statement\Partition\PartitionClause;
12
13/**
14 * `PARTITION BY …` in ALTER TABLE: a request to partition or repartition the table.
15 *
16 * Mirrors PT_alter_table_partition_by. The partitioning is derived in the
17 * scope of the changed table (MYSQL-PARTITIONING-001).
18 * Source: https://dev.mysql.com/doc/refman/8.4/en/alter-table-partition-operations.html.
19 *
20 * @visibility public
21 * @example Partitioning a table by hash
22 *     $alter = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('ALTER TABLE t PARTITION BY HASH (a) PARTITIONS 4');
23 *     $alter->toString() // => 'ALTER TABLE t PARTITION BY HASH (a) PARTITIONS 4'
24 */
25final class PartitionBy implements TrailingCommand
26{
27    use Snapshot;
28
29    /**
30     * @param PartitionClause $partitioning The new partitioning
31     */
32    public function __construct(public readonly PartitionClause $partitioning)
33    {
34    }
35
36    /**
37     * Derives the partitioning in the scope of the table.
38     */
39    public function deriveCommand(Derivation $derivation, Environment $scope): void
40    {
41        $this->partitioning->derivePartitioning($derivation, $scope);
42    }
43
44    /**
45     * Writes the clause.
46     */
47    public function render(Output $out): void
48    {
49        $out->node($this->partitioning);
50    }
51}
52