Passed
Pull Request — master (#180)
by Wilmer
04:01
created

Schema::findTableNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use JsonException;
8
use Throwable;
9
use Yiisoft\Arrays\ArrayHelper;
10
use Yiisoft\Db\Constraint\Constraint;
11
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
12
use Yiisoft\Db\Constraint\IndexConstraint;
13
use Yiisoft\Db\Exception\Exception;
14
use Yiisoft\Db\Exception\InvalidConfigException;
15
use Yiisoft\Db\Exception\NotSupportedException;
16
use Yiisoft\Db\Expression\Expression;
17
use Yiisoft\Db\Schema\ColumnSchemaInterface;
18
use Yiisoft\Db\Schema\Schema as AbstractSchema;
19
use Yiisoft\Db\Schema\TableSchemaInterface;
20
21
use function array_map;
22
use function array_merge;
23
use function array_values;
24
use function bindec;
25
use function explode;
26
use function ksort;
27
use function md5;
28
use function preg_match;
29
use function preg_match_all;
30
use function serialize;
31
use function str_replace;
32
use function stripos;
33
use function strtolower;
34
use function trim;
35
36
/**
37
 * The class Schema is the class for retrieving metadata from a Mysql database (version 5.7 and above).
38
 *
39
 * @psalm-type ColumnArray = array{
40
 *   table_schema: string,
41
 *   table_name: string,
42
 *   column_name: string,
43
 *   data_type: string,
44
 *   type_type: string|null,
45
 *   character_maximum_length: int,
46
 *   column_comment: string|null,
47
 *   modifier: int,
48
 *   is_nullable: bool,
49
 *   column_default: mixed,
50
 *   is_autoinc: bool,
51
 *   sequence_name: string|null,
52
 *   enum_values: array<array-key, float|int|string>|string|null,
53
 *   numeric_precision: int|null,
54
 *   numeric_scale: int|null,
55
 *   size: string|null,
56
 *   is_pkey: bool|null,
57
 *   dimension: int
58
 * }
59
 *
60
 * @psalm-type ColumnInfoArray = array{
61
 *   field: string,
62
 *   type: string,
63
 *   collation: string|null,
64
 *   null: string,
65
 *   key: string,
66
 *   default: string|null,
67
 *   extra: string,
68
 *   privileges: string,
69
 *   comment: string
70
 * }
71
 *
72
 * @psalm-type RowConstraint = array{
73
 *   constraint_name: string,
74
 *   column_name: string,
75
 *   referenced_table_name: string,
76
 *   referenced_column_name: string
77
 * }
78
 *
79
 * @psalm-type ConstraintArray = array<
80
 *   array-key,
81
 *   array {
82
 *     name: string,
83
 *     column_name: string,
84
 *     type: string,
85
 *     foreign_table_schema: string|null,
86
 *     foreign_table_name: string|null,
87
 *     foreign_column_name: string|null,
88
 *     on_update: string,
89
 *     on_delete: string,
90
 *     check_expr: string
91
 *   }
92
 * >
93
 */
94
final class Schema extends AbstractSchema
95
{
96
    /** @var array<array-key, string> $typeMap */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, string> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, string>.
Loading history...
97
    private array $typeMap = [
98
        'tinyint' => self::TYPE_TINYINT,
99
        'bit' => self::TYPE_INTEGER,
100
        'smallint' => self::TYPE_SMALLINT,
101
        'mediumint' => self::TYPE_INTEGER,
102
        'int' => self::TYPE_INTEGER,
103
        'integer' => self::TYPE_INTEGER,
104
        'bigint' => self::TYPE_BIGINT,
105
        'float' => self::TYPE_FLOAT,
106
        'double' => self::TYPE_DOUBLE,
107
        'real' => self::TYPE_FLOAT,
108
        'decimal' => self::TYPE_DECIMAL,
109
        'numeric' => self::TYPE_DECIMAL,
110
        'tinytext' => self::TYPE_TEXT,
111
        'mediumtext' => self::TYPE_TEXT,
112
        'longtext' => self::TYPE_TEXT,
113
        'longblob' => self::TYPE_BINARY,
114
        'blob' => self::TYPE_BINARY,
115
        'text' => self::TYPE_TEXT,
116
        'varchar' => self::TYPE_STRING,
117
        'string' => self::TYPE_STRING,
118
        'char' => self::TYPE_CHAR,
119
        'datetime' => self::TYPE_DATETIME,
120
        'year' => self::TYPE_DATE,
121
        'date' => self::TYPE_DATE,
122
        'time' => self::TYPE_TIME,
123
        'timestamp' => self::TYPE_TIMESTAMP,
124
        'enum' => self::TYPE_STRING,
125
        'varbinary' => self::TYPE_BINARY,
126
        'json' => self::TYPE_JSON,
127
    ];
128
129
    /**
130
     * Create a column schema builder instance giving the type and value precision.
131
     *
132
     * This method may be overridden by child classes to create a DBMS-specific column schema builder.
133
     *
134
     * @param string $type type of the column. See {@see ColumnSchemaBuilder::$type}.
135
     * @param array|int|string|null $length length or precision of the column. See {@see ColumnSchemaBuilder::$length}.
136
     *
137
     * @return ColumnSchemaBuilder column schema builder instance
138
     *
139
     * @psalm-param string[]|int|string|null $length
140
     */
141 11
    public function createColumnSchemaBuilder(string $type, array|int|string $length = null): ColumnSchemaBuilder
142
    {
143 11
        return new ColumnSchemaBuilder($type, $length, $this->db->getQuoter());
144
    }
145
146
    /**
147
     * Returns all unique indexes for the given table.
148
     *
149
     * Each array element is of the following structure:
150
     *
151
     * ```php
152
     * [
153
     *     'IndexName1' => ['col1' [, ...]],
154
     *     'IndexName2' => ['col2' [, ...]],
155
     * ]
156
     * ```
157
     *
158
     * @param TableSchemaInterface $table the table metadata.
159
     *
160
     * @throws Exception
161
     * @throws InvalidConfigException
162
     * @throws Throwable
163
     *
164
     * @return array all unique indexes for the given table.
165
     */
166 1
    public function findUniqueIndexes(TableSchemaInterface $table): array
167
    {
168 1
        $sql = $this->getCreateTableSql($table);
169
170 1
        $uniqueIndexes = [];
171
172 1
        $regexp = '/UNIQUE KEY\s+`(.+)`\s*\((`.+`)+\)/mi';
173
174 1
        if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
175 1
            foreach ($matches as $match) {
176 1
                $indexName = $match[1];
177 1
                $indexColumns = array_map('trim', explode('`,`', trim($match[2], '`')));
178 1
                $uniqueIndexes[$indexName] = $indexColumns;
179
            }
180
        }
181
182 1
        ksort($uniqueIndexes);
183
184 1
        return $uniqueIndexes;
185
    }
186
187
    /**
188
     * Collects the metadata of table columns.
189
     *
190
     * @param TableSchemaInterface $table the table metadata.
191
     *
192
     * @throws Exception
193
     * @throws Throwable if DB query fails.
194
     *
195
     * @return bool whether the table exists in the database.
196
     */
197 145
    protected function findColumns(TableSchemaInterface $table): bool
198
    {
199 145
        $tableName = $table->getFullName() ?? '';
200 145
        $sql = 'SHOW FULL COLUMNS FROM ' . $this->db->getQuoter()->quoteTableName($tableName);
201
202
        try {
203 145
            $columns = $this->db->createCommand($sql)->queryAll();
204 25
        } catch (Exception $e) {
205 25
            $previous = $e->getPrevious();
206
207 25
            if ($previous && str_contains($previous->getMessage(), 'SQLSTATE[42S02')) {
208
                /**
209
                 * table does not exist.
210
                 *
211
                 * https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_bad_table_error
212
                 */
213 25
                return false;
214
            }
215
216
            throw $e;
217
        }
218
219
        /** @psalm-var ColumnInfoArray $info */
220 130
        foreach ($columns as $info) {
221 130
            $info = $this->normalizeRowKeyCase($info, false);
222
223 130
            $column = $this->loadColumnSchema($info);
224 130
            $table->columns($column->getName(), $column);
225
226 130
            if ($column->isPrimaryKey()) {
227 85
                $table->primaryKey($column->getName());
228 85
                if ($column->isAutoIncrement()) {
229 73
                    $table->sequenceName('');
230
                }
231
            }
232
        }
233
234 130
        return true;
235
    }
236
237
    /**
238
     * Collects the foreign key column details for the given table.
239
     *
240
     * @param TableSchemaInterface $table the table metadata.
241
     *
242
     * @throws Exception
243
     * @throws Throwable
244
     */
245 130
    protected function findConstraints(TableSchemaInterface $table): void
246
    {
247 130
        $sql = <<<SQL
248
        SELECT
249
            `kcu`.`CONSTRAINT_NAME` AS `constraint_name`,
250
            `kcu`.`COLUMN_NAME` AS `column_name`,
251
            `kcu`.`REFERENCED_TABLE_NAME` AS `referenced_table_name`,
252
            `kcu`.`REFERENCED_COLUMN_NAME` AS `referenced_column_name`
253
        FROM `information_schema`.`REFERENTIAL_CONSTRAINTS` AS `rc`
254
        JOIN `information_schema`.`KEY_COLUMN_USAGE` AS `kcu` ON
255
            (
256
                `kcu`.`CONSTRAINT_CATALOG` = `rc`.`CONSTRAINT_CATALOG` OR
257
                (
258
                    `kcu`.`CONSTRAINT_CATALOG` IS NULL AND
259
                    `rc`.`CONSTRAINT_CATALOG` IS NULL
260
                )
261
            ) AND
262
            `kcu`.`CONSTRAINT_SCHEMA` = `rc`.`CONSTRAINT_SCHEMA` AND
263
            `kcu`.`CONSTRAINT_NAME` = `rc`.`CONSTRAINT_NAME` AND
264
            `kcu`.`TABLE_SCHEMA` = `rc`.`CONSTRAINT_SCHEMA` AND
265
            `kcu`.`TABLE_NAME` = `rc`.`TABLE_NAME`
266
        WHERE
267
            `rc`.`CONSTRAINT_SCHEMA` = COALESCE(:schemaName, DATABASE()) AND
268
            `rc`.`TABLE_NAME` = :tableName
269 130
        SQL;
270
271
        try {
272 130
            $rows = $this->db->createCommand($sql, [
273 130
                ':schemaName' => $table->getSchemaName(),
274 130
                ':tableName' => $table->getName(),
275 130
            ])->queryAll();
276
277 130
            $constraints = [];
278
279
            /**  @psalm-var RowConstraint $row */
280 130
            foreach ($rows as $row) {
281 42
                $constraints[$row['constraint_name']]['referenced_table_name'] = $row['referenced_table_name'];
282 42
                $constraints[$row['constraint_name']]['columns'][$row['column_name']] = $row['referenced_column_name'];
283
            }
284
285 130
            $table->foreignKeys([]);
286
287
            /**
288
             * @var array{referenced_table_name: string, columns: array} $constraint
289
             */
290 130
            foreach ($constraints as $name => $constraint) {
291 42
                $table->foreignKey($name, array_merge(
292 42
                    [$constraint['referenced_table_name']],
293 42
                    $constraint['columns']
294 42
                ));
295
            }
296
        } catch (Exception $e) {
297
            $previous = $e->getPrevious();
298
299
            if ($previous === null || !str_contains($previous->getMessage(), 'SQLSTATE[42S02')) {
300
                throw $e;
301
            }
302
303
            // table does not exist, try to determine the foreign keys using the table creation sql
304
            $sql = $this->getCreateTableSql($table);
305
            $regexp = '/FOREIGN KEY\s+\(([^)]+)\)\s+REFERENCES\s+([^(^\s]+)\s*\(([^)]+)\)/mi';
306
307
            if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
308
                foreach ($matches as $match) {
309
                    $fks = array_map('trim', explode(',', str_replace('`', '', $match[1])));
310
                    $pks = array_map('trim', explode(',', str_replace('`', '', $match[3])));
311
                    $constraint = [str_replace('`', '', $match[2])];
312
313
                    foreach ($fks as $k => $name) {
314
                        $constraint[$name] = $pks[$k];
315
                    }
316
317
                    $table->foreignKey(md5(serialize($constraint)), $constraint);
318
                }
319
                $table->foreignKeys(array_values($table->getForeignKeys()));
320
            }
321
        }
322
    }
