Completed
Pull Request — master (#3512)
by David
16:25
created

Table::getComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Schema\Visitor\Visitor;
7
use Doctrine\DBAL\Types\Type;
8
use const ARRAY_FILTER_USE_KEY;
9
use function array_filter;
10
use function array_merge;
11
use function in_array;
12
use function preg_match;
13
use function strlen;
14
use function strtolower;
15
16
/**
17
 * Object Representation of a table.
18
 */
19
class Table extends AbstractAsset
20
{
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
    protected $_primaryKeyName = false;
32
33
    /** @var ForeignKeyConstraint[] */
34
    protected $_fkConstraints = [];
35
36
    /** @var mixed[] */
37
    protected $_options = [];
38
39
    /** @var SchemaConfig|null */
40
    protected $_schemaConfig = null;
41
42
    /**
43
     * @param string                 $tableName
44
     * @param Column[]               $columns
45
     * @param Index[]                $indexes
46
     * @param ForeignKeyConstraint[] $fkConstraints
47
     * @param int                    $idGeneratorType
48
     * @param mixed[]                $options
49
     *
50
     * @throws DBALException
51
     */
52 24535
    public function __construct($tableName, array $columns = [], array $indexes = [], array $fkConstraints = [], $idGeneratorType = 0, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $idGeneratorType is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

52
    public function __construct($tableName, array $columns = [], array $indexes = [], array $fkConstraints = [], /** @scrutinizer ignore-unused */ $idGeneratorType = 0, array $options = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54 24535
        if (strlen($tableName) === 0) {
55 1652
            throw DBALException::invalidTableName($tableName);
56
        }
57
58 24533
        $this->_setName($tableName);
59
60 24533
        foreach ($columns as $column) {
61 22047
            $this->_addColumn($column);
62
        }
63
64 24531
        foreach ($indexes as $idx) {
65 21914
            $this->_addIndex($idx);
66
        }
67
68 24527
        foreach ($fkConstraints as $constraint) {
69 21127
            $this->_addForeignKeyConstraint($constraint);
70
        }
71
72 24527
        $this->_options = $options;
73 24527
    }
74
75
    /**
76
     * @return void
77
     */
78 22542
    public function setSchemaConfig(SchemaConfig $schemaConfig)
79
    {
80 22542
        $this->_schemaConfig = $schemaConfig;
81 22542
    }
82
83
    /**
84
     * @return int
85
     */
86 22803
    protected function _getMaxIdentifierLength()
87
    {
88 22803
        if ($this->_schemaConfig instanceof SchemaConfig) {
89 22338
            return $this->_schemaConfig->getMaxIdentifierLength();
90
        }
91
92 22742
        return 63;
93
    }
94
95
    /**
96
     * Sets the Primary Key.
97
     *
98
     * @param string[]     $columnNames
99
     * @param string|false $indexName
100
     *
101
     * @return self
102
     */
103 23491
    public function setPrimaryKey(array $columnNames, $indexName = false)
104
    {
105 23491
        $this->_addIndex($this->_createIndex($columnNames, $indexName ?: 'primary', true, true));
106
107 23491
        foreach ($columnNames as $columnName) {
108 23491
            $column = $this->getColumn($columnName);
109 23491
            $column->setNotnull(true);
110
        }
111
112 23491
        return $this;
113
    }
114
115
    /**
116
     * @param string[]    $columnNames
117
     * @param string|null $indexName
118
     * @param string[]    $flags
119
     * @param mixed[]     $options
120
     *
121
     * @return self
122
     */
123 21599
    public function addIndex(array $columnNames, $indexName = null, array $flags = [], array $options = [])
124
    {
125 21599
        if ($indexName === null) {
126 21081
            $indexName = $this->_generateIdentifierName(
127 21081
                array_merge([$this->getName()], $columnNames),
128 21081
                'idx',
129 21081
                $this->_getMaxIdentifierLength()
130
            );
131
        }
132
133 21599
        return $this->_addIndex($this->_createIndex($columnNames, $indexName, false, false, $flags, $options));
134
    }
135
136
    /**
137
     * Drops the primary key from this table.
138
     *
139
     * @return void
140
     */
141 20190
    public function dropPrimaryKey()
142
    {
143 20190
        $this->dropIndex($this->_primaryKeyName);
144 20190
        $this->_primaryKeyName = false;
0 ignored issues
show
Documentation Bug introduced by
The property $_primaryKeyName was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
145 20190
    }
146
147
    /**
148
     * Drops an index from this table.
149
     *
150
     * @param string $indexName The index name.
151
     *
152
     * @return void
153
     *
154
     * @throws SchemaException If the index does not exist.
155
     */
156 20227
    public function dropIndex($indexName)
157
    {
158 20227
        $indexName = $this->normalizeIdentifier($indexName);
159 20227
        if (! $this->hasIndex($indexName)) {
160
            throw SchemaException::indexDoesNotExist($indexName, $this->_name);
161
        }
162 20227
        unset($this->_indexes[$indexName]);
163 20227
    }
164
165
    /**
166
     * @param string[]    $columnNames
167
     * @param string|null $indexName
168
     * @param mixed[]     $options
169
     *
170
     * @return self
171
     */
172 22396
    public function addUniqueIndex(array $columnNames, $indexName = null, array $options = [])
173
    {
174 22396
        if ($indexName === null) {
175 22048
            $indexName = $this->_generateIdentifierName(
176 22048
                array_merge([$this->getName()], $columnNames),
177 22048
                'uniq',
178 22048
                $this->_getMaxIdentifierLength()
179
            );
180
        }
181
182 22396
        return $this->_addIndex($this->_createIndex($columnNames, $indexName, true, false, [], $options));
183
    }
184
185
    /**
186
     * Renames an index.
187
     *
188
     * @param string      $oldIndexName The name of the index to rename from.
189
     * @param string|null $newIndexName The name of the index to rename to.
190
     *                                  If null is given, the index name will be auto-generated.
191
     *
192
     * @return self This table instance.
193
     *
194
     * @throws SchemaException If no index exists for the given current name
195
     *                         or if an index with the given new name already exists on this table.
196
     */
197 19945
    public function renameIndex($oldIndexName, $newIndexName = null)
198
    {
199 19945
        $oldIndexName           = $this->normalizeIdentifier($oldIndexName);
200 19945
        $normalizedNewIndexName = $this->normalizeIdentifier($newIndexName);
201
202 19945
        if ($oldIndexName === $normalizedNewIndexName) {
203 493
            return $this;
204
        }
205
206 19945
        if (! $this->hasIndex($oldIndexName)) {
207 402
            throw SchemaException::indexDoesNotExist($oldIndexName, $this->_name);
208
        }
209
210 19943
        if ($this->hasIndex($normalizedNewIndexName)) {
211 377
            throw SchemaException::indexAlreadyExists($normalizedNewIndexName, $this->_name);
212
        }
213
214 19941
        $oldIndex = $this->_indexes[$oldIndexName];
215
216 19941
        if ($oldIndex->isPrimary()) {
217 477
            $this->dropPrimaryKey();
218
219 477
            return $this->setPrimaryKey($oldIndex->getColumns(), $newIndexName ?? false);
220
        }
221
222 19941
        unset($this->_indexes[$oldIndexName]);
223
224 19941
        if ($oldIndex->isUnique()) {
225 479
            return $this->addUniqueIndex($oldIndex->getColumns(), $newIndexName, $oldIndex->getOptions());
226
        }
227
228 19939
        return $this->addIndex($oldIndex->getColumns(), $newIndexName, $oldIndex->getFlags(), $oldIndex->getOptions());
229
    }
230
231
    /**
232
     * Checks if an index begins in the order of the given columns.
233
     *
234
     * @param string[] $columnNames
235
     *
236
     * @return bool
237
     */
238 20121
    public function columnsAreIndexed(array $columnNames)
239
    {
240 20121
        foreach ($this->getIndexes() as $index) {
241
            /** @var $index Index */
242 20121
            if ($index->spansColumns($columnNames)) {
243 20121
                return true;
244
            }
245
        }
246
247
        return false;
248
    }
249
250
    /**
251
     * @param string[] $columnNames
252
     * @param string   $indexName
253
     * @param bool     $isUnique
254
     * @param bool     $isPrimary
255
     * @param string[] $flags
256
     * @param mixed[]  $options
257
     *
258
     * @return Index
259
     *
260
     * @throws SchemaException
261
     */
262 23789
    private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = [], array $options = [])
263
    {
264 23789
        if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) {
265 1177
            throw SchemaException::indexNameInvalid($indexName);
266
        }
267
268 23787
        foreach ($columnNames as $columnName) {
269 23785
            if (! $this->hasColumn($columnName)) {
270 2146
                throw SchemaException::columnDoesNotExist($columnName, $this->_name);
271
            }
272
        }
273
274 23785
        return new Index($indexName, $columnNames, $isUnique, $isPrimary, $flags, $options);
275
    }
276
277
    /**
278
     * @param string  $columnName
279
     * @param string  $typeName
280
     * @param mixed[] $options
281
     *
282
     * @return Column
283
     */
284 24213
    public function addColumn($columnName, $typeName, array $options = [])
285
    {
286 24213
        $column = new Column($columnName, Type::getType($typeName), $options);
287
288 24213
        $this->_addColumn($column);
289
290 24213
        return $column;
291
    }
292
293
    /**
294
     * Renames a Column.
295
     *
296
     * @deprecated
297
     *
298
     * @param string $oldColumnName
299
     * @param string $newColumnName
300
     *
301
     * @throws DBALException
302
     */
303
    public function renameColumn($oldColumnName, $newColumnName)
0 ignored issues
show
Unused Code introduced by
The parameter $newColumnName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

303
    public function renameColumn($oldColumnName, /** @scrutinizer ignore-unused */ $newColumnName)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $oldColumnName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

303
    public function renameColumn(/** @scrutinizer ignore-unused */ $oldColumnName, $newColumnName)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
304
    {
305
        throw new DBALException('Table#renameColumn() was removed, because it drops and recreates ' .
306
            'the column instead. There is no fix available, because a schema diff cannot reliably detect if a ' .
307
            'column was renamed or one column was created and another one dropped.');
308
    }
309
310
    /**
311
     * Change Column Details.
312
     *
313
     * @param string  $columnName
314
     * @param mixed[] $options
315
     *
316
     * @return self
317
     */
318 20573
    public function changeColumn($columnName, array $options)
319
    {
320 20573
        $column = $this->getColumn($columnName);
321 20573
        $column->setOptions($options);
322
323 20573
        return $this;
324
    }
325
326
    /**
327
     * Drops a Column from the Table.
328
     *
329
     * @param string $columnName
330
     *
331
     * @return self
332
     */
333 1543
    public function dropColumn($columnName)
334
    {
335 1543
        $columnName = $this->normalizeIdentifier($columnName);
336 1543
        unset($this->_columns[$columnName]);
337
338 1543
        return $this;
339
    }
340
341
    /**
342
     * Adds a foreign key constraint.
343
     *
344
     * Name is inferred from the local columns.
345
     *
346
     * @param Table|string $foreignTable       Table schema instance or table name
347
     * @param string[]     $localColumnNames
348
     * @param string[]     $foreignColumnNames
349
     * @param mixed[]      $options
350
     * @param string|null  $constraintName
351
     *
352
     * @return self
353
     */
354 22566
    public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = [], $constraintName = null)
