2008-08-31 22:27:16 +04:00
|
|
|
<?php
|
2009-03-30 23:43:05 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
2008-08-31 22:27:16 +04:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Id;
|
|
|
|
|
2009-03-14 12:05:52 +03:00
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
/**
|
|
|
|
* Represents an ID generator that uses a database sequence.
|
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
|
|
|
class SequenceGenerator extends AbstractIdGenerator implements \Serializable
|
2008-09-07 17:48:40 +04:00
|
|
|
{
|
2009-03-14 12:05:52 +03:00
|
|
|
private $_allocationSize;
|
2008-09-07 17:48:40 +04:00
|
|
|
private $_sequenceName;
|
2009-03-14 12:05:52 +03:00
|
|
|
private $_nextValue = 0;
|
|
|
|
private $_maxValue = null;
|
2009-03-30 23:43:05 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new sequence generator.
|
|
|
|
*
|
|
|
|
* @param Doctrine\ORM\EntityManager $em The EntityManager to use.
|
|
|
|
* @param string $sequenceName The name of the sequence.
|
|
|
|
* @param integer $allocationSize The allocation size of the sequence.
|
|
|
|
*/
|
|
|
|
public function __construct($sequenceName, $allocationSize)
|
2008-09-07 17:48:40 +04:00
|
|
|
{
|
|
|
|
$this->_sequenceName = $sequenceName;
|
2009-03-14 12:05:52 +03:00
|
|
|
$this->_allocationSize = $allocationSize;
|
2008-09-07 17:48:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-03-14 12:05:52 +03:00
|
|
|
* Generates an ID for the given entity.
|
2008-09-07 17:48:40 +04:00
|
|
|
*
|
2009-03-14 12:05:52 +03:00
|
|
|
* @param object $entity
|
|
|
|
* @return integer|float The generated value.
|
2008-09-07 17:48:40 +04:00
|
|
|
* @override
|
|
|
|
*/
|
2009-03-30 23:43:05 +04:00
|
|
|
public function generate(EntityManager $em, $entity)
|
2008-09-07 17:48:40 +04:00
|
|
|
{
|
2009-03-14 12:05:52 +03:00
|
|
|
if ($this->_maxValue === null || $this->_nextValue == $this->_maxValue) {
|
|
|
|
// Allocate new values
|
2009-03-30 23:43:05 +04:00
|
|
|
$conn = $em->getConnection();
|
2009-03-14 12:05:52 +03:00
|
|
|
$sql = $conn->getDatabasePlatform()->getSequenceNextValSql($this->_sequenceName);
|
2009-11-08 14:07:49 +03:00
|
|
|
$this->_nextValue = $conn->fetchColumn($sql);
|
2009-05-05 21:20:55 +04:00
|
|
|
$this->_maxValue = $this->_nextValue + $this->_allocationSize;
|
2009-03-14 12:05:52 +03:00
|
|
|
}
|
|
|
|
return $this->_nextValue++;
|
|
|
|
}
|
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
/**
|
|
|
|
* Gets the maximum value of the currently allocated bag of values.
|
|
|
|
*
|
|
|
|
* @return integer|float
|
|
|
|
*/
|
2009-03-14 12:05:52 +03:00
|
|
|
public function getCurrentMaxValue()
|
|
|
|
{
|
|
|
|
return $this->_maxValue;
|
|
|
|
}
|
2009-03-30 23:43:05 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the next value that will be returned by generate().
|
|
|
|
*
|
|
|
|
* @return integer|float
|
|
|
|
*/
|
2009-03-14 12:05:52 +03:00
|
|
|
public function getNextValue()
|
|
|
|
{
|
|
|
|
return $this->_nextValue;
|
2008-09-07 17:48:40 +04:00
|
|
|
}
|
2009-03-30 23:43:05 +04:00
|
|
|
|
|
|
|
public function serialize()
|
|
|
|
{
|
|
|
|
return serialize(array(
|
|
|
|
'allocationSize' => $this->_allocationSize,
|
|
|
|
'sequenceName' => $this->_sequenceName
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unserialize($serialized)
|
|
|
|
{
|
|
|
|
$array = unserialize($serialized);
|
|
|
|
$this->_sequenceName = $array['sequenceName'];
|
|
|
|
$this->_allocationSize = $array['allocationSize'];
|
|
|
|
}
|
2009-03-14 12:05:52 +03:00
|
|
|
}
|