packages/sql-semantics-mysql/src/Statement/Dml/Assignment.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Dml;
6
7use SqlSemantics\Platform\MySql\Model\Rendering\Output;
8use SqlSemantics\Platform\MySql\Model\Statement\Node;
9use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
12
13/**
14 * One assignment `column = value` of SET, ON DUPLICATE KEY UPDATE, UPDATE or LOAD DATA.
15 *
16 * The statement that holds the assignment resolves the column among the
17 * tables it writes and derives the value; `=` and `:=` are one operator here.
18 * Source: https://dev.mysql.com/doc/refman/8.4/en/update.html.
19 *
20 * @visibility public
21 * @example Reading an assignment of UPDATE
22 *     $update = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('UPDATE t SET a = a + 1');
23 *     $update->statement->assignments[0]->column->name->value // => 'a'
24 */
25final class Assignment implements Node
26{
27    use Snapshot;
28
29    /**
30     * @param ColumnUse $column The column assigned to
31     * @param Scalar $value The value: an expression or DEFAULT
32     */
33    public function __construct(public readonly ColumnUse $column, public readonly Scalar $value)
34    {
35    }
36
37    /**
38     * Writes the assignment.
39     */
40    public function render(Output $out): void
41    {
42        $out->node($this->column)->symbol('=')->node($this->value);
43    }
44}
45