355
    {
356 22566
        $constraintName = $constraintName ?: $this->_generateIdentifierName(array_merge((array) $this->getName(), $localColumnNames), 'fk', $this->_getMaxIdentifierLength());
357
358 22566
        return $this->addNamedForeignKeyConstraint($constraintName, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Tab...dForeignKeyConstraint() has been deprecated: Use {@link addForeignKeyConstraint} ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

358
        return /** @scrutinizer ignore-deprecated */ $this->addNamedForeignKeyConstraint($constraintName, $foreignTable, $localColumnNames, $foreignColumnNames, $options);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
359
    }
360
361
    /**
362
     * Adds a foreign key constraint.
363
     *
364
     * Name is to be generated by the database itself.
365
     *
366
     * @deprecated Use {@link addForeignKeyConstraint}
367
     *
368
     * @param Table|string $foreignTable       Table schema instance or table name
369
     * @param string[]     $localColumnNames
370
     * @param string[]     $foreignColumnNames
371
     * @param mixed[]      $options
372
     *
373
     * @return self
374
     */
375 16070
    public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = [])
376
    {
377 16070
        return $this->addForeignKeyConstraint($foreignTable, $localColumnNames, $foreignColumnNames, $options);
378
    }
379
380
    /**
381
     * Adds a foreign key constraint with a given name.
382
     *
383
     * @deprecated Use {@link addForeignKeyConstraint}
384
     *
385
     * @param string       $name
386
     * @param Table|string $foreignTable       Table schema instance or table name
387
     * @param string[]     $localColumnNames
388
     * @param string[]     $foreignColumnNames
389
     * @param mixed[]      $options
390
     *
391
     * @return self
392
     *
393
     * @throws SchemaException
394
     */
