Index.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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\Platforms\AbstractPlatform;
  21. class Index extends AbstractAsset implements Constraint
  22. {
  23. /**
  24. * Asset identifier instances of the column names the index is associated with.
  25. * array($columnName => Identifier)
  26. *
  27. * @var Identifier[]
  28. */
  29. protected $_columns;
  30. /**
  31. * @var boolean
  32. */
  33. protected $_isUnique = false;
  34. /**
  35. * @var boolean
  36. */
  37. protected $_isPrimary = false;
  38. /**
  39. * Platform specific flags for indexes.
  40. *
  41. * @var array
  42. */
  43. protected $_flags = array();
  44. /**
  45. * @param string $indexName
  46. * @param array $columns
  47. * @param boolean $isUnique
  48. * @param boolean $isPrimary
  49. * @param array $flags
  50. */
  51. public function __construct($indexName, array $columns, $isUnique = false, $isPrimary = false, array $flags = array())
  52. {
  53. $isUnique = ($isPrimary)?true:$isUnique;
  54. $this->_setName($indexName);
  55. $this->_isUnique = $isUnique;
  56. $this->_isPrimary = $isPrimary;
  57. foreach ($columns as $column) {
  58. $this->_addColumn($column);
  59. }
  60. foreach ($flags as $flag) {
  61. $this->addFlag($flag);
  62. }
  63. }
  64. /**
  65. * @param string $column
  66. *
  67. * @return void
  68. *
  69. * @throws \InvalidArgumentException
  70. */
  71. protected function _addColumn($column)
  72. {
  73. if(is_string($column)) {
  74. $this->_columns[$column] = new Identifier($column);
  75. } else {
  76. throw new \InvalidArgumentException("Expecting a string as Index Column");
  77. }
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getColumns()
  83. {
  84. return array_keys($this->_columns);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getQuotedColumns(AbstractPlatform $platform)
  90. {
  91. $columns = array();
  92. foreach ($this->_columns as $column) {
  93. $columns[] = $column->getQuotedName($platform);
  94. }
  95. return $columns;
  96. }
  97. /**
  98. * @return array
  99. */
  100. public function getUnquotedColumns()
  101. {
  102. return array_map(array($this, 'trimQuotes'), $this->getColumns());
  103. }
  104. /**
  105. * Is the index neither unique nor primary key?
  106. *
  107. * @return boolean
  108. */
  109. public function isSimpleIndex()
  110. {
  111. return !$this->_isPrimary && !$this->_isUnique;
  112. }
  113. /**
  114. * @return boolean
  115. */
  116. public function isUnique()
  117. {
  118. return $this->_isUnique;
  119. }
  120. /**
  121. * @return boolean
  122. */
  123. public function isPrimary()
  124. {
  125. return $this->_isPrimary;
  126. }
  127. /**
  128. * @param string $columnName
  129. * @param integer $pos
  130. *
  131. * @return boolean
  132. */
  133. public function hasColumnAtPosition($columnName, $pos = 0)
  134. {
  135. $columnName = $this->trimQuotes(strtolower($columnName));
  136. $indexColumns = array_map('strtolower', $this->getUnquotedColumns());
  137. return array_search($columnName, $indexColumns) === $pos;
  138. }
  139. /**
  140. * Checks if this index exactly spans the given column names in the correct order.
  141. *
  142. * @param array $columnNames
  143. *
  144. * @return boolean
  145. */
  146. public function spansColumns(array $columnNames)
  147. {
  148. $columns = $this->getColumns();
  149. $numberOfColumns = count($columns);
  150. $sameColumns = true;
  151. for ($i = 0; $i < $numberOfColumns; $i++) {
  152. if ( ! isset($columnNames[$i]) || $this->trimQuotes(strtolower($columns[$i])) !== $this->trimQuotes(strtolower($columnNames[$i]))) {
  153. $sameColumns = false;
  154. }
  155. }
  156. return $sameColumns;
  157. }
  158. /**
  159. * Checks if the other index already fulfills all the indexing and constraint needs of the current one.
  160. *
  161. * @param \Doctrine\DBAL\Schema\Index $other
  162. *
  163. * @return boolean
  164. */
  165. public function isFullfilledBy(Index $other)
  166. {
  167. // allow the other index to be equally large only. It being larger is an option
  168. // but it creates a problem with scenarios of the kind PRIMARY KEY(foo,bar) UNIQUE(foo)
  169. if (count($other->getColumns()) != count($this->getColumns())) {
  170. return false;
  171. }
  172. // Check if columns are the same, and even in the same order
  173. $sameColumns = $this->spansColumns($other->getColumns());
  174. if ($sameColumns) {
  175. if ( ! $this->isUnique() && !$this->isPrimary()) {
  176. // this is a special case: If the current key is neither primary or unique, any uniqe or
  177. // primary key will always have the same effect for the index and there cannot be any constraint
  178. // overlaps. This means a primary or unique index can always fulfill the requirements of just an
  179. // index that has no constraints.
  180. return true;
  181. } else if ($other->isPrimary() != $this->isPrimary()) {
  182. return false;
  183. } else if ($other->isUnique() != $this->isUnique()) {
  184. return false;
  185. }
  186. return true;
  187. }
  188. return false;
  189. }
  190. /**
  191. * Detects if the other index is a non-unique, non primary index that can be overwritten by this one.
  192. *
  193. * @param \Doctrine\DBAL\Schema\Index $other
  194. *
  195. * @return boolean
  196. */
  197. public function overrules(Index $other)
  198. {
  199. if ($other->isPrimary()) {
  200. return false;
  201. } else if ($this->isSimpleIndex() && $other->isUnique()) {
  202. return false;
  203. }
  204. if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique())) {
  205. return true;
  206. }
  207. return false;
  208. }
  209. /**
  210. * Returns platform specific flags for indexes.
  211. *
  212. * @return array
  213. */
  214. public function getFlags()
  215. {
  216. return array_keys($this->_flags);
  217. }
  218. /**
  219. * Adds Flag for an index that translates to platform specific handling.
  220. *
  221. * @example $index->addFlag('CLUSTERED')
  222. *
  223. * @param string $flag
  224. *
  225. * @return \Doctrine\DBAL\Schema\Index
  226. */
  227. public function addFlag($flag)
  228. {
  229. $this->flags[strtolower($flag)] = true;
  230. return $this;
  231. }
  232. /**
  233. * Does this index have a specific flag?
  234. *
  235. * @param string $flag
  236. *
  237. * @return boolean
  238. */
  239. public function hasFlag($flag)
  240. {
  241. return isset($this->flags[strtolower($flag)]);
  242. }
  243. /**
  244. * Removes a flag.
  245. *
  246. * @param string $flag
  247. *
  248. * @return void
  249. */
  250. public function removeFlag($flag)
  251. {
  252. unset($this->flags[strtolower($flag)]);
  253. }
  254. }