From 8fc0c60fbdba378d5f872878163fdc19f2dfc5dd Mon Sep 17 00:00:00 2001 From: SAY-5 Date: Thu, 30 Apr 2026 00:38:43 -0700 Subject: [PATCH 1/3] fix(builder): preserve whitespace between SELECT and UNION body in CREATE VIEW (#655) Signed-off-by: SAY-5 --- src/Statements/CreateStatement.php | 13 ++++++++++++- tests/Builder/CreateStatementTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Statements/CreateStatement.php b/src/Statements/CreateStatement.php index 7b2b65a5..598e4c8a 100644 --- a/src/Statements/CreateStatement.php +++ b/src/Statements/CreateStatement.php @@ -462,11 +462,22 @@ public function build(): string $builtStatement = $this->with->build(); } + $body = TokensList::buildFromArray($this->body); + // Body tokens (e.g. trailing `UNION ALL (SELECT ...)` after the + // primary SELECT) are concatenated raw, so without a separator + // the rebuilt SQL collides with the SELECT's last token — + // `WHERE 3 = 3union all` instead of `WHERE 3 = 3 union all`. + // Only insert a space when neither side already has one to + // avoid double-spacing the WITH...AS path. + $separator = ($builtStatement !== '' && $body !== '' + && substr($builtStatement, -1) !== ' ' + && $body[0] !== ' ') ? ' ' : ''; + return 'CREATE ' . $this->options->build() . ' ' . $this->name->build() . ' ' . $fields . ' AS ' . $builtStatement - . TokensList::buildFromArray($this->body) . ' ' + . $separator . $body . ' ' . ($this->entityOptions?->build() ?? ''); } diff --git a/tests/Builder/CreateStatementTest.php b/tests/Builder/CreateStatementTest.php index 6c8fea6d..12ed2029 100644 --- a/tests/Builder/CreateStatementTest.php +++ b/tests/Builder/CreateStatementTest.php @@ -919,4 +919,29 @@ public function testBuildCreateTableWithUppercaseHexDefault(): void $this->assertEmpty($parser->errors); $this->assertStringContainsString("DEFAULT X'FF'", $parser->statements[0]->build()); } + + /** + * Regression for #655: `CREATE VIEW ... AS SELECT ... WHERE ... UNION ALL (...)` + * used to lose the whitespace between the SELECT body and the trailing + * UNION clause, producing invalid SQL like `WHERE 3 = 3union all (...)`. + */ + public function testBuilderViewWithUnionPreservesWhitespace(): void + { + $sql = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER " + . "VIEW `v1` AS select 1 AS `a` where 3 = 3 union all (select 2 AS `a`)"; + + $parser = new Parser($sql); + $built = $parser->statements[0]->build(); + + $this->assertStringNotContainsString( + '3union all', + $built, + 'rebuilt CREATE VIEW must keep the space between WHERE clause and UNION', + ); + $this->assertStringContainsString( + '3 union all', + // case-insensitive match: builder may upper-case the keyword + preg_replace('/UNION\s+ALL/i', 'union all', $built), + ); + } } From 5ba1a02f2425089886080d7a962c7983ebab67c9 Mon Sep 17 00:00:00 2001 From: SAY-5 Date: Mon, 11 May 2026 13:02:33 -0700 Subject: [PATCH 2/3] chore: remove em-dashes from comments --- src/Statements/CreateStatement.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Statements/CreateStatement.php b/src/Statements/CreateStatement.php index 598e4c8a..3b786693 100644 --- a/src/Statements/CreateStatement.php +++ b/src/Statements/CreateStatement.php @@ -465,7 +465,7 @@ public function build(): string $body = TokensList::buildFromArray($this->body); // Body tokens (e.g. trailing `UNION ALL (SELECT ...)` after the // primary SELECT) are concatenated raw, so without a separator - // the rebuilt SQL collides with the SELECT's last token — + // the rebuilt SQL collides with the SELECT's last token, // `WHERE 3 = 3union all` instead of `WHERE 3 = 3 union all`. // Only insert a space when neither side already has one to // avoid double-spacing the WITH...AS path. From cc26c8f8f2703e3d8bb233e9e34bd0aa6f9d7f24 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Wed, 25 Nov 2026 16:55:41 -0800 Subject: [PATCH 3/3] Fix phpcs and phpstan issues in union view fix Signed-off-by: Sai Asish Y --- phpstan-baseline.neon | 2 +- src/Statements/CreateStatement.php | 13 +++++-------- tests/Builder/CreateStatementTest.php | 27 ++++++++++----------------- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 0d6f646d..e0469592 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1908,7 +1908,7 @@ parameters: - message: '#^Dynamic call to static method PHPUnit\\Framework\\Assert\:\:assertEquals\(\)\.$#' identifier: staticMethod.dynamicCall - count: 35 + count: 36 path: tests/Builder/CreateStatementTest.php - diff --git a/src/Statements/CreateStatement.php b/src/Statements/CreateStatement.php index 3b786693..639b1a0a 100644 --- a/src/Statements/CreateStatement.php +++ b/src/Statements/CreateStatement.php @@ -27,6 +27,7 @@ use function is_array; use function str_replace; +use function substr; use function trim; /** @@ -463,15 +464,11 @@ public function build(): string } $body = TokensList::buildFromArray($this->body); - // Body tokens (e.g. trailing `UNION ALL (SELECT ...)` after the - // primary SELECT) are concatenated raw, so without a separator - // the rebuilt SQL collides with the SELECT's last token, - // `WHERE 3 = 3union all` instead of `WHERE 3 = 3 union all`. - // Only insert a space when neither side already has one to - // avoid double-spacing the WITH...AS path. - $separator = ($builtStatement !== '' && $body !== '' + // Trailing body tokens (e.g. `UNION ALL ...`) need a space after the SELECT, + // unless one side already has it (the WITH ... AS path does). + $separator = $builtStatement !== '' && $body !== '' && substr($builtStatement, -1) !== ' ' - && $body[0] !== ' ') ? ' ' : ''; + && $body[0] !== ' ' ? ' ' : ''; return 'CREATE ' . $this->options->build() . ' ' diff --git a/tests/Builder/CreateStatementTest.php b/tests/Builder/CreateStatementTest.php index 12ed2029..f81bda4c 100644 --- a/tests/Builder/CreateStatementTest.php +++ b/tests/Builder/CreateStatementTest.php @@ -921,27 +921,20 @@ public function testBuildCreateTableWithUppercaseHexDefault(): void } /** - * Regression for #655: `CREATE VIEW ... AS SELECT ... WHERE ... UNION ALL (...)` - * used to lose the whitespace between the SELECT body and the trailing - * UNION clause, producing invalid SQL like `WHERE 3 = 3union all (...)`. + * Regression for #655: the UNION body used to be glued to the SELECT, + * producing invalid SQL like `WHERE 3 = 3union all (...)`. */ public function testBuilderViewWithUnionPreservesWhitespace(): void { - $sql = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER " - . "VIEW `v1` AS select 1 AS `a` where 3 = 3 union all (select 2 AS `a`)"; - - $parser = new Parser($sql); - $built = $parser->statements[0]->build(); - - $this->assertStringNotContainsString( - '3union all', - $built, - 'rebuilt CREATE VIEW must keep the space between WHERE clause and UNION', + $parser = new Parser( + 'CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER ' + . 'VIEW `v1` AS select 1 AS `a` where 3 = 3 union all (select 2 AS `a`)', ); - $this->assertStringContainsString( - '3 union all', - // case-insensitive match: builder may upper-case the keyword - preg_replace('/UNION\s+ALL/i', 'union all', $built), + + $this->assertEquals( + 'CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER ' + . 'VIEW `v1` AS SELECT 1 AS `a` WHERE 3 = 3 union all (select 2 AS `a`) ', + $parser->statements[0]->build(), ); } }