395 22568
    public function addNamedForeignKeyConstraint($name, $foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = [])
396
    {
397 22568
        if ($foreignTable instanceof Table) {
398 22087
            foreach ($foreignColumnNames as $columnName) {
399 22087
                if (! $foreignTable->hasColumn($columnName)) {
400 1191
                    throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName());
401
                }
402
            }
403
        }
404
405 22566
        foreach ($localColumnNames as $columnName) {
406 22566
            if (! $this->hasColumn($columnName)) {
407 1326
                throw SchemaException::columnDoesNotExist($columnName, $this->_name);
408
            }
409
        }
410
411 22564
        $constraint = new ForeignKeyConstraint(
412 22564
            $localColumnNames,
413 224
            $foreignTable,
414 224
            $foreignColumnNames,
415 224
            $name,
416 224
            $options
417
        );
418 22564
        $this->_addForeignKeyConstraint($constraint);
419
420 22564
        return $this;
421
    }
422
423
    /**
424
     * @param string $name
425
     * @param mixed  $value
426
     *
427
     * @return self
428
     */
429 21986
    public function addOption($name, $value)
430
    {
431 21986
        $this->_options[$name] = $value;
432
433 21986
        return $this;
434
    }
435
436
    /**
437
     * @return void
438
     *
439
     * @throws SchemaException
440
     */
