Table.php 21.7 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 22
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Schema;

use Doctrine\DBAL\Types\Type;
23
use Doctrine\DBAL\Schema\Visitor\Visitor;
24
use Doctrine\DBAL\DBALException;
25 26

/**
Benjamin Morel's avatar
Benjamin Morel committed
27
 * Object Representation of a table.
28
 *
Benjamin Morel's avatar
Benjamin Morel committed
29 30 31
 * @link   www.doctrine-project.org
 * @since  2.0
 * @author Benjamin Eberlei <kontakt@beberlei.de>
32 33 34 35 36 37 38 39 40
 */
class Table extends AbstractAsset
{
    /**
     * @var string
     */
    protected $_name = null;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
41
     * @var \Doctrine\DBAL\Schema\Column[]
42 43 44 45
     */
    protected $_columns = array();

    /**
Benjamin Morel's avatar
Benjamin Morel committed
46
     * @var \Doctrine\DBAL\Schema\Index[]
47 48 49 50 51 52 53 54 55
     */
    protected $_indexes = array();

    /**
     * @var string
     */
    protected $_primaryKeyName = false;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
56
     * @var \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
57
     */
58
    protected $_fkConstraints = array();
59 60 61 62 63 64

    /**
     * @var array
     */
    protected $_options = array();

65
    /**
Benjamin Morel's avatar
Benjamin Morel committed
66
     * @var \Doctrine\DBAL\Schema\SchemaConfig
67 68 69
     */
    protected $_schemaConfig = null;

70
    /**
Benjamin Morel's avatar
Benjamin Morel committed
71 72 73 74 75 76
     * @param string  $tableName
     * @param array   $columns
     * @param array   $indexes
     * @param array   $fkConstraints
     * @param integer $idGeneratorType
     * @param array   $options
77
     *
Benjamin Morel's avatar
Benjamin Morel committed
78
     * @throws \Doctrine\DBAL\DBALException
79
     */
80
    public function __construct($tableName, array $columns=array(), array $indexes=array(), array $fkConstraints=array(), $idGeneratorType = 0, array $options=array())
81
    {
82 83 84 85
        if (strlen($tableName) == 0) {
            throw DBALException::invalidTableName($tableName);
        }

86
        $this->_setName($tableName);
87

88
        foreach ($columns as $column) {
89 90
            $this->_addColumn($column);
        }
91

92
        foreach ($indexes as $idx) {
93 94 95
            $this->_addIndex($idx);
        }

96
        foreach ($fkConstraints as $constraint) {
97
            $this->_addForeignKeyConstraint($constraint);
98 99 100 101 102
        }

        $this->_options = $options;
    }

103
    /**
Benjamin Morel's avatar
Benjamin Morel committed
104 105 106
     * @param \Doctrine\DBAL\Schema\SchemaConfig $schemaConfig
     *
     * @return void
107 108 109 110 111 112 113
     */
    public function setSchemaConfig(SchemaConfig $schemaConfig)
    {
        $this->_schemaConfig = $schemaConfig;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
114
     * @return integer
115 116 117 118 119 120 121 122 123 124
     */
    protected function _getMaxIdentifierLength()
    {
        if ($this->_schemaConfig instanceof SchemaConfig) {
            return $this->_schemaConfig->getMaxIdentifierLength();
        } else {
            return 63;
        }
    }

125
    /**
Benjamin Morel's avatar
Benjamin Morel committed
126
     * Sets the Primary Key.
127
     *
Benjamin Morel's avatar
Benjamin Morel committed
128 129 130 131
     * @param array          $columns
     * @param string|boolean $indexName
     *
     * @return \Doctrine\DBAL\Schema\Table
132
     */
133
    public function setPrimaryKey(array $columns, $indexName = false)
134
    {
135 136
        $primaryKey = $this->_createIndex($columns, $indexName ?: "primary", true, true);

137
        foreach ($columns as $columnName) {
138 139 140 141 142
            $column = $this->getColumn($columnName);
            $column->setNotnull(true);
        }

        return $primaryKey;
143 144 145
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
146 147 148 149 150
     * @param array       $columnNames
     * @param string|null $indexName
     * @param array       $flags
     *
     * @return \Doctrine\DBAL\Schema\Table
151
     */
152
    public function addIndex(array $columnNames, $indexName = null, array $flags = array())
153
    {
Steve Müller's avatar
Steve Müller committed
154
        if ($indexName == null) {
155
            $indexName = $this->_generateIdentifierName(
156
                array_merge(array($this->getName()), $columnNames), "idx", $this->_getMaxIdentifierLength()
157
            );
158 159
        }

160
        return $this->_createIndex($columnNames, $indexName, false, false, $flags);
161 162
    }

163
    /**
Benjamin Morel's avatar
Benjamin Morel committed
164
     * Drops the primary key from this table.
165 166 167 168 169 170
     *
     * @return void
     */
    public function dropPrimaryKey()
    {
        $this->dropIndex($this->_primaryKeyName);
171
        $this->_primaryKeyName = false;
172 173 174
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
175 176 177
     * Drops an index from this table.
     *
     * @param string $indexName The index name.
178 179
     *
     * @return void
Benjamin Morel's avatar
Benjamin Morel committed
180 181
     *
     * @throws \Doctrine\DBAL\Schema\SchemaException If the index does not exist.
182 183 184 185
     */
    public function dropIndex($indexName)
    {
        $indexName = strtolower($indexName);
186
        if ( ! $this->hasIndex($indexName)) {
187 188 189 190 191
            throw SchemaException::indexDoesNotExist($indexName, $this->_name);
        }
        unset($this->_indexes[$indexName]);
    }

192
    /**
Benjamin Morel's avatar
Benjamin Morel committed
193 194
     * @param array       $columnNames
     * @param string|null $indexName
195
     *
Benjamin Morel's avatar
Benjamin Morel committed
196
     * @return \Doctrine\DBAL\Schema\Table
197
     */
198
    public function addUniqueIndex(array $columnNames, $indexName = null)
199
    {
200
        if ($indexName === null) {
201
            $indexName = $this->_generateIdentifierName(
202
                array_merge(array($this->getName()), $columnNames), "uniq", $this->_getMaxIdentifierLength()
203
            );
204 205
        }

206 207 208
        return $this->_createIndex($columnNames, $indexName, true, false);
    }

209
    /**
Benjamin Morel's avatar
Benjamin Morel committed
210 211 212
     * Checks if an index begins in the order of the given columns.
     *
     * @param array $columnsNames
213
     *
Benjamin Morel's avatar
Benjamin Morel committed
214
     * @return boolean
215 216 217
     */
    public function columnsAreIndexed(array $columnsNames)
    {
218
        foreach ($this->getIndexes() as $index) {
219 220
            /* @var $index Index */
            if ($index->spansColumns($columnsNames)) {
221 222 223
                return true;
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
224

225 226 227
        return false;
    }

228
    /**
Benjamin Morel's avatar
Benjamin Morel committed
229 230 231 232 233
     * @param array   $columnNames
     * @param string  $indexName
     * @param boolean $isUnique
     * @param boolean $isPrimary
     * @param array   $flags
234
     *
Benjamin Morel's avatar
Benjamin Morel committed
235 236 237
     * @return \Doctrine\DBAL\Schema\Table
     *
     * @throws \Doctrine\DBAL\Schema\SchemaException
238
     */
239
    private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = array())
240 241 242 243 244
    {
        if (preg_match('(([^a-zA-Z0-9_]+))', $indexName)) {
            throw SchemaException::indexNameInvalid($indexName);
        }

245
        foreach ($columnNames as $columnName => $indexColOptions) {
beberlei's avatar
beberlei committed
246 247 248 249
            if (is_numeric($columnName) && is_string($indexColOptions)) {
                $columnName = $indexColOptions;
            }

250
            if ( ! $this->hasColumn($columnName)) {
251
                throw SchemaException::columnDoesNotExist($columnName, $this->_name);
252 253
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
254

255
        $this->_addIndex(new Index($indexName, $columnNames, $isUnique, $isPrimary, $flags));
Benjamin Morel's avatar
Benjamin Morel committed
256

257 258 259 260 261
        return $this;
    }

    /**
     * @param string $columnName
Benjamin Morel's avatar
Benjamin Morel committed
262 263 264 265
     * @param string $typeName
     * @param array  $options
     *
     * @return \Doctrine\DBAL\Schema\Column
266
     */
267
    public function addColumn($columnName, $typeName, array $options=array())
268 269 270 271
    {
        $column = new Column($columnName, Type::getType($typeName), $options);

        $this->_addColumn($column);
Benjamin Morel's avatar
Benjamin Morel committed
272

273
        return $column;
274 275 276
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
277
     * Renames a Column.
278 279 280
     *
     * @param string $oldColumnName
     * @param string $newColumnName
Benjamin Morel's avatar
Benjamin Morel committed
281 282 283 284
     *
     * @return \Doctrine\DBAL\Schema\Table
     *
     * @throws \Doctrine\DBAL\DBALException
285 286 287
     */
    public function renameColumn($oldColumnName, $newColumnName)
    {
288 289 290
        throw new DBALException("Table#renameColumn() was removed, because it drops and recreates " .
            "the column instead. There is no fix available, because a schema diff cannot reliably detect if a " .
            "column was renamed or one column was created and another one dropped.");
291 292 293
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
294
     * Change Column Details.
295
     *
296
     * @param string $columnName
Benjamin Morel's avatar
Benjamin Morel committed
297 298 299
     * @param array  $options
     *
     * @return \Doctrine\DBAL\Schema\Table
300 301 302 303 304
     */
    public function changeColumn($columnName, array $options)
    {
        $column = $this->getColumn($columnName);
        $column->setOptions($options);
Benjamin Morel's avatar
Benjamin Morel committed
305

306 307 308 309
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
310
     * Drops a Column from the Table.
311
     *
312
     * @param string $columnName
Benjamin Morel's avatar
Benjamin Morel committed
313 314
     *
     * @return \Doctrine\DBAL\Schema\Table
315 316 317
     */
    public function dropColumn($columnName)
    {
318
        $columnName = strtolower($columnName);
319
        unset($this->_columns[$columnName]);
Benjamin Morel's avatar
Benjamin Morel committed
320

321 322 323 324
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
325
     * Adds a foreign key constraint.
326
     *
Benjamin Morel's avatar
Benjamin Morel committed
327
     * Name is inferred from the local columns.
328
     *
329 330 331 332 333
     * @param \Doctrine\DBAL\Schema\Table|string  $foreignTable Table schema instance or table name
     * @param array                               $localColumnNames
     * @param array                               $foreignColumnNames
     * @param array                               $options
     * @param string|null                         $constraintName
Benjamin Morel's avatar
Benjamin Morel committed
334 335
     *
     * @return \Doctrine\DBAL\Schema\Table
336
     */
337
    public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array(), $constraintName = null)
338
    {
339
        $constraintName = $constraintName ?: $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength());
Benjamin Morel's avatar
Benjamin Morel committed
340

341
        return $this->addNamedForeignKeyConstraint($constraintName, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
342 343
    }

344
    /**
Benjamin Morel's avatar
Benjamin Morel committed
345
     * Adds a foreign key constraint.
346
     *
Pascal Borreli's avatar
Pascal Borreli committed
347
     * Name is to be generated by the database itself.
348
     *
349
     * @deprecated Use {@link addForeignKeyConstraint}
Benjamin Morel's avatar
Benjamin Morel committed
350
     *
351 352 353 354
     * @param \Doctrine\DBAL\Schema\Table|string    $foreignTable Table schema instance or table name
     * @param array                                 $localColumnNames
     * @param array                                 $foreignColumnNames
     * @param array                                 $options
Benjamin Morel's avatar
Benjamin Morel committed
355 356
     *
     * @return \Doctrine\DBAL\Schema\Table
357 358 359
     */
    public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
    {
360
        return $this->addForeignKeyConstraint($foreignTable, $localColumnNames, $foreignColumnNames, $options);
361 362
    }

363
    /**
Benjamin Morel's avatar
Benjamin Morel committed
364
     * Adds a foreign key constraint with a given name.
365
     *
366
     * @deprecated Use {@link addForeignKeyConstraint}
Benjamin Morel's avatar
Benjamin Morel committed
367
     *
368 369 370 371 372
     * @param string                                $name
     * @param \Doctrine\DBAL\Schema\Table|string    $foreignTable Table schema instance or table name
     * @param array                                 $localColumnNames
     * @param array                                 $foreignColumnNames
     * @param array                                 $options
Benjamin Morel's avatar
Benjamin Morel committed
373 374 375 376
     *
     * @return \Doctrine\DBAL\Schema\Table
     *
     * @throws \Doctrine\DBAL\Schema\SchemaException
377 378
     */
    public function addNamedForeignKeyConstraint($name, $foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
379
    {
380
        if ($foreignTable instanceof Table) {
381
            foreach ($foreignColumnNames as $columnName) {
382
                if ( ! $foreignTable->hasColumn($columnName)) {
383
                    throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName());
384
                }
385 386
            }
        }
387

388
        foreach ($localColumnNames as $columnName) {
389
            if ( ! $this->hasColumn($columnName)) {
390
                throw SchemaException::columnDoesNotExist($columnName, $this->_name);
391 392
            }
        }
393

394
        $constraint = new ForeignKeyConstraint(
395
            $localColumnNames, $foreignTable, $foreignColumnNames, $name, $options
396
        );
397
        $this->_addForeignKeyConstraint($constraint);
398

399 400 401 402 403 404
        return $this;
    }

    /**
     * @param string $name
     * @param string $value
Benjamin Morel's avatar
Benjamin Morel committed
405 406
     *
     * @return \Doctrine\DBAL\Schema\Table
407 408 409 410
     */
    public function addOption($name, $value)
    {
        $this->_options[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
411

412 413 414 415
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
416 417 418 419 420
     * @param \Doctrine\DBAL\Schema\Column $column
     *
     * @return void
     *
     * @throws \Doctrine\DBAL\Schema\SchemaException
421 422 423
     */
    protected function _addColumn(Column $column)
    {
424 425 426
        $columnName = $column->getName();
        $columnName = strtolower($columnName);

427
        if (isset($this->_columns[$columnName])) {
428
            throw SchemaException::columnAlreadyExists($this->getName(), $columnName);
429 430 431 432 433 434
        }

        $this->_columns[$columnName] = $column;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
435
     * Adds an index to the table.
436
     *
Benjamin Morel's avatar
Benjamin Morel committed
437 438 439 440 441
     * @param \Doctrine\DBAL\Schema\Index $indexCandidate
     *
     * @return \Doctrine\DBAL\Schema\Table
     *
     * @throws \Doctrine\DBAL\Schema\SchemaException
442
     */
443
    protected function _addIndex(Index $indexCandidate)
444
    {
445
        // check for duplicates
446
        foreach ($this->_indexes as $existingIndex) {
447
            if ($indexCandidate->isFullfilledBy($existingIndex)) {
448 449 450 451
                return $this;
            }
        }

452
        $indexName = $indexCandidate->getName();
453
        $indexName = strtolower($indexName);
454

455
        if (isset($this->_indexes[$indexName]) || ($this->_primaryKeyName != false && $indexCandidate->isPrimary())) {
456
            throw SchemaException::indexAlreadyExists($indexName, $this->_name);
457 458
        }

459
        // remove overruled indexes
460
        foreach ($this->_indexes as $idxKey => $existingIndex) {
461 462 463 464 465 466
            if ($indexCandidate->overrules($existingIndex)) {
                unset($this->_indexes[$idxKey]);
            }
        }

        if ($indexCandidate->isPrimary()) {
467 468 469
            $this->_primaryKeyName = $indexName;
        }

470
        $this->_indexes[$indexName] = $indexCandidate;
Benjamin Morel's avatar
Benjamin Morel committed
471

472 473 474 475
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
476 477 478
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $constraint
     *
     * @return void
479
     */
480
    protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
481
    {
482
        $constraint->setLocalTable($this);
483

Steve Müller's avatar
Steve Müller committed
484
        if (strlen($constraint->getName())) {
485 486 487
            $name = $constraint->getName();
        } else {
            $name = $this->_generateIdentifierName(
488
                array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength()
489 490
            );
        }
491
        $name = strtolower($name);
492 493

        $this->_fkConstraints[$name] = $constraint;
Pascal Borreli's avatar
Pascal Borreli committed
494
        // add an explicit index on the foreign key columns. If there is already an index that fulfils this requirements drop the request.
495
        // In the case of __construct calling this method during hydration from schema-details all the explicitly added indexes
Pascal Borreli's avatar
Pascal Borreli committed
496
        // lead to duplicates. This creates computation overhead in this case, however no duplicate indexes are ever added (based on columns).
497
        $this->addIndex($constraint->getColumns());
498 499 500
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
501 502 503 504 505
     * Returns whether this table has a foreign key constraint with the given name.
     *
     * @param string $constraintName
     *
     * @return boolean
506 507 508
     */
    public function hasForeignKey($constraintName)
    {
509
        $constraintName = strtolower($constraintName);
Benjamin Morel's avatar
Benjamin Morel committed
510

511 512 513 514
        return isset($this->_fkConstraints[$constraintName]);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
515 516 517 518 519 520 521
     * Returns the foreign key constraint with the given name.
     *
     * @param string $constraintName The constraint name.
     *
     * @return \Doctrine\DBAL\Schema\ForeignKeyConstraint
     *
     * @throws \Doctrine\DBAL\Schema\SchemaException If the foreign key does not exist.
522 523 524
     */
    public function getForeignKey($constraintName)
    {
525
        $constraintName = strtolower($constraintName);
Steve Müller's avatar
Steve Müller committed
526
        if (!$this->hasForeignKey($constraintName)) {
527
            throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
528 529 530
        }

        return $this->_fkConstraints[$constraintName];
531 532
    }

Benjamin Morel's avatar
Benjamin Morel committed
533 534 535 536 537 538 539 540 541
    /**
     * Removes the foreign key constraint with the given name.
     *
     * @param string $constraintName The constraint name.
     *
     * @return void
     *
     * @throws \Doctrine\DBAL\Schema\SchemaException
     */
542 543 544
    public function removeForeignKey($constraintName)
    {
        $constraintName = strtolower($constraintName);
Steve Müller's avatar
Steve Müller committed
545
        if (!$this->hasForeignKey($constraintName)) {
546 547 548 549 550 551
            throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
        }

        unset($this->_fkConstraints[$constraintName]);
    }

552
    /**
Benjamin Morel's avatar
Benjamin Morel committed
553
     * @return \Doctrine\DBAL\Schema\Column[]
554 555 556
     */
    public function getColumns()
    {
557 558 559 560 561
        $columns = $this->_columns;

        $pkCols = array();
        $fkCols = array();

562
        if ($this->hasPrimaryKey()) {
563 564
            $pkCols = $this->getPrimaryKey()->getColumns();
        }
565
        foreach ($this->getForeignKeys() as $fk) {
566 567 568 569 570
            /* @var $fk ForeignKeyConstraint */
            $fkCols = array_merge($fkCols, $fk->getColumns());
        }
        $colNames = array_unique(array_merge($pkCols, $fkCols, array_keys($columns)));

Steve Müller's avatar
Steve Müller committed
571
        uksort($columns, function ($a, $b) use ($colNames) {
572 573
            return (array_search($a, $colNames) >= array_search($b, $colNames));
        });
Benjamin Morel's avatar
Benjamin Morel committed
574

575
        return $columns;
576 577 578
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
579
     * Returns whether this table has a Column with the given name.
580
     *
Benjamin Morel's avatar
Benjamin Morel committed
581 582 583
     * @param string $columnName The column name.
     *
     * @return boolean
584 585 586
     */
    public function hasColumn($columnName)
    {
587
        $columnName = $this->trimQuotes(strtolower($columnName));
Benjamin Morel's avatar
Benjamin Morel committed
588

589 590 591 592
        return isset($this->_columns[$columnName]);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
593 594 595
     * Returns the Column with the given name.
     *
     * @param string $columnName The column name.
596
     *
Benjamin Morel's avatar
Benjamin Morel committed
597 598 599
     * @return \Doctrine\DBAL\Schema\Column
     *
     * @throws \Doctrine\DBAL\Schema\SchemaException If the column does not exist.
600 601 602
     */
    public function getColumn($columnName)
    {
603
        $columnName = strtolower($this->trimQuotes($columnName));
604
        if ( ! $this->hasColumn($columnName)) {
605
            throw SchemaException::columnDoesNotExist($columnName, $this->_name);
606 607 608 609 610 611
        }

        return $this->_columns[$columnName];
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
612 613 614
     * Returns the primary key.
     *
     * @return \Doctrine\DBAL\Schema\Index|null The primary key, or null if this Table has no primary key.
615 616 617
     */
    public function getPrimaryKey()
    {
618
        if ( ! $this->hasPrimaryKey()) {
619 620
            return null;
        }
Benjamin Morel's avatar
Benjamin Morel committed
621

622 623 624
        return $this->getIndex($this->_primaryKeyName);
    }

Benjamin Morel's avatar
Benjamin Morel committed
625 626 627 628 629 630 631
    /**
     * Returns the primary key columns.
     *
     * @return array
     *
     * @throws \Doctrine\DBAL\DBALException
     */
632 633 634 635 636
    public function getPrimaryKeyColumns()
    {
        if ( ! $this->hasPrimaryKey()) {
            throw new DBALException("Table " . $this->getName() . " has no primary key.");
        }
Benjamin Morel's avatar
Benjamin Morel committed
637

638 639 640
        return $this->getPrimaryKey()->getColumns();
    }

641
    /**
Benjamin Morel's avatar
Benjamin Morel committed
642
     * Returns whether this table has a primary key.
643
     *
Benjamin Morel's avatar
Benjamin Morel committed
644
     * @return boolean
645 646 647 648 649 650
     */
    public function hasPrimaryKey()
    {
        return ($this->_primaryKeyName && $this->hasIndex($this->_primaryKeyName));
    }

651
    /**
Benjamin Morel's avatar
Benjamin Morel committed
652 653 654 655 656
     * Returns whether this table has an Index with the given name.
     *
     * @param string $indexName The index name.
     *
     * @return boolean
657 658 659
     */
    public function hasIndex($indexName)
    {
660
        $indexName = strtolower($indexName);
Benjamin Morel's avatar
Benjamin Morel committed
661

662 663 664 665
        return (isset($this->_indexes[$indexName]));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
666 667 668 669 670 671 672
     * Returns the Index with the given name.
     *
     * @param string $indexName The index name.
     *
     * @return \Doctrine\DBAL\Schema\Index
     *
     * @throws \Doctrine\DBAL\Schema\SchemaException If the index does not exist.
673 674 675
     */
    public function getIndex($indexName)
    {
676
        $indexName = strtolower($indexName);
677
        if ( ! $this->hasIndex($indexName)) {
678
            throw SchemaException::indexDoesNotExist($indexName, $this->_name);
679
        }
Benjamin Morel's avatar
Benjamin Morel committed
680

681 682 683 684
        return $this->_indexes[$indexName];
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
685
     * @return \Doctrine\DBAL\Schema\Index[]
686 687 688 689 690 691 692
     */
    public function getIndexes()
    {
        return $this->_indexes;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
693
     * Returns the foreign key constraints.
694
     *
Benjamin Morel's avatar
Benjamin Morel committed
695
     * @return \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
696
     */
697
    public function getForeignKeys()
698
    {
699
        return $this->_fkConstraints;
700 701
    }

Benjamin Morel's avatar
Benjamin Morel committed
702 703 704 705 706
    /**
     * @param string $name
     *
     * @return boolean
     */
707 708 709 710 711
    public function hasOption($name)
    {
        return isset($this->_options[$name]);
    }

Benjamin Morel's avatar
Benjamin Morel committed
712 713 714 715 716
    /**
     * @param string $name
     *
     * @return mixed
     */
717 718 719 720 721
    public function getOption($name)
    {
        return $this->_options[$name];
    }

Benjamin Morel's avatar
Benjamin Morel committed
722 723 724
    /**
     * @return array
     */
725 726 727 728 729
    public function getOptions()
    {
        return $this->_options;
    }

730
    /**
Benjamin Morel's avatar
Benjamin Morel committed
731 732 733
     * @param \Doctrine\DBAL\Schema\Visitor\Visitor $visitor
     *
     * @return void
734 735 736 737 738
     */
    public function visit(Visitor $visitor)
    {
        $visitor->acceptTable($this);

739
        foreach ($this->getColumns() as $column) {
740
            $visitor->acceptColumn($this, $column);
741 742
        }

743
        foreach ($this->getIndexes() as $index) {
744 745 746
            $visitor->acceptIndex($this, $index);
        }

747
        foreach ($this->getForeignKeys() as $constraint) {
748
            $visitor->acceptForeignKey($this, $constraint);
749 750
        }
    }
751 752

    /**
Benjamin Morel's avatar
Benjamin Morel committed
753 754 755
     * Clone of a Table triggers a deep clone of all affected assets.
     *
     * @return void
756 757 758
     */
    public function __clone()
    {
759
        foreach ($this->_columns as $k => $column) {
760 761
            $this->_columns[$k] = clone $column;
        }
762
        foreach ($this->_indexes as $k => $index) {
763 764
            $this->_indexes[$k] = clone $index;
        }
765
        foreach ($this->_fkConstraints as $k => $fk) {
766 767 768 769
            $this->_fkConstraints[$k] = clone $fk;
            $this->_fkConstraints[$k]->setLocalTable($this);
        }
    }
770
}