packages/sql-semantics-mysql/src/Statement/Server/Plugin/InstallComponent.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Server\Plugin;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\InvalidConstruction;
10use SqlSemantics\Platform\MySql\Model\Rendering\Output;
11use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
12use SqlSemantics\Platform\MySql\Model\Statement\Statement;
13use SqlSemantics\Platform\MySql\Statement\Literal\Text;
14
15/**
16 * `INSTALL COMPONENT 'urn', … [SET assignment, …]`: a request to load server components (MySQL 8.0 and later).
17 *
18 * Mirrors PT_install_component (Sql_cmd_install_component). Rule:
19 * MYSQL-INSTALL-COMPONENT-001. The SET list (MySQL 8.0.33 and later) gives
20 * component variables their values as the components are loaded; each value
21 * is derived where no relation is visible, and each variable is a system
22 * variable whose facts name the server state it depends on. The statement
23 * returns no rows.
24 * Source: https://dev.mysql.com/doc/refman/8.4/en/install-component.html.
25 * Status: Implemented.
26 *
27 * @visibility public
28 * @example Installing a component with a variable value
29 * $install = (new \SqlSemantics\Platform\MySql\Semantics())->analyze("install component 'file://a', 'file://b' set global a.size = 2 * 8");
30 * [$install->toString(), count($install->statement->components)] // => ["INSTALL COMPONENT 'file://a', 'file://b' SET GLOBAL a.size = 2 * 8", 2]
31 */
32final class InstallComponent implements Statement
33{
34 use Snapshot;
35
36 /**
37 * @var list<Text> The component URNs in written order; at least one
38 */
39 public readonly array $components;
40
41 /**
42 * @var list<ComponentSetting> The variable assignments in written order
43 */
44 public readonly array $settings;
45
46 /**
47 * @param list<Text> $components The component URNs in written order; at least one
48 * @param list<ComponentSetting> $settings The variable assignments in written order
49 * @throws InvalidConstruction When no component is named
50 */
51 public function __construct(array $components, array $settings = [])
52 {
53 $this->components = Check::listOf($components, Text::class, 'INSTALL COMPONENT names at least one component.', 1);
54 $this->settings = Check::listOf($settings, ComponentSetting::class, 'INSTALL COMPONENT takes a list of variable assignments.');
55 }
56
57 /**
58 * Derives each variable and value where no relation is visible.
59 */
60 public function deriveStatement(Derivation $derivation): void
61 {
62 foreach ($this->settings as $setting) {
63 $derivation->scalar($setting->variable, $derivation->environment());
64 $derivation->scalar($setting->value, $derivation->environment());
65 }
66 }
67
68 /**
69 * Writes the request.
70 */
71 public function render(Output $out): void
72 {
73 $out->keyword('INSTALL', 'COMPONENT')->list($this->components);
74 if ($this->settings !== []) {
75 $out->keyword('SET')->list($this->settings);
76 }
77 }
78}
79