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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Dml\Load;
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\Known;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Semantics;
18use SqlSemantics\Platform\MySql\Statement\Dml\Load\LoadFormat;
19use SqlSemantics\Platform\MySql\Statement\Dml\Load\LoadInput;
20use SqlSemantics\Platform\MySql\Statement\Dml\Load\LoadSource;
21use SqlSemantics\Platform\MySql\Statement\Dml\Load\LoadTable;
22use SqlSemantics\Platform\MySql\Statement\Dml\WriteTarget;
23use SqlSemantics\Platform\MySql\Statement\Literal\Text;
24use SqlSemantics\Platform\MySql\Statement\Type\Integral;
25use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
26
27#[CoversClass(LoadTable::class)]
28#[Medium]
29final class LoadTableTest extends TestCase
30{
31    public function testDeriveStatementResolvesColumnsAndAssignments(): 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        $operation = $semantics->analyze("LOAD DATA INFILE 'f' INTO TABLE t (a, @v) SET b = @v + a, a = DEFAULT", [$t]);
36        self::assertInstanceOf(LoadTable::class, $operation->statement);
37
38        self::assertInstanceOf(ResolvedColumn::class, $operation->facts->scalar($operation->statement->assignments[0]->column)->resolution);
39        self::assertEquals(new Known(new Integral(IntegralKind::Int)), $operation->facts->scalar($operation->statement->assignments[1]->value)->type);
40        self::assertSame([], $operation->facts->diagnostics);
41    }
42
43    public function testRenderWritesEveryClause(): void
44    {
45        self::assertSame("LOAD XML CONCURRENT INFILE 'f' REPLACE INTO TABLE t PARTITION (p) CHARSET utf8mb4 ROWS IDENTIFIED BY '<r>' COLUMNS TERMINATED BY ',' LINES TERMINATED BY ';' IGNORE 1 LINES (a, @b) SET c = @b", (new Semantics('mysql-5.6.51'))->analyze("load xml concurrent infile 'f' replace into table t partition (p) charset utf8mb4 rows identified by '<r>' fields terminated by ',' lines terminated by ';' ignore 1 rows (a, @b) set c = @b")->toString());
46        self::assertSame("LOAD DATA INFILE 'f' INTO TABLE t CHARSET latin1 COMPRESSION = 'zstd'", (new Semantics('mysql-9.1.0'))->analyze("load data infile 'f' into table t character set latin1 compression = 'zstd' ()")->toString());
47    }
48
49    public function testRenderRejectsACorrelationName(): void
50    {
51        $this->expectExceptionMessage('The table of LOAD has no correlation name.');
52
53        new LoadTable(LoadFormat::Data, null, new LoadInput(false, LoadSource::Infile, new Text('f')), null, new WriteTarget(new QualifiedName(new Name('t')), new Name('x')));
54    }
55}
56