packages/sql-semantics-mysql/src/Statement/Alter/Partition/SecondaryLoad.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\Contract\NameUse;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
10use SqlSemantics\Platform\MySql\Model\Rendering\Output;
11use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
14
15/**
16 * `SECONDARY_LOAD` or `SECONDARY_UNLOAD` (8.0 and later), with `PARTITION (p, …)` from 9.0 on: a request to load the table into or unload it from its secondary engine.
17 *
18 * Mirrors PT_alter_table_secondary_load and PT_alter_table_secondary_unload.
19 * Source: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html.
20 *
21 * @visibility public
22 * @example Loading a table into the secondary engine
23 *     (new \SqlSemantics\Platform\MySql\Semantics())->analyze('ALTER TABLE t SECONDARY_LOAD')->statement->commands[0]->load // => true
24 */
25final class SecondaryLoad implements StandaloneCommand
26{
27    use Snapshot;
28
29    /**
30     * @var list<Name> The partitions in order; empty for the whole table
31     */
32    public readonly array $partitions;
33
34    /**
35     * @param bool $load Whether SECONDARY_LOAD (true) or SECONDARY_UNLOAD (false) is written
36     * @param list<Name> $partitions The partitions in order; empty for the whole table
37     */
38    public function __construct(public readonly bool $load, array $partitions = [])
39    {
40        $this->partitions = Check::listOf($partitions, Name::class, 'The partitions of a secondary load are a list of names.');
41    }
42
43    /**
44     * Derives nothing: the action holds no expression.
45     */
46    public function deriveCommand(Derivation $derivation, Environment $scope): void
47    {
48    }
49
50    /**
51     * Writes the action.
52     */
53    public function render(Output $out): void
54    {
55        $out->keyword($this->load ? 'SECONDARY_LOAD' : 'SECONDARY_UNLOAD');
56        if ($this->partitions !== []) {
57            $out->keyword('PARTITION')->symbol('(');
58            foreach ($this->partitions as $index => $partition) {
59                if ($index > 0) {
60                    $out->symbol(',');
61                }
62                $out->name($partition, NameUse::Label);
63            }
64            $out->symbol(')');
65        }
66    }
67}
68