Passed
Push — master ( 544315...ba3685 )
by Wilmer
05:24 queued 38s
created

Schema::createColumnSchema()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Throwable;
8
use Yiisoft\Db\Constraint\CheckConstraint;
9
use Yiisoft\Db\Constraint\Constraint;
10
use Yiisoft\Db\Constraint\DefaultValueConstraint;
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\Helper\ArrayHelper;
16
use Yiisoft\Db\Schema\AbstractSchema;
17
use Yiisoft\Db\Schema\Builder\ColumnInterface;
18
use Yiisoft\Db\Schema\ColumnSchemaInterface;
19
use Yiisoft\Db\Schema\TableSchemaInterface;
20
21
use function explode;
22
use function is_array;
23
use function md5;
24
use function preg_match;
25
use function serialize;
26
use function str_replace;
27
use function strcasecmp;
28
use function stripos;
29
30
/**
31
 * Implements the MSSQL Server specific schema, supporting MSSQL Server 2017 and above.
32
 *
33
 * @psalm-type ColumnArray = array{
34
 *   column_name: string,
35
 *   is_nullable: string,
36
 *   data_type: string,
37
 *   column_default: mixed,
38
 *   is_identity: string,
39
 *   is_computed: string,
40
 *   comment: null|string
41
 * }
42
 * @psalm-type ConstraintArray = array<
43
 *   array-key,
44
 *   array {
45
 *     name: string,
46
 *     column_name: string,
47
 *     type: string,
48
 *     foreign_table_schema: string|null,
49
 *     foreign_table_name: string|null,
50
 *     foreign_column_name: string|null,
51
 *     on_update: string,
52
 *     on_delete: string,
53
 *     check_expr: string,
54
 *     default_expr: string
55
 *   }
56
 * >
57
 */
