SequenceGenerator.php 3.25 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  $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>.
 */
21

22 23
namespace Doctrine\ORM\Id;

24 25
use Doctrine\ORM\EntityManager;

26 27 28 29 30 31 32
/**
 * 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
romanb's avatar
romanb committed
33
{
34
    private $_allocationSize;
romanb's avatar
romanb committed
35
    private $_sequenceName;
36 37
    private $_nextValue = 0;
    private $_maxValue = null;
38 39 40 41 42 43 44 45 46

    /**
     * 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)
romanb's avatar
romanb committed
47 48
    {
        $this->_sequenceName = $sequenceName;
49
        $this->_allocationSize = $allocationSize;
romanb's avatar
romanb committed
50 51 52
    }
    
    /**
53
     * Generates an ID for the given entity.
romanb's avatar
romanb committed
54
     *
55 56
     * @param object $entity
     * @return integer|float The generated value.
romanb's avatar
romanb committed
57 58
     * @override
     */
59
    public function generate(EntityManager $em, $entity)
romanb's avatar
romanb committed
60
    {
61 62
        if ($this->_maxValue === null || $this->_nextValue == $this->_maxValue) {
            // Allocate new values
63
            $conn = $em->getConnection();
64
            $sql = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName);
65
            $this->_nextValue = $conn->fetchColumn($sql);
66
            $this->_maxValue = $this->_nextValue + $this->_allocationSize;
67 68 69 70
        }
        return $this->_nextValue++;
    }

71 72 73 74 75
    /**
     * Gets the maximum value of the currently allocated bag of values.
     *
     * @return integer|float
     */
76 77 78 79
    public function getCurrentMaxValue()
    {
        return $this->_maxValue;
    }
80 81 82 83 84 85

    /**
     * Gets the next value that will be returned by generate().
     *
     * @return integer|float
     */
86 87 88
    public function getNextValue()
    {
        return $this->_nextValue;
romanb's avatar
romanb committed
89
    }
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    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'];
    }
105
}