packages/sql-semantics-sqlite/src/Statement/Schema/Reference/ReferenceAction.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Statement\Schema\Reference;
6
7use SqlSemantics\Platform\Sqlite\Model\Rendering\Output;
8use SqlSemantics\Platform\Sqlite\Model\Statement\Snapshot;
9
10/**
11 * An ON DELETE, ON UPDATE or ON INSERT clause of a foreign key.
12 *
13 * Rule: SQLITE-FK-ACTION-001. The reaction applies to the child rows when the
14 * parent key is deleted or updated. An ON INSERT clause is grammatical and
15 * has no effect.
16 * Source: https://sqlite.org/foreignkeys.html#fk_actions. Status: Implemented.
17 *
18 * @visibility public
19 * @example Reading a foreign key action
20 * $create = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('CREATE TABLE c (p REFERENCES parent ON DELETE NO ACTION)');
21 * $action = $create->statement->columns[0]->constraints[0]->arguments[0];
22 * [$action->event->value, $action->reaction->value] // => ['DELETE', 'NO ACTION']
23 */
24final class ReferenceAction implements ReferenceArgument
25{
26 use Snapshot;
27
28 /**
29 * @param ReferenceEvent $event The change of the parent key
30 * @param ReferenceReaction $reaction What happens to the child rows
31 */
32 public function __construct(public readonly ReferenceEvent $event, public readonly ReferenceReaction $reaction)
33 {
34 }
35
36 /**
37 * Writes the clause.
38 */
39 public function render(Output $out): void
40 {
41 $out->keyword('ON', $this->event->value, ...explode(' ', $this->reaction->value));
42 }
43}
44