packages/sql-semantics-mysql/tests/Unit/Statement/Dml/Insert/InsertRowsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Dml\Insert;
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\Insert\InsertedRow;
17use SqlSemantics\Platform\MySql\Statement\Dml\Insert\InsertInto;
18use SqlSemantics\Platform\MySql\Statement\Dml\Insert\InsertRows;
19use SqlSemantics\Platform\MySql\Statement\Dml\Insert\RowAlias;
20use SqlSemantics\Platform\MySql\Statement\Dml\WriteTarget;
21use SqlSemantics\Platform\MySql\Statement\Type\Integral;
22use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
23
24#[CoversClass(InsertRows::class)]
25#[Medium]
26final class InsertRowsTest extends TestCase
27{
28 public function testDeriveStatementChecksTheRows(): void
29 {
30 $semantics = new Semantics();
31 $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))]);
32 $operation = $semantics->inspect('INSERT INTO t (a, b, a) VALUES (1, 2, 3), (4)', [$t]);
33
34 self::assertSame(["Column 'a' specified twice", "Column count doesn't match value count at row 2"], array_map(static fn ($diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
35 }
36
37 public function testDeriveStatementAcceptsAnEmptyRowWithoutColumns(): void
38 {
39 $semantics = new Semantics();
40 $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))]);
41 $operation = $semantics->inspect('INSERT INTO t VALUES (), ()', [$t]);
42
43 self::assertSame([], $operation->facts->diagnostics);
44 self::assertNull($operation->facts->output);
45 }
46
47 public function testRenderWritesValuesAndUpdates(): void
48 {
49 self::assertSame('INSERT INTO t VALUES (1), (2) ON DUPLICATE KEY UPDATE a = VALUES(a)', (new Semantics('mysql-5.7.44'))->inspect('insert into t value (1), (2) on duplicate key update a = values(a)')->toString());
50 }
51
52 public function testRenderRejectsReplaceWithUpdates(): void
53 {
54 $this->expectExceptionMessage('REPLACE has neither a row alias nor ON DUPLICATE KEY UPDATE.');
55
56 new InsertRows(new InsertInto(true, null, false, new WriteTarget(new QualifiedName(new Name('t')))), [new InsertedRow([])], new RowAlias(new Name('n')));
57 }
58}
59