323
324
    /**
325
     * Returns all schema names in the database, including the default one but not system schemas.
326
     *
327
     * This method should be overridden by child classes in order to support this feature because the default
328
     * implementation simply throws an exception.
329
     *
330
     * @throws Exception
331
     * @throws InvalidConfigException
332
     * @throws Throwable
333
     *
334
     * @return array All schema names in the database, except system schemas.
335
     */
336 1
    protected function findSchemaNames(): array
337
    {
338 1
        $sql = <<<SQL
339
        SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
340 1
        SQL;
341
342 1
        return $this->db->createCommand($sql)->queryColumn();
343
    }
344
345 145
    protected function findTableComment(TableSchemaInterface $tableSchema): void
346
    {
347 145
        $sql = <<<SQL
348
        SELECT `TABLE_COMMENT`
349
        FROM `INFORMATION_SCHEMA`.`TABLES`
350
        WHERE
351
              `TABLE_SCHEMA` = COALESCE(:schemaName, DATABASE()) AND
352
              `TABLE_NAME` = :tableName;
353 145
        SQL;
354
355 145
        $comment = $this->db->createCommand($sql, [
356 145
            ':schemaName' => $tableSchema->getSchemaName(),
357 145
            ':tableName' => $tableSchema->getName(),
358 145
        ])->queryScalar();
359
360 145
        $tableSchema->comment(is_string($comment) ? $comment : null);
361
    }
