packages/sql-semantics-postgres/tests/Unit/Lowering/Manipulation/ManipulationsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Manipulation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Lowering\Manipulation\Manipulations::class)]
12#[Medium]
13final class ManipulationsTest extends TestCase
14{
15    public function testStatementRoutesEveryStatementOfTheFamily(): void
16    {
17        $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
18        $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text)]);
19        $u = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int8, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]);
20        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
21        self::assertSame([\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\InsertRows::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Update::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Delete::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Merge::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Copy\CopyTable::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Prepared\Prepare::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Prepared\Execute::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Prepared\Deallocate::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\DeclareCursor::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\Fetch::class, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\Close::class], array_map(static fn (string $sql): string => $semantics->analyze($sql)->statement::class, ['INSERT INTO t VALUES (1)', 'UPDATE t SET a = 1', 'DELETE FROM t', 'MERGE INTO t USING u ON true WHEN MATCHED THEN DELETE', 'COPY t FROM STDIN', 'PREPARE p AS SELECT 1', 'EXECUTE p', 'DEALLOCATE p', 'DECLARE c CURSOR FOR SELECT 1', 'FETCH c', 'CLOSE c']));
22    }
23
24    public function testPreparableForwardsAQueryToTheQueryFamily(): void
25    {
26        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
27        $query = $semantics->analyze('WITH x AS (SELECT 1 AS k) SELECT k FROM x');
28        self::assertSame('k', $query->field(0)->name?->value);
29    }
30
31    public function testPreparableLowersADataModifyingStatement(): void
32    {
33        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
34        $query = $semantics->analyze('PREPARE p AS MERGE INTO t USING u ON true WHEN MATCHED THEN DELETE');
35        $statement = $query->statement;
36        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Prepared\Prepare::class, $statement);
37        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Merge::class, $statement->statement);
38    }
39}
40