Skip to content

Commit 8c41da9

Browse files
committed
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 doctrine#3013, doctrine#3108). Instead of doing `str_replace()` on an expected string, one could specify the expression explicitly.
1 parent 0f23ed9 commit 8c41da9

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

lib/Doctrine/DBAL/Platforms/AbstractPlatform.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
use function array_values;
5050
use function count;
5151
use function explode;
52+
use function func_get_arg;
5253
use function func_get_args;
54+
use function func_num_args;
5355
use function get_class;
5456
use function implode;
5557
use function in_array;
@@ -3499,7 +3501,9 @@ public function getTruncateTableSQL($tableName, $cascade = false)
34993501
*/
35003502
public function getDummySelectSQL()
35013503
{
3502-
return 'SELECT 1';
3504+
$expression = func_num_args() > 0 ? func_get_arg(0) : '1';
3505+
3506+
return sprintf('SELECT %s', $expression);
35033507
}
35043508

35053509
/**

lib/Doctrine/DBAL/Platforms/DB2Platform.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
use function count;
3131
use function current;
3232
use function explode;
33+
use function func_get_arg;
34+
use function func_num_args;
3335
use function implode;
3436
use function sprintf;
3537
use function strpos;
@@ -863,7 +865,9 @@ public function getForUpdateSQL()
863865
*/
864866
public function getDummySelectSQL()
865867
{
866-
return 'SELECT 1 FROM sysibm.sysdummy1';
868+
$expression = func_num_args() > 0 ? func_get_arg(0) : '1';
869+
870+
return sprintf('SELECT %s FROM sysibm.sysdummy1', $expression);
867871
}
868872

869873
/**

lib/Doctrine/DBAL/Platforms/OraclePlatform.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use function array_merge;
3232
use function count;
3333
use function explode;
34+
use function func_get_arg;
35+
use function func_num_args;
3436
use function implode;
3537
use function preg_match;
3638
use function sprintf;
@@ -1120,7 +1122,9 @@ public function getTruncateTableSQL($tableName, $cascade = false)
11201122
*/
11211123
public function getDummySelectSQL()
11221124
{
1123-
return 'SELECT 1 FROM DUAL';
1125+
$expression = func_num_args() > 0 ? func_get_arg(0) : '1';
1126+
1127+
return sprintf('SELECT %s FROM DUAL', $expression);
11241128
}
11251129

11261130
/**

tests/Doctrine/Tests/DBAL/Functional/LikeWildcardsEscapingTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ public function testFetchLikeExpressionResult() : void
1313
$string = '_25% off_ your next purchase \o/ [$̲̅(̲̅5̲̅)̲̅$̲̅] (^̮^)';
1414
$escapeChar = '!';
1515
$databasePlatform = $this->_conn->getDatabasePlatform();
16-
$stmt = $this->_conn->prepare(str_replace(
17-
'1',
18-
sprintf(
19-
"(CASE WHEN '%s' LIKE '%s' ESCAPE '%s' THEN 1 ELSE 0 END)",
20-
$string,
21-
$databasePlatform->escapeStringForLike($string, $escapeChar),
22-
$escapeChar
23-
),
24-
$databasePlatform->getDummySelectSQL()
25-
));
16+
$stmt = $this->_conn->prepare(
17+
$databasePlatform->getDummySelectSQL(
18+
sprintf(
19+
"(CASE WHEN '%s' LIKE '%s' ESCAPE '%s' THEN 1 ELSE 0 END)",
20+
$string,
21+
$databasePlatform->escapeStringForLike($string, $escapeChar),
22+
$escapeChar
23+
)
24+
)
25+
);
2626
$stmt->execute();
2727
$this->assertTrue((bool) $stmt->fetchColumn());
2828
}

0 commit comments

Comments
 (0)