packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/CreateTableAsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema;
6
7use PDO;
8use PDOStatement;
9use PHPUnit\Framework\Attributes\CoversClass;
10use PHPUnit\Framework\Attributes\Medium;
11use PHPUnit\Framework\Attributes\TestWith;
12use PHPUnit\Framework\TestCase;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Column;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\Sqlite\Semantics;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTableAs;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\QualifiedTemporaryName;
18
19#[CoversClass(CreateTableAs::class)]
20#[Medium]
21final class CreateTableAsTest extends TestCase
22{
23    public function testDeriveStatementDeclaresTheColumnsOfTheQueryOutput(): void
24    {
25        $semantics = new Semantics();
26        $source = $semantics->inspect('CREATE TABLE s (a INTEGER NOT NULL, b TEXT)');
27        $operation = $semantics->inspect('CREATE TABLE t AS SELECT b, a AS n FROM s', [$source]);
28        $table = $operation->declarations()[0];
29
30        self::assertTrue($table->complete);
31        self::assertSame(['b', 'n'], array_map(static fn (object $column): string => $column->name->value, $table->columns));
32        self::assertSame(['TEXT', 'INT'], array_map(static fn (object $column): string => $column->type->name(), $table->columns));
33        self::assertSame(Nullability::Nullable, $table->columns[1]->nullability);
34        self::assertNull($operation->shape());
35    }
36
37    public function testDeriveStatementDeclaresARowIdentifierAndNewColumnObjects(): void
38    {
39        $semantics = new Semantics();
40        $source = $semantics->inspect('CREATE TABLE s (a INTEGER)');
41        $table = $semantics->inspect('CREATE TABLE t AS SELECT a FROM s', [$source])->declarations()[0];
42
43        self::assertCount(1, $table->implicit);
44        self::assertNotSame($source->declarations()[0]->columns[0], $table->columns[0]);
45    }
46
47    public function testDeriveStatementMarksTheDeclarationIncompleteWhenTheQueryShapeIsOpen(): void
48    {
49        $operation = (new Semantics())->inspect('CREATE TABLE t AS SELECT 1 AS one, * FROM undeclared');
50        $table = $operation->declarations()[0];
51
52        self::assertFalse($table->complete);
53        self::assertSame(['one'], array_map(static fn (object $column): string => $column->name->value, $table->columns));
54    }
55
56    public function testDeriveStatementDeclaresATemporaryTableInTheTempSchema(): void
57    {
58        $semantics = new Semantics();
59        $operation = $semantics->inspect('CREATE TEMP TABLE main.t AS SELECT 1 AS one');
60
61        self::assertSame('temp', $operation->declarations()[0]->name->schema?->value);
62        self::assertInstanceOf(QualifiedTemporaryName::class, $operation->facts->diagnostics[0]);
63    }
64
65    #[TestWith(["CREATE TABLE v AS SELECT a+1, \"zz\", A COLLATE nocase, likely(b), true, 1 AS false, x'0aff' FROM t"])]
66    #[TestWith(['CREATE TABLE v AS SELECT 1 + /* c */ 1 UNION SELECT 2'])]
67    #[TestWith(["CREATE TABLE v AS SELECT a+1 /* c */ , b -- d\n FROM t"])]
68    #[TestWith(['CREATE TABLE v AS SELECT * FROM (SELECT a, a, A FROM t)'])]
69    public function testDeriveStatementNamesTheColumnsAsSqliteDoes(string $sql): void
70    {
71        $semantics = new Semantics();
72        $view = $semantics->inspect($sql, [$semantics->inspect('CREATE TABLE t (a INTEGER, b TEXT)')]);
73        $source = new PDO('sqlite::memory:');
74        $rendered = new PDO('sqlite::memory:');
75        $source->exec('CREATE TABLE t (a INTEGER, b TEXT); ' . $sql);
76        $rendered->exec('CREATE TABLE t (a INTEGER, b TEXT); ' . $view->toString());
77        $original = $source->query('SELECT * FROM v');
78        $rebuilt = $rendered->query('SELECT * FROM v');
79        self::assertInstanceOf(PDOStatement::class, $original);
80        self::assertInstanceOf(PDOStatement::class, $rebuilt);
81        $names = array_map(static fn (int $position): mixed => $original->getColumnMeta($position) === false ? null : $original->getColumnMeta($position)['name'], range(0, $original->columnCount() - 1));
82
83        self::assertSame($names, array_map(static fn (int $position): mixed => $rebuilt->getColumnMeta($position) === false ? null : $rebuilt->getColumnMeta($position)['name'], range(0, $rebuilt->columnCount() - 1)));
84        self::assertSame($names, array_map(static fn (Column $column): string => $column->name->value, $view->declarations()[0]->columns));
85    }
86
87    public function testRenderWritesTheQueryAfterAs(): void
88    {
89        $operation = (new Semantics())->inspect('create temp table if not exists t as select 1 as one');
90
91        self::assertInstanceOf(CreateTableAs::class, $operation->statement);
92        self::assertSame('CREATE TEMP TABLE IF NOT EXISTS t AS SELECT 1 AS one', $operation->toString());
93    }
94}
95