1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests\Parser;
use PhpMyAdmin\SqlParser\Components\WithKeyword;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Tests\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use stdClass;
class WithStatementTest extends TestCase
{
#[DataProvider('parseWith')]
public function testParse(string $test): void
{
$this->runParserTest($test);
}
/**
* @return string[][]
*/
public static function parseWith(): array
{
return [
['parser/parseWithStatement'],
['parser/parseWithStatement1'],
['parser/parseWithStatement2'],
['parser/parseWithStatement3'],
['parser/parseWithStatement4'],
['parser/parseWithStatement5'],
['parser/parseWithStatement6'],
['parser/parseWithStatement7'],
['parser/parseWithStatementErr'],
['parser/parseWithStatementErr1'],
['parser/parseWithStatementErr2'],
['parser/parseWithStatementErr3'],
['parser/parseWithStatementErr4'],
['parser/parseWithStatementErr5'],
['parser/parseWithStatementErr6'],
['parser/parseWithStatementErr7'],
['parser/parseWithStatementErr8'],
];
}
public function testWith(): void
{
$sql = <<<SQL
WITH categories(identifier, name, parent_id) AS (
SELECT c.identifier, c.name, c.parent_id FROM category c WHERE c.identifier = 'a'
UNION ALL
SELECT c.identifier, c.name, c.parent_id FROM categories, category c WHERE c.identifier = categories.parent_id
), foo AS ( SELECT * FROM test )
SELECT * FROM categories
SQL;
$lexer = new Lexer($sql);
$lexerErrors = $this->getErrorsAsArray($lexer);
$this->assertCount(0, $lexerErrors);
$parser = new Parser($lexer->list);
$parserErrors = $this->getErrorsAsArray($parser);
$this->assertCount(0, $parserErrors);
$this->assertCount(1, $parser->statements);
// phpcs:disable Generic.Files.LineLength.TooLong
$expected = <<<SQL
WITH categories(identifier, name, parent_id) AS (SELECT c.identifier, c.name, c.parent_id FROM category AS `c` WHERE c.identifier = 'a' UNION ALL SELECT c.identifier, c.name, c.parent_id FROM categories, category AS `c` WHERE c.identifier = categories.parent_id), foo AS (SELECT * FROM test) SELECT * FROM categories
SQL;
// phpcs:enable
$this->assertEquals($expected, $parser->statements[0]->build());
}
public function testWithRecursive(): void
{
$sql = <<<SQL
WITH RECURSIVE number_sequence AS (
SELECT 1 AS `number`
UNION ALL
SELECT `number` + 1
FROM number_sequence
WHERE `number` < 5
)
SELECT * FROM number_sequence;
SQL;
$lexer = new Lexer($sql);
$lexerErrors = $this->getErrorsAsArray($lexer);
$this->assertCount(0, $lexerErrors);
$parser = new Parser($lexer->list);
$parserErrors = $this->getErrorsAsArray($parser);
$this->assertCount(0, $parserErrors);
$this->assertCount(1, $parser->statements);
// phpcs:disable Generic.Files.LineLength.TooLong
$expected = <<<SQL
WITH RECURSIVE number_sequence AS (SELECT 1 AS `number` UNION ALL SELECT `number`+ 1 FROM number_sequence WHERE `number` < 5) SELECT * FROM number_sequence
SQL;
// phpcs:enable
$this->assertEquals($expected, $parser->statements[0]->build());
}
public function testWithRecursiveWithers(): void
{
$sql = <<<SQL
WITH RECURSIVE cte AS
(
SELECT 1 AS n, CAST('abc' AS CHAR(20)) AS str
UNION ALL
SELECT n + 1, CONCAT(str, str) FROM cte WHERE n < 3
), cte2 AS
(
SELECT 1 AS n, CAST('def' AS CHAR(20)) AS str
UNION ALL
SELECT n + 1, CONCAT(str, str) FROM cte WHERE n < 3
)
SELECT * FROM cte UNION SELECT * FROM cte2;
SQL;
$lexer = new Lexer($sql);
$lexerErrors = $this->getErrorsAsArray($lexer);
$this->assertCount(0, $lexerErrors);
$parser = new Parser($lexer->list);
$parserErrors = $this->getErrorsAsArray($parser);
$this->assertCount(0, $parserErrors);
$this->assertCount(1, $parser->statements);
// phpcs:disable Generic.Files.LineLength.TooLong
$expected = <<<SQL
WITH RECURSIVE cte AS (SELECT 1 AS `n`, CAST('abc' AS CHAR(20)) AS `str` UNION ALL SELECT n+ 1, CONCAT(str, str) FROM cte WHERE n < 3), cte2 AS (SELECT 1 AS `n`, CAST('def' AS CHAR(20)) AS `str` UNION ALL SELECT n+ 1, CONCAT(str, str) FROM cte WHERE n < 3) SELECT * FROM cte UNION SELECT * FROM cte2
SQL;
// phpcs:enable
$this->assertEquals($expected, $parser->statements[0]->build());
}
public function testWithHasErrors(): void
{
$sql = <<<SQL
WITH categories(identifier, name, parent_id) AS (
SOMETHING * FROM foo
)
SELECT * FROM categories
SQL;
$lexer = new Lexer($sql);
$lexerErrors = $this->getErrorsAsArray($lexer);
$this->assertCount(0, $lexerErrors);
$parser = new Parser($lexer->list);
$parserErrors = $this->getErrorsAsArray($parser);
$this->assertCount(4, $parserErrors);
}
public function testWithEmbedParenthesis(): void
{
$sql = <<<SQL
WITH categories AS (
SELECT * FROM (SELECT * FROM foo)
)
SELECT * FROM categories
SQL;
$lexer = new Lexer($sql);
$lexerErrors = $this->getErrorsAsArray($lexer);
$this->assertCount(0, $lexerErrors);
$parser = new Parser($lexer->list);
$parserErrors = $this->getErrorsAsArray($parser);
$this->assertCount(0, $parserErrors);
// phpcs:disable Generic.Files.LineLength.TooLong
$expected = <<<SQL
WITH categories AS (SELECT * FROM (SELECT * FROM foo)) SELECT * FROM categories
SQL;
// phpcs:enable
$this->assertEquals($expected, $parser->statements[0]->build());
}
public function testWithHasUnclosedParenthesis(): void
{
$sql = <<<SQL
WITH categories(identifier, name, parent_id) AS (
SELECT * FROM (SELECT * FROM foo
)
SELECT * FROM categories
SQL;
$lexer = new Lexer($sql);
$lexerErrors = $this->getErrorsAsArray($lexer);
$this->assertCount(0, $lexerErrors);
$parser = new Parser($lexer->list);
$parserErrors = $this->getErrorsAsArray($parser);
$this->assertEquals($parserErrors[0][0], 'A closing bracket was expected.');
}
public function testBuildWrongWithKeyword(): void
{
$this->expectExceptionMessage('Can not build a component that is not a WithKeyword');
WithKeyword::build(new stdClass());
}
public function testBuildBadWithKeyword(): void
{
$this->expectExceptionMessage('No statement inside WITH');
WithKeyword::build(new WithKeyword('test'));
}
}
|