441 24285
    protected function _addColumn(Column $column)
442
    {
443 24285
        $columnName = $column->getName();
444 24285
        $columnName = $this->normalizeIdentifier($columnName);
445
446 24285
        if (isset($this->_columns[$columnName])) {
447 1477
            throw SchemaException::columnAlreadyExists($this->getName(), $columnName);
448
        }
449
450 24285
        $this->_columns[$columnName] = $column;
451 24285
    }
452
453
    /**
454
     * Adds an index to the table.
455
     *
456
     * @return self
457
     *
458
     * @throws SchemaException
459
     */
460 23799
    protected function _addIndex(Index $indexCandidate)
461
    {
462 23799
        $indexName               = $indexCandidate->getName();
463 23799
        $indexName               = $this->normalizeIdentifier($indexName);
464 23799
        $replacedImplicitIndexes = [];
465
466 23799
        foreach ($this->implicitIndexes as $name => $implicitIndex) {
467 19092
            if (! $implicitIndex->isFullfilledBy($indexCandidate) || ! isset($this->_indexes[$name])) {
468 19084
                continue;
469
            }
470
471 783
            $replacedImplicitIndexes[] = $name;
472
        }
473
474 23799
        if ((isset($this->_indexes[$indexName]) && ! in_array($indexName, $replacedImplicitIndexes, true)) ||
475 23799
            ($this->_primaryKeyName !== false && $indexCandidate->isPrimary())
476
        ) {
477 1354
            throw SchemaException::indexAlreadyExists($indexName, $this->_name);
478
        }
479
480 23799
        foreach ($replacedImplicitIndexes as $name) {
481 783
            unset($this->_indexes[$name], $this->implicitIndexes[$name]);
482
        }
483
484 23799
        if ($indexCandidate->isPrimary()) {
485 23495
            $this->_primaryKeyName = $indexName;
486
        }
487
488 23799
        $this->_indexes[$indexName] = $indexCandidate;
489
490 23799
        return $this;
491
    }
