packages/sql-semantics-mysql/tests/Unit/Statement/Dml/UpdateTest.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\Reference\Column\ResolvedColumn;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Platform\MySql\Statement\Dml\Update;
18use SqlSemantics\Platform\MySql\Statement\Dml\WriteTarget;
19use SqlSemantics\Platform\MySql\Statement\Type\Integral;
20use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
21
22#[CoversClass(Update::class)]
23#[Medium]
24final class UpdateTest extends TestCase
25{
26    public function testDeriveStatementResolvesAssignmentsAgainstEveryTable(): void
27    {
28        $semantics = new Semantics();
29        $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))]);
30        $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))]);
31        $operation = $semantics->inspect('UPDATE t JOIN u ON t.a = u.x SET b = y WHERE a > 0', [$t, $u]);
32        self::assertInstanceOf(Update::class, $operation->statement);
33        $column = $operation->facts->scalar($operation->statement->assignments[0]->column);
34        $value = $operation->facts->scalar($operation->statement->assignments[0]->value);
35
36        self::assertInstanceOf(ResolvedColumn::class, $column->resolution);
37        self::assertSame('b', $column->resolution->slot->name?->value);
38        self::assertInstanceOf(ResolvedColumn::class, $value->resolution);
39        self::assertSame([], $operation->facts->diagnostics);
40        self::assertNull($operation->facts->output);
41    }
42
43    public function testDeriveStatementReportsOrderAndLimitOfAMultipleTableUpdate(): void
44    {
45        $semantics = new Semantics();
46        $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))]);
47        $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))]);
48        $operation = $semantics->inspect('UPDATE t, u SET a = 1 ORDER BY a LIMIT 1', [$t, $u]);
49
50        self::assertSame(['Incorrect usage of UPDATE and ORDER BY', 'Incorrect usage of UPDATE and LIMIT'], array_map(static fn ($diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
51    }
52
53    public function testRenderWritesEveryClause(): void
54    {
55        self::assertSame('WITH c AS (SELECT 1 AS x) UPDATE LOW_PRIORITY IGNORE t SET a = a + 1 WHERE b IN (SELECT x FROM c) ORDER BY a DESC LIMIT 3', (new Semantics())->inspect('with c as (select 1 as x) update low_priority ignore t set a = a + 1 where b in (select x from c) order by a desc limit 3')->toString());
56        self::assertSame('UPDATE t SET a = 1 LIMIT 2', (new Semantics('mysql-5.6.51'))->inspect('update t set a = 1 limit 2')->toString());
57    }
58
59    public function testRenderRejectsAnUpdateWithoutAssignment(): void
60    {
61        $this->expectExceptionMessage('UPDATE holds at least one assignment.');
62
63        new Update(null, false, false, [new WriteTarget(new QualifiedName(new Name('t')))], []);
64    }
65}
66