From 8c41da97a63f0a970f97d4593de68736f0b65436 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 14 Apr 2018 10:06:15 -0700 Subject: [PATCH] Allow to specify arbitrary SQL expression in AbstractPlatform::getDummySelectSQL() For testing purposes and not only, it may be needed to evaluate an arbitrary SQL expression (see https://github.com/doctrine/dbal/pull/3013, https://github.com/doctrine/dbal/pull/3108). Instead of doing `str_replace()` on an expected string, one could specify the expression explicitly. --- .../DBAL/Platforms/AbstractPlatform.php | 6 +++++- lib/Doctrine/DBAL/Platforms/DB2Platform.php | 6 +++++- .../DBAL/Platforms/OraclePlatform.php | 6 +++++- .../Functional/LikeWildcardsEscapingTest.php | 20 +++++++++---------- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index daf6b8528f4..1a75872b354 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -49,7 +49,9 @@ use function array_values; use function count; use function explode; +use function func_get_arg; use function func_get_args; +use function func_num_args; use function get_class; use function implode; use function in_array; @@ -3499,7 +3501,9 @@ public function getTruncateTableSQL($tableName, $cascade = false) */ public function getDummySelectSQL() { - return 'SELECT 1'; + $expression = func_num_args() > 0 ? func_get_arg(0) : '1'; + + return sprintf('SELECT %s', $expression); } /** diff --git a/lib/Doctrine/DBAL/Platforms/DB2Platform.php b/lib/Doctrine/DBAL/Platforms/DB2Platform.php index df2faab440d..87288689a74 100644 --- a/lib/Doctrine/DBAL/Platforms/DB2Platform.php +++ b/lib/Doctrine/DBAL/Platforms/DB2Platform.php @@ -30,6 +30,8 @@ use function count; use function current; use function explode; +use function func_get_arg; +use function func_num_args; use function implode; use function sprintf; use function strpos; @@ -863,7 +865,9 @@ public function getForUpdateSQL() */ public function getDummySelectSQL() { - return 'SELECT 1 FROM sysibm.sysdummy1'; + $expression = func_num_args() > 0 ? func_get_arg(0) : '1'; + + return sprintf('SELECT %s FROM sysibm.sysdummy1', $expression); } /** diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index 1abc6cba108..fcb3d671aa8 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -31,6 +31,8 @@ use function array_merge; use function count; use function explode; +use function func_get_arg; +use function func_num_args; use function implode; use function preg_match; use function sprintf; @@ -1120,7 +1122,9 @@ public function getTruncateTableSQL($tableName, $cascade = false) */ public function getDummySelectSQL() { - return 'SELECT 1 FROM DUAL'; + $expression = func_num_args() > 0 ? func_get_arg(0) : '1'; + + return sprintf('SELECT %s FROM DUAL', $expression); } /** diff --git a/tests/Doctrine/Tests/DBAL/Functional/LikeWildcardsEscapingTest.php b/tests/Doctrine/Tests/DBAL/Functional/LikeWildcardsEscapingTest.php index ac3f6574d6f..9f2a098d7f5 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/LikeWildcardsEscapingTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/LikeWildcardsEscapingTest.php @@ -13,16 +13,16 @@ public function testFetchLikeExpressionResult() : void $string = '_25% off_ your next purchase \o/ [$̲̅(̲̅5̲̅)̲̅$̲̅] (^̮^)'; $escapeChar = '!'; $databasePlatform = $this->_conn->getDatabasePlatform(); - $stmt = $this->_conn->prepare(str_replace( - '1', - sprintf( - "(CASE WHEN '%s' LIKE '%s' ESCAPE '%s' THEN 1 ELSE 0 END)", - $string, - $databasePlatform->escapeStringForLike($string, $escapeChar), - $escapeChar - ), - $databasePlatform->getDummySelectSQL() - )); + $stmt = $this->_conn->prepare( + $databasePlatform->getDummySelectSQL( + sprintf( + "(CASE WHEN '%s' LIKE '%s' ESCAPE '%s' THEN 1 ELSE 0 END)", + $string, + $databasePlatform->escapeStringForLike($string, $escapeChar), + $escapeChar + ) + ) + ); $stmt->execute(); $this->assertTrue((bool) $stmt->fetchColumn()); }