362
363
    /**
364
     * Returns all table names in the database.
365
     *
366
     * This method should be overridden by child classes in order to support this feature because the default
367
     * implementation simply throws an exception.
368
     *
369
     * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
370
     *
371
     * @throws Exception
372
     * @throws InvalidConfigException
373
     * @throws Throwable
374
     *
375
     * @return array All table names in the database. The names have NO schema name prefix.
376
     */
377 12
    protected function findTableNames(string $schema = ''): array
378
    {
379 12
        $sql = 'SHOW TABLES';
380
381 12
        if ($schema !== '') {
382 1
            $sql .= ' FROM ' . $this->db->getQuoter()->quoteSimpleTableName($schema);
383
        }
384
385 12
        return $this->db->createCommand($sql)->queryColumn();
386
    }
387
388 1
    protected function findViewNames(string $schema = ''): array
389
    {
390 1
        $sql = match ($schema) {
391 1
            '' => <<<SQL
392
            SELECT table_name as view FROM information_schema.tables WHERE table_type LIKE 'VIEW' AND table_schema != 'sys'
393 1
            SQL,
394 1
            default => <<<SQL
395 1
            SELECT table_name as view FROM information_schema.tables WHERE table_type LIKE 'VIEW' AND table_schema = '$schema'
396 1
            SQL,
397 1
        };
398
399
        /** @psalm-var string[][] $views */
400 1
        $views = $this->db->createCommand($sql)->queryAll();
401
402 1
        foreach ($views as $key => $view) {
403 1
            $views[$key] = $view['view'];
404
        }
405
406 1
        return $views;
407
    }
