packages/sql-semantics-mysql/src/Statement/Server/KeyCache/CachedTable.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Server\KeyCache;
6
7use SqlSemantics\Platform\MySql\Model\Rendering\Output;
8use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
9use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
10use SqlSemantics\Platform\MySql\Model\Statement\Node;
11use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
12use SqlSemantics\Platform\MySql\Rules\Server\CachedIndexes;
13use SqlSemantics\Platform\MySql\Statement\Partition\PartitionSelection;
14use SqlSemantics\Platform\MySql\Statement\Relation\Hint\PrimaryIndex;
15
16/**
17 * One table whose indexes CACHE INDEX assigns to a key cache: `t [PARTITION (…)] [INDEX (…)]`.
18 *
19 * Mirrors PT_assign_to_keycache. The statement that holds it records the
20 * resolution of the table name as the relation fact of this node. An absent
21 * index list assigns every index of the table, an empty list none
22 * (MYSQL-CACHED-INDEXES-001).
23 * Source: https://dev.mysql.com/doc/refman/8.4/en/cache-index.html.
24 *
25 * @visibility public
26 * @example Holding a table and its index list
27 *     $table = new \SqlSemantics\Platform\MySql\Statement\Server\KeyCache\CachedTable(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name('t')), null, [new \SqlSemantics\Platform\MySql\Statement\Relation\Hint\PrimaryIndex()]);
28 *     count($table->indexes ?? []) // => 1
29 */
30final class CachedTable implements Node
31{
32    use Snapshot;
33
34    /**
35     * @var list<Name|PrimaryIndex>|null The indexes, when an index list is written
36     */
37    public readonly ?array $indexes;
38
39    /**
40     * @param QualifiedName $table The table name with its optional database
41     * @param PartitionSelection|null $partitions The partitions, when written
42     * @param list<Name|PrimaryIndex>|null $indexes The indexes, when an index list is written
43     */
44    public function __construct(public readonly QualifiedName $table, public readonly ?PartitionSelection $partitions = null, ?array $indexes = null)
45    {
46        $this->indexes = (new CachedIndexes())->checked($table, $indexes);
47    }
48
49    /**
50     * Writes the table, the partitions and the index list.
51     */
52    public function render(Output $out): void
53    {
54        (new CachedIndexes())->render($out, $this->table, $this->partitions, $this->indexes);
55    }
56}
57