packages/sql-semantics-sqlite/src/Statement/Schema/Column/NotNull.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Statement\Schema\Column;
6
7use SqlSemantics\Platform\Sqlite\Model\Construction\Derivation;
8use SqlSemantics\Platform\Sqlite\Model\Rendering\Output;
9use SqlSemantics\Platform\Sqlite\Model\Statement\Snapshot;
10use SqlSemantics\Platform\Sqlite\Rules\Definition\ConstraintScope;
11use SqlSemantics\Platform\Sqlite\Statement\Mutation\ConflictResolution;
12
13/**
14 * A NOT NULL column constraint.
15 *
16 * Rule: SQLITE-COLUMN-NOT-NULL-001. The column cannot hold NULL; the conflict
17 * resolution says what a violating write does and defaults to ABORT.
18 * Source: https://sqlite.org/lang_createtable.html#not_null_constraints.
19 * Status: Implemented.
20 *
21 * @visibility public
22 * @example Reading the conflict resolution of a NOT NULL constraint
23 * $create = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('CREATE TABLE t (a INT NOT NULL ON CONFLICT IGNORE)');
24 * $create->statement->columns[0]->constraints[0]->conflict // => \SqlSemantics\Platform\Sqlite\Statement\Mutation\ConflictResolution::Ignore
25 */
26final class NotNull implements ColumnConstraint
27{
28 use Snapshot;
29
30 /**
31 * @param ConflictResolution|null $conflict The written ON CONFLICT resolution
32 */
33 public function __construct(public readonly ?ConflictResolution $conflict = null)
34 {
35 }
36
37 /**
38 * Derives nothing: the clause has no operand that depends on a declaration.
39 */
40 public function deriveConstraint(Derivation $derivation, ConstraintScope $scope): void
41 {
42 }
43
44 /**
45 * Writes the clause.
46 */
47 public function render(Output $out): void
48 {
49 $out->keyword('NOT', 'NULL');
50 if ($this->conflict !== null) {
51 $out->keyword('ON', 'CONFLICT', $this->conflict->value);
52 }
53 }
54}
55