packages/sql-semantics-mysql/src/Statement/Variable/SystemVariable.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Variable;
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\ScalarFact;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Reference\Missing\SessionState;
14use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
15use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
18
19/**
20 * A system variable read as a value or named as the target of SET: `@@name`, `@@GLOBAL.name`, `@@instance.name`.
21 *
22 * A structured variable is written with the name of its instance or
23 * component before the variable name; `DEFAULT.name` names the instance
24 * `default`. Without a scope the session value is read when the variable has
25 * one and the global value otherwise.
26 *
27 * Rule: MYSQL-SYSTEM-VARIABLE-001. Facts: the type and the NULL fact are
28 * those of the variable in the running server, which no context holds; the
29 * fact names the variable as missing session state. Diagnostics: none.
30 * Source: https://dev.mysql.com/doc/refman/8.4/en/using-system-variables.html,
31 * https://dev.mysql.com/doc/refman/8.4/en/structured-system-variables.html. Status: Implemented.
32 *
33 * @visibility public
34 * @example Reading a scoped system variable
35 * $query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SELECT a FROM t WHERE a = @@GLOBAL.sort_buffer_size');
36 * [$query->statement->where->right->scope, $query->statement->where->right->name->value] // => [\SqlSemantics\Platform\MySql\Statement\Variable\VariableScope::Global, 'sort_buffer_size']
37 */
38final class SystemVariable implements Scalar
39{
40 use Snapshot;
41
42 /**
43 * @param Name $name The variable name
44 * @param VariableScope|null $scope The scope written before the name; the grammar reads GLOBAL or SESSION in an expression and also PERSIST and PERSIST_ONLY in SET
45 * @param Name|null $instance The instance or component name of a structured variable
46 */
47 public function __construct(public readonly Name $name, public readonly ?VariableScope $scope = null, public readonly ?Name $instance = null)
48 {
49 }
50
51 /**
52 * Derives the dependence on the server.
53 */
54 public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
55 {
56 $subject = 'system variable @@' . ($this->scope === null ? '' : $this->scope->value . '.') . ($this->instance === null ? '' : $this->instance->value . '.') . $this->name->value;
57
58 return new ScalarFact(new Dependent([new SessionState($subject)]), Nullability::Dependent);
59 }
60
61 /**
62 * Writes both at signs, the scope, the instance and the name without spaces.
63 */
64 public function render(Output $out): void
65 {
66 $out->symbol('@')->glue()->symbol('@')->glue();
67 if ($this->scope !== null) {
68 $out->keyword($this->scope->value)->symbol('.');
69 }
70 if ($this->instance !== null) {
71 $out->name($this->instance, NameUse::Label)->symbol('.');
72 }
73 $out->name($this->name, NameUse::Label);
74 }
75}
76