492
493
    /**
494
     * @return void
495
     */
496 22654
    protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
497
    {
498 22654
        $constraint->setLocalTable($this);
499
500 22654
        if (strlen($constraint->getName())) {
501 22572
            $name = $constraint->getName();
502
        } else {
503 2076
            $name = $this->_generateIdentifierName(
504 2076
                array_merge((array) $this->getName(), $constraint->getLocalColumns()),
505 2076
                'fk',
506 2076
                $this->_getMaxIdentifierLength()
507
            );
508
        }
509 22654
        $name = $this->normalizeIdentifier($name);
510
511 22654
        $this->_fkConstraints[$name] = $constraint;
512
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
515
        // lead to duplicates. This creates computation overhead in this case, however no duplicate indexes are ever added (based on columns).
516 22654
        $indexName      = $this->_generateIdentifierName(
517 22654
            array_merge([$this->getName()], $constraint->getColumns()),
518 22654
            'idx',
519 22654
            $this->_getMaxIdentifierLength()
520
        );
521 22654
        $indexCandidate = $this->_createIndex($constraint->getColumns(), $indexName, false, false);
522
523 22654
        foreach ($this->_indexes as $existingIndex) {
524 22602
            if ($indexCandidate->isFullfilledBy($existingIndex)) {
525 21205
                return;
526
            }
527
        }
528
529 22594
        $this->_addIndex($indexCandidate);
530 22594
        $this->implicitIndexes[$this->normalizeIdentifier($indexName)] = $indexCandidate;
531 22594
    }
532
533
    /**
534
     * Returns whether this table has a foreign key constraint with the given name.
535
     *
536
     * @param string $constraintName
537
     *
538
     * @return bool
539
     */
540 19936
    public function hasForeignKey($constraintName)
541
    {
542 19936
        $constraintName = $this->normalizeIdentifier($constraintName);
543
544 19936
        return isset($this->_fkConstraints[$constraintName]);
545
    }
546
547
    /**
548
     * Returns the foreign key constraint with the given name.
549
     *
550
     * @param string $constraintName The constraint name.
551
     *
552
     * @return ForeignKeyConstraint
553
     *
554
     * @throws SchemaException If the foreign key does not exist.
555
     */
556 368
    public function getForeignKey($constraintName)
557
    {
558 368
        $constraintName = $this->normalizeIdentifier($constraintName);
559 368
        if (! $this->hasForeignKey($constraintName)) {
560
            throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
561
        }
562
563 368
        return $this->_fkConstraints[$constraintName];
564
    }
565
566
    /**
567
     * Removes the foreign key constraint with the given name.
568
     *
569
     * @param string $constraintName The constraint name.
570
     *
571
     * @return void
572
     *
573
     * @throws SchemaException
574
     */
575 370
    public function removeForeignKey($constraintName)
576
    {
577 370
        $constraintName = $this->normalizeIdentifier($constraintName);
578 370
        if (! $this->hasForeignKey($constraintName)) {
579
            throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
580
        }
581
582 370
        unset($this->_fkConstraints[$constraintName]);
583 370
    }