408
409
    /**
410
     * Returns the cache key for the specified table name.
411
     *
412
     * @param string $name the table name.
413
     *
414
     * @return array the cache key.
415
     */
416 212
    protected function getCacheKey(string $name): array
417
    {
418 212
        return array_merge([self::class], $this->db->getCacheKey(), [$this->getRawTableName($name)]);
419
    }
420
421
    /**
422
     * Returns the cache tag name.
423
     *
424
     * This allows {@see refresh()} to invalidate all cached table schemas.
425
     *
426
     * @return string the cache tag name.
427
     */
428 213
    protected function getCacheTag(): string
429
    {
430 213
        return md5(serialize(array_merge([self::class], $this->db->getCacheKey())));
431
    }
432
433
    /**
434
     * Gets the CREATE TABLE sql string.
435
     *
436
     * @param TableSchemaInterface $table the table metadata.
437
     *
438
     * @throws Exception
439
     * @throws InvalidConfigException
440
     * @throws Throwable
441
     *
442
     * @return string $sql the result of 'SHOW CREATE TABLE'.
443
     */
444 145
    protected function getCreateTableSql(TableSchemaInterface $table): string
445
    {
446 145
        $tableName = $table->getFullName() ?? '';
447
448
        try {
449
            /** @var array<array-key, string> $row */
450 145
            $row = $this->db->createCommand(
451 145
                'SHOW CREATE TABLE ' . $this->db->getQuoter()->quoteTableName($tableName)
452 145
            )->queryOne();
453
454 130
            if (isset($row['Create Table'])) {
455 128
                $sql = $row['Create Table'];
456
            } else {
457 4
                $row = array_values($row);
0 ignored issues
show
Bug introduced by
$row of type null is incompatible with the type array expected by parameter $array of array_values(). ( Ignorable by Annotation )

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

457
                $row = array_values(/** @scrutinizer ignore-type */ $row);
Loading history...
458 130
                $sql = $row[1];
459
            }
460 25
        } catch (Exception) {
461 25
            $sql = '';
462
        }
463
464 145
        return $sql;
465
    }
