Sequence.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Doctrine\DBAL\Schema;
  3. use Doctrine\DBAL\Schema\Visitor\Visitor;
  4. use function count;
  5. use function sprintf;
  6. /**
  7. * Sequence structure.
  8. */
  9. class Sequence extends AbstractAsset
  10. {
  11. /** @var int */
  12. protected $allocationSize = 1;
  13. /** @var int */
  14. protected $initialValue = 1;
  15. /** @var int|null */
  16. protected $cache;
  17. /**
  18. * @param string $name
  19. * @param int $allocationSize
  20. * @param int $initialValue
  21. * @param int|null $cache
  22. */
  23. public function __construct($name, $allocationSize = 1, $initialValue = 1, $cache = null)
  24. {
  25. $this->_setName($name);
  26. $this->setAllocationSize($allocationSize);
  27. $this->setInitialValue($initialValue);
  28. $this->cache = $cache;
  29. }
  30. /**
  31. * @return int
  32. */
  33. public function getAllocationSize()
  34. {
  35. return $this->allocationSize;
  36. }
  37. /**
  38. * @return int
  39. */
  40. public function getInitialValue()
  41. {
  42. return $this->initialValue;
  43. }
  44. /**
  45. * @return int|null
  46. */
  47. public function getCache()
  48. {
  49. return $this->cache;
  50. }
  51. /**
  52. * @param int $allocationSize
  53. *
  54. * @return Sequence
  55. */
  56. public function setAllocationSize($allocationSize)
  57. {
  58. $this->allocationSize = (int) $allocationSize ?: 1;
  59. return $this;
  60. }
  61. /**
  62. * @param int $initialValue
  63. *
  64. * @return Sequence
  65. */
  66. public function setInitialValue($initialValue)
  67. {
  68. $this->initialValue = (int) $initialValue ?: 1;
  69. return $this;
  70. }
  71. /**
  72. * @param int $cache
  73. *
  74. * @return Sequence
  75. */
  76. public function setCache($cache)
  77. {
  78. $this->cache = $cache;
  79. return $this;
  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. * @return bool
  88. */
  89. public function isAutoIncrementsFor(Table $table)
  90. {
  91. $primaryKey = $table->getPrimaryKey();
  92. if ($primaryKey === null) {
  93. return false;
  94. }
  95. $pkColumns = $primaryKey->getColumns();
  96. if (count($pkColumns) !== 1) {
  97. return false;
  98. }
  99. $column = $table->getColumn($pkColumns[0]);
  100. if (! $column->getAutoincrement()) {
  101. return false;
  102. }
  103. $sequenceName = $this->getShortestName($table->getNamespaceName());
  104. $tableName = $table->getShortestName($table->getNamespaceName());
  105. $tableSequenceName = sprintf('%s_%s_seq', $tableName, $column->getShortestName($table->getNamespaceName()));
  106. return $tableSequenceName === $sequenceName;
  107. }
  108. /**
  109. * @return void
  110. */
  111. public function visit(Visitor $visitor)
  112. {
  113. $visitor->acceptSequence($this);
  114. }
  115. }