Table.php 21.6 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
        $this->_idGeneratorType = $idGeneratorType;
88

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

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

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

        $this->_options = $options;
    }

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

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

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

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

        return $primaryKey;
144 145 146
    }

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

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

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

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

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

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

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

226 227 228
        return false;
    }

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

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

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

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

258 259 260 261 262
        return $this;
    }

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

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

274
        return $column;
275 276 277
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
278
     * Renames a Column.
279 280 281
     *
     * @param string $oldColumnName
     * @param string $newColumnName
Benjamin Morel's avatar
Benjamin Morel committed
282 283 284 285
     *
     * @return \Doctrine\DBAL\Schema\Table
     *
     * @throws \Doctrine\DBAL\DBALException
286 287 288
     */
    public function renameColumn($oldColumnName, $newColumnName)
    {
289 290 291
        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.");
292 293 294
    }

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

307 308 309 310
        return $this;
    }

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

322 323 324 325
        return $this;
    }

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

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

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

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

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

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

400 401 402 403 404 405
        return $this;
    }

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

413 414 415 416
        return $this;
    }

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

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

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

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

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

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

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

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

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

473 474 475 476
        return $this;
    }

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

485 486 487 488
        if(strlen($constraint->getName())) {
            $name = $constraint->getName();
        } else {
            $name = $this->_generateIdentifierName(
489
                array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength()
490 491
            );
        }
492
        $name = strtolower($name);
493 494

        $this->_fkConstraints[$name] = $constraint;
Pascal Borreli's avatar
Pascal Borreli committed
495
        // add an explicit index on the foreign key columns. If there is already an index that fulfils this requirements drop the request.
496
        // 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
497
        // lead to duplicates. This creates computation overhead in this case, however no duplicate indexes are ever added (based on columns).
498
        $this->addIndex($constraint->getColumns());
499 500 501
    }

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
516 517 518 519 520 521 522
     * 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.
523 524 525
     */
    public function getForeignKey($constraintName)
    {
526
        $constraintName = strtolower($constraintName);
527
        if(!$this->hasForeignKey($constraintName)) {
528
            throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
529 530 531
        }

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

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

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

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

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

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

        uksort($columns, function($a, $b) use($colNames) {
            return (array_search($a, $colNames) >= array_search($b, $colNames));
        });
Benjamin Morel's avatar
Benjamin Morel committed
575

576
        return $columns;
577 578 579
    }

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

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

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

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

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

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

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

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

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

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
667 668 669 670 671 672 673
     * 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.
674 675 676
     */
    public function getIndex($indexName)
    {
677
        $indexName = strtolower($indexName);
678
        if ( ! $this->hasIndex($indexName)) {
679
            throw SchemaException::indexDoesNotExist($indexName, $this->_name);
680
        }
Benjamin Morel's avatar
Benjamin Morel committed
681

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

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

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

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

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

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

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

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

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

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

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