Skip to content

Commit

Permalink
Merge branch '5.10.x' into 5.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Jan 16, 2025
2 parents d3c1f11 + 439b54d commit 2142141
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

- Add Translator::setLocale() method (#599)

# [5.10.3] - YYYY-MM-DD

### Fixed

- Add "RECURSIVE" on build() for "WITH RECURSIVE" on the WithStatement class (#605)

## [5.10.2] - 2024-12-05

### Added
Expand Down
15 changes: 5 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -722,12 +722,7 @@ parameters:

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\:\\:\\$options \\(array\\<int, mixed\\>\\) does not accept array\\.$#"
count: 2
path: src/Components/OptionsArray.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\:\\:\\$options \\(array\\<int, mixed\\>\\) does not accept array\\<int\\|string, mixed\\>\\.$#"
count: 8
count: 10
path: src/Components/OptionsArray.php

-
Expand Down Expand Up @@ -2117,7 +2112,7 @@ parameters:

-
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
count: 33
count: 34
path: tests/Builder/CreateStatementTest.php

-
Expand Down Expand Up @@ -2407,7 +2402,7 @@ parameters:

-
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
count: 8
count: 9
path: tests/Components/OptionsArrayTest.php

-
Expand Down Expand Up @@ -2642,12 +2637,12 @@ parameters:

-
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertCount\\(\\)\\.$#"
count: 8
count: 14
path: tests/Parser/WithStatementTest.php

-
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
count: 3
count: 5
path: tests/Parser/WithStatementTest.php

-
Expand Down
10 changes: 8 additions & 2 deletions src/Statements/WithStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,17 @@ public function parse(Parser $parser, TokensList $list)
*/
public function build()
{
$str = 'WITH ';
$initial = true;
$str = 'WITH';

if ($this->options !== null && $this->options->options !== []) {
$str .= ' ' . OptionsArray::build($this->options);
}

foreach ($this->withers as $wither) {
$str .= $str === 'WITH ' ? '' : ', ';
$str .= $initial ? ' ' : ', ';
$str .= WithKeyword::build($wither);
$initial = false;
}

$str .= ' ';
Expand Down
29 changes: 29 additions & 0 deletions tests/Builder/CreateStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use PhpMyAdmin\SqlParser\Tests\TestCase;
use PhpMyAdmin\SqlParser\TokensList;

use function implode;

class CreateStatementTest extends TestCase
{
public function testBuilder(): void
Expand Down Expand Up @@ -399,6 +401,33 @@ public function testBuilderView(): void
. ' AS (SELECT 1 UNION ALL SELECT 2) SELECT col1 FROM cte AS `d` ',
$stmt->build()
);

$parser = new Parser(
implode("\n", [
'CREATE VIEW number_sequence_view AS',
'WITH RECURSIVE number_sequence AS (',
' SELECT 1 AS `number`',
' UNION ALL',
' SELECT `number` + 1',
' FROM number_sequence',
' WHERE `number` < 5',
')',
'SELECT * FROM number_sequence;',
])
);
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE VIEW number_sequence_view AS'
. ' WITH RECURSIVE number_sequence AS ('
. 'SELECT 1 AS `number`'
. ' UNION ALL'
. ' SELECT `number`+ 1'
. ' FROM number_sequence'
. ' WHERE `number` < 5'
. ')'
. ' SELECT * FROM number_sequence ',
$stmt->build()
);
}

public function testBuilderViewComplex(): void
Expand Down
13 changes: 13 additions & 0 deletions tests/Components/OptionsArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,17 @@ public function testBuild(): void
'ALL SQL_CALC_FOUND_ROWS MAX_STATEMENT_TIME=42'
);
}

public function testBuildWithRecursive(): void
{
$component = OptionsArray::parse(
new Parser(),
$this->getTokensList('RECURSIVE'),
['RECURSIVE' => 1]
);
$this->assertEquals(
OptionsArray::build($component),
'RECURSIVE'
);
}
}
64 changes: 64 additions & 0 deletions tests/Parser/WithStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,70 @@ public function testWith(): void
$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
Expand Down

0 comments on commit 2142141

Please sign in to comment.