diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/orm/src/Id/SequenceGenerator.php | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/orm/src/Id/SequenceGenerator.php')
-rw-r--r-- | vendor/doctrine/orm/src/Id/SequenceGenerator.php | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Id/SequenceGenerator.php b/vendor/doctrine/orm/src/Id/SequenceGenerator.php new file mode 100644 index 0000000..659bb58 --- /dev/null +++ b/vendor/doctrine/orm/src/Id/SequenceGenerator.php | |||
@@ -0,0 +1,112 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Id; | ||
6 | |||
7 | use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; | ||
8 | use Doctrine\Deprecations\Deprecation; | ||
9 | use Doctrine\ORM\EntityManagerInterface; | ||
10 | use Serializable; | ||
11 | |||
12 | use function serialize; | ||
13 | use function unserialize; | ||
14 | |||
15 | /** | ||
16 | * Represents an ID generator that uses a database sequence. | ||
17 | */ | ||
18 | class SequenceGenerator extends AbstractIdGenerator implements Serializable | ||
19 | { | ||
20 | private int $nextValue = 0; | ||
21 | private int|null $maxValue = null; | ||
22 | |||
23 | /** | ||
24 | * Initializes a new sequence generator. | ||
25 | * | ||
26 | * @param string $sequenceName The name of the sequence. | ||
27 | * @param int $allocationSize The allocation size of the sequence. | ||
28 | */ | ||
29 | public function __construct( | ||
30 | private string $sequenceName, | ||
31 | private int $allocationSize, | ||
32 | ) { | ||
33 | } | ||
34 | |||
35 | public function generateId(EntityManagerInterface $em, object|null $entity): int | ||
36 | { | ||
37 | if ($this->maxValue === null || $this->nextValue === $this->maxValue) { | ||
38 | // Allocate new values | ||
39 | $connection = $em->getConnection(); | ||
40 | $sql = $connection->getDatabasePlatform()->getSequenceNextValSQL($this->sequenceName); | ||
41 | |||
42 | if ($connection instanceof PrimaryReadReplicaConnection) { | ||
43 | $connection->ensureConnectedToPrimary(); | ||
44 | } | ||
45 | |||
46 | $this->nextValue = (int) $connection->fetchOne($sql); | ||
47 | $this->maxValue = $this->nextValue + $this->allocationSize; | ||
48 | } | ||
49 | |||
50 | return $this->nextValue++; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Gets the maximum value of the currently allocated bag of values. | ||
55 | */ | ||
56 | public function getCurrentMaxValue(): int|null | ||
57 | { | ||
58 | return $this->maxValue; | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * Gets the next value that will be returned by generate(). | ||
63 | */ | ||
64 | public function getNextValue(): int | ||
65 | { | ||
66 | return $this->nextValue; | ||
67 | } | ||
68 | |||
69 | /** @deprecated without replacement. */ | ||
70 | final public function serialize(): string | ||
71 | { | ||
72 | Deprecation::trigger( | ||
73 | 'doctrine/orm', | ||
74 | 'https://github.com/doctrine/orm/pull/11468', | ||
75 | '%s() is deprecated, use __serialize() instead. %s won\'t implement the Serializable interface anymore in ORM 4.', | ||
76 | __METHOD__, | ||
77 | self::class, | ||
78 | ); | ||
79 | |||
80 | return serialize($this->__serialize()); | ||
81 | } | ||
82 | |||
83 | /** @return array<string, mixed> */ | ||
84 | public function __serialize(): array | ||
85 | { | ||
86 | return [ | ||
87 | 'allocationSize' => $this->allocationSize, | ||
88 | 'sequenceName' => $this->sequenceName, | ||
89 | ]; | ||
90 | } | ||
91 | |||
92 | /** @deprecated without replacement. */ | ||
93 | final public function unserialize(string $serialized): void | ||
94 | { | ||
95 | Deprecation::trigger( | ||
96 | 'doctrine/orm', | ||
97 | 'https://github.com/doctrine/orm/pull/11468', | ||
98 | '%s() is deprecated, use __unserialize() instead. %s won\'t implement the Serializable interface anymore in ORM 4.', | ||
99 | __METHOD__, | ||
100 | self::class, | ||
101 | ); | ||
102 | |||
103 | $this->__unserialize(unserialize($serialized)); | ||
104 | } | ||
105 | |||
106 | /** @param array<string, mixed> $data */ | ||
107 | public function __unserialize(array $data): void | ||
108 | { | ||
109 | $this->sequenceName = $data['sequenceName']; | ||
110 | $this->allocationSize = $data['allocationSize']; | ||
111 | } | ||
112 | } | ||