packages/sql-semantics-mysql/src/Statement/Utility/Set/BareName.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Utility\Set;
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\Resolution\Environment;
12use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
16use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
18use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
19use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
20use SqlSemantics\Platform\MySql\Rules\ColumnResolver;
21use SqlSemantics\Platform\MySql\Statement\Type\Character;
22use SqlSemantics\Platform\MySql\Statement\Type\Kind\CharacterKind;
23use SqlSemantics\Platform\MySql\Statement\Utility\Problem\ProgramVariable;
24
25/**
26 * A bare name written as the value of a variable assignment in SET, such as `TRADITIONAL` in `SET sql_mode = TRADITIONAL`.
27 *
28 * Rule: MYSQL-SET-WORD-001. When the assigned variable is a system variable,
29 * the server does not resolve the name as a column: it assigns the text of
30 * the last name part (`b` for `a.b`) as a string. Facts: assigned to a
31 * system variable (`@@x`, or a name with a scope keyword), the value is a
32 * known non-NULL VARCHAR; assigned to a name without a scope, which inside a
33 * stored program can be a declared variable whose value is an expression,
34 * it depends on the declarations of the enclosing program (ProgramVariable).
35 * In a trigger body `NEW.x` and `OLD.x` are a column of the row whatever
36 * the variable (the parser makes them Item_trigger_field, which is not
37 * turned into text), so they resolve as a column name does
38 * (MYSQL-COLUMN-LOOKUP-001). The qualifiers are kept and written back.
39 * Terminates: no child is derived.
40 * Source: https://dev.mysql.com/doc/refman/8.4/en/set-variable.html,
41 * `set_var::set_var` in sql/set_var.cc ("If the set value is a field, change
42 * it to a string to allow things like SET table_type=MYISAM;").
43 * Status: Implemented.
44 *
45 * @visibility public
46 * @example Reading a word assigned to a system variable
47 * $set = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SET @@sql_mode = TRADITIONAL');
48 * [$set->statement->items[0]->value->word->value, $set->facts->scalar($set->statement->items[0]->value)->type->descriptor->name()] // => ['TRADITIONAL', 'VARCHAR']
49 */
50final class BareName implements Scalar
51{
52 use Snapshot;
53
54 /**
55 * @param Name $word The last name part, whose text is the value
56 * @param QualifiedName|null $qualifier The name parts written before it
57 * @param bool $system Whether the assigned variable is known to be a system variable
58 */
59 public function __construct(public readonly Name $word, public readonly ?QualifiedName $qualifier = null, public readonly bool $system = true)
60 {
61 Check::input($qualifier?->catalog === null, 'A word value has at most two qualifiers.');
62 }
63
64 /**
65 * Derives a known string for a system variable, and the dependence on the enclosing program otherwise.
66 */
67 public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
68 {
69 $resolver = new ColumnResolver();
70 if ($this->qualifier !== null && $resolver->row($environment, $this->qualifier)) {
71 return $resolver->fact($environment, $this->word, $this->qualifier);
72 }
73 if ($this->system) {
74 return new ScalarFact(new Known(new Character(CharacterKind::VarChar)), Nullability::NotNull);
75 }
76
77 return new ScalarFact(new Dependent([new ProgramVariable($this->word)]), Nullability::Dependent);
78 }
79
80 /**
81 * Writes the name parts separated by dots.
82 */
83 public function render(Output $out): void
84 {
85 if ($this->qualifier?->schema !== null) {
86 $out->name($this->qualifier->schema, NameUse::Qualifier)->symbol('.');
87 }
88 if ($this->qualifier !== null) {
89 $out->name($this->qualifier->name, NameUse::Qualifier)->symbol('.');
90 }
91 $out->name($this->word, NameUse::Column);
92 }
93}
94