diff --git a/composer.json b/composer.json index d8c1448..d9f30bb 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,8 @@ "symfony/config": "^3.4|^4.1|^5.0", "symfony/console": "^3.4|^4.1|^5.0", "symfony/dependency-injection": "^3.4|^4.1|^5.0", - "doctrine/doctrine-bundle": "^1.8|^2.0" + "doctrine/doctrine-bundle": "^1.8|^2.0", + "doctrine/orm": "^2.8" }, "require-dev": { "symfony/monolog-bundle": "^3.0", diff --git a/src/Service/DataLocker.php b/src/Service/DataLocker.php index e2b04d7..9a2f2c8 100755 --- a/src/Service/DataLocker.php +++ b/src/Service/DataLocker.php @@ -5,15 +5,12 @@ namespace Webonaute\DoctrineDataLockingBundle\Service; use DateTimeImmutable; -use DateTimeInterface; use DateTimeZone; -use Doctrine\Common\Persistence\ManagerRegistry; -use Doctrine\ORM\EntityManager; -use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Mapping\ClassMetadataInfo; +use Doctrine\Persistence\ManagerRegistry; +use Doctrine\Persistence\Mapping\ClassMetadata; +use Doctrine\Persistence\ObjectManager; use Psr\Log\LoggerInterface; use function sprintf; -use Symfony\Bridge\Doctrine\RegistryInterface; /** * Class DataLocker. @@ -30,7 +27,7 @@ class DataLocker */ private $logger; - public function __construct(RegistryInterface $doctrine, LoggerInterface $logger) + public function __construct(ManagerRegistry $doctrine, LoggerInterface $logger) { $this->doctrine = $doctrine; $this->logger = $logger; @@ -64,6 +61,29 @@ public function customLock($entityClass, $sqlQuery, $lockId, $parameters = []): return $lockedEntities; } + public function executeLock(string $entityClass, string $sqlQuery, array $parameters): int + { + /** @var ObjectManager $manager */ + $manager = $this->doctrine + ->getManagerForClass($entityClass); + $connection = $manager->getConnection(); + + return $connection->executeUpdate($sqlQuery, $parameters); + } + + public function findLocked(string $entityClass, string $lockId) + { + /** @var ObjectManager $manager */ + $manager = $this->doctrine->getManagerForClass($entityClass); + + return $manager->createQueryBuilder() + ->select('e') + ->from($entityClass, 'e') + ->where('e.processLock.lockId = :lockId')->setParameter('lockId', $lockId) + ->getQuery() + ->execute(); + } + /** * @param string $entityClass * @param int $limit @@ -83,19 +103,6 @@ public function lockAndSelect(string $entityClass, int $limit = 50, ?string $ext return $lockedEntities; } - public function findLocked(string $entityClass, string $lockId) - { - /** @var EntityManager $manager */ - $manager = $this->doctrine->getManagerForClass($entityClass); - - return $manager->createQueryBuilder() - ->select('e') - ->from($entityClass, 'e') - ->where('e.processLock.lockId = :lockId')->setParameter('lockId', $lockId) - ->getQuery() - ->execute(); - } - /** * Locks requested number of entries and returns lock id. * @@ -131,9 +138,35 @@ public function lock(string $entityClass, int $limit = 50, ?string $extraWhere = return null; } + protected function createLockQuery(string $entityClass, int $limit, ?string $extraWhere, bool $lockAtCondition = false): string + { + $manager = $this->doctrine->getManagerForClass($entityClass); + /** @var $classMetadata ClassMetadata */ + $classMetadata = $manager->getClassMetadata($entityClass); + + if (true === $lockAtCondition) { + $extraWhere = "{$classMetadata->getColumnName('processLock.lockingAt')} <= ?" + . ($extraWhere ? " AND ({$extraWhere})" : ''); + } + + $sql = sprintf( + 'UPDATE %1$s SET %2$s = ?, %3$s = NOW() WHERE %4$s IS NULL %5$s', + $classMetadata->getTableName(), + $classMetadata->getColumnName('processLock.lockId'), + $classMetadata->getColumnName('processLock.lockedAt'), + $classMetadata->getColumnName('processLock.lockId'), + $extraWhere ? 'AND (' . $extraWhere . ')' : '', + ); + if ($limit > 0) { + $sql .= " LIMIT {$limit}"; + } + + return $sql; + } + public function unlock(string $entityClass, string $lockId) { - /** @var EntityManagerInterface $manager */ + /** @var ObjectManager $manager */ $manager = $this->doctrine ->getManagerForClass($entityClass); @@ -148,7 +181,7 @@ public function unlock(string $entityClass, string $lockId) public function deleteLocked(string $entityClass, string $lockId) { - /** @var EntityManagerInterface $manager */ + /** @var ObjectManager $manager */ $manager = $this->doctrine ->getManagerForClass($entityClass); @@ -157,40 +190,4 @@ public function deleteLocked(string $entityClass, string $lockId) ->getQuery() ->execute(); } - - public function executeLock(string $entityClass, string $sqlQuery, array $parameters): int - { - /** @var EntityManagerInterface $manager */ - $manager = $this->doctrine - ->getManagerForClass($entityClass); - $connection = $manager->getConnection(); - - return $connection->executeUpdate($sqlQuery, $parameters); - } - - protected function createLockQuery(string $entityClass, int $limit, ?string $extraWhere, bool $lockAtCondition = false): string - { - $manager = $this->doctrine->getManagerForClass($entityClass); - /** @var $classMetadata ClassMetadataInfo */ - $classMetadata = $manager->getClassMetadata($entityClass); - - if (true === $lockAtCondition) { - $extraWhere = "{$classMetadata->getColumnName('processLock.lockingAt')} <= ?" - .($extraWhere ? " AND ({$extraWhere})" : ''); - } - - $sql = sprintf( - 'UPDATE %1$s SET %2$s = ?, %3$s = NOW() WHERE %4$s IS NULL %5$s', - $classMetadata->getTableName(), - $classMetadata->getColumnName('processLock.lockId'), - $classMetadata->getColumnName('processLock.lockedAt'), - $classMetadata->getColumnName('processLock.lockId'), - $extraWhere ? 'AND (' . $extraWhere . ')' : '', - ); - if ($limit > 0) { - $sql .= " LIMIT {$limit}"; - } - - return $sql; - } }