packages/sql-semantics-mysql/src/Statement/Utility/Explain/ExplainConnection.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Utility\Explain;
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\Statement\Identifier\Name;
12use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
13use SqlSemantics\Platform\MySql\Model\Statement\Statement;
14use SqlSemantics\Platform\MySql\Rules\Utility\ExplainFacts;
15use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
16use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
17
18/**
19 * EXPLAIN FOR CONNECTION: the plan of the statement another connection is running (MySQL 5.7 and later).
20 *
21 * Rule: MYSQL-EXPLAIN-CONNECTION-001. The connection is a thread number
22 * as SHOW PROCESSLIST reports it; the statement it runs is not part of the
23 * request. The options and the rows of the report follow
24 * MYSQL-EXPLAIN-ROWS-001. Terminates: no nested statement.
25 * Source: https://dev.mysql.com/doc/refman/8.4/en/explain-for-connection.html.
26 * Status: Implemented.
27 *
28 * @visibility public
29 * @example Reading the connection
30 * $explain = (new \SqlSemantics\Platform\MySql\Semantics('mysql-5.7.44'))->analyze('EXPLAIN FORMAT = JSON FOR CONNECTION 42');
31 * [$explain->statement->connection->text, $explain->field(0)->name?->value, $explain->toString()] // => ['42', 'EXPLAIN', 'EXPLAIN FORMAT = `JSON` FOR CONNECTION 42']
32 */
33final class ExplainConnection implements Statement
34{
35 use Snapshot;
36
37 /**
38 * @param Numeral $connection The connection identifier
39 * @param Name|null $format The format name after FORMAT =
40 * @param bool $analyze Whether ANALYZE is written
41 * @param ExplainModifier|null $modifier The EXTENDED or PARTITIONS keyword of MySQL 5.7
42 * @param UserVariable|null $into The variable after INTO
43 */
44 public function __construct(
45 public readonly Numeral $connection,
46 public readonly ?Name $format = null,
47 public readonly bool $analyze = false,
48 public readonly ?ExplainModifier $modifier = null,
49 public readonly ?UserVariable $into = null,
50 ) {
51 Check::input($modifier === null || ($format === null && !$analyze && $into === null), 'EXTENDED and PARTITIONS are written alone.');
52 }
53
54 /**
55 * Records the rows of the report.
56 */
57 public function deriveStatement(Derivation $derivation): void
58 {
59 (new ExplainFacts())->derive($derivation, $this->format, $this->analyze, $this->modifier, $this->into);
60 }
61
62 /**
63 * Writes EXPLAIN, the options and the connection.
64 */
65 public function render(Output $out): void
66 {
67 $out->keyword('EXPLAIN');
68 if ($this->analyze) {
69 $out->keyword('ANALYZE');
70 }
71 if ($this->modifier !== null) {
72 $out->keyword($this->modifier->value);
73 }
74 if ($this->format !== null) {
75 $out->keyword('FORMAT')->symbol('=')->name($this->format, NameUse::Label);
76 }
77 if ($this->into !== null) {
78 $out->keyword('INTO')->node($this->into);
79 }
80 $out->keyword('FOR', 'CONNECTION')->node($this->connection);
81 }
82}
83