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.
parent 0f23ed9b
...@@ -49,7 +49,9 @@ use function array_unique; ...@@ -49,7 +49,9 @@ use function array_unique;
use function array_values; use function array_values;
use function count; use function count;
use function explode; use function explode;
use function func_get_arg;
use function func_get_args; use function func_get_args;
use function func_num_args;
use function get_class; use function get_class;
use function implode; use function implode;
use function in_array; use function in_array;
...@@ -3499,7 +3501,9 @@ abstract class AbstractPlatform ...@@ -3499,7 +3501,9 @@ abstract class AbstractPlatform
*/ */
public function getDummySelectSQL() public function getDummySelectSQL()
{ {
return 'SELECT 1'; $expression = func_num_args() > 0 ? func_get_arg(0) : '1';
return sprintf('SELECT %s', $expression);
} }
/** /**
......
...@@ -30,6 +30,8 @@ use function array_merge; ...@@ -30,6 +30,8 @@ use function array_merge;
use function count; use function count;
use function current; use function current;
use function explode; use function explode;
use function func_get_arg;
use function func_num_args;
use function implode; use function implode;
use function sprintf; use function sprintf;
use function strpos; use function strpos;
...@@ -863,7 +865,9 @@ class DB2Platform extends AbstractPlatform ...@@ -863,7 +865,9 @@ class DB2Platform extends AbstractPlatform
*/ */
public function getDummySelectSQL() 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);
} }
/** /**
......
...@@ -31,6 +31,8 @@ use Doctrine\DBAL\Types\BinaryType; ...@@ -31,6 +31,8 @@ use Doctrine\DBAL\Types\BinaryType;
use function array_merge; use function array_merge;
use function count; use function count;
use function explode; use function explode;
use function func_get_arg;
use function func_num_args;
use function implode; use function implode;
use function preg_match; use function preg_match;
use function sprintf; use function sprintf;
...@@ -1120,7 +1122,9 @@ END;'; ...@@ -1120,7 +1122,9 @@ END;';
*/ */
public function getDummySelectSQL() 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);
} }
/** /**
......
...@@ -13,16 +13,16 @@ final class LikeWildcardsEscapingTest extends DbalFunctionalTestCase ...@@ -13,16 +13,16 @@ final class LikeWildcardsEscapingTest extends DbalFunctionalTestCase
$string = '_25% off_ your next purchase \o/ [$̲̅(̲̅5̲̅)̲̅$̲̅] (^̮^)'; $string = '_25% off_ your next purchase \o/ [$̲̅(̲̅5̲̅)̲̅$̲̅] (^̮^)';
$escapeChar = '!'; $escapeChar = '!';
$databasePlatform = $this->_conn->getDatabasePlatform(); $databasePlatform = $this->_conn->getDatabasePlatform();
$stmt = $this->_conn->prepare(str_replace( $stmt = $this->_conn->prepare(
'1', $databasePlatform->getDummySelectSQL(
sprintf( sprintf(
"(CASE WHEN '%s' LIKE '%s' ESCAPE '%s' THEN 1 ELSE 0 END)", "(CASE WHEN '%s' LIKE '%s' ESCAPE '%s' THEN 1 ELSE 0 END)",
$string, $string,
$databasePlatform->escapeStringForLike($string, $escapeChar), $databasePlatform->escapeStringForLike($string, $escapeChar),
$escapeChar $escapeChar
), )
$databasePlatform->getDummySelectSQL() )
)); );
$stmt->execute(); $stmt->execute();
$this->assertTrue((bool) $stmt->fetchColumn()); $this->assertTrue((bool) $stmt->fetchColumn());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment