packages/sql-semantics-mysql/tests/Unit/Rules/Dml/ChangeFactsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\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\Reference\Column\MissingColumn;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Rules\Dml\ChangeFacts;
18use SqlSemantics\Platform\MySql\Semantics;
19use SqlSemantics\Platform\MySql\Statement\Dml\Delete;
20use SqlSemantics\Platform\MySql\Statement\Dml\Problem\UnknownDeleteTable;
21use SqlSemantics\Platform\MySql\Statement\Dml\Problem\WriteMisuse;
22use SqlSemantics\Platform\MySql\Statement\Dml\Problem\WriteRule;
23use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
24use SqlSemantics\Platform\MySql\Statement\Type\Integral;
25use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
26
27#[CoversClass(ChangeFacts::class)]
28#[Medium]
29final class ChangeFactsTest extends TestCase
30{
31    public function testUpdateSeesEveryTable(): void
32    {
33        $semantics = new Semantics();
34        $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))]);
35        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [new Column(new Name('x'), new Integral(IntegralKind::Int)), new Column(new Name('y'), new Integral(IntegralKind::Int))]);
36        $operation = $semantics->inspect('UPDATE t, u SET t.b = u.y WHERE a = x', [$t, $u]);
37
38        self::assertSame([], $operation->facts->diagnostics);
39    }
40
41    public function testDeleteSeesTheTable(): void
42    {
43        $semantics = new Semantics();
44        $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))]);
45        $operation = $semantics->inspect('DELETE FROM t WHERE z = 1', [$t]);
46
47        self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[0]);
48    }
49
50    public function testDeleteMultipleChecksTheTargets(): void
51    {
52        $semantics = new Semantics();
53        $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))]);
54        $operation = $semantics->inspect('DELETE t FROM t AS x', [$t]);
55
56        self::assertInstanceOf(UnknownDeleteTable::class, $operation->facts->diagnostics[0]);
57    }
58
59    public function testUpdatableRefusesDerivedAndCommonTables(): void
60    {
61        $semantics = new Semantics();
62        $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))]);
63        $operation = $semantics->inspect('WITH c AS (SELECT 1 AS k) UPDATE t, c, (SELECT 2 AS m) AS d SET c.k = 1, d.m = 2, t.a = 3', [$t]);
64
65        self::assertSame([WriteRule::NonUpdatableTarget, WriteRule::NonUpdatableTarget], array_map(static fn ($diagnostic) => $diagnostic instanceof WriteMisuse ? $diagnostic->rule : null, $operation->facts->diagnostics));
66    }
67
68    public function testBaseBindsTheCommonTables(): void
69    {
70        $operation = (new Semantics())->inspect('WITH c AS (SELECT 1 AS k) DELETE t FROM t JOIN c ON t.a = c.k');
71
72        self::assertSame([], $operation->facts->diagnostics);
73    }
74
75    public function testReferencesLetLaterTablesSeeEarlierOnes(): void
76    {
77        $semantics = new Semantics();
78        $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))]);
79        $operation = $semantics->inspect('UPDATE t, LATERAL (SELECT t.a AS k) AS d SET t.b = d.k', [$t]);
80
81        self::assertSame([], $operation->facts->diagnostics);
82    }
83
84    public function testClausesDeriveLimitWithoutTables(): void
85    {
86        $semantics = new Semantics();
87        $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))]);
88        $operation = $semantics->inspect('DELETE FROM t ORDER BY b LIMIT ?', [$t]);
89        self::assertInstanceOf(Delete::class, $operation->statement);
90        self::assertInstanceOf(RowLimit::class, $operation->statement->limit);
91
92        self::assertInstanceOf(Dependent::class, $operation->facts->scalar($operation->statement->limit->count)->type);
93    }
94}
95