packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/ColumnUseTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
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\Column\AliasTarget;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ConditionalColumn;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\UndeclaredRelation;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Dependent;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Invalid;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
21use SqlSemantics\Platform\Sqlite\Semantics;
22use SqlSemantics\Platform\Sqlite\Statement\Expression\ColumnUse;
23use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
24use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
25
26#[CoversClass(ColumnUse::class)]
27#[Medium]
28final class ColumnUseTest extends TestCase
29{
30 public function testDeriveScalarResolvesADeclaredColumnWithItsFacts(): void
31 {
32 $semantics = new Semantics();
33 $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
34 $operation = $semantics->inspect('SELECT a, t.b, main.t.a FROM t', [$create]);
35 $statement = $operation->statement;
36
37 self::assertInstanceOf(Select::class, $statement);
38 self::assertInstanceOf(ResultColumn::class, $statement->columns[1]);
39 self::assertInstanceOf(ColumnUse::class, $statement->columns[1]->expression);
40 $use = $statement->columns[1]->expression;
41 self::assertSame('b', $use->name->value);
42 self::assertSame('t', $use->qualifier?->name->value);
43 self::assertNull($use->qualifier->schema);
44 $fact = $operation->facts->scalar($use);
45 self::assertInstanceOf(ResolvedColumn::class, $fact->resolution);
46 self::assertSame($create->declarations()[0]->columns[1], $fact->resolution->slot->column);
47 self::assertSame(Nullability::Nullable, $fact->nullability);
48 self::assertSame(Nullability::NotNull, $operation->field(0)->nullability);
49 self::assertSame($create->declarations()[0]->columns[0], $operation->field(2)->column());
50 self::assertSame([], $operation->facts->diagnostics);
51 }
52
53 public function testDeriveScalarReportsAMissingColumn(): void
54 {
55 $semantics = new Semantics();
56 $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
57 $operation = $semantics->inspect('SELECT nope, t.nope FROM t', [$create]);
58 $statement = $operation->statement;
59
60 self::assertInstanceOf(Select::class, $statement);
61 self::assertInstanceOf(ResultColumn::class, $statement->columns[1]);
62 $fact = $operation->facts->scalar($statement->columns[1]->expression);
63 self::assertInstanceOf(Invalid::class, $fact->type);
64 self::assertInstanceOf(MissingColumn::class, $fact->resolution);
65 self::assertSame($fact->resolution, $fact->type->cause);
66 self::assertSame(Nullability::Dependent, $fact->nullability);
67 self::assertSame(['Column nope does not exist.', 'Column t.nope does not exist.'], array_map(static fn (object $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
68 }
69
70 public function testDeriveScalarDependsOnAnUndeclaredRelation(): void
71 {
72 $operation = (new Semantics())->inspect('SELECT a, u.a FROM u');
73 $statement = $operation->statement;
74
75 self::assertInstanceOf(Select::class, $statement);
76 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
77 $fact = $operation->facts->scalar($statement->columns[0]->expression);
78 self::assertInstanceOf(ConditionalColumn::class, $fact->resolution);
79 self::assertInstanceOf(Dependent::class, $fact->type);
80 self::assertInstanceOf(UndeclaredRelation::class, $fact->type->missing[0]);
81 self::assertSame(Nullability::Dependent, $fact->nullability);
82 self::assertSame(Nullability::Dependent, $operation->field(1)->nullability);
83 }
84
85 public function testDeriveScalarResolvesAResultAliasInTheOrdering(): void
86 {
87 $semantics = new Semantics();
88 $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
89 $operation = $semantics->inspect('SELECT b AS c FROM t ORDER BY c', [$create]);
90 $statement = $operation->statement;
91
92 self::assertInstanceOf(Select::class, $statement);
93 $fact = $operation->facts->scalar($statement->orderBy[0]->expression);
94 self::assertInstanceOf(AliasTarget::class, $fact->resolution);
95 self::assertSame($operation->field(0), $fact->resolution->field);
96 self::assertSame(Nullability::Nullable, $fact->nullability);
97 }
98
99 public function testRenderWritesTheQualifierPartsAndTheName(): void
100 {
101 $operation = (new Semantics())->inspect('select a AS c1, t.b AS c2, main.t.a AS c3, [c] AS c4, `d` AS c5 from t');
102
103 self::assertSame('SELECT a AS c1, t.b AS c2, main.t.a AS c3, c AS c4, d AS c5 FROM t', $operation->toString());
104 }
105
106 public function testRenderQuotesANameThatNeedsIt(): void
107 {
108 $semantics = new Semantics();
109 $operation = new Operation($semantics->context(), new Select([new ResultColumn(new ColumnUse(new Name('select'), new QualifiedName(new Name('my table'), new Name('main'))))]));
110
111 self::assertSame('SELECT main.`my table`.`select`', $operation->toString());
112 }
113}
114