Index.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\DBAL\Schema;
  20. use Doctrine\DBAL\Schema\Visitor\Visitor;
  21. class Index extends AbstractAsset implements Constraint
  22. {
  23. /**
  24. * @var array
  25. */
  26. protected $_columns;
  27. /**
  28. * @var bool
  29. */
  30. protected $_isUnique = false;
  31. /**
  32. * @var bool
  33. */
  34. protected $_isPrimary = false;
  35. /**
  36. * @param string $indexName
  37. * @param array $column
  38. * @param bool $isUnique
  39. * @param bool $isPrimary
  40. */
  41. public function __construct($indexName, array $columns, $isUnique=false, $isPrimary=false)
  42. {
  43. $isUnique = ($isPrimary)?true:$isUnique;
  44. $this->_setName($indexName);
  45. $this->_isUnique = $isUnique;
  46. $this->_isPrimary = $isPrimary;
  47. foreach($columns AS $column) {
  48. $this->_addColumn($column);
  49. }
  50. }
  51. /**
  52. * @param string $column
  53. */
  54. protected function _addColumn($column)
  55. {
  56. if(is_string($column)) {
  57. $this->_columns[] = $column;
  58. } else {
  59. throw new \InvalidArgumentException("Expecting a string as Index Column");
  60. }
  61. }
  62. /**
  63. * @return array
  64. */
  65. public function getColumns()
  66. {
  67. return $this->_columns;
  68. }
  69. /**
  70. * Is the index neither unique nor primary key?
  71. *
  72. * @return bool
  73. */
  74. public function isSimpleIndex()
  75. {
  76. return !$this->_isPrimary && !$this->_isUnique;
  77. }
  78. /**
  79. * @return bool
  80. */
  81. public function isUnique()
  82. {
  83. return $this->_isUnique;
  84. }
  85. /**
  86. * @return bool
  87. */
  88. public function isPrimary()
  89. {
  90. return $this->_isPrimary;
  91. }
  92. /**
  93. * @param string $columnName
  94. * @param int $pos
  95. * @return bool
  96. */
  97. public function hasColumnAtPosition($columnName, $pos=0)
  98. {
  99. $columnName = strtolower($columnName);
  100. $indexColumns = \array_map('strtolower', $this->getColumns());
  101. return \array_search($columnName, $indexColumns) === $pos;
  102. }
  103. /**
  104. * Check if this index exactly spans the given column names in the correct order.
  105. *
  106. * @param array $columnNames
  107. * @return boolean
  108. */
  109. public function spansColumns(array $columnNames)
  110. {
  111. $sameColumns = true;
  112. for ($i = 0; $i < count($this->_columns); $i++) {
  113. if (!isset($columnNames[$i]) || strtolower($this->_columns[$i]) != strtolower($columnNames[$i])) {
  114. $sameColumns = false;
  115. }
  116. }
  117. return $sameColumns;
  118. }
  119. /**
  120. * Check if the other index already fullfills all the indexing and constraint needs of the current one.
  121. *
  122. * @param Index $other
  123. * @return bool
  124. */
  125. public function isFullfilledBy(Index $other)
  126. {
  127. // allow the other index to be equally large only. It being larger is an option
  128. // but it creates a problem with scenarios of the kind PRIMARY KEY(foo,bar) UNIQUE(foo)
  129. if (count($other->getColumns()) != count($this->getColumns())) {
  130. return false;
  131. }
  132. // Check if columns are the same, and even in the same order
  133. $sameColumns = $this->spansColumns($other->getColumns());
  134. if ($sameColumns) {
  135. if (!$this->isUnique() && !$this->isPrimary()) {
  136. // this is a special case: If the current key is neither primary or unique, any uniqe or
  137. // primary key will always have the same effect for the index and there cannot be any constraint
  138. // overlaps. This means a primary or unique index can always fullfill the requirements of just an
  139. // index that has no constraints.
  140. return true;
  141. } else if ($other->isPrimary() != $this->isPrimary()) {
  142. return false;
  143. } else if ($other->isUnique() != $this->isUnique()) {
  144. return false;
  145. }
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * Detect if the other index is a non-unique, non primary index that can be overwritten by this one.
  152. *
  153. * @param Index $other
  154. * @return bool
  155. */
  156. public function overrules(Index $other)
  157. {
  158. if ($other->isPrimary()) {
  159. return false;
  160. } else if ($this->isSimpleIndex() && $other->isUnique()) {
  161. return false;
  162. }
  163. if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique())) {
  164. return true;
  165. }
  166. return false;
  167. }
  168. }