Sequence.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. use Doctrine\DBAL\Schema\Visitor\Visitor;
  21. /**
  22. * Sequence structure.
  23. *
  24. * @link www.doctrine-project.org
  25. * @since 2.0
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. */
  28. class Sequence extends AbstractAsset
  29. {
  30. /**
  31. * @var integer
  32. */
  33. protected $_allocationSize = 1;
  34. /**
  35. * @var integer
  36. */
  37. protected $_initialValue = 1;
  38. /**
  39. * @param string $name
  40. * @param integer $allocationSize
  41. * @param integer $initialValue
  42. */
  43. public function __construct($name, $allocationSize=1, $initialValue=1)
  44. {
  45. $this->_setName($name);
  46. $this->_allocationSize = (is_numeric($allocationSize))?$allocationSize:1;
  47. $this->_initialValue = (is_numeric($initialValue))?$initialValue:1;
  48. }
  49. /**
  50. * @return integer
  51. */
  52. public function getAllocationSize()
  53. {
  54. return $this->_allocationSize;
  55. }
  56. /**
  57. * @return integer
  58. */
  59. public function getInitialValue()
  60. {
  61. return $this->_initialValue;
  62. }
  63. /**
  64. * @param integer $allocationSize
  65. *
  66. * @return void
  67. */
  68. public function setAllocationSize($allocationSize)
  69. {
  70. $this->_allocationSize = (is_numeric($allocationSize))?$allocationSize:1;
  71. }
  72. /**
  73. * @param integer $initialValue
  74. *
  75. * @return void
  76. */
  77. public function setInitialValue($initialValue)
  78. {
  79. $this->_initialValue = (is_numeric($initialValue))?$initialValue:1;
  80. }
  81. /**
  82. * Checks if this sequence is an autoincrement sequence for a given table.
  83. *
  84. * This is used inside the comparator to not report sequences as missing,
  85. * when the "from" schema implicitly creates the sequences.
  86. *
  87. * @param \Doctrine\DBAL\Schema\Table $table
  88. *
  89. * @return boolean
  90. */
  91. public function isAutoIncrementsFor(Table $table)
  92. {
  93. if ( ! $table->hasPrimaryKey()) {
  94. return false;
  95. }
  96. $pkColumns = $table->getPrimaryKey()->getColumns();
  97. if (count($pkColumns) != 1) {
  98. return false;
  99. }
  100. $column = $table->getColumn($pkColumns[0]);
  101. if ( ! $column->getAutoincrement()) {
  102. return false;
  103. }
  104. $sequenceName = $this->getShortestName($table->getNamespaceName());
  105. $tableName = $table->getShortestName($table->getNamespaceName());
  106. $tableSequenceName = sprintf('%s_%s_seq', $tableName, $pkColumns[0]);
  107. return $tableSequenceName === $sequenceName;
  108. }
  109. /**
  110. * @param \Doctrine\DBAL\Schema\Visitor\Visitor $visitor
  111. *
  112. * @return void
  113. */
  114. public function visit(Visitor $visitor)
  115. {
  116. $visitor->acceptSequence($this);
  117. }
  118. }