|
0
|
1 |
<?php |
|
|
2 |
/* |
|
|
3 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
4 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
5 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
6 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
7 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
8 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
9 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
10 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
11 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
12 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
13 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
14 |
* |
|
|
15 |
* This software consists of voluntary contributions made by many individuals |
|
|
16 |
* and is licensed under the LGPL. For more information, see |
|
|
17 |
* <http://www.doctrine-project.org>. |
|
|
18 |
*/ |
|
|
19 |
|
|
|
20 |
namespace Doctrine\ORM\Id; |
|
|
21 |
|
|
|
22 |
use Serializable, Doctrine\ORM\EntityManager; |
|
|
23 |
|
|
|
24 |
/** |
|
|
25 |
* Represents an ID generator that uses a database sequence. |
|
|
26 |
* |
|
|
27 |
* @since 2.0 |
|
|
28 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
29 |
*/ |
|
|
30 |
class SequenceGenerator extends AbstractIdGenerator implements Serializable |
|
|
31 |
{ |
|
|
32 |
private $_allocationSize; |
|
|
33 |
private $_sequenceName; |
|
|
34 |
private $_nextValue = 0; |
|
|
35 |
private $_maxValue = null; |
|
|
36 |
|
|
|
37 |
/** |
|
|
38 |
* Initializes a new sequence generator. |
|
|
39 |
* |
|
|
40 |
* @param Doctrine\ORM\EntityManager $em The EntityManager to use. |
|
|
41 |
* @param string $sequenceName The name of the sequence. |
|
|
42 |
* @param integer $allocationSize The allocation size of the sequence. |
|
|
43 |
*/ |
|
|
44 |
public function __construct($sequenceName, $allocationSize) |
|
|
45 |
{ |
|
|
46 |
$this->_sequenceName = $sequenceName; |
|
|
47 |
$this->_allocationSize = $allocationSize; |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
/** |
|
|
51 |
* Generates an ID for the given entity. |
|
|
52 |
* |
|
|
53 |
* @param object $entity |
|
|
54 |
* @return integer|float The generated value. |
|
|
55 |
* @override |
|
|
56 |
*/ |
|
|
57 |
public function generate(EntityManager $em, $entity) |
|
|
58 |
{ |
|
|
59 |
if ($this->_maxValue === null || $this->_nextValue == $this->_maxValue) { |
|
|
60 |
// Allocate new values |
|
|
61 |
$conn = $em->getConnection(); |
|
|
62 |
$sql = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName); |
|
|
63 |
$this->_nextValue = $conn->fetchColumn($sql); |
|
|
64 |
$this->_maxValue = $this->_nextValue + $this->_allocationSize; |
|
|
65 |
} |
|
|
66 |
return $this->_nextValue++; |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
/** |
|
|
70 |
* Gets the maximum value of the currently allocated bag of values. |
|
|
71 |
* |
|
|
72 |
* @return integer|float |
|
|
73 |
*/ |
|
|
74 |
public function getCurrentMaxValue() |
|
|
75 |
{ |
|
|
76 |
return $this->_maxValue; |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
/** |
|
|
80 |
* Gets the next value that will be returned by generate(). |
|
|
81 |
* |
|
|
82 |
* @return integer|float |
|
|
83 |
*/ |
|
|
84 |
public function getNextValue() |
|
|
85 |
{ |
|
|
86 |
return $this->_nextValue; |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
public function serialize() |
|
|
90 |
{ |
|
|
91 |
return serialize(array( |
|
|
92 |
'allocationSize' => $this->_allocationSize, |
|
|
93 |
'sequenceName' => $this->_sequenceName |
|
|
94 |
)); |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
public function unserialize($serialized) |
|
|
98 |
{ |
|
|
99 |
$array = unserialize($serialized); |
|
|
100 |
$this->_sequenceName = $array['sequenceName']; |
|
|
101 |
$this->_allocationSize = $array['allocationSize']; |
|
|
102 |
} |
|
|
103 |
} |