TableGenerator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Doctrine\ORM\EntityManager;
  21. /**
  22. * Id generator that uses a single-row database table and a hi/lo algorithm.
  23. *
  24. * @since 2.0
  25. * @author Benjamin Eberlei <kontakt@beberlei.de>
  26. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  27. * @author Jonathan Wage <jonwage@gmail.com>
  28. * @author Roman Borschel <roman@code-factory.org>
  29. */
  30. class TableGenerator extends AbstractIdGenerator
  31. {
  32. private $_tableName;
  33. private $_sequenceName;
  34. private $_allocationSize;
  35. private $_nextValue;
  36. private $_maxValue;
  37. public function __construct($tableName, $sequenceName = 'default', $allocationSize = 10)
  38. {
  39. $this->_tableName = $tableName;
  40. $this->_sequenceName = $sequenceName;
  41. $this->_allocationSize = $allocationSize;
  42. }
  43. public function generate(EntityManager $em, $entity)
  44. {
  45. if ($this->_maxValue === null || $this->_nextValue == $this->_maxValue) {
  46. // Allocate new values
  47. $conn = $em->getConnection();
  48. if ($conn->getTransactionNestingLevel() === 0) {
  49. // use select for update
  50. $sql = $conn->getDatabasePlatform()->getTableHiLoCurrentValSql($this->_tableName, $this->_sequenceName);
  51. $currentLevel = $conn->fetchColumn($sql);
  52. if ($currentLevel != null) {
  53. $this->_nextValue = $currentLevel;
  54. $this->_maxValue = $this->_nextValue + $this->_allocationSize;
  55. $updateSql = $conn->getDatabasePlatform()->getTableHiLoUpdateNextValSql(
  56. $this->_tableName, $this->_sequenceName, $this->_allocationSize
  57. );
  58. if ($conn->executeUpdate($updateSql, array(1 => $currentLevel, 2 => $currentLevel+1)) !== 1) {
  59. // no affected rows, concurrency issue, throw exception
  60. }
  61. } else {
  62. // no current level returned, TableGenerator seems to be broken, throw exception
  63. }
  64. } else {
  65. // only table locks help here, implement this or throw exception?
  66. // or do we want to work with table locks exclusively?
  67. }
  68. }
  69. return $this->_nextValue++;
  70. }
  71. }