Sequence.php 3.4 KB

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