packages/sql-semantics-sqlite/tests/Unit/Statement/Mutation/InsertSelectTest.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\Shape\Field;
14use SqlSemantics\Platform\Sqlite\Semantics;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
16use SqlSemantics\Platform\Sqlite\Statement\Mutation\ConflictTarget;
17use SqlSemantics\Platform\Sqlite\Statement\Mutation\InsertInto;
18use SqlSemantics\Platform\Sqlite\Statement\Mutation\InsertSelect;
19use SqlSemantics\Platform\Sqlite\Statement\Mutation\MutationTarget;
20use SqlSemantics\Platform\Sqlite\Statement\Mutation\Upsert;
21use SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\SortTerm;
22use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\ArityMismatch;
23use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\ArityRule;
24use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
25use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
26use SqlSemantics\Platform\Sqlite\Statement\Query\ValueRow;
27use SqlSemantics\Platform\Sqlite\Statement\Query\ValuesClause;
28
29#[CoversClass(InsertSelect::class)]
30#[Medium]
31final class InsertSelectTest extends TestCase
32{
33 public function testDeriveStatementComparesTheQueryWidthWithTheColumns(): void
34 {
35 $semantics = new Semantics();
36 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
37 $u = $semantics->inspect('CREATE TABLE u (a INTEGER NOT NULL, c TEXT)');
38 $narrow = $semantics->inspect('INSERT INTO t SELECT a, c FROM u', [$t, $u]);
39 $wide = $semantics->inspect('INSERT INTO t (a) SELECT * FROM u', [$t, $u]);
40 $exact = $semantics->inspect('INSERT INTO t (a, b) SELECT a, c FROM u', [$t, $u]);
41 $open = $semantics->inspect('INSERT INTO t SELECT * FROM u');
42
43 self::assertInstanceOf(ArityMismatch::class, $narrow->facts->diagnostics[0]);
44 self::assertSame(ArityRule::InsertedValues, $narrow->facts->diagnostics[0]->rule);
45 self::assertSame('2 values for 3 columns.', $narrow->facts->diagnostics[0]->message());
46 self::assertInstanceOf(ArityMismatch::class, $wide->facts->diagnostics[0]);
47 self::assertSame('2 values for 1 columns.', $wide->facts->diagnostics[0]->message());
48 self::assertSame([], $exact->facts->diagnostics);
49 self::assertSame([], $open->facts->diagnostics);
50 }
51
52 public function testDeriveWithinBindsTheWithClauseForTheQueryAndReturnsTheTargetRow(): void
53 {
54 $semantics = new Semantics();
55 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
56 $query = $semantics->inspect('WITH w AS (SELECT 1 AS q) INSERT INTO t (a) SELECT q FROM w RETURNING id, b', [$t]);
57 $fields = $query->fields();
58
59 self::assertInstanceOf(InsertSelect::class, $query->statement);
60 self::assertInstanceOf(Select::class, $query->statement->query);
61 self::assertCount(1, $query->facts->query($query->statement->query)->shape->slots);
62 self::assertNotNull($fields);
63 self::assertSame(['id', 'b'], array_map(static fn (Field $field): ?string => $field->name?->value, $fields->items));
64 self::assertSame($t->declarations()[0]->columns[2], $query->field('b')->column());
65 self::assertSame([], $query->facts->diagnostics);
66 }
67
68 public function testDeriveWithinAnswersNullWithoutReturning(): void
69 {
70 $semantics = new Semantics();
71 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
72 $statement = $semantics->inspect('INSERT INTO t (a) SELECT 1', [$t])->statement;
73 $derivation = new Derivation($semantics->context([$t]));
74
75 self::assertInstanceOf(InsertSelect::class, $statement);
76 self::assertNull($statement->deriveWithin($derivation, $derivation->environment()));
77 self::assertTrue($derivation->facts()->covers($statement->query));
78 }
79
80 public function testRenderWritesIntoQueryUpsertsAndReturning(): void
81 {
82 $query = (new Semantics())->inspect('insert into t (id) select 1 union select 2 on conflict (id) do nothing returning *');
83
84 self::assertSame('INSERT INTO t (id) SELECT 1 UNION SELECT 2 ON CONFLICT (id) DO NOTHING RETURNING *', $query->toString());
85 self::assertInstanceOf(InsertSelect::class, $query->statement);
86 self::assertCount(1, $query->statement->upserts);
87 }
88
89 public function testRejectsABareValuesClause(): void
90 {
91 $this->expectExceptionMessage('An INSERT of a bare VALUES clause is an insert of written rows.');
92
93 new InsertSelect(new InsertInto(new MutationTarget(new QualifiedName(new Name('t')))), new ValuesClause([new ValueRow([new IntegerLiteral('1')])]));
94 }
95
96 public function testRejectsANonLastUpsertWithoutAConflictTarget(): void
97 {
98 $into = new InsertInto(new MutationTarget(new QualifiedName(new Name('t'))));
99 $query = new Select([new ResultColumn(new IntegerLiteral('1'))]);
100 $target = new ConflictTarget([new SortTerm(new IntegerLiteral('1'))]);
101
102 $this->expectExceptionMessage('Only the last ON CONFLICT clause may omit the conflict target.');
103
104 new InsertSelect($into, $query, [new Upsert(), new Upsert($target)]);
105 }
106}
107