From 471ba6ab356eb44256a28006544bf42c64d73e17 Mon Sep 17 00:00:00 2001 From: "Fawzi E. Abdulfattah" Date: Thu, 3 Feb 2022 15:43:31 +0200 Subject: [PATCH] Fixing the issue of the create view's builder, additional tokens Signed-off-by: Fawzi E. Abdulfattah --- src/Statements/CreateStatement.php | 1 + tests/Builder/CreateStatementTest.php | 27 +++++++++++++++++++ tests/data/parser/parseCreateView.out | 6 +---- tests/data/parser/parseCreateView3.out | 3 --- .../data/parser/parseCreateViewWithQuotes.out | 6 +---- .../data/parser/parseCreateViewWithUnion.out | 3 --- .../parser/parseCreateViewWithWrongSyntax.out | 6 +---- .../parser/parseCreateViewWithoutQuotes.out | 6 +---- 8 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/Statements/CreateStatement.php b/src/Statements/CreateStatement.php index 055947771..9ca14e9cb 100644 --- a/src/Statements/CreateStatement.php +++ b/src/Statements/CreateStatement.php @@ -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); diff --git a/tests/Builder/CreateStatementTest.php b/tests/Builder/CreateStatementTest.php index d10e80670..7bc7100ae 100644 --- a/tests/Builder/CreateStatementTest.php +++ b/tests/Builder/CreateStatementTest.php @@ -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 diff --git a/tests/data/parser/parseCreateView.out b/tests/data/parser/parseCreateView.out index 49f04ab38..e6fed5105 100644 --- a/tests/data/parser/parseCreateView.out +++ b/tests/data/parser/parseCreateView.out @@ -3534,11 +3534,7 @@ "table": null, "return": null, "parameters": null, - "body": [ - { - "@type": "@286" - } - ], + "body": [], "CLAUSES": [], "END_OPTIONS": [], "options": { diff --git a/tests/data/parser/parseCreateView3.out b/tests/data/parser/parseCreateView3.out index d977f6710..072a3e225 100644 --- a/tests/data/parser/parseCreateView3.out +++ b/tests/data/parser/parseCreateView3.out @@ -1171,9 +1171,6 @@ "return": null, "parameters": null, "body": [ - { - "@type": "@28" - }, { "@type": "@29" }, diff --git a/tests/data/parser/parseCreateViewWithQuotes.out b/tests/data/parser/parseCreateViewWithQuotes.out index 86c12e688..246e5c051 100644 --- a/tests/data/parser/parseCreateViewWithQuotes.out +++ b/tests/data/parser/parseCreateViewWithQuotes.out @@ -1223,11 +1223,7 @@ "table": null, "return": null, "parameters": null, - "body": [ - { - "@type": "@43" - } - ], + "body": [], "CLAUSES": [], "END_OPTIONS": [], "options": { diff --git a/tests/data/parser/parseCreateViewWithUnion.out b/tests/data/parser/parseCreateViewWithUnion.out index d122d3b42..f9321bdc8 100644 --- a/tests/data/parser/parseCreateViewWithUnion.out +++ b/tests/data/parser/parseCreateViewWithUnion.out @@ -1266,9 +1266,6 @@ "return": null, "parameters": null, "body": [ - { - "@type": "@27" - }, { "@type": "@28" }, diff --git a/tests/data/parser/parseCreateViewWithWrongSyntax.out b/tests/data/parser/parseCreateViewWithWrongSyntax.out index 2a306e1cd..2dc1edf27 100644 --- a/tests/data/parser/parseCreateViewWithWrongSyntax.out +++ b/tests/data/parser/parseCreateViewWithWrongSyntax.out @@ -1007,11 +1007,7 @@ "table": null, "return": null, "parameters": null, - "body": [ - { - "@type": "@20" - } - ], + "body": [], "CLAUSES": [], "END_OPTIONS": [], "options": { diff --git a/tests/data/parser/parseCreateViewWithoutQuotes.out b/tests/data/parser/parseCreateViewWithoutQuotes.out index 1e6c43351..7c0d865ae 100644 --- a/tests/data/parser/parseCreateViewWithoutQuotes.out +++ b/tests/data/parser/parseCreateViewWithoutQuotes.out @@ -1232,11 +1232,7 @@ "table": null, "return": null, "parameters": null, - "body": [ - { - "@type": "@44" - } - ], + "body": [], "CLAUSES": [], "END_OPTIONS": [], "options": {