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
SequenceGenerator.php
61 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Id; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; |
| 6 | use MailPoetVendor\Doctrine\ORM\EntityManagerInterface; |
| 7 | use Serializable; |
| 8 | use function serialize; |
| 9 | use function unserialize; |
| 10 | class SequenceGenerator extends AbstractIdGenerator implements Serializable |
| 11 | { |
| 12 | private $allocationSize; |
| 13 | private $sequenceName; |
| 14 | private $nextValue = 0; |
| 15 | private $maxValue = null; |
| 16 | public function __construct($sequenceName, $allocationSize) |
| 17 | { |
| 18 | $this->sequenceName = $sequenceName; |
| 19 | $this->allocationSize = $allocationSize; |
| 20 | } |
| 21 | public function generateId(EntityManagerInterface $em, $entity) |
| 22 | { |
| 23 | if ($this->maxValue === null || $this->nextValue === $this->maxValue) { |
| 24 | // Allocate new values |
| 25 | $connection = $em->getConnection(); |
| 26 | $sql = $connection->getDatabasePlatform()->getSequenceNextValSQL($this->sequenceName); |
| 27 | if ($connection instanceof PrimaryReadReplicaConnection) { |
| 28 | $connection->ensureConnectedToPrimary(); |
| 29 | } |
| 30 | $this->nextValue = (int) $connection->fetchOne($sql); |
| 31 | $this->maxValue = $this->nextValue + $this->allocationSize; |
| 32 | } |
| 33 | return $this->nextValue++; |
| 34 | } |
| 35 | public function getCurrentMaxValue() |
| 36 | { |
| 37 | return $this->maxValue; |
| 38 | } |
| 39 | public function getNextValue() |
| 40 | { |
| 41 | return $this->nextValue; |
| 42 | } |
| 43 | public function serialize() |
| 44 | { |
| 45 | return serialize($this->__serialize()); |
| 46 | } |
| 47 | public function __serialize() : array |
| 48 | { |
| 49 | return ['allocationSize' => $this->allocationSize, 'sequenceName' => $this->sequenceName]; |
| 50 | } |
| 51 | public function unserialize($serialized) |
| 52 | { |
| 53 | $this->__unserialize(unserialize($serialized)); |
| 54 | } |
| 55 | public function __unserialize(array $data) : void |
| 56 | { |
| 57 | $this->sequenceName = $data['sequenceName']; |
| 58 | $this->allocationSize = $data['allocationSize']; |
| 59 | } |
| 60 | } |
| 61 |