58
final class Schema extends AbstractSchema
59
{
60
    /**
61
     * @var string|null The default schema used for the current session.
62
     */
63
    protected string|null $defaultSchema = 'dbo';
64
65
    /**
66
     * @var array Mapping from physical column types (keys) to abstract column types (values).
67
     *
68
     * @psalm-var string[]
69
     */
70
    private array $typeMap = [
71
        /** Exact numbers */
72
        'bigint' => self::TYPE_BIGINT,
73
        'numeric' => self::TYPE_DECIMAL,
74
        'bit' => self::TYPE_SMALLINT,
75
        'smallint' => self::TYPE_SMALLINT,
76
        'decimal' => self::TYPE_DECIMAL,
77
        'smallmoney' => self::TYPE_MONEY,
78
        'int' => self::TYPE_INTEGER,
79
        'tinyint' => self::TYPE_TINYINT,
80
        'money' => self::TYPE_MONEY,
81
82
        /** Approximate numbers */
83
        'float' => self::TYPE_FLOAT,
84
        'double' => self::TYPE_DOUBLE,
85
        'real' => self::TYPE_FLOAT,
86
87
        /** Date and time */
88
        'date' => self::TYPE_DATE,
89
        'datetimeoffset' => self::TYPE_DATETIME,
90
        'datetime2' => self::TYPE_DATETIME,
91
        'smalldatetime' => self::TYPE_DATETIME,
92
        'datetime' => self::TYPE_DATETIME,
93
        'time' => self::TYPE_TIME,
94
95
        /** Character strings */
96
        'char' => self::TYPE_CHAR,
97
        'varchar' => self::TYPE_STRING,
98
        'text' => self::TYPE_TEXT,
99
100
        /** Unicode character strings */
101
        'nchar' => self::TYPE_CHAR,
102
        'nvarchar' => self::TYPE_STRING,
103
        'ntext' => self::TYPE_TEXT,
104
105
        /** Binary strings */
106
        'binary' => self::TYPE_BINARY,
107
        'varbinary' => self::TYPE_BINARY,
108
        'image' => self::TYPE_BINARY,
109
110
        /**
111
         * Other data types 'cursor' type can't be used with tables
112
         */
113
        'timestamp' => self::TYPE_TIMESTAMP,
114
        'hierarchyid' => self::TYPE_STRING,
115
        'uniqueidentifier' => self::TYPE_STRING,
116
        'sql_variant' => self::TYPE_STRING,
117
        'xml' => self::TYPE_STRING,
118
        'table' => self::TYPE_STRING,
119
    ];
120
121 14
    public function createColumn(string $type, array|int|string|null $length = null): ColumnInterface
122
    {
123 14
        return new Column($type, $length);
124
    }
125
126
    /**
127
     * Resolves the table name and schema name (if any).
128
     *
129
     * @param string $name The table name.
130
     *
131
     * @return TableSchemaInterface The resolved table name.
132
     */
133 579
    protected function resolveTableName(string $name): TableSchemaInterface
134
    {
135 579
        $resolvedName = new TableSchema();
136
137 579
        $parts = array_reverse(
138 579
            $this->db->getQuoter()->getTableNameParts($name)
139 579
        );
140
141 579
        $resolvedName->name($parts[0] ?? '');
142 579
        $resolvedName->schemaName($parts[1] ?? $this->defaultSchema);
143 579
        $resolvedName->catalogName($parts[2] ?? null);
144 579
        $resolvedName->serverName($parts[3] ?? null);
145
146 579
        if (empty($parts[2]) && $resolvedName->getSchemaName() === $this->defaultSchema) {
147 573
            $resolvedName->fullName($parts[0]);
148
        } else {
149 7
            $resolvedName->fullName(implode('.', array_reverse($parts)));
150
        }
151
152 579
        return $resolvedName;
153
    }
154
155
    /**
156
     * Returns all schema names in the database, including the default one but not system schemas.
157
     *
158
     * This method should be overridden by child classes to support this feature because the default implementation
159
     * simply throws an exception.
160
     *
161
     * @throws Exception
162
     * @throws InvalidConfigException
163
     * @throws Throwable
164
     *
165
     * @return array All schema name in the database, except system schemas.
166
     *
167
     * @link https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-database-principals-transact-sql
168
     */
169 1
    protected function findSchemaNames(): array
170
    {
171 1
        $sql = <<<SQL
172
        SELECT [s].[name]
173
        FROM [sys].[schemas] AS [s]
174
        INNER JOIN [sys].[database_principals] AS [p] ON [p].[principal_id] = [s].[principal_id]
175
        WHERE [p].[is_fixed_role] = 0 AND [p].[sid] IS NOT NULL
176
        ORDER BY [s].[name] ASC
177 1
        SQL;
178
179 1
        return $this->db->createCommand($sql)->queryColumn();
180
    }
181
182
    /**
183
     * @throws Exception
184
     * @throws InvalidConfigException
185
     * @throws Throwable
186
     */
187 505
    protected function findTableComment(TableSchemaInterface $tableSchema): void
188
    {
189 505
        $schemaName = $tableSchema->getSchemaName()
190 505
            ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()';
191 505
        $tableName = 'N' . (string) $this->db->getQuoter()->quoteValue($tableSchema->getName());
192
193 505
        $sql = <<<SQL
194 505
        SELECT [value]
195
        FROM fn_listextendedproperty (
196
            N'MS_description',
197 505
            'SCHEMA', $schemaName,
198 505
            'TABLE', $tableName,
199
            DEFAULT, DEFAULT)
200 505
        SQL;
201
202 505
        $comment = $this->db->createCommand($sql)->queryScalar();
203
204 505
        $tableSchema->comment(is_string($comment) ? $comment : null);
205
    }
206
207
    /**
208
     * Returns all table names in the database.
209
     *
210
     * This method should be overridden by child classes to support this feature because the default implementation
211
     * simply throws an exception.
212
     *
213
     * @param string $schema The schema of the tables.
214
     * Defaults to empty string, meaning the current or default schema.
215
     *
216
     * @throws Exception
217
     * @throws InvalidConfigException
218
     * @throws Throwable
219
     *
220
     * @return array All tables name in the database. The names have NO schema name prefix.
221
     */
222 11
    protected function findTableNames(string $schema = ''): array
223
    {
224 11
        if ($schema === '') {
225 11
            $schema = $this->defaultSchema;
226
        }
227
228 11
        $sql = <<<SQL
229
        SELECT [t].[table_name]
230
        FROM [INFORMATION_SCHEMA].[TABLES] AS [t]
231
        WHERE [t].[table_schema] = :schema AND [t].[table_type] IN ('BASE TABLE', 'VIEW')
232
        ORDER BY [t].[table_name]
233 11
        SQL;
234
235 11
        return $this->db->createCommand($sql, [':schema' => $schema])->queryColumn();
236
    }
237
238
    /**
239
     * Loads the metadata for the specified table.
240
     *
241
     * @param string $name The table name.
242
     *
243
     * @throws Exception
244
     * @throws InvalidConfigException
245
     * @throws Throwable
246
     *
247
     * @return TableSchemaInterface|null DBMS-dependent table metadata, `null` if the table doesn't exist.
248
     */
249 505
    protected function loadTableSchema(string $name): TableSchemaInterface|null
250
    {
251 505
        $table = $this->resolveTableName($name);
252 505
        $this->findPrimaryKeys($table);
253 505
        $this->findTableComment($table);
254
255 505
        if ($this->findColumns($table)) {
256 485
            $this->findForeignKeys($table);
257 485
            return $table;
258
        }
259
260 166
        return null;
261
    }
262
263
    /**
264
     * Loads a primary key for the given table.
265
     *
266
     * @param string $tableName The table name.
267
     *
268
     * @throws Exception
269
     * @throws InvalidConfigException
270
     * @throws Throwable
271
     *
272
     * @return Constraint|null The primary key for the given table, `null` if the table has no primary key.
273
     */
274 50
    protected function loadTablePrimaryKey(string $tableName): Constraint|null
275
    {
276
        /** @psalm-var mixed $tablePrimaryKey */
277 50
        $tablePrimaryKey = $this->loadTableConstraints($tableName, self::PRIMARY_KEY);
278 50
        return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null;
279
    }
280
281
    /**
282
     * Loads all foreign keys for the given table.
283
     *
284
     * @param string $tableName The table name.
285
     *
286
     * @throws Exception
287
     * @throws InvalidConfigException
288
     * @throws Throwable
289
     *
290
     * @return array The foreign keys for the given table.
291
     */
292 8
    protected function loadTableForeignKeys(string $tableName): array
293
    {
294
        /** @psalm-var mixed $tableForeignKeys */
295 8
        $tableForeignKeys = $this->loadTableConstraints($tableName, self::FOREIGN_KEYS);
296 8
        return is_array($tableForeignKeys) ? $tableForeignKeys : [];
297
    }
298
299
    /**
300
     * Loads all indexes for the given table.
301
     *
302
     * @param string $tableName The table name.
303
     *
304
     * @throws Exception
305
     * @throws InvalidConfigException
306
     * @throws Throwable
307
     *
308
     * @return array Indexes for the given table.
309
     */
310 39
    protected function loadTableIndexes(string $tableName): array
311
    {
312 39
        $sql = <<<SQL
313
        SELECT
314
            [i].[name] AS [name],
315
            [iccol].[name] AS [column_name],
316
            [i].[is_unique] AS [index_is_unique],
317
            [i].[is_primary_key] AS [index_is_primary]
318
        FROM [sys].[indexes] AS [i]
319
        INNER JOIN [sys].[index_columns] AS [ic]
320
            ON [ic].[object_id] = [i].[object_id] AND [ic].[index_id] = [i].[index_id]
321
        INNER JOIN [sys].[columns] AS [iccol]
322
            ON [iccol].[object_id] = [ic].[object_id] AND [iccol].[column_id] = [ic].[column_id]
323
        WHERE [i].[object_id] = OBJECT_ID(:fullName)
324
        ORDER BY [ic].[key_ordinal] ASC
325 39
        SQL;
326
327 39
        $resolvedName = $this->resolveTableName($tableName);
328 39
        $indexes = $this->db->createCommand($sql, [':fullName' => $resolvedName->getFullName()])->queryAll();
329
330
        /** @psalm-var array[] $indexes */
331 39
        $indexes = $this->normalizeRowKeyCase($indexes, true);
332 39
        $indexes = ArrayHelper::index($indexes, null, ['name']);
333
334 39
        $result = [];
335
336
        /**
337
         * @psalm-var array<
338
         *   string,
339
         *   array<
340
         *     array-key,
341
         *     array{name: string, column_name: string, index_is_unique: string, index_is_primary: string}
342
         *   >
343
         * > $indexes
344
         */
345 39
        foreach ($indexes as $name => $index) {
346 36
            $result[] = (new IndexConstraint())
347 36
                ->primary((bool) $index[0]['index_is_primary'])
348 36
                ->unique((bool) $index[0]['index_is_unique'])
349 36
                ->columnNames(ArrayHelper::getColumn($index, 'column_name'))
350 36
                ->name($name);
351
        }
352
353 39
        return $result;
354
    }
355
356
    /**
357
     * Loads all unique constraints for the given table.
358
     *
359
     * @param string $tableName The table name.
360
     *
361
     * @throws Exception|InvalidConfigException|Throwable
362
     *
363
     * @return array The unique constraints for the given table.
364
     */
365 17
    protected function loadTableUniques(string $tableName): array
366
    {
367
        /** @psalm-var mixed $tableUniques */
368 17
        $tableUniques = $this->loadTableConstraints($tableName, self::UNIQUES);
369 17
        return is_array($tableUniques) ? $tableUniques : [];
370
    }
371
372
    /**
373
     * Loads all check constraints for the given table.
374
     *
375
     * @param string $tableName The table name.
376
     *
377
     * @throws Exception
378
     * @throws InvalidConfigException
379
     * @throws Throwable
380
     *
381
     * @return array The check constraints for the given table.
382
     */
383 17
    protected function loadTableChecks(string $tableName): array
384
    {
385
        /** @psalm-var mixed $tableCheck */
386 17
        $tableCheck = $this->loadTableConstraints($tableName, self::CHECKS);
387 17
        return is_array($tableCheck) ? $tableCheck : [];
388
    }
389
390
    /**
391
     * Loads all default value constraints for the given table.
392
     *
393
     * @param string $tableName The table name.
394
     *
395
     * @throws Exception
396
     * @throws InvalidConfigException
397
     * @throws Throwable
398
     *
399
     * @return array The default value constraints for the given table.
400
     */
401 16
    protected function loadTableDefaultValues(string $tableName): array
402
    {
403
        /** @psalm-var mixed $tableDefault */
404 16
        $tableDefault = $this->loadTableConstraints($tableName, self::DEFAULTS);
405 16
        return is_array($tableDefault) ? $tableDefault : [];
406
    }
407
408
    /**
409
     * Creates a column schema for the database.
410
     *
411
     * This method may be overridden by child classes to create a DBMS-specific column schema.
412
     *
413
     * @param string $name Name of the column.
414
     *
415
     * @return ColumnSchema
416
     */
417 485
    protected function createColumnSchema(string $name): ColumnSchema
418
    {
419 485
        return new ColumnSchema($name);
420
    }
421
422
    /**
423
     * Loads the column information into a {@see ColumnSchemaInterface} object.
424
     *
425
     * @psalm-param ColumnArray $info The column information.
426
     */
427 485
    protected function loadColumnSchema(array $info): ColumnSchemaInterface
428
    {
429 485
        $dbType = $info['data_type'] ?? '';
430
431 485
        $column = $this->createColumnSchema($info['column_name']);
432 485
        $column->allowNull($info['is_nullable'] === 'YES');
433 485
        $column->dbType($dbType);
434 485
        $column->enumValues([]); // MSSQL has only vague equivalents to enum.
435 485
        $column->primaryKey(false); // The primary key will be determined in the `findColumns()` method.
436 485
        $column->autoIncrement($info['is_identity'] === '1');
437 485
        $column->computed($info['is_computed'] === '1');
438 485
        $column->unsigned(stripos($dbType, 'unsigned') !== false);
439 485
        $column->comment($info['comment'] ?? '');
440 485
        $column->type(self::TYPE_STRING);
441
442 485
        if (preg_match('/^(\w+)(?:\(([^)]+)\))?/', $dbType, $matches)) {
443 485
            $type = $matches[1];
444
445 485
            if (isset($this->typeMap[$type])) {
446 485
                $column->type($this->typeMap[$type]);
447
            }
448
449 485
            if (!empty($matches[2])) {
450 318
                $values = explode(',', $matches[2]);
451 318
                $column->precision((int) $values[0]);
452 318
                $column->size((int) $values[0]);
453
454 318
                if (isset($values[1])) {
455 91
                    $column->scale((int) $values[1]);
456
                }
457
458 318
                if ($column->getSize() === 1 && ($type === 'tinyint' || $type === 'bit')) {
459
                    $column->type(self::TYPE_BOOLEAN);
460 318
                } elseif ($type === 'bit') {
461
                    if ($column->getSize() > 32) {
462
                        $column->type(self::TYPE_BIGINT);
463
                    } elseif ($column->getSize() === 32) {
464
                        $column->type(self::TYPE_INTEGER);
465
                    }
466
                }
467
            }
468
        }
469
470 485
        $column->phpType($this->getColumnPhpType($column));
471
472 485
        if ($info['column_default'] === '(NULL)') {
473 8
            $column->defaultValue(null);
474
        }
475
476 485
        if (!$column->isPrimaryKey() && !$column->isComputed() && $info['column_default'] !== null) {
477
            /** @psalm-var mixed $value */
478 367
            $value = $this->parseDefaultValue($info['column_default']);
479
480 367
            if (is_numeric($value)) {
481
                /** @psalm-var mixed $value */
482 128
                $value = $column->phpTypeCast($value);
483
            }
484
485 367
            $column->defaultValue($value);
486
        }
487
488 485
        return $column;
489
    }
490
491
    /**
492
     * Collects the metadata of table columns.
493
     *
494
     * @param TableSchemaInterface $table The table metadata.
495
     *
496
     * @throws Throwable
497
     *
498
     * @return bool Whether the table exists in the database.
499
     */
500 505
    protected function findColumns(TableSchemaInterface $table): bool
501
    {
502 505
        $columnsTableName = 'INFORMATION_SCHEMA.COLUMNS';
503
504 505
        $whereParams = [':table_name' => $table->getName()];
505 505
        $whereSql = '[t1].[table_name] = :table_name';
506
507 505
        if ($table->getCatalogName() !== null) {
508
            $columnsTableName = "{$table->getCatalogName()}.$columnsTableName";
509
            $whereSql .= ' AND [t1].[table_catalog] = :catalog';
510
            $whereParams[':catalog'] = $table->getCatalogName();
511
        }
512
513 505
        if ($table->getSchemaName() !== null) {
514 505
            $whereSql .= " AND [t1].[table_schema] = '{$table->getSchemaName()}'";
515
        }
516
517 505
        $columnsTableName = $this->db->getQuoter()->quoteTableName($columnsTableName);
518
519 505
        $sql = <<<SQL
520 505
        SELECT
521
            [t1].[column_name],
522
            [t1].[is_nullable],
523
        CASE WHEN [t1].[data_type] IN ('char','varchar','nchar','nvarchar','binary','varbinary') THEN
524
        CASE WHEN [t1].[character_maximum_length] = NULL OR [t1].[character_maximum_length] = -1 THEN
525
            [t1].[data_type]
526
        ELSE
527
            [t1].[data_type] + '(' + LTRIM(RTRIM(CONVERT(CHAR,[t1].[character_maximum_length]))) + ')'
528
        END
529
        WHEN [t1].[data_type] IN ('decimal','numeric') THEN
530
        CASE WHEN [t1].[numeric_precision] = NULL OR [t1].[numeric_precision] = -1 THEN
531
            [t1].[data_type]
532
        ELSE
533
            [t1].[data_type] + '(' + LTRIM(RTRIM(CONVERT(CHAR,[t1].[numeric_precision]))) + ',' + LTRIM(RTRIM(CONVERT(CHAR,[t1].[numeric_scale]))) + ')'
534
        END
535
        ELSE
536
            [t1].[data_type]
537
        END AS 'data_type',
538
        [t1].[column_default],
539
        COLUMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsIdentity') AS is_identity,
540
        COLUMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsComputed') AS is_computed,
541
        (
542
        SELECT CONVERT(VARCHAR, [t2].[value])
543
        FROM [sys].[extended_properties] AS [t2]
544
        WHERE
545
        [t2].[class] = 1 AND
546
        [t2].[class_desc] = 'OBJECT_OR_COLUMN' AND
547
        [t2].[name] = 'MS_Description' AND
548
        [t2].[major_id] = OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[table_name]) AND
549
        [t2].[minor_id] = COLUMNPROPERTY(OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[TABLE_NAME]), [t1].[COLUMN_NAME], 'ColumnID')
550
        ) as comment
