packages/sql-semantics-sqlite/tests/Unit/Statement/Query/With/WithQueryTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query\With;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\CommonTable as CommonTableReference;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Choice;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\Sqlite\Semantics;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\Binary;
20use SqlSemantics\Platform\Sqlite\Statement\Query\Compound;
21use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\MisuseRule;
22use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
23use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
24use SqlSemantics\Platform\Sqlite\Statement\Query\Star;
25use SqlSemantics\Platform\Sqlite\Statement\Query\ValuesClause;
26use SqlSemantics\Platform\Sqlite\Statement\Query\With\CommonTable;
27use SqlSemantics\Platform\Sqlite\Statement\Query\With\WithClause;
28use SqlSemantics\Platform\Sqlite\Statement\Query\With\WithQuery;
29use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
30use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
31
32#[CoversClass(WithQuery::class)]
33#[Medium]
34final class WithQueryTest extends TestCase
35{
36 public function testDeriveStatementRecordsTheOutputOfTheBody(): void
37 {
38 $query = (new Semantics())->inspect('WITH c AS (SELECT 1 AS x) SELECT x FROM c', []);
39
40 self::assertInstanceOf(WithQuery::class, $query->statement);
41 self::assertInstanceOf(Select::class, $query->statement->body);
42 self::assertSame('x', $query->field(0)->name?->value);
43 self::assertInstanceOf(Known::class, $query->field('x')->type);
44 self::assertSame(Storage::Integer, $query->field('x')->type->descriptor);
45 $input = $query->statement->body->from;
46 self::assertNotNull($input);
47 $reference = $query->facts->relation($input)->table;
48 self::assertInstanceOf(CommonTableReference::class, $reference);
49 self::assertSame($query->statement->with->tables[0], $reference->definition);
50 self::assertSame([], $query->facts->diagnostics);
51 }
52
53 public function testDeriveQueryMakesTheCommonTablesVisibleInNestedQueries(): void
54 {
55 $query = (new Semantics())->inspect('WITH c AS (SELECT 1 AS x) SELECT (SELECT x FROM c) AS y FROM (SELECT x FROM c)', []);
56
57 self::assertInstanceOf(Known::class, $query->field('y')->type);
58 self::assertSame(Nullability::Nullable, $query->field('y')->nullability);
59 self::assertSame([], $query->facts->diagnostics);
60 }
61
62 public function testDeriveQueryTypesARecursiveColumnAsAnyStorageClassInTheRecursiveArm(): void
63 {
64 $query = (new Semantics())->inspect('WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c WHERE x < 5) SELECT x FROM c', []);
65
66 self::assertInstanceOf(WithQuery::class, $query->statement);
67 $compound = $query->statement->with->tables[0]->query;
68 self::assertInstanceOf(Compound::class, $compound);
69 $recursive = $compound->steps[0]->query;
70 self::assertInstanceOf(Select::class, $recursive);
71 self::assertInstanceOf(ResultColumn::class, $recursive->columns[0]);
72 self::assertInstanceOf(Binary::class, $recursive->columns[0]->expression);
73 $reference = $query->facts->scalar($recursive->columns[0]->expression->left);
74 self::assertInstanceOf(Choice::class, $reference->type);
75 self::assertSame(Storage::cases(), $reference->type->alternatives);
76 self::assertSame(Nullability::Nullable, $reference->nullability);
77 self::assertInstanceOf(Choice::class, $query->field('x')->type);
78 self::assertSame([Storage::Integer, Storage::Real], $query->field('x')->type->alternatives);
79 }
80
81 public function testDeriveStatementReportsRaiseOutsideATrigger(): void
82 {
83 $query = (new Semantics())->inspect('WITH c AS (SELECT 1) SELECT RAISE(IGNORE)');
84
85 self::assertSame(MisuseRule::RaiseOutsideTrigger->value, $query->facts->diagnostics[0]->message());
86 }
87
88 public function testRenderWritesTheClauseAndTheBody(): void
89 {
90 $semantics = new Semantics();
91 $compound = $semantics->inspect('with recursive c(n) as (select 1 union all select n + 1 from c) select n from c limit 3');
92 $values = $semantics->inspect('with c as (select 1) values (2)');
93
94 self::assertSame('WITH RECURSIVE c (n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM c) SELECT n FROM c LIMIT 3', $compound->toString());
95 self::assertInstanceOf(WithQuery::class, $values->statement);
96 self::assertInstanceOf(ValuesClause::class, $values->statement->body);
97 self::assertSame('WITH c AS (SELECT 1) VALUES (2)', $values->toString());
98 }
99
100 public function testRenderWritesANewlyBuiltQuery(): void
101 {
102 $semantics = new Semantics();
103 $with = new WithClause([new CommonTable(new Name('c'), new Select([new ResultColumn(new IntegerLiteral('1'), new Name('x'))]))]);
104 $operation = new Operation($semantics->context([]), new WithQuery($with, new Select([new Star()], new TableInput(new QualifiedName(new Name('c'))))));
105
106 self::assertSame('WITH c AS (SELECT 1 AS x) SELECT * FROM c', $operation->toString());
107 self::assertSame('x', $operation->field(0)->name?->value);
108 }
109}
110