Index.php 8.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Schema;

22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23 24 25 26 27 28
use function array_keys;
use function array_map;
use function array_search;
use function count;
use function is_string;
use function strtolower;
29

30
class Index extends AbstractAsset implements Constraint
31 32
{
    /**
33 34 35 36
     * Asset identifier instances of the column names the index is associated with.
     * array($columnName => Identifier)
     *
     * @var Identifier[]
37
     */
38
    protected $_columns = [];
39 40

    /**
41
     * @var bool
42 43 44 45
     */
    protected $_isUnique = false;

    /**
46
     * @var bool
47 48 49
     */
    protected $_isPrimary = false;

50 51
    /**
     * Platform specific flags for indexes.
52
     * array($flagName => true)
53 54 55
     *
     * @var array
     */
56
    protected $_flags = [];
57

58
    /**
59
     * Platform specific options
60
     *
61
     * @todo $_flags should eventually be refactored into options
62 63
     *
     * @var array
64
     */
65
    private $options = [];
66

67
    /**
68 69
     * @param string   $indexName
     * @param string[] $columns
70 71
     * @param bool     $isUnique
     * @param bool     $isPrimary
72
     * @param string[] $flags
73
     * @param array    $options
74
     */
75
    public function __construct($indexName, array $columns, $isUnique = false, $isPrimary = false, array $flags = [], array $options = [])
76
    {
77
        $isUnique = $isUnique || $isPrimary;
78

79 80 81
        $this->_setName($indexName);
        $this->_isUnique = $isUnique;
        $this->_isPrimary = $isPrimary;
82
        $this->options = $options;
83

84
        foreach ($columns as $column) {
85 86
            $this->_addColumn($column);
        }
87 88 89
        foreach ($flags as $flag) {
            $this->addFlag($flag);
        }
90 91 92 93
    }

    /**
     * @param string $column
Benjamin Morel's avatar
Benjamin Morel committed
94 95 96 97
     *
     * @return void
     *
     * @throws \InvalidArgumentException
98 99 100
     */
    protected function _addColumn($column)
    {
Steve Müller's avatar
Steve Müller committed
101
        if (is_string($column)) {
102
            $this->_columns[$column] = new Identifier($column);
103 104 105 106 107 108
        } else {
            throw new \InvalidArgumentException("Expecting a string as Index Column");
        }
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
109
     * {@inheritdoc}
110 111 112
     */
    public function getColumns()
    {
113 114 115 116
        return array_keys($this->_columns);
    }

    /**
117
     * {@inheritdoc}
118 119 120
     */
    public function getQuotedColumns(AbstractPlatform $platform)
    {
121
        $columns = [];
122 123 124 125 126 127

        foreach ($this->_columns as $column) {
            $columns[] = $column->getQuotedName($platform);
        }

        return $columns;
128
    }
129

130
    /**
131
     * @return string[]
132 133 134
     */
    public function getUnquotedColumns()
    {
135
        return array_map([$this, 'trimQuotes'], $this->getColumns());
136 137
    }

138 139
    /**
     * Is the index neither unique nor primary key?
140
     *
141
     * @return bool
142 143 144 145 146
     */
    public function isSimpleIndex()
    {
        return !$this->_isPrimary && !$this->_isUnique;
    }
147 148

    /**
149
     * @return bool
150 151 152 153 154 155 156
     */
    public function isUnique()
    {
        return $this->_isUnique;
    }

    /**
157
     * @return bool
158 159 160 161 162
     */
    public function isPrimary()
    {
        return $this->_isPrimary;
    }
163 164

    /**
165 166
     * @param string $columnName
     * @param int    $pos
Benjamin Morel's avatar
Benjamin Morel committed
167
     *
168
     * @return bool
169
     */
170
    public function hasColumnAtPosition($columnName, $pos = 0)
171
    {
172 173
        $columnName   = $this->trimQuotes(strtolower($columnName));
        $indexColumns = array_map('strtolower', $this->getUnquotedColumns());
Benjamin Morel's avatar
Benjamin Morel committed
174

175
        return array_search($columnName, $indexColumns) === $pos;
176
    }
177 178

    /**
Benjamin Morel's avatar
Benjamin Morel committed
179
     * Checks if this index exactly spans the given column names in the correct order.
180 181
     *
     * @param array $columnNames
Benjamin Morel's avatar
Benjamin Morel committed
182
     *
183
     * @return bool
184 185 186
     */
    public function spansColumns(array $columnNames)
    {
Steve Müller's avatar
Steve Müller committed
187
        $columns         = $this->getColumns();
188
        $numberOfColumns = count($columns);
Steve Müller's avatar
Steve Müller committed
189 190
        $sameColumns     = true;

191
        for ($i = 0; $i < $numberOfColumns; $i++) {
Steve Müller's avatar
Steve Müller committed
192
            if ( ! isset($columnNames[$i]) || $this->trimQuotes(strtolower($columns[$i])) !== $this->trimQuotes(strtolower($columnNames[$i]))) {
193 194 195
                $sameColumns = false;
            }
        }
Steve Müller's avatar
Steve Müller committed
196

197 198 199 200
        return $sameColumns;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
201 202
     * Checks if the other index already fulfills all the indexing and constraint needs of the current one.
     *
203
     * @param Index $other
204
     *
205
     * @return bool
206 207 208 209 210 211 212 213 214 215 216 217 218
     */
    public function isFullfilledBy(Index $other)
    {
        // allow the other index to be equally large only. It being larger is an option
        // but it creates a problem with scenarios of the kind PRIMARY KEY(foo,bar) UNIQUE(foo)
        if (count($other->getColumns()) != count($this->getColumns())) {
            return false;
        }

        // Check if columns are the same, and even in the same order
        $sameColumns = $this->spansColumns($other->getColumns());

        if ($sameColumns) {
219
            if ( ! $this->samePartialIndex($other)) {
220
                return false;
221 222
            }

223
            if ( ! $this->isUnique() && ! $this->isPrimary()) {
Possum's avatar
Possum committed
224
                // this is a special case: If the current key is neither primary or unique, any unique or
225
                // primary key will always have the same effect for the index and there cannot be any constraint
Pascal Borreli's avatar
Pascal Borreli committed
226
                // overlaps. This means a primary or unique index can always fulfill the requirements of just an
227 228
                // index that has no constraints.
                return true;
229 230 231
            }

            if ($other->isPrimary() != $this->isPrimary()) {
232
                return false;
233 234
            }

Gabriel Caruso's avatar
Gabriel Caruso committed
235
            return $other->isUnique() === $this->isUnique();
236
        }
Benjamin Morel's avatar
Benjamin Morel committed
237

238 239 240 241
        return false;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
242 243
     * Detects if the other index is a non-unique, non primary index that can be overwritten by this one.
     *
244
     * @param Index $other
245
     *
246
     * @return bool
247 248 249
     */
    public function overrules(Index $other)
    {
250 251
        if ($other->isPrimary()) {
            return false;
Steve Müller's avatar
Steve Müller committed
252
        } elseif ($this->isSimpleIndex() && $other->isUnique()) {
253 254 255
            return false;
        }

Gabriel Caruso's avatar
Gabriel Caruso committed
256
        return $this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique()) && $this->samePartialIndex($other);
257
    }
258

259 260 261
    /**
     * Returns platform specific flags for indexes.
     *
262
     * @return string[]
263 264 265 266 267 268
     */
    public function getFlags()
    {
        return array_keys($this->_flags);
    }

269
    /**
Benjamin Morel's avatar
Benjamin Morel committed
270
     * Adds Flag for an index that translates to platform specific handling.
271 272
     *
     * @example $index->addFlag('CLUSTERED')
Benjamin Morel's avatar
Benjamin Morel committed
273
     *
274
     * @param string $flag
Benjamin Morel's avatar
Benjamin Morel committed
275
     *
276
     * @return Index
277 278 279
     */
    public function addFlag($flag)
    {
Steve Müller's avatar
Steve Müller committed
280
        $this->_flags[strtolower($flag)] = true;
Benjamin Morel's avatar
Benjamin Morel committed
281

282 283 284 285 286 287 288
        return $this;
    }

    /**
     * Does this index have a specific flag?
     *
     * @param string $flag
Benjamin Morel's avatar
Benjamin Morel committed
289
     *
290
     * @return bool
291 292 293
     */
    public function hasFlag($flag)
    {
Steve Müller's avatar
Steve Müller committed
294
        return isset($this->_flags[strtolower($flag)]);
295 296 297
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
298
     * Removes a flag.
299 300
     *
     * @param string $flag
Benjamin Morel's avatar
Benjamin Morel committed
301
     *
302 303 304 305
     * @return void
     */
    public function removeFlag($flag)
    {
Steve Müller's avatar
Steve Müller committed
306
        unset($this->_flags[strtolower($flag)]);
307
    }
308 309

    /**
310 311
     * @param string $name
     *
312
     * @return bool
313 314 315
     */
    public function hasOption($name)
    {
316
        return isset($this->options[strtolower($name)]);
317 318 319 320 321 322
    }

    /**
     * @param string $name
     *
     * @return mixed
323
     */
324
    public function getOption($name)
325
    {
326
        return $this->options[strtolower($name)];
327
    }
328 329 330 331 332 333 334 335 336 337 338 339

    /**
     * @return array
     */
    public function getOptions()
    {
        return $this->options;
    }

    /**
     * Return whether the two indexes have the same partial index
     * @param \Doctrine\DBAL\Schema\Index $other
340
     *
341
     * @return bool
342 343 344 345 346
     */
    private function samePartialIndex(Index $other)
    {
        if ($this->hasOption('where') && $other->hasOption('where') && $this->getOption('where') == $other->getOption('where')) {
            return true;
347 348
        }

Gabriel Caruso's avatar
Gabriel Caruso committed
349
        return ! $this->hasOption('where') && ! $other->hasOption('where');
350 351
    }

352
}