551 505
        FROM $columnsTableName AS [t1]
552 505
        WHERE $whereSql
553 505
        SQL;
554
555
        try {
556
            /** @psalm-var ColumnArray[] $columns */
557 505
            $columns = $this->db->createCommand($sql, $whereParams)->queryAll();
558
559 505
            if (empty($columns)) {
560 505
                return false;
561
            }
562
        } catch (Exception) {
563
            return false;
564
        }
565
566 485
        foreach ($columns as $column) {
567 485
            $column = $this->loadColumnSchema($column);
568 485
            foreach ($table->getPrimaryKey() as $primaryKey) {
569 297
                if (strcasecmp($column->getName(), $primaryKey) === 0) {
570 297
                    $column->primaryKey(true);
571 297
                    break;
572
                }
573
            }
574
575 485
            if ($column->isPrimaryKey() && $column->isAutoIncrement()) {
576 286
                $table->sequenceName('');
577
            }
578
579 485
            $table->columns($column->getName(), $column);
580
        }
581
582 485
        return true;
583
    }
584
585
    /**
586
     * Collects the constraint details for the given table and constraint type.
587
     *
588
     * @param string $type Either PRIMARY KEY or UNIQUE.
589
     *
590
     * @throws Exception
591
     * @throws InvalidConfigException
592
     * @throws Throwable
593
     *
594
     * @return array Each entry has index_name and field_name.
595
     */
