packages/sql-semantics-mysql/tests/Unit/Statement/Dml/WriteTargetTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Dml;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Dml\Delete;
17use SqlSemantics\Platform\MySql\Statement\Dml\WriteTarget;
18use SqlSemantics\Platform\MySql\Statement\Type\Integral;
19use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
20
21#[CoversClass(WriteTarget::class)]
22#[Medium]
23final class WriteTargetTest extends TestCase
24{
25    public function testNameAnswersTheTableName(): void
26    {
27        $target = new WriteTarget(new QualifiedName(new Name('t'), new Name('shop')));
28
29        self::assertSame('shop', $target->name()->schema?->value);
30    }
31
32    public function testAliasAnswersTheCorrelationName(): void
33    {
34        $target = new WriteTarget(new QualifiedName(new Name('t')), new Name('x'));
35
36        self::assertSame('x', $target->alias()?->value);
37    }
38
39    public function testDeriveRelationResolvesTheTable(): void
40    {
41        $semantics = new Semantics();
42        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull), new Column(new Name('b'), new Integral(IntegralKind::BigInt))]);
43        $operation = $semantics->inspect('DELETE FROM t WHERE a = 1', [$t]);
44        self::assertInstanceOf(Delete::class, $operation->statement);
45        $fact = $operation->facts->relation($operation->statement->table);
46
47        self::assertCount(2, $fact->shape->slots);
48        self::assertSame([], $operation->facts->diagnostics);
49    }
50
51    public function testDeriveRelationReportsACommonTable(): void
52    {
53        $operation = (new Semantics())->inspect('WITH c AS (SELECT 1) DELETE FROM c');
54
55        self::assertCount(1, $operation->facts->diagnostics);
56        self::assertSame('The target table is a common table expression, which is not updatable', $operation->facts->diagnostics[0]->message());
57    }
58
59    public function testRenderWritesNameAliasAndPartitions(): void
60    {
61        self::assertSame('DELETE FROM shop.t x PARTITION (p0, p1) WHERE x.a = 1', (new Semantics())->inspect('delete from shop.t x partition (p0, p1) where x.a = 1')->toString());
62    }
63
64    public function testRenderRejectsACatalog(): void
65    {
66        $this->expectExceptionMessage('A table is qualified by at most a database.');
67
68        new WriteTarget(new QualifiedName(new Name('t'), new Name('s'), new Name('c')));
69    }
70}
71