466
467
    /**
468
     * Loads the column information into a {@see ColumnSchemaInterface} object.
469
     *
470
     * @param array $info column information.
471
     *
472
     * @throws JsonException
473
     *
474
     * @return ColumnSchemaInterface the column schema object.
475
     */
476 131
    protected function loadColumnSchema(array $info): ColumnSchemaInterface
477
    {
478 131
        $column = $this->createColumnSchema();
479
480
        /** @psalm-var ColumnInfoArray $info */
481 131
        $column->name($info['field']);
482 131
        $column->allowNull($info['null'] === 'YES');
483 131
        $column->primaryKey(str_contains($info['key'], 'PRI'));
484 131
        $column->autoIncrement(stripos($info['extra'], 'auto_increment') !== false);
485 131
        $column->comment($info['comment']);
486 131
        $column->dbType($info['type']);
487 131
        $column->unsigned(stripos($column->getDbType(), 'unsigned') !== false);
488 131
        $column->type(self::TYPE_STRING);
489
490 131
        $extra = $info['extra'];
491 131
        if (str_starts_with($extra, 'DEFAULT_GENERATED')) {
492 30
            $extra = strtoupper(substr($extra, 18));
493
        }
494 131
        $column->extra(trim($extra));
495
496 131
        if (preg_match('/^(\w+)(?:\(([^)]+)\))?/', $column->getDbType(), $matches)) {
497 131
            $type = strtolower($matches[1]);
498
499 131
            if (isset($this->typeMap[$type])) {
500 131
                $column->type($this->typeMap[$type]);
501
            }
502
503 131
            if (!empty($matches[2])) {
504 106
                if ($type === 'enum') {
505 28
                    preg_match_all("/'[^']*'/", $matches[2], $values);
506
507 28
                    foreach ($values[0] as $i => $value) {
508 28
                        $values[$i] = trim($value, "'");
509
                    }
510
511 28
                    $column->enumValues($values);
512
                } else {
513 106
                    $values = explode(',', $matches[2]);
514 106
                    $column->precision((int) $values[0]);
515 106
                    $column->size((int) $values[0]);
516
517 106
                    if (isset($values[1])) {
518 42
                        $column->scale((int) $values[1]);
519
                    }
520
521 106
                    if ($column->getSize() === 1 && $type === 'tinyint') {
522 29
                        $column->type(self::TYPE_BOOLEAN);
523 106
                    } elseif ($type === 'bit') {
524 28
                        if ($column->getSize() > 32) {
525
                            $column->type(self::TYPE_BIGINT);
526 28
                        } elseif ($column->getSize() === 32) {
527
                            $column->type(self::TYPE_INTEGER);
528
                        }
529
                    }
530
                }
531
            }
532
        }
533
534 131
        $column->phpType($this->getColumnPhpType($column));
535
536 131
        if (!$column->isPrimaryKey()) {
537
            /**
538
             * When displayed in the INFORMATION_SCHEMA.COLUMNS table, a default CURRENT TIMESTAMP is displayed
539
             * as CURRENT_TIMESTAMP up until MariaDB 10.2.2, and as current_timestamp() from MariaDB 10.2.3.
540
             *
541
             * See details here: https://mariadb.com/kb/en/library/now/#description
542
             */
543
            if (
544 128
                ($column->getType() === 'timestamp' || $column->getType() === 'datetime')
545 128
                && preg_match('/^current_timestamp(?:\((\d*)\))?$/i', (string) $info['default'], $matches)
546
            ) {
547 31
                $column->defaultValue(new Expression('CURRENT_TIMESTAMP' . (!empty($matches[1])
548 31
                    ? '(' . $matches[1] . ')' : '')));
549 125
            } elseif (isset($type) && $type === 'bit') {
550 28
                $column->defaultValue(bindec(trim((string) $info['default'], 'b\'')));
551
            } else {
552 128
                $column->defaultValue($column->phpTypecast($info['default']));
553
            }
554 85
        } elseif ($info['default'] !== null) {
555 3
            $column->defaultValue($column->phpTypecast($info['default']));
556
        }
557
558 131
        return $column;
559
    }
560
561
    /**
562
     * Loads all check constraints for the given table.
563
     *
564
     * @param string $tableName table name.
565
     *
566
     * @throws NotSupportedException
567
     *
568
     * @return array check constraints for the given table.
569
     */
570 16
    protected function loadTableChecks(string $tableName): array
571
    {
572 16
        throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
573
    }