596 505
    protected function findTableConstraints(TableSchemaInterface $table, string $type): array
597
    {
598 505
        $keyColumnUsageTableName = 'INFORMATION_SCHEMA.KEY_COLUMN_USAGE';
599 505
        $tableConstraintsTableName = 'INFORMATION_SCHEMA.TABLE_CONSTRAINTS';
600
601 505
        $catalogName = $table->getCatalogName();
602 505
        if ($catalogName !== null) {
603
            $keyColumnUsageTableName = $catalogName . '.' . $keyColumnUsageTableName;
604
            $tableConstraintsTableName = $catalogName . '.' . $tableConstraintsTableName;
605
        }
606
607 505
        $keyColumnUsageTableName = $this->db->getQuoter()->quoteTableName($keyColumnUsageTableName);
608 505
        $tableConstraintsTableName = $this->db->getQuoter()->quoteTableName($tableConstraintsTableName);
609
610 505
        $sql = <<<SQL
611 505
        SELECT
612
            [kcu].[constraint_name] AS [index_name],
613
            [kcu].[column_name] AS [field_name]
614 505
        FROM $keyColumnUsageTableName AS [kcu]
615 505
        LEFT JOIN $tableConstraintsTableName AS [tc] ON
616
            [kcu].[table_schema] = [tc].[table_schema] AND
617
            [kcu].[table_name] = [tc].[table_name] AND
618
            [kcu].[constraint_name] = [tc].[constraint_name]
619
        WHERE
620
            [tc].[constraint_type] = :type AND
621
            [kcu].[table_name] = :tableName AND
622
            [kcu].[table_schema] = :schemaName
623 505
        SQL;
624
625 505
        return $this->db->createCommand(
626 505
            $sql,
627 505
            [
628 505
                ':tableName' => $table->getName(),
629 505
                ':schemaName' => $table->getSchemaName(),
630 505
                ':type' => $type,
631 505
            ]
632 505
        )->queryAll();
633
    }
