Skip to content

Commit

Permalink
update doctrine version
Browse files Browse the repository at this point in the history
  • Loading branch information
Webonaute committed Apr 30, 2021
1 parent 9df84e9 commit d177f56
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 59 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
113 changes: 55 additions & 58 deletions src/Service/DataLocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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;
}
}

0 comments on commit d177f56

Please sign in to comment.