584
585
    /**
586
     * Returns ordered list of columns (primary keys are first, then foreign keys, then the rest)
587
     *
588
     * @return Column[]
589
     */
590 23867
    public function getColumns()
591
    {
592 23867
        $primaryKey        = $this->getPrimaryKey();
593 23867
        $primaryKeyColumns = [];
594
595 23867
        if ($primaryKey !== null) {
596 23255
            $primaryKeyColumns = $this->filterColumns($primaryKey->getColumns());
597
        }
598
599 23867
        return array_merge($primaryKeyColumns, $this->getForeignKeyColumns(), $this->_columns);
600
    }
601
602
    /**
603
     * Returns foreign key columns
604
     *
605
     * @return Column[]
606
     */
607 23867
    private function getForeignKeyColumns()
608
    {
609 23867
        $foreignKeyColumns = [];
610 23867
        foreach ($this->getForeignKeys() as $foreignKey) {
611 22530
            $foreignKeyColumns = array_merge($foreignKeyColumns, $foreignKey->getColumns());
612
        }
613
614 23867
        return $this->filterColumns($foreignKeyColumns);
615
    }
616
617
    /**
618
     * Returns only columns that have specified names
619
     *
620
     * @param string[] $columnNames
621
     *
622
     * @return Column[]
623
     */
624 23867
    private function filterColumns(array $columnNames)
625
    {
626
        return array_filter($this->_columns, static function ($columnName) use ($columnNames) {
627 23815
            return in_array($columnName, $columnNames, true);
628 23867
        }, ARRAY_FILTER_USE_KEY);
629
    }
630
631
    /**
632
     * Returns whether this table has a Column with the given name.
633
     *
634
     * @param string $columnName The column name.
635
     *
636
     * @return bool
637
     */
638 23953
    public function hasColumn($columnName)
639
    {
640 23953
        $columnName = $this->normalizeIdentifier($columnName);
641
642 23953
        return isset($this->_columns[$columnName]);
643
    }
644
645
    /**
646
     * Returns the Column with the given name.
647
     *
648
     * @param string $columnName The column name.
649
     *
650
     * @return Column
651
     *
652
     * @throws SchemaException If the column does not exist.
653
     */
654 23685
    public function getColumn($columnName)
655
    {
656 23685
        $columnName = $this->normalizeIdentifier($columnName);
657 23685
        if (! $this->hasColumn($columnName)) {
658 1502
            throw SchemaException::columnDoesNotExist($columnName, $this->_name);
659
        }
660
661 23683
        return $this->_columns[$columnName];
662
    }
663
664
    /**
665
     * Returns the primary key.
666
     *
667
     * @return Index|null The primary key, or null if this Table has no primary key.
668
     */
669 23877
    public function getPrimaryKey()
670
    {
671 23877
        if (! $this->hasPrimaryKey()) {
672 23101
            return null;
673
        }
674
675 23265
        return $this->getIndex($this->_primaryKeyName);
676
    }
677
678
    /**
679
     * Returns the primary key columns.
680
     *
681
     * @return string[]
682
     *
683
     * @throws DBALException
684
     */
685 20180
    public function getPrimaryKeyColumns()
686
    {
687 20180
        $primaryKey = $this->getPrimaryKey();
688
689 20180
        if ($primaryKey === null) {
690
            throw new DBALException('Table ' . $this->getName() . ' has no primary key.');
691
        }
692
693 20180
        return $primaryKey->getColumns();
694
    }
695
696
    /**
697
     * Returns whether this table has a primary key.
698
     *
699
     * @return bool
700
     */
701 23883
    public function hasPrimaryKey()
702
    {
703 23883
        return $this->_primaryKeyName && $this->hasIndex($this->_primaryKeyName);
704
    }
705
706
    /**
707
     * Returns whether this table has an Index with the given name.
708
     *
709
     * @param string $indexName The index name.
710
     *
711
     * @return bool
712
     */
713 23369
    public function hasIndex($indexName)
