packages/sql-semantics-mysql/src/Rules/Routine/TriggerAssignments.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Rules\Routine;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
9use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\MissingColumn;
10use SqlSemantics\Platform\MySql\Rules\ColumnResolver;
11use SqlSemantics\Platform\MySql\Statement\Routine\Problem\ProgramProblem;
12use SqlSemantics\Platform\MySql\Statement\Routine\Problem\ProgramRule;
13use SqlSemantics\Platform\MySql\Statement\Routine\Trigger\TriggerEvent;
14use SqlSemantics\Platform\MySql\Statement\Routine\Trigger\TriggerTime;
15use SqlSemantics\Platform\MySql\Statement\Utility\Set\NameAssignment;
16use SqlSemantics\Platform\MySql\Statement\Utility\Set\SetVariables;
17
18/**
19 * Checks the assignments of SET to a column of the NEW or OLD row in a trigger body.
20 *
21 * Rule: MYSQL-TRIGGER-ASSIGNMENT-001. In a trigger, a SET item without a
22 * scope keyword whose name is qualified by NEW or OLD, in any letter case,
23 * assigns to a column of the row of the trigger table. The OLD row cannot
24 * be changed (ER_TRG_CANT_CHANGE_ROW); a DELETE trigger has no NEW row
25 * (ER_TRG_NO_SUCH_ROW_IN_TRG); an AFTER trigger cannot change the NEW row
26 * (ER_TRG_CANT_CHANGE_ROW). Otherwise the name is resolved as a column of
27 * the NEW row, and a column the table does not have is reported
28 * (ER_BAD_FIELD_ERROR); a table that is not completely known reports
29 * nothing. The value is derived by the SET statement itself at the position
30 * of the body, where NEW and OLD are visible. Terminates: one pass over the
31 * items. Source: https://dev.mysql.com/doc/refman/8.4/en/trigger-syntax.html
32 * ("In a BEFORE trigger, you can also change its value with SET NEW.col_name
33 * = value"; "OLD columns are read only").
34 * Status: Implemented.
35 *
36 * @visibility SqlSemantics\Platform\MySql
37 */
38final class TriggerAssignments
39{
40 /**
41 * Checks the items of a SET statement of a trigger body that assign to the NEW or OLD row.
42 */
43 public function check(SetVariables $set, TriggerTime $time, TriggerEvent $event, Derivation $derivation, ProgramScope $scope): void
44 {
45 foreach ($set->items as $item) {
46 if (!$item instanceof NameAssignment || $item->scope !== null || $item->qualifier === null) {
47 continue;
48 }
49 $row = strtoupper($item->qualifier->value);
50 $problem = match (true) {
51 $row === 'OLD' => new ProgramProblem(ProgramRule::OldRowUpdate),
52 $row !== 'NEW' => null,
53 $event === TriggerEvent::Delete => new ProgramProblem(ProgramRule::NoNewRow),
54 $time === TriggerTime::After => new ProgramProblem(ProgramRule::AfterRowUpdate),
55 default => null,
56 };
57 if ($problem !== null) {
58 $derivation->report($problem);
59 } elseif ($row === 'NEW') {
60 $resolution = (new ColumnResolver())->find($scope->environment, $item->name, new QualifiedName($item->qualifier));
61 if ($resolution instanceof MissingColumn) {
62 $derivation->report($resolution);
63 }
64 }
65 }
66 }
67}
68