SequenceGenerator.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. namespace Doctrine\ORM\Id;
  20. use Serializable, Doctrine\ORM\EntityManager;
  21. /**
  22. * Represents an ID generator that uses a database sequence.
  23. *
  24. * @since 2.0
  25. * @author Roman Borschel <roman@code-factory.org>
  26. */
  27. class SequenceGenerator extends AbstractIdGenerator implements Serializable
  28. {
  29. private $_allocationSize;
  30. private $_sequenceName;
  31. private $_nextValue = 0;
  32. private $_maxValue = null;
  33. /**
  34. * Initializes a new sequence generator.
  35. *
  36. * @param \Doctrine\ORM\EntityManager $em The EntityManager to use.
  37. * @param string $sequenceName The name of the sequence.
  38. * @param integer $allocationSize The allocation size of the sequence.
  39. */
  40. public function __construct($sequenceName, $allocationSize)
  41. {
  42. $this->_sequenceName = $sequenceName;
  43. $this->_allocationSize = $allocationSize;
  44. }
  45. /**
  46. * Generates an ID for the given entity.
  47. *
  48. * @param object $entity
  49. * @return integer|float The generated value.
  50. * @override
  51. */
  52. public function generate(EntityManager $em, $entity)
  53. {
  54. if ($this->_maxValue === null || $this->_nextValue == $this->_maxValue) {
  55. // Allocate new values
  56. $conn = $em->getConnection();
  57. $sql = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName);
  58. $this->_nextValue = $conn->fetchColumn($sql);
  59. $this->_maxValue = $this->_nextValue + $this->_allocationSize;
  60. }
  61. return $this->_nextValue++;
  62. }
  63. /**
  64. * Gets the maximum value of the currently allocated bag of values.
  65. *
  66. * @return integer|float
  67. */
  68. public function getCurrentMaxValue()
  69. {
  70. return $this->_maxValue;
  71. }
  72. /**
  73. * Gets the next value that will be returned by generate().
  74. *
  75. * @return integer|float
  76. */
  77. public function getNextValue()
  78. {
  79. return $this->_nextValue;
  80. }
  81. public function serialize()
  82. {
  83. return serialize(array(
  84. 'allocationSize' => $this->_allocationSize,
  85. 'sequenceName' => $this->_sequenceName
  86. ));
  87. }
  88. public function unserialize($serialized)
  89. {
  90. $array = unserialize($serialized);
  91. $this->_sequenceName = $array['sequenceName'];
  92. $this->_allocationSize = $array['allocationSize'];
  93. }
  94. }