634
635
    /**
636
     * Collects the primary key column details for the given table.
637
     *
638
     * @param TableSchemaInterface $table The table metadata
639
     *
640
     * @throws Exception
641
     * @throws InvalidConfigException
642
     * @throws Throwable
643
     */
644 505
    protected function findPrimaryKeys(TableSchemaInterface $table): void
645
    {
646
        /** @psalm-var array<array-key, array{index_name: string, field_name: string}> $primaryKeys */
647 505
        $primaryKeys = $this->findTableConstraints($table, 'PRIMARY KEY');
648
649 505
        foreach ($primaryKeys as $row) {
650 297
            $table->primaryKey($row['field_name']);
651
        }
652
    }
653
654
    /**
655
     * Collects the foreign key column details for the given table.
656
     *
657
     * @param TableSchemaInterface $table The table metadata
658
     *
659
     * @throws Exception
660
     * @throws InvalidConfigException
661
     * @throws Throwable
662
     */
663 485
    protected function findForeignKeys(TableSchemaInterface $table): void
664
    {
665 485
        $catalogName = $table->getCatalogName();
666 485
        $fk = [];
667 485
        $object = $table->getName();
668 485
        $schemaName = $table->getSchemaName();
669
670 485
        if ($schemaName !== null) {
671 485
            $object = $schemaName . '.' . $object;
672
        }
673
674 485
        if ($catalogName !== null) {
675
            $object = $catalogName . '.' . $object;
676
        }
677
678 485
        $sql = <<<SQL
679
        SELECT
680
        [fk].[name] AS [fk_name],
681
        [cp].[name] AS [fk_column_name],
682
        OBJECT_NAME([fk].[referenced_object_id]) AS [uq_table_name],
683
        [cr].[name] AS [uq_column_name]
684
        FROM [sys].[foreign_keys] AS [fk]
685
        INNER JOIN [sys].[foreign_key_columns] AS [fkc]
686
            ON [fk].[object_id] = [fkc].[constraint_object_id]
687
        INNER JOIN [sys].[columns] AS [cp]
688
            ON [fk].[parent_object_id] = [cp].[object_id] AND [fkc].[parent_column_id] = [cp].[column_id]
689
        INNER JOIN [sys].[columns] AS [cr]
690
            ON [fk].[referenced_object_id] = [cr].[object_id] AND [fkc].[referenced_column_id] = [cr].[column_id]
691
        WHERE [fk].[parent_object_id] = OBJECT_ID(:object)
692 485
        SQL;
693
694
        /**
695
         * @psalm-var array<
696
         *   array-key,
697
         *   array{fk_name: string, fk_column_name: string, uq_table_name: string, uq_column_name: string}
698
         * > $rows
699
         */
700 485
        $rows = $this->db->createCommand($sql, [':object' => $object])->queryAll();
701 485
        $table->foreignKeys([]);
702
703 485
        foreach ($rows as $row) {
704 10
            if (!isset($table->getForeignKeys()[$row['fk_name']])) {
705 10
                $fk[$row['fk_name']][] = $row['uq_table_name'];
706 10
                $table->foreignKeys($fk);
707
            }
708
709 10
            $fk[$row['fk_name']][$row['fk_column_name']] = $row['uq_column_name'];
710 10
            $table->foreignKeys($fk);
711
        }
712
    }
