Index.php 8.89 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
class Index extends AbstractAsset implements Constraint
25 26
{
    /**
27 28 29 30
     * Asset identifier instances of the column names the index is associated with.
     * array($columnName => Identifier)
     *
     * @var Identifier[]
31
     */
Steve Müller's avatar
Steve Müller committed
32
    protected $_columns = array();
33 34

    /**
Benjamin Morel's avatar
Benjamin Morel committed
35
     * @var boolean
36 37 38 39
     */
    protected $_isUnique = false;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
40
     * @var boolean
41 42 43
     */
    protected $_isPrimary = false;

44 45
    /**
     * Platform specific flags for indexes.
46
     * array($flagName => true)
47 48 49 50 51
     *
     * @var array
     */
    protected $_flags = array();

52
    /**
53
     * Platform specific options
54
     *
55
     * @todo $_flags should eventually be refactored into options
56 57
     *
     * @var array
58
     */
59
    private $options = array();
60

61
    /**
62 63 64 65 66
     * @param string   $indexName
     * @param string[] $columns
     * @param boolean  $isUnique
     * @param boolean  $isPrimary
     * @param string[] $flags
67
     * @param array    $options
68
     */
69
    public function __construct($indexName, array $columns, $isUnique = false, $isPrimary = false, array $flags = array(), array $options = array())
70
    {
71
        $isUnique = $isUnique || $isPrimary;
72

73 74 75
        $this->_setName($indexName);
        $this->_isUnique = $isUnique;
        $this->_isPrimary = $isPrimary;
76
        $this->options = $options;
77

78
        foreach ($columns as $column) {
79 80
            $this->_addColumn($column);
        }
81 82 83
        foreach ($flags as $flag) {
            $this->addFlag($flag);
        }
84 85 86 87
    }

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
103
     * {@inheritdoc}
104 105 106
     */
    public function getColumns()
    {
107 108 109 110
        return array_keys($this->_columns);
    }

    /**
111
     * {@inheritdoc}
112 113 114 115 116 117 118 119 120 121
     */
    public function getQuotedColumns(AbstractPlatform $platform)
    {
        $columns = array();

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

        return $columns;
122
    }
123

124
    /**
125
     * @return string[]
126 127 128 129 130 131
     */
    public function getUnquotedColumns()
    {
        return array_map(array($this, 'trimQuotes'), $this->getColumns());
    }

132 133
    /**
     * Is the index neither unique nor primary key?
134
     *
Benjamin Morel's avatar
Benjamin Morel committed
135
     * @return boolean
136 137 138 139 140
     */
    public function isSimpleIndex()
    {
        return !$this->_isPrimary && !$this->_isUnique;
    }
141 142

    /**
Benjamin Morel's avatar
Benjamin Morel committed
143
     * @return boolean
144 145 146 147 148 149 150
     */
    public function isUnique()
    {
        return $this->_isUnique;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
151
     * @return boolean
152 153 154 155 156
     */
    public function isPrimary()
    {
        return $this->_isPrimary;
    }
157 158

    /**
Benjamin Morel's avatar
Benjamin Morel committed
159 160 161 162
     * @param string  $columnName
     * @param integer $pos
     *
     * @return boolean
163
     */
164
    public function hasColumnAtPosition($columnName, $pos = 0)
165
    {
166 167
        $columnName   = $this->trimQuotes(strtolower($columnName));
        $indexColumns = array_map('strtolower', $this->getUnquotedColumns());
Benjamin Morel's avatar
Benjamin Morel committed
168

169
        return array_search($columnName, $indexColumns) === $pos;
170
    }
171 172

    /**
Benjamin Morel's avatar
Benjamin Morel committed
173
     * Checks if this index exactly spans the given column names in the correct order.
174 175
     *
     * @param array $columnNames
Benjamin Morel's avatar
Benjamin Morel committed
176
     *
177 178 179 180
     * @return boolean
     */
    public function spansColumns(array $columnNames)
    {
Steve Müller's avatar
Steve Müller committed
181
        $columns         = $this->getColumns();
182
        $numberOfColumns = count($columns);
Steve Müller's avatar
Steve Müller committed
183 184
        $sameColumns     = true;

185
        for ($i = 0; $i < $numberOfColumns; $i++) {
Steve Müller's avatar
Steve Müller committed
186
            if ( ! isset($columnNames[$i]) || $this->trimQuotes(strtolower($columns[$i])) !== $this->trimQuotes(strtolower($columnNames[$i]))) {
187 188 189
                $sameColumns = false;
            }
        }
Steve Müller's avatar
Steve Müller committed
190

191 192 193 194
        return $sameColumns;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
195 196
     * Checks if the other index already fulfills all the indexing and constraint needs of the current one.
     *
197
     * @param Index $other
198
     *
Benjamin Morel's avatar
Benjamin Morel committed
199
     * @return boolean
200 201 202 203 204 205 206 207 208 209 210 211 212
     */
    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) {
213
            if ( ! $this->samePartialIndex($other)) {
214
                return false;
215 216
            }

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

            if ($other->isPrimary() != $this->isPrimary()) {
226
                return false;
227 228 229
            }

            if ($other->isUnique() != $this->isUnique()) {
230 231
                return false;
            }
Benjamin Morel's avatar
Benjamin Morel committed
232

233 234
            return true;
        }
Benjamin Morel's avatar
Benjamin Morel committed
235

236 237 238 239
        return false;
    }

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

254
        if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique()) && $this->samePartialIndex($other)) {
255 256
            return true;
        }
Benjamin Morel's avatar
Benjamin Morel committed
257

258 259
        return false;
    }
260

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

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

284 285 286 287 288 289 290
        return $this;
    }

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

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

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

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

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

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

351
        if ( ! $this->hasOption('where') && ! $other->hasOption('where')) {
352 353
            return true;
        }
354 355

        return false;
356 357
    }

358
}