packages/sql-semantics-postgres/src/Statement/Constraint/Reference/ReferenceAction.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Constraint\Reference;
6
7/**
8 * What a foreign key does when a referenced row changes.
9 *
10 * Mirrors the `FKCONSTR_ACTION_*` codes. NO ACTION is the default and is kept
11 * when written.
12 * Source: https://www.postgresql.org/docs/17/sql-createtable.html#SQL-CREATETABLE-PARMS-REFERENCES.
13 *
14 * @visibility public
15 * @example Reading a referential action
16 *     $create = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('CREATE TABLE t (a int REFERENCES u ON UPDATE SET NULL)');
17 *     $create->statement->definition->elements[0]->qualifiers[0]->actions[0]->action // => \SqlSemantics\Platform\PostgreSql\Statement\Constraint\Reference\ReferenceAction::SetNull
18 */
19enum ReferenceAction: string
20{
21    case NoAction = 'NO ACTION';
22    case Restrict = 'RESTRICT';
23    case Cascade = 'CASCADE';
24    case SetNull = 'SET NULL';
25    case SetDefault = 'SET DEFAULT';
26
27    /**
28     * Tells whether the action may name the columns it sets.
29     */
30    public function setsColumns(): bool
31    {
32        return match ($this) {
33            self::SetNull, self::SetDefault => true,
34            self::NoAction, self::Restrict, self::Cascade => false,
35        };
36    }
37}
38