MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
Index.php
Ir para a documentação deste ficheiro.
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
20namespace Doctrine\DBAL\Schema;
21
23
24class Index extends AbstractAsset implements Constraint
25{
29 protected $_columns;
30
34 protected $_isUnique = false;
35
39 protected $_isPrimary = false;
40
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
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
75 public function getColumns()
76 {
77 return $this->_columns;
78 }
79
83 public function isUnique()
84 {
85 return $this->_isUnique;
86 }
87
91 public function isPrimary()
92 {
93 return $this->_isPrimary;
94 }
95
101 public function hasColumnAtPosition($columnName, $pos=0)
102 {
103 $columnName = strtolower($columnName);
104 $indexColumns = \array_map('strtolower', $this->getColumns());
105 return \array_search($columnName, $indexColumns) === $pos;
106 }
107
114 public function spansColumns(array $columnNames)
115 {
116 $sameColumns = true;
117 for ($i = 0; $i < count($this->_columns); $i++) {
118 if (!isset($columnNames[$i]) || strtolower($this->_columns[$i]) != strtolower($columnNames[$i])) {
119 $sameColumns = false;
120 }
121 }
122 return $sameColumns;
123 }
124
131 public function isFullfilledBy(Index $other)
132 {
133 // allow the other index to be equally large only. It being larger is an option
134 // but it creates a problem with scenarios of the kind PRIMARY KEY(foo,bar) UNIQUE(foo)
135 if (count($other->getColumns()) != count($this->getColumns())) {
136 return false;
137 }
138
139 // Check if columns are the same, and even in the same order
140 $sameColumns = $this->spansColumns($other->getColumns());
141
142 if ($sameColumns) {
143 if (!$this->isUnique() && !$this->isPrimary()) {
144 // this is a special case: If the current key is neither primary or unique, any uniqe or
145 // primary key will always have the same effect for the index and there cannot be any constraint
146 // overlaps. This means a primary or unique index can always fullfill the requirements of just an
147 // index that has no constraints.
148 return true;
149 } else if ($other->isPrimary() != $this->isPrimary()) {
150 return false;
151 } else if ($other->isUnique() != $this->isUnique()) {
152 return false;
153 }
154 return true;
155 }
156 return false;
157 }
158
165 public function overrules(Index $other)
166 {
167 if ($other->isPrimary() || $other->isUnique()) {
168 return false;
169 }
170
171 if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique())) {
172 return true;
173 }
174 return false;
175 }
176}
__construct($indexName, array $columns, $isUnique=false, $isPrimary=false)
Definição Index.php:47
spansColumns(array $columnNames)
Definição Index.php:114
overrules(Index $other)
Definição Index.php:165
isFullfilledBy(Index $other)
Definição Index.php:131
hasColumnAtPosition($columnName, $pos=0)
Definição Index.php:101