713
714
    /**
715
     * @throws Exception
716
     * @throws InvalidConfigException
717
     * @throws Throwable
718
     */
719 4
    protected function findViewNames(string $schema = ''): array
720
    {
721 4
        if ($schema === '') {
722 1
            $schema = $this->defaultSchema;
723
        }
724
725 4
        $sql = <<<SQL
726
        SELECT [t].[table_name]
727
        FROM [INFORMATION_SCHEMA].[TABLES] AS [t]
728
        WHERE [t].[table_schema] = :schema AND [t].[table_type] = 'VIEW'
729
        ORDER BY [t].[table_name]
730 4
        SQL;
731
732 4
        return $this->db->createCommand($sql, [':schema' => $schema])->queryColumn();
733
    }
734
735
    /**
736
     * Returns all unique indexes for the given table.
737
     *
738
     * Each array element is of the following structure:
739
     *
740
     * ```php
741
     * [
742
     *     'IndexName1' => ['col1' [, ...]],
743
     *     'IndexName2' => ['col2' [, ...]],
744
     * ]
745
     * ```
746
     *
747
     * @param TableSchemaInterface $table The table metadata.
748
     *
749
     * @throws Exception
750
     * @throws InvalidConfigException
751
     * @throws Throwable
752
     *
753
     * @return array All unique indexes for the given table.
754
     */
