Skip to content

Commit

Permalink
Fixing the issue of the create view's builder, additional tokens
Browse files Browse the repository at this point in the history
Signed-off-by: Fawzi E. Abdulfattah <iifawzie@gmail.com>
  • Loading branch information
iifawzi committed Feb 3, 2022
1 parent 0deffe5 commit 471ba6a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/Statements/CreateStatement.php
Expand Up @@ -746,6 +746,7 @@ public function parse(Parser $parser, TokensList $list)
if ($list->tokens[$nextidx]->value === 'SELECT') {
$list->idx = $nextidx;
$this->select = new SelectStatement($parser, $list);
++$list->idx; // Skipping last token from the select.
} elseif ($list->tokens[$nextidx]->value === 'WITH') {
++$list->idx;
$this->with = new WithStatement($parser, $list);
Expand Down
27 changes: 27 additions & 0 deletions tests/Builder/CreateStatementTest.php
Expand Up @@ -342,6 +342,33 @@ public function testBuilderView(): void
'SELECT id, first_name, FROMzz employee WHERE id = 2 ',
$stmt->build()
);

$parser = new Parser('CREATE VIEW `view_programlevelpartner` AS SELECT `p`.`country_id`'
. 'AS `country_id` FROM `program_level_partner` `p` ORDER BY `p`.`id` asc');
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE VIEW `view_programlevelpartner` AS SELECT `p`.`country_id`'
. ' AS `country_id` FROM `program_level_partner` AS `p` ORDER BY `p`.`id` ASC ',
$stmt->build()
);

$parser = new Parser('CREATE VIEW `view_zg_bycountry` AS '
. 'SELECT `d`.`zg_id` FROM `view_zg_value` AS `d` GROUP BY `d`.`ind_id`;');
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE VIEW `view_zg_bycountry` AS '
. 'SELECT `d`.`zg_id` FROM `view_zg_value` AS `d` GROUP BY `d`.`ind_id` ',
$stmt->build()
);

$parser = new Parser('CREATE view view_name AS WITH aa(col1)'
. ' AS ( SELECT 1 UNION ALL SELECT 2 ) SELECT col1 FROM cte AS `d` ');
$stmt = $parser->statements[0];
$this->assertEquals(
'CREATE view view_name AS WITH aa(col1)'
. ' AS (SELECT 1 UNION ALL SELECT 2) SELECT col1 FROM cte AS `d` ',
$stmt->build()
);
}

public function testBuilderViewComplex(): void
Expand Down
6 changes: 1 addition & 5 deletions tests/data/parser/parseCreateView.out
Expand Up @@ -3534,11 +3534,7 @@
"table": null,
"return": null,
"parameters": null,
"body": [
{
"@type": "@286"
}
],
"body": [],
"CLAUSES": [],
"END_OPTIONS": [],
"options": {
Expand Down
3 changes: 0 additions & 3 deletions tests/data/parser/parseCreateView3.out
Expand Up @@ -1171,9 +1171,6 @@
"return": null,
"parameters": null,
"body": [
{
"@type": "@28"
},
{
"@type": "@29"
},
Expand Down
6 changes: 1 addition & 5 deletions tests/data/parser/parseCreateViewWithQuotes.out
Expand Up @@ -1223,11 +1223,7 @@
"table": null,
"return": null,
"parameters": null,
"body": [
{
"@type": "@43"
}
],
"body": [],
"CLAUSES": [],
"END_OPTIONS": [],
"options": {
Expand Down
3 changes: 0 additions & 3 deletions tests/data/parser/parseCreateViewWithUnion.out
Expand Up @@ -1266,9 +1266,6 @@
"return": null,
"parameters": null,
"body": [
{
"@type": "@27"
},
{
"@type": "@28"
},
Expand Down
6 changes: 1 addition & 5 deletions tests/data/parser/parseCreateViewWithWrongSyntax.out
Expand Up @@ -1007,11 +1007,7 @@
"table": null,
"return": null,
"parameters": null,
"body": [
{
"@type": "@20"
}
],
"body": [],
"CLAUSES": [],
"END_OPTIONS": [],
"options": {
Expand Down
6 changes: 1 addition & 5 deletions tests/data/parser/parseCreateViewWithoutQuotes.out
Expand Up @@ -1232,11 +1232,7 @@
"table": null,
"return": null,
"parameters": null,
"body": [
{
"@type": "@44"
}
],
"body": [],
"CLAUSES": [],
"END_OPTIONS": [],
"options": {
Expand Down

0 comments on commit 471ba6a

Please sign in to comment.