From fca96ac15e53a9e208215ce7c2ac4971d10ff1cb Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 6 Nov 2023 22:39:41 +0100 Subject: [PATCH] fix compatibility with Doctrine DBAL 4 --- .../Tests/Transport/ConnectionTest.php | 13 ++++++-- .../Bridge/Doctrine/Transport/Connection.php | 30 ++++++++++++++----- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/ConnectionTest.php b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/ConnectionTest.php index a85c2fab85736..51f6963a318b3 100644 --- a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/ConnectionTest.php +++ b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/ConnectionTest.php @@ -82,6 +82,9 @@ public function testGetWithNoPendingMessageWillReturnNull() $queryBuilder ->method('getParameterTypes') ->willReturn([]); + $queryBuilder + ->method('getSQL') + ->willReturn('SELECT FOR UPDATE'); $driverConnection->expects($this->once()) ->method('createQueryBuilder') ->willReturn($queryBuilder); @@ -120,7 +123,11 @@ private function getDBALConnectionMock() { $driverConnection = $this->createMock(DBALConnection::class); $platform = $this->createMock(AbstractPlatform::class); - $platform->method('getWriteLockSQL')->willReturn('FOR UPDATE'); + + if (!method_exists(QueryBuilder::class, 'forUpdate')) { + $platform->method('getWriteLockSQL')->willReturn('FOR UPDATE'); + } + $configuration = $this->createMock(\Doctrine\DBAL\Configuration::class); $driverConnection->method('getDatabasePlatform')->willReturn($platform); $driverConnection->method('getConfiguration')->willReturn($configuration); @@ -381,7 +388,9 @@ public function testGeneratedSql(AbstractPlatform $platform, string $expectedSql $driverConnection ->expects($this->once()) ->method('executeQuery') - ->with($expectedSql) + ->with($this->callback(function ($sql) use ($expectedSql) { + return trim($expectedSql) === trim($sql); + })) ->willReturn($result) ; $driverConnection->expects($this->once())->method('commit'); diff --git a/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php b/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php index b3981e8a63461..8b348e708d128 100644 --- a/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php +++ b/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php @@ -177,7 +177,24 @@ public function get(): ?array // Append pessimistic write lock to FROM clause if db platform supports it $sql = $query->getSQL(); - if (preg_match('/FROM (.+) WHERE/', (string) $sql, $matches)) { + + // Wrap the rownum query in a sub-query to allow writelocks without ORA-02014 error + if ($this->driverConnection->getDatabasePlatform() instanceof OraclePlatform) { + $query = $this->createQueryBuilder('w') + ->where('w.id IN ('.str_replace('SELECT a.* FROM', 'SELECT a.id FROM', $sql).')'); + + if (method_exists(QueryBuilder::class, 'forUpdate')) { + $query->forUpdate(); + } + + $sql = $query->getSQL(); + } elseif (method_exists(QueryBuilder::class, 'forUpdate')) { + $query->forUpdate(); + try { + $sql = $query->getSQL(); + } catch (DBALException $e) { + } + } elseif (preg_match('/FROM (.+) WHERE/', (string) $sql, $matches)) { $fromClause = $matches[1]; $sql = str_replace( sprintf('FROM %s WHERE', $fromClause), @@ -186,16 +203,13 @@ public function get(): ?array ); } - // Wrap the rownum query in a sub-query to allow writelocks without ORA-02014 error - if ($this->driverConnection->getDatabasePlatform() instanceof OraclePlatform) { - $sql = $this->createQueryBuilder('w') - ->where('w.id IN ('.str_replace('SELECT a.* FROM', 'SELECT a.id FROM', $sql).')') - ->getSQL(); + // use SELECT ... FOR UPDATE to lock table + if (!method_exists(QueryBuilder::class, 'forUpdate')) { + $sql .= ' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(); } - // use SELECT ... FOR UPDATE to lock table $stmt = $this->executeQuery( - $sql.' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(), + $sql, $query->getParameters(), $query->getParameterTypes() );