AbstractIdGenerator.php
9 months ago
AssignedGenerator.php
9 months ago
BigIntegerIdentityGenerator.php
9 months ago
IdentityGenerator.php
9 months ago
SequenceGenerator.php
9 months ago
TableGenerator.php
9 months ago
UuidGenerator.php
9 months ago
index.php
9 months ago
TableGenerator.php
46 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Id; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\ORM\EntityManagerInterface; |
| 6 | class TableGenerator extends AbstractIdGenerator |
| 7 | { |
| 8 | private $tableName; |
| 9 | private $sequenceName; |
| 10 | private $allocationSize; |
| 11 | private $nextValue; |
| 12 | private $maxValue; |
| 13 | public function __construct($tableName, $sequenceName = 'default', $allocationSize = 10) |
| 14 | { |
| 15 | $this->tableName = $tableName; |
| 16 | $this->sequenceName = $sequenceName; |
| 17 | $this->allocationSize = $allocationSize; |
| 18 | } |
| 19 | public function generateId(EntityManagerInterface $em, $entity) |
| 20 | { |
| 21 | if ($this->maxValue === null || $this->nextValue === $this->maxValue) { |
| 22 | // Allocate new values |
| 23 | $conn = $em->getConnection(); |
| 24 | if ($conn->getTransactionNestingLevel() === 0) { |
| 25 | // use select for update |
| 26 | $sql = $conn->getDatabasePlatform()->getTableHiLoCurrentValSql($this->tableName, $this->sequenceName); |
| 27 | $currentLevel = $conn->fetchOne($sql); |
| 28 | if ($currentLevel !== null) { |
| 29 | $this->nextValue = $currentLevel; |
| 30 | $this->maxValue = $this->nextValue + $this->allocationSize; |
| 31 | $updateSql = $conn->getDatabasePlatform()->getTableHiLoUpdateNextValSql($this->tableName, $this->sequenceName, $this->allocationSize); |
| 32 | if ($conn->executeStatement($updateSql, [1 => $currentLevel, 2 => $currentLevel + 1]) !== 1) { |
| 33 | // no affected rows, concurrency issue, throw exception |
| 34 | } |
| 35 | } else { |
| 36 | // no current level returned, TableGenerator seems to be broken, throw exception |
| 37 | } |
| 38 | } else { |
| 39 | // only table locks help here, implement this or throw exception? |
| 40 | // or do we want to work with table locks exclusively? |
| 41 | } |
| 42 | } |
| 43 | return $this->nextValue++; |
| 44 | } |
| 45 | } |
| 46 |