Skip to content

Commit de90e71

Browse files
committed
Update the with Build statement to build the cteStatement
Signed-off-by: Fawzi E. Abdulfattah <[email protected]>
1 parent cd49677 commit de90e71

20 files changed

+6869
-7
lines changed

src/Statements/WithStatement.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ final class WithStatement extends Statement
6060
/** @var WithKeyword[] */
6161
public $withers = [];
6262

63+
/**
64+
* holds the CTE parser.
65+
*
66+
* @var Parser
67+
*/
68+
public $cteStatementParser;
69+
6370
/**
6471
* @param Parser $parser the instance that requests parsing
6572
* @param TokensList $list the list of tokens to be parsed
@@ -243,8 +250,12 @@ public function parse(Parser $parser, TokensList $list)
243250
foreach ($subParser->errors as $error) {
244251
$parser->errors[] = $error;
245252
}
253+
254+
break;
246255
}
247256

257+
$this->cteStatementParser = $subParser;
258+
248259
$list->idx = $idxOfLastParsedToken;
249260
break;
250261
}
@@ -277,6 +288,12 @@ public function build()
277288
$str .= WithKeyword::build($wither);
278289
}
279290

291+
$str .= ' ';
292+
293+
foreach ($this->cteStatementParser->statements as $statement) {
294+
$str .= $statement->build();
295+
}
296+
280297
return $str;
281298
}
282299

tests/Builder/CreateStatementTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ public function testBuilderTable(): void
175175

176176
$this->assertEquals(
177177
'CREATE TABLE table_name WITH' .
178-
' cte(col1) AS (SELECT 1 UNION ALL SELECT 2)',
178+
' cte(col1) AS (SELECT 1 UNION ALL SELECT 2)' .
179+
' SELECT col1 FROM cte',
179180
$stmt->build()
180181
);
181182
}
@@ -370,9 +371,12 @@ public function testBuilderViewComplex(): void
370371
$this->assertEquals(
371372
'CREATE VIEW withclause AS '
372373
. 'WITH cte AS ('
373-
. 'SELECT p.name, p.shape'
374-
. ' FROM gis_all AS `p`'
375-
. ') ',
374+
. 'SELECT p.name, p.shape '
375+
. 'FROM gis_all AS `p`'
376+
. ') '
377+
. 'SELECT cte.* '
378+
. 'FROM cte '
379+
. 'CROSS JOIN gis_all ',
376380
$stmt->build()
377381
);
378382
$parser = new Parser(
@@ -400,7 +404,10 @@ public function testBuilderViewComplex(): void
400404
. '), cte2 AS ('
401405
. 'SELECT p.name AS `n2`, p.shape AS `sh2`'
402406
. ' FROM gis_all AS `p`'
403-
. ') ',
407+
. ')'
408+
. ' SELECT cte.*, cte2.* '
409+
. 'FROM cte, cte2'
410+
. ' CROSS JOIN gis_all ',
404411
$stmt->build()
405412
);
406413
}

tests/Parser/WithStatementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testWith(): void
6868

6969
// phpcs:disable Generic.Files.LineLength.TooLong
7070
$expected = <<<SQL
71-
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)
71+
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
7272
SQL;
7373
// phpcs:enable
7474
$this->assertEquals($expected, $parser->statements[0]->build());
@@ -110,7 +110,7 @@ public function testWithEmbedParenthesis(): void
110110

111111
// phpcs:disable Generic.Files.LineLength.TooLong
112112
$expected = <<<SQL
113-
WITH categories AS (SELECT * FROM (SELECT * FROM foo))
113+
WITH categories AS (SELECT * FROM (SELECT * FROM foo)) SELECT * FROM categories
114114
SQL;
115115
// phpcs:enable
116116
$this->assertEquals($expected, $parser->statements[0]->build());

tests/data/parser/parseCreateViewAsWithAs.out

Lines changed: 3157 additions & 0 deletions
Large diffs are not rendered by default.

tests/data/parser/parseWithStatement.out

Lines changed: 555 additions & 0 deletions
Large diffs are not rendered by default.

tests/data/parser/parseWithStatement1.out

Lines changed: 555 additions & 0 deletions
Large diffs are not rendered by default.

tests/data/parser/parseWithStatement2.out

Lines changed: 555 additions & 0 deletions
Large diffs are not rendered by default.

tests/data/parser/parseWithStatement3.out

Lines changed: 555 additions & 0 deletions
Large diffs are not rendered by default.

tests/data/parser/parseWithStatement4.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,7 @@
16531653
}
16541654
}
16551655
},
1656+
"cteStatement": null,
16561657
"END_OPTIONS": [],
16571658
"options": {
16581659
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",

tests/data/parser/parseWithStatement5.out

Lines changed: 555 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)