Skip to content

Commit cc26c8f

Browse files
committed
Fix phpcs and phpstan issues in union view fix
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
1 parent 5ba1a02 commit cc26c8f

3 files changed

Lines changed: 16 additions & 26 deletions

File tree

‎phpstan-baseline.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ parameters:
19081908
-
19091909
message: '#^Dynamic call to static method PHPUnit\\Framework\\Assert\:\:assertEquals\(\)\.$#'
19101910
identifier: staticMethod.dynamicCall
1911-
count: 35
1911+
count: 36
19121912
path: tests/Builder/CreateStatementTest.php
19131913

19141914
-

‎src/Statements/CreateStatement.php‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
use function is_array;
2929
use function str_replace;
30+
use function substr;
3031
use function trim;
3132

3233
/**
@@ -463,15 +464,11 @@ public function build(): string
463464
}
464465

465466
$body = TokensList::buildFromArray($this->body);
466-
// Body tokens (e.g. trailing `UNION ALL (SELECT ...)` after the
467-
// primary SELECT) are concatenated raw, so without a separator
468-
// the rebuilt SQL collides with the SELECT's last token,
469-
// `WHERE 3 = 3union all` instead of `WHERE 3 = 3 union all`.
470-
// Only insert a space when neither side already has one to
471-
// avoid double-spacing the WITH...AS path.
472-
$separator = ($builtStatement !== '' && $body !== ''
467+
// Trailing body tokens (e.g. `UNION ALL ...`) need a space after the SELECT,
468+
// unless one side already has it (the WITH ... AS path does).
469+
$separator = $builtStatement !== '' && $body !== ''
473470
&& substr($builtStatement, -1) !== ' '
474-
&& $body[0] !== ' ') ? ' ' : '';
471+
&& $body[0] !== ' ' ? ' ' : '';
475472

476473
return 'CREATE '
477474
. $this->options->build() . ' '

‎tests/Builder/CreateStatementTest.php‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -921,27 +921,20 @@ public function testBuildCreateTableWithUppercaseHexDefault(): void
921921
}
922922

923923
/**
924-
* Regression for #655: `CREATE VIEW ... AS SELECT ... WHERE ... UNION ALL (...)`
925-
* used to lose the whitespace between the SELECT body and the trailing
926-
* UNION clause, producing invalid SQL like `WHERE 3 = 3union all (...)`.
924+
* Regression for #655: the UNION body used to be glued to the SELECT,
925+
* producing invalid SQL like `WHERE 3 = 3union all (...)`.
927926
*/
928927
public function testBuilderViewWithUnionPreservesWhitespace(): void
929928
{
930-
$sql = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER "
931-
. "VIEW `v1` AS select 1 AS `a` where 3 = 3 union all (select 2 AS `a`)";
932-
933-
$parser = new Parser($sql);
934-
$built = $parser->statements[0]->build();
935-
936-
$this->assertStringNotContainsString(
937-
'3union all',
938-
$built,
939-
'rebuilt CREATE VIEW must keep the space between WHERE clause and UNION',
929+
$parser = new Parser(
930+
'CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER '
931+
. 'VIEW `v1` AS select 1 AS `a` where 3 = 3 union all (select 2 AS `a`)',
940932
);
941-
$this->assertStringContainsString(
942-
'3 union all',
943-
// case-insensitive match: builder may upper-case the keyword
944-
preg_replace('/UNION\s+ALL/i', 'union all', $built),
933+
934+
$this->assertEquals(
935+
'CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER '
936+
. 'VIEW `v1` AS SELECT 1 AS `a` WHERE 3 = 3 union all (select 2 AS `a`) ',
937+
$parser->statements[0]->build(),
945938
);
946939
}
947940
}

0 commit comments

Comments
 (0)