Table.php 22.9 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema;

5
use Doctrine\DBAL\DBALException;
6 7
use Doctrine\DBAL\Schema\Visitor\Visitor;
use Doctrine\DBAL\Types\Type;
8 9 10 11 12 13 14
use const ARRAY_FILTER_USE_KEY;
use function array_filter;
use function array_merge;
use function in_array;
use function preg_match;
use function strlen;
use function strtolower;
15 16

/**
Benjamin Morel's avatar
Benjamin Morel committed
17
 * Object Representation of a table.
18 19 20
 */
class Table extends AbstractAsset
{
21
    /** @var Column[] */
22
    protected $_columns = [];
23

24
    /** @var Index[] */
25
    private $implicitIndexes = [];
26

27
    /** @var Index[] */
28
    protected $_indexes = [];
29

30
    /** @var string */
31 32
    protected $_primaryKeyName = false;

33
    /** @var ForeignKeyConstraint[] */
34
    protected $_fkConstraints = [];
35

36
    /** @var mixed[] */
37
    protected $_options = [];
38

39
    /** @var SchemaConfig|null */
40 41
    protected $_schemaConfig = null;

42
    /**
43 44 45 46
     * @param string                 $tableName
     * @param Column[]               $columns
     * @param Index[]                $indexes
     * @param ForeignKeyConstraint[] $fkConstraints
47
     * @param int                    $idGeneratorType
48
     * @param mixed[]                $options
49
     *
50
     * @throws DBALException
51
     */
52
    public function __construct($tableName, array $columns = [], array $indexes = [], array $fkConstraints = [], $idGeneratorType = 0, array $options = [])
53
    {
54
        if (strlen($tableName) === 0) {
55 56 57
            throw DBALException::invalidTableName($tableName);
        }

58
        $this->_setName($tableName);
59

60
        foreach ($columns as $column) {
61 62
            $this->_addColumn($column);
        }
63

64
        foreach ($indexes as $idx) {
65 66 67
            $this->_addIndex($idx);
        }

68
        foreach ($fkConstraints as $constraint) {
69
            $this->_addForeignKeyConstraint($constraint);
70 71 72 73 74
        }

        $this->_options = $options;
    }

75
    /**
Benjamin Morel's avatar
Benjamin Morel committed
76
     * @return void
77 78 79 80 81 82 83
     */
    public function setSchemaConfig(SchemaConfig $schemaConfig)
    {
        $this->_schemaConfig = $schemaConfig;
    }

    /**
84
     * @return int
85 86 87 88 89 90
     */
    protected function _getMaxIdentifierLength()
    {
        if ($this->_schemaConfig instanceof SchemaConfig) {
            return $this->_schemaConfig->getMaxIdentifierLength();
        }
Gabriel Caruso's avatar
Gabriel Caruso committed
91 92

        return 63;
93 94
    }

95
    /**
Benjamin Morel's avatar
Benjamin Morel committed
96
     * Sets the Primary Key.
97
     *
Sergei Morozov's avatar
Sergei Morozov committed
98 99
     * @param string[]     $columnNames
     * @param string|false $indexName
Benjamin Morel's avatar
Benjamin Morel committed
100
     *
101
     * @return self
102
     */
103
    public function setPrimaryKey(array $columnNames, $indexName = false)
104
    {
105
        $this->_addIndex($this->_createIndex($columnNames, $indexName ?: 'primary', true, true));
106

107
        foreach ($columnNames as $columnName) {
108 109 110 111
            $column = $this->getColumn($columnName);
            $column->setNotnull(true);
        }

112
        return $this;
113 114 115
    }

