Index.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. 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. * Platform specific flags for indexes.
  37. *
  38. * @var array
  39. */
  40. protected $_flags = array();
  41. /**
  42. * @param string $indexName
  43. * @param array $column
  44. * @param bool $isUnique
  45. * @param bool $isPrimary
  46. */
  47. public function __construct($indexName, array $columns, $isUnique = false, $isPrimary = false, array $flags = array())
  48. {
  49. $isUnique = ($isPrimary)?true:$isUnique;
  50. $this->_setName($indexName);
  51. $this->_isUnique = $isUnique;
  52. $this->_isPrimary = $isPrimary;
  53. foreach ($columns as $column) {
  54. $this->_addColumn($column);
  55. }
  56. foreach ($flags as $flag) {
  57. $this->addFlag($flag);
  58. }
  59. }
  60. /**
  61. * @param string $column
  62. */
  63. protected function _addColumn($column)
  64. {
  65. if(is_string($column)) {
  66. $this->_columns[] = $column;
  67. } else {
  68. throw new \InvalidArgumentException("Expecting a string as Index Column");
  69. }
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function getColumns()
  75. {
  76. return $this->_columns;
  77. }
  78. /**
  79. * @return array
  80. */
  81. public function getUnquotedColumns()
  82. {
  83. return array_map(array($this, 'trimQuotes'), $this->getColumns());
  84. }
  85. /**
  86. * Is the index neither unique nor primary key?
  87. *
  88. * @return bool
  89. */
  90. public function isSimpleIndex()
  91. {
  92. return !$this->_isPrimary && !$this->_isUnique;
  93. }
  94. /**
  95. * @return bool
  96. */
  97. public function isUnique()
  98. {
  99. return $this->_isUnique;
  100. }
  101. /**
  102. * @return bool
  103. */
  104. public function isPrimary()
  105. {
  106. return $this->_isPrimary;
  107. }
  108. /**
  109. * @param string $columnName
  110. * @param int $pos
  111. * @return bool
  112. */
  113. public function hasColumnAtPosition($columnName, $pos = 0)
  114. {
  115. $columnName = $this->trimQuotes(strtolower($columnName));
  116. $indexColumns = array_map('strtolower', $this->getUnquotedColumns());
  117. return array_search($columnName, $indexColumns) === $pos;
  118. }
  119. /**
  120. * Check if this index exactly spans the given column names in the correct order.
  121. *
  122. * @param array $columnNames
  123. * @return boolean
  124. */
  125. public function spansColumns(array $columnNames)
  126. {
  127. $sameColumns = true;
  128. for ($i = 0; $i < count($this->_columns); $i++) {
  129. if (!isset($columnNames[$i]) || $this->trimQuotes(strtolower($this->_columns[$i])) != $this->trimQuotes(strtolower($columnNames[$i]))) {
  130. $sameColumns = false;
  131. }
  132. }
  133. return $sameColumns;
  134. }
  135. /**
  136. * Check if the other index already fullfills all the indexing and constraint needs of the current one.
  137. *
  138. * @param Index $other
  139. * @return bool
  140. */
  141. public function isFullfilledBy(Index $other)
  142. {
  143. // allow the other index to be equally large only. It being larger is an option
  144. // but it creates a problem with scenarios of the kind PRIMARY KEY(foo,bar) UNIQUE(foo)
  145. if (count($other->getColumns()) != count($this->getColumns())) {
  146. return false;
  147. }
  148. // Check if columns are the same, and even in the same order
  149. $sameColumns = $this->spansColumns($other->getColumns());
  150. if ($sameColumns) {
  151. if ( ! $this->isUnique() && !$this->isPrimary()) {
  152. // this is a special case: If the current key is neither primary or unique, any uniqe or
  153. // primary key will always have the same effect for the index and there cannot be any constraint
  154. // overlaps. This means a primary or unique index can always fullfill the requirements of just an
  155. // index that has no constraints.
  156. return true;
  157. } else if ($other->isPrimary() != $this->isPrimary()) {
  158. return false;
  159. } else if ($other->isUnique() != $this->isUnique()) {
  160. return false;
  161. }
  162. return true;
  163. }
  164. return false;
  165. }
  166. /**
  167. * Detect if the other index is a non-unique, non primary index that can be overwritten by this one.
  168. *
  169. * @param Index $other
  170. * @return bool
  171. */
  172. public function overrules(Index $other)
  173. {
  174. if ($other->isPrimary()) {
  175. return false;
  176. } else if ($this->isSimpleIndex() && $other->isUnique()) {
  177. return false;
  178. }
  179. if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique())) {
  180. return true;
  181. }
  182. return false;
  183. }
  184. /**
  185. * Add Flag for an index that translates to platform specific handling.
  186. *
  187. * @example $index->addFlag('CLUSTERED')
  188. * @param string $flag
  189. * @return Index
  190. */
  191. public function addFlag($flag)
  192. {
  193. $this->flags[strtolower($flag)] = true;
  194. return $this;
  195. }
  196. /**
  197. * Does this index have a specific flag?
  198. *
  199. * @param string $flag
  200. * @return bool
  201. */
  202. public function hasFlag($flag)
  203. {
  204. return isset($this->flags[strtolower($flag)]);
  205. }
  206. /**
  207. * Remove a flag
  208. *
  209. * @param string $flag
  210. * @return void
  211. */
  212. public function removeFlag($flag)
  213. {
  214. unset($this->flags[strtolower($flag)]);
  215. }
  216. }