packages/sql-semantics-mysql/src/Statement/Utility/Show/Server/ShowEngineStatus.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Utility\Show\Server;
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\Statement\Identifier\Name;
11use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
12use SqlSemantics\Platform\MySql\Model\Statement\Statement;
13use SqlSemantics\Platform\MySql\Rules\Utility\Report;
14use SqlSemantics\Platform\MySql\Rules\Utility\ShowFacts;
15
16/**
17 * SHOW ENGINE … STATUS: the status report of a storage engine.
18 *
19 * Rule: MYSQL-SHOW-ENGINESTATUS-001. The engine is a name the server resolves, or ALL for every
20 * engine. The three SHOW ENGINE reports are distinct commands with the
21 * same columns: those of the layout of MYSQL-SHOW-ROWS-001. Terminates: a
22 * fixed layout.
23 * Source: https://dev.mysql.com/doc/refman/8.4/en/show-engine.html.
24 * Status: Implemented.
25 *
26 * @visibility public
27 * @example Reading the statement
28 *     $show = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SHOW ENGINE InnoDB STATUS');
29 *     [$show->statement->engine?->value, $show->toString()] // => ['InnoDB', 'SHOW ENGINE InnoDB STATUS']
30 */
31final class ShowEngineStatus implements Statement
32{
33    use Snapshot;
34
35    /**
36     * @param ?Name $engine The storage engine, or null for ALL
37     */
38    public function __construct(public readonly ?Name $engine = null)
39    {
40    }
41
42    /**
43     * Derives the rows.
44     */
45    public function deriveStatement(Derivation $derivation): void
46    {
47        (new ShowFacts())->rows($derivation, Report::Engine);
48    }
49
50    /**
51     * Writes the statement.
52     */
53    public function render(Output $out): void
54    {
55        $out->keyword('SHOW', 'ENGINE');
56        if ($this->engine === null) {
57            $out->keyword('ALL');
58        } else {
59            $out->name($this->engine, NameUse::Label);
60        }
61        $out->keyword('STATUS');
62    }
63}
64