[345] | 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 | |
---|
| 20 | namespace Doctrine\DBAL\Schema; |
---|
| 21 | |
---|
| 22 | use Doctrine\DBAL\Schema\Visitor\Visitor; |
---|
| 23 | |
---|
| 24 | class Index extends AbstractAsset implements Constraint |
---|
| 25 | { |
---|
| 26 | /** |
---|
| 27 | * @var array |
---|
| 28 | */ |
---|
| 29 | protected $_columns; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * @var bool |
---|
| 33 | */ |
---|
| 34 | protected $_isUnique = false; |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * @var bool |
---|
| 38 | */ |
---|
| 39 | protected $_isPrimary = false; |
---|
| 40 | |
---|
| 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) |
---|
| 48 | { |
---|
| 49 | $isUnique = ($isPrimary)?true:$isUnique; |
---|
| 50 | |
---|
| 51 | $this->_setName($indexName); |
---|
| 52 | $this->_isUnique = $isUnique; |
---|
| 53 | $this->_isPrimary = $isPrimary; |
---|
| 54 | |
---|
| 55 | foreach($columns AS $column) { |
---|
| 56 | $this->_addColumn($column); |
---|
| 57 | } |
---|
| 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 | /** |
---|
| 73 | * @return array |
---|
| 74 | */ |
---|
| 75 | public function getColumns() |
---|
| 76 | { |
---|
| 77 | return $this->_columns; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Is the index neither unique nor primary key? |
---|
| 82 | * |
---|
| 83 | * @return bool |
---|
| 84 | */ |
---|
| 85 | public function isSimpleIndex() |
---|
| 86 | { |
---|
| 87 | return !$this->_isPrimary && !$this->_isUnique; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * @return bool |
---|
| 92 | */ |
---|
| 93 | public function isUnique() |
---|
| 94 | { |
---|
| 95 | return $this->_isUnique; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * @return bool |
---|
| 100 | */ |
---|
| 101 | public function isPrimary() |
---|
| 102 | { |
---|
| 103 | return $this->_isPrimary; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * @param string $columnName |
---|
| 108 | * @param int $pos |
---|
| 109 | * @return bool |
---|
| 110 | */ |
---|
| 111 | public function hasColumnAtPosition($columnName, $pos=0) |
---|
| 112 | { |
---|
| 113 | $columnName = strtolower($columnName); |
---|
| 114 | $indexColumns = \array_map('strtolower', $this->getColumns()); |
---|
| 115 | return \array_search($columnName, $indexColumns) === $pos; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Check if this index exactly spans the given column names in the correct order. |
---|
| 120 | * |
---|
| 121 | * @param array $columnNames |
---|
| 122 | * @return boolean |
---|
| 123 | */ |
---|
| 124 | public function spansColumns(array $columnNames) |
---|
| 125 | { |
---|
| 126 | $sameColumns = true; |
---|
| 127 | for ($i = 0; $i < count($this->_columns); $i++) { |
---|
| 128 | if (!isset($columnNames[$i]) || strtolower($this->_columns[$i]) != strtolower($columnNames[$i])) { |
---|
| 129 | $sameColumns = false; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | return $sameColumns; |
---|
| 133 | } |
---|
| 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 | |
---|
| 149 | // Check if columns are the same, and even in the same order |
---|
| 150 | $sameColumns = $this->spansColumns($other->getColumns()); |
---|
| 151 | |
---|
| 152 | if ($sameColumns) { |
---|
| 153 | if (!$this->isUnique() && !$this->isPrimary()) { |
---|
| 154 | // this is a special case: If the current key is neither primary or unique, any uniqe or |
---|
| 155 | // primary key will always have the same effect for the index and there cannot be any constraint |
---|
| 156 | // overlaps. This means a primary or unique index can always fullfill the requirements of just an |
---|
| 157 | // index that has no constraints. |
---|
| 158 | return true; |
---|
| 159 | } else if ($other->isPrimary() != $this->isPrimary()) { |
---|
| 160 | return false; |
---|
| 161 | } else if ($other->isUnique() != $this->isUnique()) { |
---|
| 162 | return false; |
---|
| 163 | } |
---|
| 164 | return true; |
---|
| 165 | } |
---|
| 166 | return false; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * Detect if the other index is a non-unique, non primary index that can be overwritten by this one. |
---|
| 171 | * |
---|
| 172 | * @param Index $other |
---|
| 173 | * @return bool |
---|
| 174 | */ |
---|
| 175 | public function overrules(Index $other) |
---|
| 176 | { |
---|
| 177 | if ($other->isPrimary()) { |
---|
| 178 | return false; |
---|
| 179 | } else if ($this->isSimpleIndex() && $other->isUnique()) { |
---|
| 180 | return false; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique())) { |
---|
| 184 | return true; |
---|
| 185 | } |
---|
| 186 | return false; |
---|
| 187 | } |
---|
| 188 | } |
---|