Skip to content

Commit 95bec67

Browse files
committed
Fix calls to removed lock methods
1 parent 21466a0 commit 95bec67

File tree

7 files changed

+67
-22
lines changed

7 files changed

+67
-22
lines changed

lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Doctrine\ORM\Repository\Exception\InvalidFindByCall;
3838
use Doctrine\ORM\UnitOfWork;
3939
use Doctrine\ORM\Utility\IdentifierFlattener;
40+
use Doctrine\ORM\Utility\LockSqlHelper;
4041
use Doctrine\ORM\Utility\PersisterHelper;
4142
use LengthException;
4243

@@ -96,6 +97,8 @@
9697
*/
9798
class BasicEntityPersister implements EntityPersister
9899
{
100+
use LockSqlHelper;
101+
99102
/** @var array<string,string> */
100103
private static array $comparisonMap = [
101104
Comparison::EQ => '= %s',
@@ -1074,8 +1077,8 @@ public function getSelectSQL(
10741077
: $this->getSelectConditionSQL($criteria, $assoc);
10751078

10761079
$lockSql = match ($lockMode) {
1077-
LockMode::PESSIMISTIC_READ => ' ' . $this->platform->getReadLockSQL(),
1078-
LockMode::PESSIMISTIC_WRITE => ' ' . $this->platform->getWriteLockSQL(),
1080+
LockMode::PESSIMISTIC_READ => ' ' . $this->getReadLockSQL($this->platform),
1081+
LockMode::PESSIMISTIC_WRITE => ' ' . $this->getWriteLockSQL($this->platform),
10791082
default => '',
10801083
};
10811084

@@ -1505,8 +1508,8 @@ public function lock(array $criteria, LockMode|int $lockMode): void
15051508
$conditionSql = $this->getSelectConditionSQL($criteria);
15061509

15071510
$lockSql = match ($lockMode) {
1508-
LockMode::PESSIMISTIC_READ => $this->platform->getReadLockSQL(),
1509-
LockMode::PESSIMISTIC_WRITE => $this->platform->getWriteLockSQL(),
1511+
LockMode::PESSIMISTIC_READ => $this->getReadLockSQL($this->platform),
1512+
LockMode::PESSIMISTIC_WRITE => $this->getWriteLockSQL($this->platform),
15101513
default => '',
15111514
};
15121515

lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Doctrine\ORM\Internal\SQLResultCasing;
1212
use Doctrine\ORM\Mapping\AssociationMapping;
1313
use Doctrine\ORM\Mapping\ClassMetadata;
14+
use Doctrine\ORM\Utility\LockSqlHelper;
1415
use Doctrine\ORM\Utility\PersisterHelper;
1516
use LengthException;
1617

@@ -27,6 +28,7 @@
2728
*/
2829
class JoinedSubclassPersister extends AbstractEntityInheritancePersister
2930
{
31+
use LockSqlHelper;
3032
use SQLResultCasing;
3133

3234
/**
@@ -279,12 +281,12 @@ public function getSelectSQL(
279281

280282
switch ($lockMode) {
281283
case LockMode::PESSIMISTIC_READ:
282-
$lockSql = ' ' . $this->platform->getReadLockSQL();
284+
$lockSql = ' ' . $this->getReadLockSQL($this->platform);
283285

284286
break;
285287

286288
case LockMode::PESSIMISTIC_WRITE:
287-
$lockSql = ' ' . $this->platform->getWriteLockSQL();
289+
$lockSql = ' ' . $this->getWriteLockSQL($this->platform);
288290

289291
break;
290292
}

lib/Doctrine/ORM/Query/SqlWalker.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\ORM\OptimisticLockException;
1616
use Doctrine\ORM\Query;
1717
use Doctrine\ORM\Utility\HierarchyDiscriminatorResolver;
18+
use Doctrine\ORM\Utility\LockSqlHelper;
1819
use Doctrine\ORM\Utility\PersisterHelper;
1920
use InvalidArgumentException;
2021
use LogicException;
@@ -46,6 +47,8 @@
4647
*/
4748
class SqlWalker
4849
{
50+
use LockSqlHelper;
51+
4952
public const HINT_DISTINCT = 'doctrine.distinct';
5053

5154
private readonly ResultSetMapping $rsm;
@@ -475,11 +478,11 @@ public function walkSelectStatement(AST\SelectStatement $selectStatement): strin
475478
}
476479

477480
if ($lockMode === LockMode::PESSIMISTIC_READ) {
478-
return $sql . ' ' . $this->platform->getReadLockSQL();
481+
return $sql . ' ' . $this->getReadLockSQL($this->platform);
479482
}
480483

481484
if ($lockMode === LockMode::PESSIMISTIC_WRITE) {
482-
return $sql . ' ' . $this->platform->getWriteLockSQL();
485+
return $sql . ' ' . $this->getWriteLockSQL($this->platform);
483486
}
484487

485488
if ($lockMode !== LockMode::OPTIMISTIC) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ORM\Utility;
6+
7+
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
9+
use Doctrine\DBAL\Platforms\DB2Platform;
10+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
11+
use Doctrine\DBAL\Platforms\SQLitePlatform;
12+
use Doctrine\DBAL\Platforms\SQLServerPlatform;
13+
14+
/** @internal */
15+
trait LockSqlHelper
16+
{
17+
private function getReadLockSQL(AbstractPlatform $platform): string
18+
{
19+
return match (true) {
20+
$platform instanceof AbstractMySQLPlatform => 'LOCK IN SHARE MODE',
21+
$platform instanceof PostgreSQLPlatform => 'FOR SHARE',
22+
default => $this->getWriteLockSQL($platform),
23+
};
24+
}
25+
26+
private function getWriteLockSQL(AbstractPlatform $platform): string
27+
{
28+
return match (true) {
29+
$platform instanceof DB2Platform => 'WITH RR USE AND KEEP UPDATE LOCKS',
30+
$platform instanceof SQLitePlatform,
31+
$platform instanceof SQLServerPlatform => '',
32+
default => 'FOR UPDATE',
33+
};
34+
}
35+
}

phpstan-dbal3.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ parameters:
2424
message: '~^Unreachable statement \- code above always terminates\.$~'
2525
path: lib/Doctrine/ORM/Mapping/AssociationMapping.php
2626

27+
- '~^Class Doctrine\\DBAL\\Platforms\\SQLitePlatform not found\.$~'
28+
2729
# To be removed in 4.0
2830
-
2931
message: '#Negated boolean expression is always false\.#'

psalm.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,13 @@
193193
<file name="lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php"/>
194194
<!-- DBAL 3 compatibility -->
195195
<file name="lib/Doctrine/ORM/UnitOfWork.php"/>
196+
<file name="lib/Doctrine/ORM/Utility/LockSqlHelper.php"/>
196197
</errorLevel>
197198
</TypeDoesNotContainType>
198199
<UndefinedClass>
199200
<errorLevel type="suppress">
200-
<!-- Persistence 2 compatibility -->
201-
<referencedClass name="Doctrine\Persistence\ObjectManagerAware"/>
201+
<!-- Compatibility with DBAL 3 -->
202+
<referencedClass name="Doctrine\DBAL\Platforms\SQLitePlatform"/>
202203
</errorLevel>
203204
</UndefinedClass>
204205
<UndefinedMethod>

tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\Tests\ORM\Functional\Locking;
66

77
use Doctrine\DBAL\LockMode;
8+
use Doctrine\DBAL\Platforms\SQLitePlatform;
89
use Doctrine\ORM\OptimisticLockException;
910
use Doctrine\ORM\Query;
1011
use Doctrine\ORM\TransactionRequiredException;
@@ -136,9 +137,7 @@ public function testRefreshWithLockPessimisticWriteNoTransactionThrowsException(
136137
#[Group('locking')]
137138
public function testLockPessimisticWrite(): void
138139
{
139-
$writeLockSql = $this->_em->getConnection()->getDatabasePlatform()->getWriteLockSQL();
140-
141-
if (! $writeLockSql) {
140+
if ($this->_em->getConnection()->getDatabasePlatform() instanceof SQLitePlatform) {
142141
self::markTestSkipped('Database Driver has no Write Lock support.');
143142
}
144143

@@ -163,15 +162,13 @@ public function testLockPessimisticWrite(): void
163162
$lastLoggedQuery = $this->getLastLoggedQuery(1)['sql'];
164163
}
165164

166-
self::assertStringContainsString($writeLockSql, $lastLoggedQuery);
165+
self::assertStringContainsString('FOR UPDATE', $lastLoggedQuery);
167166
}
168167

169168
#[Group('locking')]
170169
public function testRefreshWithLockPessimisticWrite(): void
171170
{
172-
$writeLockSql = $this->_em->getConnection()->getDatabasePlatform()->getWriteLockSQL();
173-
174-
if (! $writeLockSql) {
171+
if ($this->_em->getConnection()->getDatabasePlatform() instanceof SQLitePlatform) {
175172
self::markTestSkipped('Database Driver has no Write Lock support.');
176173
}
177174

@@ -196,15 +193,13 @@ public function testRefreshWithLockPessimisticWrite(): void
196193
$lastLoggedQuery = $this->getLastLoggedQuery(1)['sql'];
197194
}
198195

199-
self::assertStringContainsString($writeLockSql, $lastLoggedQuery);
196+
self::assertStringContainsString('FOR UPDATE', $lastLoggedQuery);
200197
}
201198

202199
#[Group('DDC-178')]
203200
public function testLockPessimisticRead(): void
204201
{
205-
$readLockSql = $this->_em->getConnection()->getDatabasePlatform()->getReadLockSQL();
206-
207-
if (! $readLockSql) {
202+
if ($this->_em->getConnection()->getDatabasePlatform() instanceof SQLitePlatform) {
208203
self::markTestSkipped('Database Driver has no Write Lock support.');
209204
}
210205

@@ -230,7 +225,11 @@ public function testLockPessimisticRead(): void
230225
$lastLoggedQuery = $this->getLastLoggedQuery(1)['sql'];
231226
}
232227

233-
self::assertStringContainsString($readLockSql, $lastLoggedQuery);
228+
self::assertThat($lastLoggedQuery, self::logicalOr(
229+
self::stringContains('FOR UPDATE'),
230+
self::stringContains('FOR SHARE'),
231+
self::stringContains('LOCK IN SHARE MODE'),
232+
));
234233
}
235234

236235
#[Group('DDC-1693')]

0 commit comments

Comments
 (0)