packages/sql-semantics-mysql/src/Statement/Alter/Command/IndexVisibility.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Alter\Command;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Contract\NameUse;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
11use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
13use SqlSemantics\Platform\MySql\Statement\Alter\AlterCommand;
14
15/**
16 * `ALTER INDEX i VISIBLE | INVISIBLE` (8.0 and later): a request to make an index visible to or hidden from the optimizer.
17 *
18 * Mirrors PT_alter_table_index_visible.
19 * Source: https://dev.mysql.com/doc/refman/8.4/en/invisible-indexes.html.
20 *
21 * @visibility public
22 * @example Hiding an index
23 *     $alter = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('ALTER TABLE t ALTER INDEX i INVISIBLE');
24 *     $alter->statement->commands[0]->visible // => false
25 */
26final class IndexVisibility implements AlterCommand
27{
28    use Snapshot;
29
30    /**
31     * @param Name $index The index name
32     * @param bool $visible Whether VISIBLE (true) or INVISIBLE (false) is written
33     */
34    public function __construct(public readonly Name $index, public readonly bool $visible)
35    {
36    }
37
38    /**
39     * Derives nothing: the action holds no expression.
40     */
41    public function deriveCommand(Derivation $derivation, Environment $scope): void
42    {
43    }
44
45    /**
46     * Writes the action.
47     */
48    public function render(Output $out): void
49    {
50        $out->keyword('ALTER', 'INDEX')->name($this->index, NameUse::Label)->keyword($this->visible ? 'VISIBLE' : 'INVISIBLE');
51    }
52}
53