755 1
    public function findUniqueIndexes(TableSchemaInterface $table): array
756
    {
757 1
        $result = [];
758
759
        /** @psalm-var array<array-key, array{index_name: string, field_name: string}> $tableUniqueConstraints */
760 1
        $tableUniqueConstraints = $this->findTableConstraints($table, 'UNIQUE');
761
762 1
        foreach ($tableUniqueConstraints as $row) {
763 1
            $result[$row['index_name']][] = $row['field_name'];
764
        }
765
766 1
        return $result;
767
    }
768
769
    /**
770
     * Loads multiple types of constraints and returns the specified ones.
771
     *
772
     * @param string $tableName table name.
773
     * @param string $returnType return type:
774
     * - primaryKey
775
     * - foreignKeys
776
     * - uniques
777
     * - checks
778
     * - defaults
779
     *
780
     * @throws Exception
781
     * @throws InvalidConfigException
782
     * @throws Throwable
783
     *
784
     * @return mixed Constraints of the specified type.
785
     */
786 108
    private function loadTableConstraints(string $tableName, string $returnType): mixed
787
    {
788 108
        $sql = <<<SQL
789
        SELECT
790
            [o].[name] AS [name],
791
            COALESCE([ccol].[name], [dcol].[name], [fccol].[name], [kiccol].[name]) AS [column_name],
792
            RTRIM([o].[type]) AS [type],
793
            OBJECT_SCHEMA_NAME([f].[referenced_object_id]) AS [foreign_table_schema],
794
            OBJECT_NAME([f].[referenced_object_id]) AS [foreign_table_name],
795
            [ffccol].[name] AS [foreign_column_name],
796
            [f].[update_referential_action_desc] AS [on_update],
797
            [f].[delete_referential_action_desc] AS [on_delete],
798
            [c].[definition] AS [check_expr],
799
            [d].[definition] AS [default_expr]
800
        FROM (SELECT OBJECT_ID(:fullName) AS [object_id]) AS [t]
801
        INNER JOIN [sys].[objects] AS [o]
802
            ON [o].[parent_object_id] = [t].[object_id] AND [o].[type] IN ('PK', 'UQ', 'C', 'D', 'F')
803
        LEFT JOIN [sys].[check_constraints] AS [c]
804
            ON [c].[object_id] = [o].[object_id]
805
        LEFT JOIN [sys].[columns] AS [ccol]
806
            ON [ccol].[object_id] = [c].[parent_object_id] AND [ccol].[column_id] = [c].[parent_column_id]
807
        LEFT JOIN [sys].[default_constraints] AS [d]
808
            ON [d].[object_id] = [o].[object_id]
809
        LEFT JOIN [sys].[columns] AS [dcol]
810
            ON [dcol].[object_id] = [d].[parent_object_id] AND [dcol].[column_id] = [d].[parent_column_id]
811
        LEFT JOIN [sys].[key_constraints] AS [k]
812
            ON [k].[object_id] = [o].[object_id]
813
        LEFT JOIN [sys].[index_columns] AS [kic]
814
            ON [kic].[object_id] = [k].[parent_object_id] AND [kic].[index_id] = [k].[unique_index_id]
815
        LEFT JOIN [sys].[columns] AS [kiccol]
816
            ON [kiccol].[object_id] = [kic].[object_id] AND [kiccol].[column_id] = [kic].[column_id]
817
        LEFT JOIN [sys].[foreign_keys] AS [f]
818
            ON [f].[object_id] = [o].[object_id]
819
        LEFT JOIN [sys].[foreign_key_columns] AS [fc]
820
            ON [fc].[constraint_object_id] = [o].[object_id]
821
        LEFT JOIN [sys].[columns] AS [fccol]
822
            ON [fccol].[object_id] = [fc].[parent_object_id] AND [fccol].[column_id] = [fc].[parent_column_id]
823
        LEFT JOIN [sys].[columns] AS [ffccol]
824
            ON [ffccol].[object_id] = [fc].[referenced_object_id] AND [ffccol].[column_id] = [fc].[referenced_column_id]
825
        ORDER BY [kic].[key_ordinal] ASC, [fc].[constraint_column_id] ASC
826 108
        SQL;
827
828 108
        $resolvedName = $this->resolveTableName($tableName);
829 108
        $constraints = $this->db->createCommand($sql, [':fullName' => $resolvedName->getFullName()])->queryAll();
830
831
        /** @psalm-var array[] $constraints */
832 108
        $constraints = $this->normalizeRowKeyCase($constraints, true);
833 108
        $constraints = ArrayHelper::index($constraints, null, ['type', 'name']);
834
835 108
        $result = [
836 108
            self::PRIMARY_KEY => null,
837 108
            self::FOREIGN_KEYS => [],
838 108
            self::UNIQUES => [],
839 108
            self::CHECKS => [],
840 108
            self::DEFAULTS => [],
841 108
        ];
842
843
        /** @psalm-var array<array-key, array> $constraints */
844 108
        foreach ($constraints as $type => $names) {
845
            /**
846
             * @psalm-var object|string|null $name
847
             * @psalm-var ConstraintArray $constraint
848
             */
849 99
            foreach ($names as $name => $constraint) {
850
                switch ($type) {
851 99
                    case 'PK':
852
                        /** @psalm-var Constraint */
853 65
                        $result[self::PRIMARY_KEY] = (new Constraint())
854 65
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
855 65
                            ->name($name);
856 65
                        break;
857 93
                    case 'F':
858 23
                        $result[self::FOREIGN_KEYS][] = (new ForeignKeyConstraint())
859 23
                            ->foreignSchemaName($constraint[0]['foreign_table_schema'])
860 23
                            ->foreignTableName($constraint[0]['foreign_table_name'])
861 23
                            ->foreignColumnNames(ArrayHelper::getColumn($constraint, 'foreign_column_name'))
862 23
                            ->onDelete(str_replace('_', '', $constraint[0]['on_delete']))
863 23
                            ->onUpdate(str_replace('_', '', $constraint[0]['on_update']))
864 23
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
865 23
                            ->name($name);
866 23
                        break;
867 77
                    case 'UQ':
868 71
                        $result[self::UNIQUES][] = (new Constraint())
869 71
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
870 71
                            ->name($name);
871 71
                        break;
872 41
                    case 'C':
873 19
                        $result[self::CHECKS][] = (new CheckConstraint())
874 19
                            ->expression($constraint[0]['check_expr'])
875 19
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
876 19
                            ->name($name);
877 19
                        break;
878 38
                    case 'D':
879 38
                        $result[self::DEFAULTS][] = (new DefaultValueConstraint())
880 38
                            ->value($constraint[0]['default_expr'])
881 38
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
882 38
                            ->name($name);
883 38
                        break;
884
                }
885
            }
886
        }
887
888 108
        foreach ($result as $type => $data) {
889 108
            $this->setTableMetadata($tableName, $type, $data);
890
        }
891
892 108
        return $result[$returnType];
893
    }
894
895
    /**
896
     * Returns the cache key for the specified table name.
897
     *
898
     * @param string $name The table name.
899
     *
900
     * @return array The cache key.
901
     */
902 607
    protected function getCacheKey(string $name): array
903
    {
904 607
        return array_merge([self::class], $this->db->getCacheKey(), [$this->getRawTableName($name)]);
905
    }
906
907
    /**
908
     * Returns the cache tag name.
909
     *
910
     * This allows {@see refresh()} to invalidate all cached table schemas.
911
     *
912
     * @return string The cache tag name.
913
     */
914 608
    protected function getCacheTag(): string
915
    {
916 608
        return md5(serialize(array_merge([self::class], $this->db->getCacheKey())));
917
    }
918
919 367
    private function parseDefaultValue(mixed $value): mixed
920
    {
921 367
        $value = (string) $value;
922
923 367
        if (preg_match('/^\'(.*)\'$/', $value, $matches)) {
924 83
            return $matches[1];
925
        }
926
927 367
        if (preg_match('/^\((.*)\)$/', $value, $matches)) {
928 367
            return $this->parseDefaultValue($matches[1]);
929
        }
930
931 295
        return $value;
932
    }
933
}
934