    /**
116
     * @param string[]    $columnNames
Benjamin Morel's avatar
Benjamin Morel committed
117
     * @param string|null $indexName
118 119
     * @param string[]    $flags
     * @param mixed[]     $options
Benjamin Morel's avatar
Benjamin Morel committed
120
     *
121
     * @return self
122
     */
123
    public function addIndex(array $columnNames, $indexName = null, array $flags = [], array $options = [])
124
    {
125
        if ($indexName === null) {
126
            $indexName = $this->_generateIdentifierName(
127 128 129
                array_merge([$this->getName()], $columnNames),
                'idx',
                $this->_getMaxIdentifierLength()
130
            );
131 132
        }

133
        return $this->_addIndex($this->_createIndex($columnNames, $indexName, false, false, $flags, $options));
134 135
    }

136
    /**
Benjamin Morel's avatar
Benjamin Morel committed
137
     * Drops the primary key from this table.
138 139 140 141 142 143
     *
     * @return void
     */
    public function dropPrimaryKey()
    {
        $this->dropIndex($this->_primaryKeyName);
144
        $this->_primaryKeyName = false;
145 146 147
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
148 149 150
     * Drops an index from this table.
     *
     * @param string $indexName The index name.
151 152
     *
     * @return void
Benjamin Morel's avatar
Benjamin Morel committed
153
     *
154
     * @throws SchemaException If the index does not exist.
155 156 157
     */
    public function dropIndex($indexName)
    {
158
        $indexName = $this->normalizeIdentifier($indexName);
159
        if (! $this->hasIndex($indexName)) {
160 161 162 163 164
            throw SchemaException::indexDoesNotExist($indexName, $this->_name);
        }
        unset($this->_indexes[$indexName]);
    }

165
    /**
166
     * @param string[]    $columnNames
Benjamin Morel's avatar
Benjamin Morel committed
167
     * @param string|null $indexName
168
     * @param mixed[]     $options
169
     *
170
     * @return self
171
     */
172
    public function addUniqueIndex(array $columnNames, $indexName = null, array $options = [])
173
    {
174
        if ($indexName === null) {
175
            $indexName = $this->_generateIdentifierName(
176 177 178
                array_merge([$this->getName()], $columnNames),
                'uniq',
                $this->_getMaxIdentifierLength()
179
            );
180 181
        }

182
        return $this->_addIndex($this->_createIndex($columnNames, $indexName, true, false, [], $options));
183 184
    }

185 186 187 188 189 190 191
    /**
     * Renames an index.
     *
     * @param string      $oldIndexName The name of the index to rename from.
     * @param string|null $newIndexName The name of the index to rename to.
     *                                  If null is given, the index name will be auto-generated.
     *
192
     * @return self This table instance.
193
     *
194
     * @throws SchemaException If no index exists for the given current name
195 196 197 198
     *                         or if an index with the given new name already exists on this table.
     */
    public function renameIndex($oldIndexName, $newIndexName = null)
    {
199 200
        $oldIndexName           = $this->normalizeIdentifier($oldIndexName);
        $normalizedNewIndexName = $this->normalizeIdentifier($newIndexName);
201 202 203 204 205

        if ($oldIndexName === $normalizedNewIndexName) {
            return $this;
        }

206
        if (! $this->hasIndex($oldIndexName)) {
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
            throw SchemaException::indexDoesNotExist($oldIndexName, $this->_name);
        }

        if ($this->hasIndex($normalizedNewIndexName)) {
            throw SchemaException::indexAlreadyExists($normalizedNewIndexName, $this->_name);
        }

        $oldIndex = $this->_indexes[$oldIndexName];

        if ($oldIndex->isPrimary()) {
            $this->dropPrimaryKey();

            return $this->setPrimaryKey($oldIndex->getColumns(), $newIndexName);
        }

        unset($this->_indexes[$oldIndexName]);

        if ($oldIndex->isUnique()) {
225
            return $this->addUniqueIndex($oldIndex->getColumns(), $newIndexName, $oldIndex->getOptions());
226 227
        }

228
        return $this->addIndex($oldIndex->getColumns(), $newIndexName, $oldIndex->getFlags(), $oldIndex->getOptions());
229 230
    }

231
    /**
Benjamin Morel's avatar
Benjamin Morel committed
232 233
     * Checks if an index begins in the order of the given columns.
     *
234
     * @param string[] $columnNames
235
     *
236
     * @return bool
237
     */
238
    public function columnsAreIndexed(array $columnNames)
239
    {
240
        foreach ($this->getIndexes() as $index) {
241
            /** @var $index Index */
242
            if ($index->spansColumns($columnNames)) {
243 244 245
                return true;
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
246

247 248 249
        return false;
    }

250
    /**
251 252 253 254 255 256
     * @param string[] $columnNames
     * @param string   $indexName
     * @param bool     $isUnique
     * @param bool     $isPrimary
     * @param string[] $flags
     * @param mixed[]  $options
257
     *
258
     * @return Index
Benjamin Morel's avatar
Benjamin Morel committed
259
     *
260
     * @throws SchemaException
261
     */
262
    private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = [], array $options = [])
263
    {
264
        if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) {
265 266 267
            throw SchemaException::indexNameInvalid($indexName);
        }

268
        foreach ($columnNames as $columnName) {
269
            if (! $this->hasColumn($columnName)) {
270
                throw SchemaException::columnDoesNotExist($columnName, $this->_name);
271 272
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
273

274
        return new Index($indexName, $columnNames, $isUnique, $isPrimary, $flags, $options);
275 276 277
    }

    /**
278 279 280
     * @param string  $columnName
     * @param string  $typeName
     * @param mixed[] $options
Benjamin Morel's avatar
Benjamin Morel committed
281
     *
282
     * @return Column
283
     */
284
    public function addColumn($columnName, $typeName, array $options = [])
285 286 287 288
    {
        $column = new Column($columnName, Type::getType($typeName), $options);

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

290
        return $column;
291 292 293
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
294
     * Renames a Column.
295
     *
296 297
     * @deprecated
     *
298 299
     * @param string $oldColumnName
     * @param string $newColumnName
Benjamin Morel's avatar
Benjamin Morel committed
300
     *
301
     * @throws DBALException
302 303 304
     */
    public function renameColumn($oldColumnName, $newColumnName)
    {
305 306 307
        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.');
308 309 310
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
311
     * Change Column Details.
312
     *
313 314
     * @param string  $columnName
     * @param mixed[] $options
Benjamin Morel's avatar
Benjamin Morel committed
315
     *
316
     * @return self
317 318 319 320 321
     */
    public function changeColumn($columnName, array $options)
    {
        $column = $this->getColumn($columnName);
        $column->setOptions($options);
Benjamin Morel's avatar
Benjamin Morel committed
322

323 324 325 326
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
327
     * Drops a Column from the Table.
328
     *
329
     * @param string $columnName
Benjamin Morel's avatar
Benjamin Morel committed
330
     *
331
     * @return self
332 333 334
     */
    public function dropColumn($columnName)
    {
335
        $columnName = $this->normalizeIdentifier($columnName);
336
        unset($this->_columns[$columnName]);
Benjamin Morel's avatar
Benjamin Morel committed
337

338 339 340 341
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
342
     * Adds a foreign key constraint.
343
     *
Benjamin Morel's avatar
Benjamin Morel committed
344
     * Name is inferred from the local columns.
345
     *
346
     * @param Table|string $foreignTable       Table schema instance or table name
347 348 349
     * @param string[]     $localColumnNames
     * @param string[]     $foreignColumnNames
     * @param mixed[]      $options
350
     * @param string|null  $constraintName
Benjamin Morel's avatar
Benjamin Morel committed
351
     *
352
     * @return self
353
     */
354
    public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = [], $constraintName = null)
355
    {
356
        $constraintName = $constraintName ?: $this->_generateIdentifierName(array_merge((array) $this->getName(), $localColumnNames), 'fk', $this->_getMaxIdentifierLength());
Benjamin Morel's avatar
Benjamin Morel committed
357

358
        return $this->addNamedForeignKeyConstraint($constraintName, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
359 360
    }

361
    /**
Benjamin Morel's avatar
Benjamin Morel committed
362
     * Adds a foreign key constraint.
363
     *
Pascal Borreli's avatar
Pascal Borreli committed
364
     * Name is to be generated by the database itself.
365
     *
366
     * @deprecated Use {@link addForeignKeyConstraint}
Benjamin Morel's avatar
Benjamin Morel committed
367
     *
368
     * @param Table|string $foreignTable       Table schema instance or table name
369 370 371
     * @param string[]     $localColumnNames
     * @param string[]     $foreignColumnNames
     * @param mixed[]      $options
Benjamin Morel's avatar
Benjamin Morel committed
372
     *
373
     * @return self
374
     */
375
    public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = [])
376
    {
377
        return $this->addForeignKeyConstraint($foreignTable, $localColumnNames, $foreignColumnNames, $options);
378 379
    }

380
    /**
Benjamin Morel's avatar
Benjamin Morel committed
381
     * Adds a foreign key constraint with a given name.
382
     *
383
     * @deprecated Use {@link addForeignKeyConstraint}
Benjamin Morel's avatar
Benjamin Morel committed
384
     *
385
     * @param string       $name
386
     * @param Table|string $foreignTable       Table schema instance or table name
387 388 389
     * @param string[]     $localColumnNames
     * @param string[]     $foreignColumnNames
     * @param mixed[]      $options
Benjamin Morel's avatar
Benjamin Morel committed
390
     *
391
     * @return self
Benjamin Morel's avatar
Benjamin Morel committed
392
     *
393
     * @throws SchemaException
394
     */
395
    public function addNamedForeignKeyConstraint($name, $foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = [])
396
    {
397
        if ($foreignTable instanceof Table) {
398
            foreach ($foreignColumnNames as $columnName) {
399
                if (! $foreignTable->hasColumn($columnName)) {
400
                    throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName());
401
                }
402 403
            }
        }
404

405
        foreach ($localColumnNames as $columnName) {
406
            if (! $this->hasColumn($columnName)) {
407
                throw SchemaException::columnDoesNotExist($columnName, $this->_name);
408 409
            }
        }
410

411
        $constraint = new ForeignKeyConstraint(
412 413 414 415 416
            $localColumnNames,
            $foreignTable,
            $foreignColumnNames,
            $name,
            $options
417
        );
418
        $this->_addForeignKeyConstraint($constraint);
419

420 421 422 423 424
        return $this;
    }

    /**
     * @param string $name
Sergei Morozov's avatar
Sergei Morozov committed
425
     * @param mixed  $value
Benjamin Morel's avatar
Benjamin Morel committed
426
     *
427
     * @return self
428 429 430 431
     */
    public function addOption($name, $value)
    {
        $this->_options[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
432

433 434 435 436
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
437 438
     * @return void
     *
439
     * @throws SchemaException
440 441 442
     */
    protected function _addColumn(Column $column)
    {
443
        $columnName = $column->getName();
444
        $columnName = $this->normalizeIdentifier($columnName);
445

446
        if (isset($this->_columns[$columnName])) {
447
            throw SchemaException::columnAlreadyExists($this->getName(), $columnName);
448 449 450 451 452 453
        }

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
454
     * Adds an index to the table.
455
     *
456
     * @return self
Benjamin Morel's avatar
Benjamin Morel committed
457
     *
458
     * @throws SchemaException
459
     */
460
    protected function _addIndex(Index $indexCandidate)
461
    {
462 463
        $indexName               = $indexCandidate->getName();
        $indexName               = $this->normalizeIdentifier($indexName);
464
        $replacedImplicitIndexes = [];
465

466
        foreach ($this->implicitIndexes as $name => $implicitIndex) {
467 468
            if (! $implicitIndex->isFullfilledBy($indexCandidate) || ! isset($this->_indexes[$name])) {
                continue;
469
            }
470 471

            $replacedImplicitIndexes[] = $name;
472 473 474
        }

        if ((isset($this->_indexes[$indexName]) && ! in_array($indexName, $replacedImplicitIndexes, true)) ||
475
            ($this->_primaryKeyName !== false && $indexCandidate->isPrimary())
476
        ) {
477
            throw SchemaException::indexAlreadyExists($indexName, $this->_name);
478 479
        }

480 481 482 483
        foreach ($replacedImplicitIndexes as $name) {
            unset($this->_indexes[$name], $this->implicitIndexes[$name]);
        }

484
        if ($indexCandidate->isPrimary()) {
485 486 487
            $this->_primaryKeyName = $indexName;
        }

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

490 491 492 493
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
494
     * @return void
495
     */
496
    protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
497
    {
498
        $constraint->setLocalTable($this);
499

Steve Müller's avatar
Steve Müller committed
500
        if (strlen($constraint->getName())) {
501 502 503
            $name = $constraint->getName();
        } else {
            $name = $this->_generateIdentifierName(
504 505 506
                array_merge((array) $this->getName(), $constraint->getLocalColumns()),
                'fk',
                $this->_getMaxIdentifierLength()
507 508
            );
        }
509
        $name = $this->normalizeIdentifier($name);
510 511

        $this->_fkConstraints[$name] = $constraint;
512

Pascal Borreli's avatar
Pascal Borreli committed
513
        // add an explicit index on the foreign key columns. If there is already an index that fulfils this requirements drop the request.
514
        // 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
515
        // lead to duplicates. This creates computation overhead in this case, however no duplicate indexes are ever added (based on columns).
516
        $indexName      = $this->_generateIdentifierName(
517
            array_merge([$this->getName()], $constraint->getColumns()),
518
            'idx',
519 520 521 522 523 524 525 526 527 528
            $this->_getMaxIdentifierLength()
        );
        $indexCandidate = $this->_createIndex($constraint->getColumns(), $indexName, false, false);

        foreach ($this->_indexes as $existingIndex) {
            if ($indexCandidate->isFullfilledBy($existingIndex)) {
                return;
            }
        }

529 530
        $this->_addIndex($indexCandidate);
        $this->implicitIndexes[$this->normalizeIdentifier($indexName)] = $indexCandidate;
531 532 533
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
534 535 536 537
     * Returns whether this table has a foreign key constraint with the given name.
     *
     * @param string $constraintName
     *
538
     * @return bool
539 540 541
     */
    public function hasForeignKey($constraintName)
    {
542
        $constraintName = $this->normalizeIdentifier($constraintName);
Benjamin Morel's avatar
Benjamin Morel committed
543

544 545 546 547
        return isset($this->_fkConstraints[$constraintName]);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
548 549 550 551
     * Returns the foreign key constraint with the given name.
     *
     * @param string $constraintName The constraint name.
     *
552
     * @return ForeignKeyConstraint
Benjamin Morel's avatar
Benjamin Morel committed
553
     *
554
     * @throws SchemaException If the foreign key does not exist.
555 556 557
     */
    public function getForeignKey($constraintName)
    {
558
        $constraintName = $this->normalizeIdentifier($constraintName);
559
        if (! $this->hasForeignKey($constraintName)) {
560
            throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
561 562 563
        }

        return $this->_fkConstraints[$constraintName];
564 565
    }

Benjamin Morel's avatar
Benjamin Morel committed
566 567 568 569 570 571 572
    /**
     * Removes the foreign key constraint with the given name.
     *
     * @param string $constraintName The constraint name.
     *
     * @return void
     *
573
     * @throws SchemaException
Benjamin Morel's avatar
Benjamin Morel committed
574
     */
575 576
    public function removeForeignKey($constraintName)
    {
577
        $constraintName = $this->normalizeIdentifier($constraintName);
578
        if (! $this->hasForeignKey($constraintName)) {
579 580 581 582 583 584
            throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
        }

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

585
    /**
586
     * Returns ordered list of columns (primary keys are first, then foreign keys, then the rest)
587
     *
588
     * @return Column[]
589 590 591
     */
    public function getColumns()
    {
592 593 594 595
        $primaryKeyColumns = [];
        if ($this->hasPrimaryKey()) {
            $primaryKeyColumns = $this->filterColumns($this->getPrimaryKey()->getColumns());
        }
596

597
        return array_merge($primaryKeyColumns, $this->getForeignKeyColumns(), $this->_columns);
598
    }
599

600 601
    /**
     * Returns foreign key columns
602
     *
603 604
     * @return Column[]
     */
605
    private function getForeignKeyColumns()
606
    {
607 608 609
        $foreignKeyColumns = [];
        foreach ($this->getForeignKeys() as $foreignKey) {
            $foreignKeyColumns = array_merge($foreignKeyColumns, $foreignKey->getColumns());
610
        }
611
        return $this->filterColumns($foreignKeyColumns);
612
    }
Benjamin Morel's avatar
Benjamin Morel committed
613

614
    /**
615
     * Returns only columns that have specified names
616
     *
617
     * @param string[] $columnNames
618
     *
619
     * @return Column[]
620
     */
621
    private function filterColumns(array $columnNames)
622
    {
623
        return array_filter($this->_columns, static function ($columnName) use ($columnNames) {
624 625
            return in_array($columnName, $columnNames, true);
        }, ARRAY_FILTER_USE_KEY);
626 627 628
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
629
     * Returns whether this table has a Column with the given name.
630
     *
Benjamin Morel's avatar
Benjamin Morel committed
631 632
     * @param string $columnName The column name.
     *
633
     * @return bool
634 635 636
     */
    public function hasColumn($columnName)
    {
637
        $columnName = $this->normalizeIdentifier($columnName);
Benjamin Morel's avatar
Benjamin Morel committed
638

639 640 641 642
        return isset($this->_columns[$columnName]);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
643 644 645
     * Returns the Column with the given name.
     *
     * @param string $columnName The column name.
646
     *
647
     * @return Column
Benjamin Morel's avatar
Benjamin Morel committed
648
     *
649
     * @throws SchemaException If the column does not exist.
650 651 652
     */
    public function getColumn($columnName)
    {
653
        $columnName = $this->normalizeIdentifier($columnName);
654
        if (! $this->hasColumn($columnName)) {
655
            throw SchemaException::columnDoesNotExist($columnName, $this->_name);
656 657 658 659 660 661
        }

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
662 663
     * Returns the primary key.
     *
664
     * @return Index|null The primary key, or null if this Table has no primary key.
665 666 667
     */
    public function getPrimaryKey()
    {
668
        if (! $this->hasPrimaryKey()) {
669 670
            return null;
        }
Benjamin Morel's avatar
Benjamin Morel committed
671

672 673 674
        return $this->getIndex($this->_primaryKeyName);
    }

675 676 677
    /**
     * Returns the primary key columns.
     *
678
     * @return string[]
679 680 681 682 683
     *
     * @throws DBALException
     */
    public function getPrimaryKeyColumns()
    {
684 685
        if (! $this->hasPrimaryKey()) {
            throw new DBALException('Table ' . $this->getName() . ' has no primary key.');
686 687 688 689
        }
        return $this->getPrimaryKey()->getColumns();
    }

690
    /**
Benjamin Morel's avatar
Benjamin Morel committed
691
     * Returns whether this table has a primary key.
692
     *
693
     * @return bool
694 695 696
     */
    public function hasPrimaryKey()
    {
697
        return $this->_primaryKeyName && $this->hasIndex($this->_primaryKeyName);
698 699
    }

700
    /**
Benjamin Morel's avatar
Benjamin Morel committed
701 702 703 704
     * Returns whether this table has an Index with the given name.
     *
     * @param string $indexName The index name.
     *
705
     * @return bool
706 707 708
     */
    public function hasIndex($indexName)
    {
709
        $indexName = $this->normalizeIdentifier($indexName);
Benjamin Morel's avatar
Benjamin Morel committed
710

711
        return isset($this->_indexes[$indexName]);
712 713 714
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
715 716 717 718
     * Returns the Index with the given name.
     *
     * @param string $indexName The index name.
     *
719
     * @return Index
Benjamin Morel's avatar
Benjamin Morel committed
720
     *
721
     * @throws SchemaException If the index does not exist.
722 723 724
     */
    public function getIndex($indexName)
    {
725
        $indexName = $this->normalizeIdentifier($indexName);
726
        if (! $this->hasIndex($indexName)) {
727
            throw SchemaException::indexDoesNotExist($indexName, $this->_name);
728
        }
Benjamin Morel's avatar
Benjamin Morel committed
729

730 731 732 733
        return $this->_indexes[$indexName];
    }

    /**
734
     * @return Index[]
735 736 737 738 739 740 741
     */
    public function getIndexes()
    {
        return $this->_indexes;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
742
     * Returns the foreign key constraints.
743
     *
744
     * @return ForeignKeyConstraint[]
745
     */
746
    public function getForeignKeys()
747
    {
748
        return $this->_fkConstraints;
749 750
    }

Benjamin Morel's avatar
Benjamin Morel committed
751 752 753
    /**
     * @param string $name
     *
754
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
755
     */
756 757 758 759 760
    public function hasOption($name)
    {
        return isset($this->_options[$name]);
    }

Benjamin Morel's avatar
Benjamin Morel committed
761 762 763 764 765
    /**
     * @param string $name
     *
     * @return mixed
     */
766 767 768 769 770
    public function getOption($name)
    {
        return $this->_options[$name];
    }

Benjamin Morel's avatar
Benjamin Morel committed
771
    /**
772
     * @return mixed[]
Benjamin Morel's avatar
Benjamin Morel committed
773
     */
774 775 776 777 778
    public function getOptions()
    {
        return $this->_options;
    }

779
    /**
Benjamin Morel's avatar
Benjamin Morel committed
780
     * @return void
781 782 783 784 785
     */
    public function visit(Visitor $visitor)
    {
        $visitor->acceptTable($this);

786
        foreach ($this->getColumns() as $column) {
787
            $visitor->acceptColumn($this, $column);
788 789
        }

790
        foreach ($this->getIndexes() as $index) {
791 792 793
            $visitor->acceptIndex($this, $index);
        }

794
        foreach ($this->getForeignKeys() as $constraint) {
795
            $visitor->acceptForeignKey($this, $constraint);
796 797
        }
    }
798 799

    /**
Benjamin Morel's avatar
Benjamin Morel committed
800 801 802
     * Clone of a Table triggers a deep clone of all affected assets.
     *
     * @return void
803 804 805
     */
    public function __clone()
    {
806
        foreach ($this->_columns as $k => $column) {
807 808
            $this->_columns[$k] = clone $column;
        }
809
        foreach ($this->_indexes as $k => $index) {
810 811
            $this->_indexes[$k] = clone $index;
        }
812
        foreach ($this->_fkConstraints as $k => $fk) {
813 814 815 816
            $this->_fkConstraints[$k] = clone $fk;
            $this->_fkConstraints[$k]->setLocalTable($this);
        }
    }
817 818 819 820 821 822 823 824 825 826 827 828 829 830

    /**
     * Normalizes a given identifier.
     *
     * Trims quotes and lowercases the given identifier.
     *
     * @param string $identifier The identifier to normalize.
     *
     * @return string The normalized identifier.
     */
    private function normalizeIdentifier($identifier)
    {
        return $this->trimQuotes(strtolower($identifier));
    }
831
}