574
575
    /**
576
     * Loads multiple types of constraints and returns the specified ones.
577
     *
578
     * @param string $tableName table name.
579
     * @param string $returnType return type:
580
     * - primaryKey
581
     * - foreignKeys
582
     * - uniques
583
     *
584
     * @throws Exception
585
     * @throws InvalidConfigException
586
     * @throws Throwable
587
     *
588
     * @return array|Constraint|null (Constraint|ForeignKeyConstraint)[]|Constraint|null constraints.
589
     */
590 60
    private function loadTableConstraints(string $tableName, string $returnType): array|Constraint|null
591
    {
592 60
        $sql = <<<SQL
593
        SELECT
594
            `kcu`.`CONSTRAINT_NAME` AS `name`,
595
            `kcu`.`COLUMN_NAME` AS `column_name`,
596
            `tc`.`CONSTRAINT_TYPE` AS `type`,
597
        CASE
598
            WHEN :schemaName IS NULL AND `kcu`.`REFERENCED_TABLE_SCHEMA` = DATABASE() THEN NULL
599
        ELSE `kcu`.`REFERENCED_TABLE_SCHEMA`
600
        END AS `foreign_table_schema`,
601
            `kcu`.`REFERENCED_TABLE_NAME` AS `foreign_table_name`,
602
            `kcu`.`REFERENCED_COLUMN_NAME` AS `foreign_column_name`,
603
            `rc`.`UPDATE_RULE` AS `on_update`,
604
            `rc`.`DELETE_RULE` AS `on_delete`,
605
            `kcu`.`ORDINAL_POSITION` AS `position`
606
        FROM `information_schema`.`KEY_COLUMN_USAGE` AS `kcu`
607
        JOIN `information_schema`.`REFERENTIAL_CONSTRAINTS` AS `rc` ON
608
            `rc`.`CONSTRAINT_SCHEMA` = `kcu`.`TABLE_SCHEMA` AND
609
            `rc`.`TABLE_NAME` = `kcu`.`TABLE_NAME` AND
610
            `rc`.`CONSTRAINT_NAME` = `kcu`.`CONSTRAINT_NAME`
611
        JOIN `information_schema`.`TABLE_CONSTRAINTS` AS `tc` ON
612
            `tc`.`TABLE_SCHEMA` = `kcu`.`TABLE_SCHEMA` AND
613
            `tc`.`TABLE_NAME` = `kcu`.`TABLE_NAME` AND
614
            `tc`.`CONSTRAINT_NAME` = `kcu`.`CONSTRAINT_NAME` AND
615
            `tc`.`CONSTRAINT_TYPE` = 'FOREIGN KEY'
616
        WHERE
617
            `kcu`.`TABLE_SCHEMA` = COALESCE(:schemaName, DATABASE()) AND
618
            `kcu`.`CONSTRAINT_SCHEMA` = `kcu`.`TABLE_SCHEMA` AND
619
            `kcu`.`TABLE_NAME` = :tableName
620
        UNION
621
        SELECT
622
            `kcu`.`CONSTRAINT_NAME` AS `name`,
623
            `kcu`.`COLUMN_NAME` AS `column_name`,
624
            `tc`.`CONSTRAINT_TYPE` AS `type`,
625
        NULL AS `foreign_table_schema`,
626
        NULL AS `foreign_table_name`,
627
        NULL AS `foreign_column_name`,
628
        NULL AS `on_update`,
629
        NULL AS `on_delete`,
630
            `kcu`.`ORDINAL_POSITION` AS `position`
631
        FROM `information_schema`.`KEY_COLUMN_USAGE` AS `kcu`
632
        JOIN `information_schema`.`TABLE_CONSTRAINTS` AS `tc` ON
633
            `tc`.`TABLE_SCHEMA` = `kcu`.`TABLE_SCHEMA` AND
634
            `tc`.`TABLE_NAME` = `kcu`.`TABLE_NAME` AND
635
            `tc`.`CONSTRAINT_NAME` = `kcu`.`CONSTRAINT_NAME` AND
636
            `tc`.`CONSTRAINT_TYPE` IN ('PRIMARY KEY', 'UNIQUE')
637
        WHERE
638
            `kcu`.`TABLE_SCHEMA` = COALESCE(:schemaName, DATABASE()) AND
639
            `kcu`.`TABLE_NAME` = :tableName
640
        ORDER BY `position` ASC
641 60
        SQL;
642
643 60
        $resolvedName = $this->resolveTableName($tableName);
644
645 60
        $constraints = $this->db->createCommand($sql, [
646 60
            ':schemaName' => $resolvedName->getSchemaName(),
647 60
            ':tableName' => $resolvedName->getName(),
648 60
        ])->queryAll();
649
650
        /** @var array<array-key, array> $constraints */
651 60
        $constraints = $this->normalizeRowKeyCase($constraints, true);
652 60
        $constraints = ArrayHelper::index($constraints, null, ['type', 'name']);
653
654 60
        $result = [
655 60
            self::PRIMARY_KEY => null,
656 60
            self::FOREIGN_KEYS => [],
657 60
            self::UNIQUES => [],
658 60
        ];
659
660
        /**
661
         * @var string $type
662
         * @var array $names
663
         */
664 60
        foreach ($constraints as $type => $names) {
665
            /**
666
             * @psalm-var object|string|null $name
667
             * @psalm-var ConstraintArray $constraint
668
             */
669 60
            foreach ($names as $name => $constraint) {
670
                switch ($type) {
671 60
                    case 'PRIMARY KEY':
672 43
                        $result[self::PRIMARY_KEY] = (new Constraint())
673 43
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'));
674 43
                        break;
675 56
                    case 'FOREIGN KEY':
676 17
                        $result[self::FOREIGN_KEYS][] = (new ForeignKeyConstraint())
677 17
                            ->foreignSchemaName($constraint[0]['foreign_table_schema'])
678 17
                            ->foreignTableName($constraint[0]['foreign_table_name'])
679 17
                            ->foreignColumnNames(ArrayHelper::getColumn($constraint, 'foreign_column_name'))
680 17
                            ->onDelete($constraint[0]['on_delete'])
681 17
                            ->onUpdate($constraint[0]['on_update'])
682 17
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
683 17
                            ->name($name);
684 17
                        break;
685 46
                    case 'UNIQUE':
686 46
                        $result[self::UNIQUES][] = (new Constraint())
687 46
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
688 46
                            ->name($name);
689 46
                        break;
690
                }
691
            }
692
        }
693
694 60
        foreach ($result as $type => $data) {
695 60
            $this->setTableMetadata($tableName, $type, $data);
696
        }
697
698 60
        return $result[$returnType];
699
    }
