Skip to content

Commit e5cb8a1

Browse files
committed
Merge #371 - Fixing the additional body tokens issue when create view statements
Ref: phpmyadmin/phpmyadmin#17321 Pull-request: #371 Signed-off-by: William Desportes <[email protected]>
2 parents 94652f2 + 471ba6a commit e5cb8a1

File tree

8 files changed

+32
-26
lines changed

8 files changed

+32
-26
lines changed

src/Statements/CreateStatement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ public function parse(Parser $parser, TokensList $list)
746746
if ($list->tokens[$nextidx]->value === 'SELECT') {
747747
$list->idx = $nextidx;
748748
$this->select = new SelectStatement($parser, $list);
749+
++$list->idx; // Skipping last token from the select.
749750
} elseif ($list->tokens[$nextidx]->value === 'WITH') {
750751
++$list->idx;
751752
$this->with = new WithStatement($parser, $list);

tests/Builder/CreateStatementTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,33 @@ public function testBuilderView(): void
342342
'SELECT id, first_name, FROMzz employee WHERE id = 2 ',
343343
$stmt->build()
344344
);
345+
346+
$parser = new Parser('CREATE VIEW `view_programlevelpartner` AS SELECT `p`.`country_id`'
347+
. 'AS `country_id` FROM `program_level_partner` `p` ORDER BY `p`.`id` asc');
348+
$stmt = $parser->statements[0];
349+
$this->assertEquals(
350+
'CREATE VIEW `view_programlevelpartner` AS SELECT `p`.`country_id`'
351+
. ' AS `country_id` FROM `program_level_partner` AS `p` ORDER BY `p`.`id` ASC ',
352+
$stmt->build()
353+
);
354+
355+
$parser = new Parser('CREATE VIEW `view_zg_bycountry` AS '
356+
. 'SELECT `d`.`zg_id` FROM `view_zg_value` AS `d` GROUP BY `d`.`ind_id`;');
357+
$stmt = $parser->statements[0];
358+
$this->assertEquals(
359+
'CREATE VIEW `view_zg_bycountry` AS '
360+
. 'SELECT `d`.`zg_id` FROM `view_zg_value` AS `d` GROUP BY `d`.`ind_id` ',
361+
$stmt->build()
362+
);
363+
364+
$parser = new Parser('CREATE view view_name AS WITH aa(col1)'
365+
. ' AS ( SELECT 1 UNION ALL SELECT 2 ) SELECT col1 FROM cte AS `d` ');
366+
$stmt = $parser->statements[0];
367+
$this->assertEquals(
368+
'CREATE view view_name AS WITH aa(col1)'
369+
. ' AS (SELECT 1 UNION ALL SELECT 2) SELECT col1 FROM cte AS `d` ',
370+
$stmt->build()
371+
);
345372
}
346373

347374
public function testBuilderViewComplex(): void

tests/data/parser/parseCreateView.out

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,11 +3534,7 @@
35343534
"table": null,
35353535
"return": null,
35363536
"parameters": null,
3537-
"body": [
3538-
{
3539-
"@type": "@286"
3540-
}
3541-
],
3537+
"body": [],
35423538
"CLAUSES": [],
35433539
"END_OPTIONS": [],
35443540
"options": {

tests/data/parser/parseCreateView3.out

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,9 +1171,6 @@
11711171
"return": null,
11721172
"parameters": null,
11731173
"body": [
1174-
{
1175-
"@type": "@28"
1176-
},
11771174
{
11781175
"@type": "@29"
11791176
},

tests/data/parser/parseCreateViewWithQuotes.out

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,11 +1223,7 @@
12231223
"table": null,
12241224
"return": null,
12251225
"parameters": null,
1226-
"body": [
1227-
{
1228-
"@type": "@43"
1229-
}
1230-
],
1226+
"body": [],
12311227
"CLAUSES": [],
12321228
"END_OPTIONS": [],
12331229
"options": {

tests/data/parser/parseCreateViewWithUnion.out

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,9 +1266,6 @@
12661266
"return": null,
12671267
"parameters": null,
12681268
"body": [
1269-
{
1270-
"@type": "@27"
1271-
},
12721269
{
12731270
"@type": "@28"
12741271
},

tests/data/parser/parseCreateViewWithWrongSyntax.out

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,11 +1007,7 @@
10071007
"table": null,
10081008
"return": null,
10091009
"parameters": null,
1010-
"body": [
1011-
{
1012-
"@type": "@20"
1013-
}
1014-
],
1010+
"body": [],
10151011
"CLAUSES": [],
10161012
"END_OPTIONS": [],
10171013
"options": {

tests/data/parser/parseCreateViewWithoutQuotes.out

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,11 +1232,7 @@
12321232
"table": null,
12331233
"return": null,
12341234
"parameters": null,
1235-
"body": [
1236-
{
1237-
"@type": "@44"
1238-
}
1239-
],
1235+
"body": [],
12401236
"CLAUSES": [],
12411237
"END_OPTIONS": [],
12421238
"options": {

0 commit comments

Comments
 (0)