packages/sql-semantics-mysql/src/Statement/Utility/Show/Schema/ShowOpenTables.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Utility\Show\Schema;
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\Fact\RelationFact;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Relation;
14use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
15use SqlSemantics\Platform\MySql\Model\Statement\Statement;
16use SqlSemantics\Platform\MySql\Rules\Utility\Report;
17use SqlSemantics\Platform\MySql\Rules\Utility\ShowFacts;
18use SqlSemantics\Platform\MySql\Statement\Utility\Show\ShowLike;
19use SqlSemantics\Platform\MySql\Statement\Utility\Show\ShowWhere;
20
21/**
22 * SHOW OPEN TABLES: the tables open in the table cache.
23 *
24 * Rule: MYSQL-SHOW-OPEN-TABLES-001. The columns are those of the layout of MYSQL-SHOW-ROWS-001 for
25 * the release; the database written after FROM or IN only selects the
26 * rows. The rows and the WHERE condition are derived by
27 * MYSQL-SHOW-FACTS-001. Terminates: a fixed layout.
28 * Source: https://dev.mysql.com/doc/refman/8.4/en/show-open-tables.html.
29 * Status: Implemented.
30 *
31 * @visibility public
32 * @example Reading the statement
33 *     $show = (new \SqlSemantics\Platform\MySql\Semantics())->analyze("SHOW OPEN TABLES FROM shop LIKE 'a%'");
34 *     [$show->statement->database?->value, $show->toString()] // => ['shop', "SHOW OPEN TABLES FROM shop LIKE 'a%'"]
35 */
36final class ShowOpenTables implements Statement, Relation
37{
38    use Snapshot;
39
40    /**
41     * @param ?Name $database The database written after FROM or IN; null for the current database
42     * @param ShowLike|ShowWhere|null $filter The LIKE or WHERE clause
43     */
44    public function __construct(public readonly ?Name $database = null, public readonly ShowLike|ShowWhere|null $filter = null)
45    {
46    }
47
48    /**
49     * Derives the rows and the filter.
50     */
51    public function deriveStatement(Derivation $derivation): void
52    {
53        (new ShowFacts())->derive($derivation, $this, $this->filter);
54    }
55
56    /**
57     * Derives the result columns.
58     */
59    public function deriveRelation(Derivation $derivation, Environment $environment): RelationFact
60    {
61        return (new ShowFacts())->fact($derivation, Report::OpenTables);
62    }
63
64    /**
65     * Writes the statement.
66     */
67    public function render(Output $out): void
68    {
69        $out->keyword('SHOW', 'OPEN', 'TABLES');
70        if ($this->database !== null) {
71            $out->keyword('FROM')->name($this->database, NameUse::Qualifier);
72        }
73        $out->node($this->filter);
74    }
75}
76