700
701
    /**
702
     * Loads all default value constraints for the given table.
703
     *
704
     * @param string $tableName table name.
705
     *
706
     * @throws NotSupportedException
707
     *
708
     * @return array default value constraints for the given table.
709
     */
710 15
    protected function loadTableDefaultValues(string $tableName): array
711
    {
712 15
        throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
713
    }
714
715
    /**
716
     * Loads all foreign keys for the given table.
717
     *
718
     * @param string $tableName table name.
719
     *
720
     * @throws Exception
721
     * @throws InvalidConfigException
722
     * @throws Throwable
723
     *
724
     * @return array foreign keys for the given table.
725
     */
726 9
    protected function loadTableForeignKeys(string $tableName): array
727
    {
728 9
        $tableForeignKeys = $this->loadTableConstraints($tableName, self::FOREIGN_KEYS);
729
730 9
        return is_array($tableForeignKeys) ? $tableForeignKeys : [];
731
    }
732
733
    /**
734
     * Loads all indexes for the given table.
735
     *
736
     * @param string $tableName table name.
737
     *
738
     * @throws Exception
739
     * @throws InvalidConfigException
740
     * @throws Throwable
741
     *
742
     * @return IndexConstraint[] indexes for the given table.
743
     */
744 32
    protected function loadTableIndexes(string $tableName): array
745
    {
746 32
        $sql = <<<SQL
747
        SELECT
748
            `s`.`INDEX_NAME` AS `name`,
749
            `s`.`COLUMN_NAME` AS `column_name`,
750
            `s`.`NON_UNIQUE` ^ 1 AS `index_is_unique`,
751
            `s`.`INDEX_NAME` = 'PRIMARY' AS `index_is_primary`
752
        FROM `information_schema`.`STATISTICS` AS `s`
753
        WHERE
754
            `s`.`TABLE_SCHEMA` = COALESCE(:schemaName, DATABASE()) AND
755
            `s`.`INDEX_SCHEMA` = `s`.`TABLE_SCHEMA` AND
756
            `s`.`TABLE_NAME` = :tableName
757
        ORDER BY `s`.`SEQ_IN_INDEX` ASC
758 32
        SQL;
759
760 32
        $resolvedName = $this->resolveTableName($tableName);
761
762 32
        $indexes = $this->db->createCommand($sql, [
763 32
            ':schemaName' => $resolvedName->getSchemaName(),
764 32
            ':tableName' => $resolvedName->getName(),
765 32
        ])->queryAll();
766
767
        /** @var array[] $indexes */
768 32
        $indexes = $this->normalizeRowKeyCase($indexes, true);
769 32
        $indexes = ArrayHelper::index($indexes, null, 'name');
770 32
        $result = [];
771
772
        /**
773
         * @psalm-var object|string|null $name
774
         * @psalm-var array[] $index
775
         */
776 32
        foreach ($indexes as $name => $index) {
777 32
            $ic = new IndexConstraint();
778
779 32
            $ic->primary((bool) $index[0]['index_is_primary']);
780 32
            $ic->unique((bool) $index[0]['index_is_unique']);
781 32
            $ic->name($name !== 'PRIMARY' ? $name : null);
782 32
            $ic->columnNames(ArrayHelper::getColumn($index, 'column_name'));
783
784 32
            $result[] = $ic;
785
        }
786
787 32
        return $result;
788
    }
