Skip to content

Commit

Permalink
Allow to specify arbitrary SQL expression in AbstractPlatform::getDum…
Browse files Browse the repository at this point in the history
…mySelectSQL()

For testing purposes and not only, it may be needed to evaluate an arbitrary SQL expression (see #3013, #3108). Instead of doing `str_replace()` on an expected string, one could specify the expression explicitly.
  • Loading branch information
morozov committed Apr 29, 2018
1 parent 0f23ed9 commit 8c41da9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
6 changes: 5 additions & 1 deletion lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion lib/Doctrine/DBAL/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions tests/Doctrine/Tests/DBAL/Functional/LikeWildcardsEscapingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 8c41da9

Please sign in to comment.