packages/sql-semantics-mysql/tests/Unit/Lowering/Dml/LoadRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Dml;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Dml\LoadRule;
11use SqlSemantics\Platform\MySql\Semantics;
12
13#[CoversClass(LoadRule::class)]
14#[Medium]
15final class LoadRuleTest extends TestCase
16{
17 public function testStatementLowersEveryGeneration(): void
18 {
19 self::assertSame('LOAD DATA INFILE \'f\' INTO TABLE t', (new Semantics('mysql-5.6.51'))->analyze('load data infile \'f\' into table t')->toString());
20 self::assertSame('LOAD DATA S3 \'f\' INTO TABLE t', (new Semantics('mysql-9.1.0'))->analyze('load data from s3 \'f\' into table t')->toString());
21 }
22
23 public function testStatementRejectsAnotherWordThanCount(): void
24 {
25 $this->expectExceptionMessage('Syntax error: COUNT expected before the number of files.');
26
27 (new Semantics())->analyze("LOAD DATA INFILE 'f' many 2 INTO TABLE t");
28 }
29
30 public function testFormatLowersDataAndXml(): void
31 {
32 self::assertSame('LOAD XML INFILE \'f\' INTO TABLE t', (new Semantics())->analyze('load xml infile \'f\' into table t')->toString());
33 }
34
35 public function testLockLowersTheModifiers(): void
36 {
37 self::assertSame('LOAD DATA LOW_PRIORITY INFILE \'f\' INTO TABLE t', (new Semantics())->analyze('load data low_priority infile \'f\' into table t')->toString());
38 self::assertSame('LOAD DATA CONCURRENT INFILE \'f\' INTO TABLE t', (new Semantics())->analyze('load data concurrent infile \'f\' into table t')->toString());
39 }
40
41 public function testSourceLowersTheSourceKinds(): void
42 {
43 self::assertSame('LOAD DATA URL \'u\' INTO TABLE t', (new Semantics('mysql-8.2.0'))->analyze('load data url \'u\' into table t')->toString());
44 }
45
46 public function testFlagLowersTheOptionalWords(): void
47 {
48 self::assertSame('LOAD DATA LOCAL INFILE \'f\' INTO TABLE t ALGORITHM = BULK', (new Semantics())->analyze('load data from local infile \'f\' into table t algorithm = bulk')->toString());
49 }
50
51 public function testInputLowersTheSource(): void
52 {
53 self::assertSame('LOAD DATA LOCAL URL \'u\' `count` 3 INTO TABLE t', (new Semantics('mysql-8.2.0'))->analyze('load data local url \'u\' count 3 into table t')->toString());
54 }
55
56 public function testTaggedLowersTheRowTag(): void
57 {
58 self::assertSame('LOAD XML INFILE \'f\' INTO TABLE t ROWS IDENTIFIED BY \'<r>\'', (new Semantics())->analyze('load xml infile \'f\' into table t rows identified by \'<r>\'')->toString());
59 }
60
61 public function testIgnoredLowersTheCount(): void
62 {
63 self::assertSame('LOAD DATA INFILE \'f\' INTO TABLE t IGNORE 3 LINES', (new Semantics())->analyze('load data infile \'f\' into table t ignore 3 rows')->toString());
64 }
65
66 public function testColumnsLowersColumnsAndVariables(): void
67 {
68 self::assertSame('LOAD DATA INFILE \'f\' INTO TABLE t (a, @b, t.c)', (new Semantics())->analyze('load data infile \'f\' into table t (a, @b, t.c)')->toString());
69 self::assertSame('LOAD DATA INFILE \'f\' INTO TABLE t', (new Semantics())->analyze('load data infile \'f\' into table t ()')->toString());
70 }
71
72 public function testAssignmentsLowersBothGenerations(): void
73 {
74 self::assertSame('LOAD DATA INFILE \'f\' INTO TABLE t SET a = 1', (new Semantics('mysql-5.7.44'))->analyze('load data infile \'f\' into table t set a = 1')->toString());
75 self::assertSame('LOAD DATA INFILE \'f\' INTO TABLE t SET a = DEFAULT', (new Semantics())->analyze('load data infile \'f\' into table t set a = default')->toString());
76 }
77
78 public function testBulkLowersTheOptions(): void
79 {
80 self::assertSame('LOAD DATA INFILE \'f\' INTO TABLE t PARALLEL = 2 MEMORY = 1024', (new Semantics('mysql-8.3.0'))->analyze('load data infile \'f\' into table t parallel = 2 memory = 1024')->toString());
81 }
82}
83