789
790
    /**
791
     * Loads a primary key for the given table.
792
     *
793
     * @param string $tableName table name.
794
     *
795
     * @throws Exception
796
     * @throws InvalidConfigException
797
     * @throws Throwable
798
     *
799
     * @return Constraint|null primary key for the given table, `null` if the table has no primary key.*
800
     */
801 34
    protected function loadTablePrimaryKey(string $tableName): Constraint|null
802
    {
803 34
        $tablePrimaryKey = $this->loadTableConstraints($tableName, self::PRIMARY_KEY);
804
805 34
        return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null;
806
    }
807
808
    /**
809
     * Loads the metadata for the specified table.
810
     *
811
     * @param string $name table name.
812
     *
813
     * @throws Exception
814
     * @throws Throwable
815
     *
816
     * @return TableSchemaInterface|null DBMS-dependent table metadata, `null` if the table does not exist.
817
     */
818 145
    protected function loadTableSchema(string $name): TableSchemaInterface|null
819
    {
820 145
        $table = $this->resolveTableName($name);
821 145
        $this->resolveTableCreateSql($table);
822 145
        $this->findTableComment($table);
823
824 145
        if ($this->findColumns($table)) {
825 130
            $this->findConstraints($table);
826
827 130
            return $table;
828
        }
829
830 25
        return null;
831
    }
832
833
    /**
834
     * Loads all unique constraints for the given table.
835
     *
836
     * @param string $tableName table name.
837
     *
838
     * @throws Exception
839
     * @throws InvalidConfigException
840
     * @throws Throwable
841
     *
842
     * @return array unique constraints for the given table.
843
     */
844 17
    protected function loadTableUniques(string $tableName): array
845
    {
846 17
        $tableUniques = $this->loadTableConstraints($tableName, self::UNIQUES);
847
848 17
        return is_array($tableUniques) ? $tableUniques : [];
849
    }
850
851
    /**
852
     * Resolves the table name and schema name (if any).
853
     *
854
     * @param string $name the table name.
855
     *
856
     * {@see TableSchemaInterface}
857
     */
858 185
    protected function resolveTableName(string $name): TableSchemaInterface
859
    {
860 185
        $resolvedName = new TableSchema();
861
862 185
        $parts = array_reverse(
863 185
            $this->db->getQuoter()->getTableNameParts($name)
864 185
        );
865
866 185
        $resolvedName->name($parts[0] ?? '');
867 185
        $resolvedName->schemaName($parts[1] ?? $this->defaultSchema);
868
869 185
        $resolvedName->fullName(
870 185
            $resolvedName->getSchemaName() !== $this->defaultSchema ?
871 185
            implode('.', array_reverse($parts)) : $resolvedName->getName()
872 185
        );
873
874 185
        return $resolvedName;
875
    }
876
877
    /**
878
     * @throws Exception
879
     * @throws InvalidConfigException
880
     * @throws Throwable
881
     */
882 145
    protected function resolveTableCreateSql(TableSchemaInterface $table): void
883
    {
884 145
        $sql = $this->getCreateTableSql($table);
885 145
        $table->createSql($sql);
886
    }
887
888
    /**
889
     * Creates a column schema for the database.
890
     *
891
     * This method may be overridden by child classes to create a DBMS-specific column schema.
892
     *
893
     * @return ColumnSchema column schema instance.
894
     */
895 131
    private function createColumnSchema(): ColumnSchema
896
    {
897 131
        return new ColumnSchema();
898
    }
899
}
900