Sequence.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Doctrine\DBAL\Schema;
  3. use Doctrine\DBAL\Schema\Visitor\Visitor;
  4. use function count;
  5. use function is_numeric;
  6. use function sprintf;
  7. /**
  8. * Sequence structure.
  9. */
  10. class Sequence extends AbstractAsset
  11. {
  12. /** @var int */
  13. protected $allocationSize = 1;
  14. /** @var int */
  15. protected $initialValue = 1;
  16. /** @var int|null */
  17. protected $cache = null;
  18. /**
  19. * @param string $name
  20. * @param int $allocationSize
  21. * @param int $initialValue
  22. * @param int|null $cache
  23. */
  24. public function __construct($name, $allocationSize = 1, $initialValue = 1, $cache = null)
  25. {
  26. $this->_setName($name);
  27. $this->setAllocationSize($allocationSize);
  28. $this->setInitialValue($initialValue);
  29. $this->cache = $cache;
  30. }
  31. /**
  32. * @return int
  33. */
  34. public function getAllocationSize()
  35. {
  36. return $this->allocationSize;
  37. }
  38. /**
  39. * @return int
  40. */
  41. public function getInitialValue()
  42. {
  43. return $this->initialValue;
  44. }
  45. /**
  46. * @return int|null
  47. */
  48. public function getCache()
  49. {
  50. return $this->cache;
  51. }
  52. /**
  53. * @param int $allocationSize
  54. *
  55. * @return \Doctrine\DBAL\Schema\Sequence
  56. */
  57. public function setAllocationSize($allocationSize)
  58. {
  59. $this->allocationSize = is_numeric($allocationSize) ? (int) $allocationSize : 1;
  60. return $this;
  61. }
  62. /**
  63. * @param int $initialValue
  64. *
  65. * @return \Doctrine\DBAL\Schema\Sequence
  66. */
  67. public function setInitialValue($initialValue)
  68. {
  69. $this->initialValue = is_numeric($initialValue) ? (int) $initialValue : 1;
  70. return $this;
  71. }
  72. /**
  73. * @param int $cache
  74. *
  75. * @return \Doctrine\DBAL\Schema\Sequence
  76. */
  77. public function setCache($cache)
  78. {
  79. $this->cache = $cache;
  80. return $this;
  81. }
  82. /**
  83. * Checks if this sequence is an autoincrement sequence for a given table.
  84. *
  85. * This is used inside the comparator to not report sequences as missing,
  86. * when the "from" schema implicitly creates the sequences.
  87. *
  88. * @return bool
  89. */
  90. public function isAutoIncrementsFor(Table $table)
  91. {
  92. if (! $table->hasPrimaryKey()) {
  93. return false;
  94. }
  95. $pkColumns = $table->getPrimaryKey()->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. }