Skip to content

Commit 77e1f7b

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 77e1f7b

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

lib/Doctrine/DBAL/Platforms/AbstractPlatform.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3497,9 +3497,9 @@ public function getTruncateTableSQL($tableName, $cascade = false)
34973497
*
34983498
* @return string
34993499
*/
3500-
public function getDummySelectSQL()
3500+
public function getDummySelectSQL(string $expression = '1')
35013501
{
3502-
return 'SELECT 1';
3502+
return sprintf('SELECT %s', $expression);
35033503
}
35043504

35053505
/**

lib/Doctrine/DBAL/Platforms/DB2Platform.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,9 +861,9 @@ public function getForUpdateSQL()
861861
/**
862862
* {@inheritDoc}
863863
*/
864-
public function getDummySelectSQL()
864+
public function getDummySelectSQL(string $expression = '1')
865865
{
866-
return 'SELECT 1 FROM sysibm.sysdummy1';
866+
return sprintf('SELECT %s FROM sysibm.sysdummy1', $expression);
867867
}
868868

869869
/**

lib/Doctrine/DBAL/Platforms/OraclePlatform.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,9 +1118,9 @@ public function getTruncateTableSQL($tableName, $cascade = false)
11181118
/**
11191119
* {@inheritDoc}
11201120
*/
1121-
public function getDummySelectSQL()
1121+
public function getDummySelectSQL(string $expression = '1')
11221122
{
1123-
return 'SELECT 1 FROM DUAL';
1123+
return sprintf('SELECT %s FROM DUAL', $expression);
11241124
}
11251125

11261126
/**

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)