714
    {
715 23369
        $indexName = $this->normalizeIdentifier($indexName);
716
717 23369
        return isset($this->_indexes[$indexName]);
718
    }
719
720
    /**
721
     * Returns the Index with the given name.
722
     *
723
     * @param string $indexName The index name.
724
     *
725
     * @return Index
726
     *
727
     * @throws SchemaException If the index does not exist.
728
     */
729 23325
    public function getIndex($indexName)
730
    {
731 23325
        $indexName = $this->normalizeIdentifier($indexName);
732 23325
        if (! $this->hasIndex($indexName)) {
733 1377
            throw SchemaException::indexDoesNotExist($indexName, $this->_name);
734
        }
735
736 23323
        return $this->_indexes[$indexName];
737
    }
738
739
    /**
740
     * @return Index[]
741
     */
742 23811
    public function getIndexes()
743
    {
744 23811
        return $this->_indexes;
745
    }
746
747
    /**
748
     * Returns the foreign key constraints.
749
     *
750
     * @return ForeignKeyConstraint[]
751
     */
752 23903
    public function getForeignKeys()
753
    {
754 23903
        return $this->_fkConstraints;
755
    }
756
757
    /**
758
     * @param string $name
759
     *
760
     * @return bool
761
     */
762 22605
    public function hasOption($name)
763
    {
764 22605
        return isset($this->_options[$name]);
765
    }
766
767
    /**
768
     * @param string $name
769
     *
770
     * @return mixed
771
     */
772 20703
    public function getOption($name)
773
    {
774 20703
        return $this->_options[$name];
775
    }
776
777
    /**
778
     * @return mixed[]
779
     */
780 23597
    public function getOptions()
781
    {
782 23597
        return $this->_options;
783
    }
784
785
    /**
786
     * @return void
787
     */
788 22269
    public function visit(Visitor $visitor)
789
    {
790 22269
        $visitor->acceptTable($this);
791
792 22269
        foreach ($this->getColumns() as $column) {
793 22263
            $visitor->acceptColumn($this, $column);
794
        }
795
796 22269
        foreach ($this->getIndexes() as $index) {
797 20105
            $visitor->acceptIndex($this, $index);
798
        }
799
800 22269
        foreach ($this->getForeignKeys() as $constraint) {
801 158
            $visitor->acceptForeignKey($this, $constraint);
802
        }
803 22269
    }
804
805
    /**
806
     * Clone of a Table triggers a deep clone of all affected assets.
807
     *
808
     * @return void
809
     */
810 21626
    public function __clone()
811
    {
812 21626
        foreach ($this->_columns as $k => $column) {
813 21624
            $this->_columns[$k] = clone $column;
814
        }
815 21626
        foreach ($this->_indexes as $k => $index) {
816 21602
            $this->_indexes[$k] = clone $index;
817
        }
818 21626
        foreach ($this->_fkConstraints as $k => $fk) {
819 20612
            $this->_fkConstraints[$k] = clone $fk;
820 20612
            $this->_fkConstraints[$k]->setLocalTable($this);
821
        }
822 21626
    }
823
824
    /**
825
     * Normalizes a given identifier.
826
     *
827
     * Trims quotes and lowercases the given identifier.
828
     *
829
     * @param string|null $identifier The identifier to normalize.
830
     *
831
     * @return string The normalized identifier.
832
     */
833 24293
    private function normalizeIdentifier($identifier)
834
    {
835 24293
        if ($identifier === null) {
836 477
            return '';
837
        }
838
839 24293
        return $this->trimQuotes(strtolower($identifier));
840
    }
841
842 20121
    public function setComment(?string $comment) : self
843
    {
844
        // For keeping backward compatibility with MySQL in previous releases, table comments are stored as options.
845 20121
        $this->addOption('comment', $comment);
846
847 20121
        return $this;
848
    }
849
850 20121
    public function getComment() : ?string
851
    {
852 20121
        return $this->_options['comment'] ?? null;
853
    }
854
}
855