packages/sql-semantics-sqlite/tests/Unit/Statement/Mutation/UpdateTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Mutation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Construction\Derivation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Field;
16use SqlSemantics\Platform\Sqlite\Semantics;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\Binary;
18use SqlSemantics\Platform\Sqlite\Statement\Mutation\Assignment;
19use SqlSemantics\Platform\Sqlite\Statement\Mutation\MutationTarget;
20use SqlSemantics\Platform\Sqlite\Statement\Mutation\Update;
21use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\Misuse;
22use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\MisuseRule;
23use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
24
25#[CoversClass(Update::class)]
26#[Medium]
27final class UpdateTest extends TestCase
28{
29 public function testInputAnswersTheFromRelation(): void
30 {
31 $semantics = new Semantics();
32 $joined = $semantics->inspect('UPDATE t SET a = u.a FROM u');
33 $plain = $semantics->inspect('UPDATE t SET a = 1');
34
35 self::assertInstanceOf(Update::class, $joined->statement);
36 self::assertInstanceOf(TableInput::class, $joined->statement->input());
37 self::assertSame('u', $joined->singleNamedInput()->name()->name->value);
38 self::assertInstanceOf(Update::class, $plain->statement);
39 self::assertNull($plain->statement->input());
40 self::assertNull($plain->inputRelation());
41 }
42
43 public function testDeriveWithinSeesTheTargetAndTheFromTablesInAssignmentsAndWhere(): void
44 {
45 $semantics = new Semantics();
46 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
47 $u = $semantics->inspect('CREATE TABLE u (a INTEGER NOT NULL, c TEXT)');
48 $query = $semantics->inspect('UPDATE t SET a = u.a FROM u WHERE u.c = t.b', [$t, $u]);
49
50 self::assertInstanceOf(Update::class, $query->statement);
51 self::assertInstanceOf(Assignment::class, $query->statement->assignments[0]);
52 $value = $query->facts->scalar($query->statement->assignments[0]->value)->resolution;
53 self::assertInstanceOf(ResolvedColumn::class, $value);
54 self::assertSame($query->statement->from, $value->relation);
55 self::assertSame($u->declarations()[0]->columns[0], $value->declaration());
56 self::assertInstanceOf(Binary::class, $query->statement->where);
57 $right = $query->facts->scalar($query->statement->where->right)->resolution;
58 self::assertInstanceOf(ResolvedColumn::class, $right);
59 self::assertSame($query->statement->target, $right->relation);
60 self::assertSame([], $query->facts->diagnostics);
61 }
62
63 public function testDeriveWithinLetsReturningSeeTheTargetOnly(): void
64 {
65 $semantics = new Semantics();
66 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
67 $u = $semantics->inspect('CREATE TABLE u (a INTEGER NOT NULL, c TEXT)');
68 $star = $semantics->inspect('UPDATE t SET a = u.a FROM u WHERE u.a = t.a RETURNING *', [$t, $u]);
69 $foreign = $semantics->inspect('UPDATE t SET a = u.a FROM u WHERE u.a = t.a RETURNING u.c', [$t, $u]);
70
71 self::assertSame(['id', 'a', 'b'], array_map(static fn (Field $field): ?string => $field->name?->value, $star->fields()->items ?? []));
72 self::assertSame($t->declarations()[0]->columns[0], $star->field('id')->column());
73 self::assertInstanceOf(MissingColumn::class, $foreign->field(0)->resolution);
74 self::assertInstanceOf(MissingColumn::class, $foreign->facts->diagnostics[0]);
75 }
76
77 public function testDeriveStatementReportsAnAssignmentToAColumnTheTableLacks(): void
78 {
79 $semantics = new Semantics();
80 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
81 $query = $semantics->inspect('UPDATE t SET zz = 1', [$t]);
82
83 self::assertInstanceOf(MissingColumn::class, $query->facts->diagnostics[0]);
84 self::assertSame('zz', $query->facts->diagnostics[0]->name->value);
85 self::assertSame([], $semantics->inspect('UPDATE t SET zz = 1')->facts->diagnostics);
86 }
87
88 public function testDeriveWithinAnswersNullWithoutReturning(): void
89 {
90 $semantics = new Semantics();
91 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
92 $statement = $semantics->inspect('UPDATE t SET a = 1 WHERE b IS NULL', [$t])->statement;
93 $derivation = new Derivation($semantics->context([$t]));
94
95 self::assertInstanceOf(Update::class, $statement);
96 self::assertNull($statement->deriveWithin($derivation, $derivation->environment()));
97 self::assertNotNull($statement->where);
98 self::assertTrue($derivation->facts()->covers($statement->where));
99 }
100
101 public function testDeriveStatementReportsRaiseOutsideATrigger(): void
102 {
103 $query = (new Semantics())->inspect('UPDATE t SET a = RAISE(IGNORE)');
104
105 self::assertInstanceOf(Misuse::class, $query->facts->diagnostics[0]);
106 self::assertSame(MisuseRule::RaiseOutsideTrigger, $query->facts->diagnostics[0]->rule);
107 }
108
109 public function testRenderWritesEveryClauseInGrammarOrder(): void
110 {
111 $query = (new Semantics())->inspect("with w as (select 1 as q) update or fail main.t indexed by i set a = 1, (b, a) = ('x', 2) from u, w where u.a = t.a returning id");
112
113 self::assertSame("WITH w AS (SELECT 1 AS q) UPDATE OR FAIL main.t INDEXED BY i SET a = 1, (b, a) = ('x', 2) FROM u, w WHERE u.a = t.a RETURNING id", $query->toString());
114 }
115
116 public function testRejectsAnUpdateWithoutAssignments(): void
117 {
118 $this->expectExceptionMessage('An UPDATE has at least one assignment of a column or of a column row.');
119
120 new Update(new MutationTarget(new QualifiedName(new Name('t'))), []);
121 }
122}
123