Skip to content

Commit a454d0c

Browse files
bug symfony#52476 [Messenger] fix compatibility with Doctrine DBAL 4 (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [Messenger] fix compatibility with Doctrine DBAL 4 | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT Commits ------- fca96ac fix compatibility with Doctrine DBAL 4
2 parents 03ef859 + fca96ac commit a454d0c

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/ConnectionTest.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public function testGetWithNoPendingMessageWillReturnNull()
8282
$queryBuilder
8383
->method('getParameterTypes')
8484
->willReturn([]);
85+
$queryBuilder
86+
->method('getSQL')
87+
->willReturn('SELECT FOR UPDATE');
8588
$driverConnection->expects($this->once())
8689
->method('createQueryBuilder')
8790
->willReturn($queryBuilder);
@@ -120,7 +123,11 @@ private function getDBALConnectionMock()
120123
{
121124
$driverConnection = $this->createMock(DBALConnection::class);
122125
$platform = $this->createMock(AbstractPlatform::class);
123-
$platform->method('getWriteLockSQL')->willReturn('FOR UPDATE');
126+
127+
if (!method_exists(QueryBuilder::class, 'forUpdate')) {
128+
$platform->method('getWriteLockSQL')->willReturn('FOR UPDATE');
129+
}
130+
124131
$configuration = $this->createMock(\Doctrine\DBAL\Configuration::class);
125132
$driverConnection->method('getDatabasePlatform')->willReturn($platform);
126133
$driverConnection->method('getConfiguration')->willReturn($configuration);
@@ -381,7 +388,9 @@ public function testGeneratedSql(AbstractPlatform $platform, string $expectedSql
381388
$driverConnection
382389
->expects($this->once())
383390
->method('executeQuery')
384-
->with($expectedSql)
391+
->with($this->callback(function ($sql) use ($expectedSql) {
392+
return trim($expectedSql) === trim($sql);
393+
}))
385394
->willReturn($result)
386395
;
387396
$driverConnection->expects($this->once())->method('commit');

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php

+22-8
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,24 @@ public function get(): ?array
177177

178178
// Append pessimistic write lock to FROM clause if db platform supports it
179179
$sql = $query->getSQL();
180-
if (preg_match('/FROM (.+) WHERE/', (string) $sql, $matches)) {
180+
181+
// Wrap the rownum query in a sub-query to allow writelocks without ORA-02014 error
182+
if ($this->driverConnection->getDatabasePlatform() instanceof OraclePlatform) {
183+
$query = $this->createQueryBuilder('w')
184+
->where('w.id IN ('.str_replace('SELECT a.* FROM', 'SELECT a.id FROM', $sql).')');
185+
186+
if (method_exists(QueryBuilder::class, 'forUpdate')) {
187+
$query->forUpdate();
188+
}
189+
190+
$sql = $query->getSQL();
191+
} elseif (method_exists(QueryBuilder::class, 'forUpdate')) {
192+
$query->forUpdate();
193+
try {
194+
$sql = $query->getSQL();
195+
} catch (DBALException $e) {
196+
}
197+
} elseif (preg_match('/FROM (.+) WHERE/', (string) $sql, $matches)) {
181198
$fromClause = $matches[1];
182199
$sql = str_replace(
183200
sprintf('FROM %s WHERE', $fromClause),
@@ -186,16 +203,13 @@ public function get(): ?array
186203
);
187204
}
188205

189-
// Wrap the rownum query in a sub-query to allow writelocks without ORA-02014 error
190-
if ($this->driverConnection->getDatabasePlatform() instanceof OraclePlatform) {
191-
$sql = $this->createQueryBuilder('w')
192-
->where('w.id IN ('.str_replace('SELECT a.* FROM', 'SELECT a.id FROM', $sql).')')
193-
->getSQL();
206+
// use SELECT ... FOR UPDATE to lock table
207+
if (!method_exists(QueryBuilder::class, 'forUpdate')) {
208+
$sql .= ' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL();
194209
}
195210

196-
// use SELECT ... FOR UPDATE to lock table
197211
$stmt = $this->executeQuery(
198-
$sql.' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(),
212+
$sql,
199213
$query->getParameters(),
200214
$query->getParameterTypes()
201215
);

0 commit comments

Comments
 (0)