Test Failed
Push — dev ( 45bca2...2b4c0b )
by Def
03:48
created

SchemaPDOMssql::resolveTableNames()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0549

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 35
ccs 13
cts 23
cp 0.5652
crap 7.0549
rs 9.2248
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql\PDO;
6
7
use Throwable;
8
use Yiisoft\Arrays\ArrayHelper;
9
use Yiisoft\Db\Cache\SchemaCache;
10
use Yiisoft\Db\Connection\ConnectionPDOInterface;
11
use Yiisoft\Db\Constraint\CheckConstraint;
12
use Yiisoft\Db\Constraint\Constraint;
13
use Yiisoft\Db\Constraint\DefaultValueConstraint;
14
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
15
use Yiisoft\Db\Constraint\IndexConstraint;
16
use Yiisoft\Db\Exception\Exception;
17
use Yiisoft\Db\Exception\InvalidCallException;
18
use Yiisoft\Db\Exception\InvalidConfigException;
19
use Yiisoft\Db\Exception\NotSupportedException;
20
use Yiisoft\Db\Mssql\ColumnSchema;
21
use Yiisoft\Db\Mssql\PDO;
22
use Yiisoft\Db\Mssql\TableSchema;
23
use Yiisoft\Db\Schema\ColumnSchemaBuilder;
24
use Yiisoft\Db\Schema\Schema;
25
use Yiisoft\Db\View\ViewInterface;
26
27
use function array_map;
28
use function count;
29
use function explode;
30
use function preg_match;
31
use function preg_match_all;
32
use function str_replace;
33
use function strcasecmp;
34
use function stripos;
35
36
/**
37
 * Schema is the class for retrieving metadata from MS SQL Server databases (version 2008 and above).
38
 *
39
 * @psalm-type ColumnArray = array{
40
 *   column_name: string,
41
 *   is_nullable: string,
42
 *   data_type: string,
43
 *   column_default: mixed,
44
 *   is_identity: string,
45
 *   comment: null|string
46
 * }
47
 *
48
 * @psalm-type ConstraintArray = array<
49
 *   array-key,
50
 *   array {
51
 *     name: string,
52
 *     column_name: string,
53
 *     type: string,
54
 *     foreign_table_schema: string|null,
55
 *     foreign_table_name: string|null,
56
 *     foreign_column_name: string|null,
57
 *     on_update: string,
58
 *     on_delete: string,
59
 *     check_expr: string,
60
 *     default_expr: string
61
 *   }
62
 * >
63
 */
64
final class SchemaPDOMssql extends Schema implements ViewInterface
65
{
66
    /**
67
     * @var string|null the default schema used for the current session.
68
     */
69
    protected ?string $defaultSchema = 'dbo';
70
71
    /**
72
     * @var array mapping from physical column types (keys) to abstract column types (values)
73
     *
74
     * @psalm-var string[]
75
     */
76
    private array $typeMap = [
77
        /** exact numbers */
78
        'bigint' => self::TYPE_BIGINT,
79
        'numeric' => self::TYPE_DECIMAL,
80
        'bit' => self::TYPE_SMALLINT,
81
        'smallint' => self::TYPE_SMALLINT,
82
        'decimal' => self::TYPE_DECIMAL,
83
        'smallmoney' => self::TYPE_MONEY,
84
        'int' => self::TYPE_INTEGER,
85
        'tinyint' => self::TYPE_TINYINT,
86
        'money' => self::TYPE_MONEY,
87
88
        /** approximate numbers */
89
        'float' => self::TYPE_FLOAT,
90
        'double' => self::TYPE_DOUBLE,
91
        'real' => self::TYPE_FLOAT,
92
93
        /** date and time */
94
        'date' => self::TYPE_DATE,
95
        'datetimeoffset' => self::TYPE_DATETIME,
96
        'datetime2' => self::TYPE_DATETIME,
97
        'smalldatetime' => self::TYPE_DATETIME,
98
        'datetime' => self::TYPE_DATETIME,
99
        'time' => self::TYPE_TIME,
100
101
        /** character strings */
102
        'char' => self::TYPE_CHAR,
103
        'varchar' => self::TYPE_STRING,
104
        'text' => self::TYPE_TEXT,
105
106
        /** unicode character strings */
107
        'nchar' => self::TYPE_CHAR,
108
        'nvarchar' => self::TYPE_STRING,
109
        'ntext' => self::TYPE_TEXT,
110
111
        /** binary strings */
112
        'binary' => self::TYPE_BINARY,
113
        'varbinary' => self::TYPE_BINARY,
114
        'image' => self::TYPE_BINARY,
115
116
        /**
117
         * other data types 'cursor' type cannot be used with tables
118
         */
119
        'timestamp' => self::TYPE_TIMESTAMP,
120
        'hierarchyid' => self::TYPE_STRING,
121
        'uniqueidentifier' => self::TYPE_STRING,
122
        'sql_variant' => self::TYPE_STRING,
123
        'xml' => self::TYPE_STRING,
124
        'table' => self::TYPE_STRING,
125
    ];
126
127
    private ?string $serverVersion = null;
0 ignored issues
show
introduced by
The private property $serverVersion is not used, and could be removed.
Loading history...
128
    private array $viewNames = [];
129
130
    public function __construct(private ConnectionPDOInterface $db, SchemaCache $schemaCache)
131 372
    {
132
        parent::__construct($schemaCache);
133 372
    }
134
135
    /**
136
     * Resolves the table name and schema name (if any).
137
     *
138
     * @param string $name the table name.
139
     *
140
     * @return TableSchema resolved table, schema, etc. names.
141
     *
142
     * @todo Review this method and see if it can be simplified @darkdef.
143
     */
144
    protected function resolveTableName(string $name): TableSchema
145 84
    {
146
        $resolvedName = new TableSchema();
147 84
148
        $parts = $this->getTableNameParts($name);
149 84
150
        $partCount = count($parts);
151 84
152
        if ($partCount === 4) {
153 84
            /** server name, catalog name, schema name and table name passed - not coverage tests */
154
            $resolvedName->catalogName($parts[1]);
155
            $resolvedName->schemaName($parts[2]);
156
            $resolvedName->name($parts[3]);
157
            $resolvedName->fullName(
158
                (string) $resolvedName->getCatalogName() . '.' .
159
                (string) $resolvedName->getSchemaName() . '.' .
160
                $resolvedName->getName()
161
            );
162
        } elseif ($partCount === 3) {
163 84
            /** catalog name, schema name and table name passed - not coverage tests */
164
            $resolvedName->catalogName($parts[0]);
165
            $resolvedName->schemaName($parts[1]);
166
            $resolvedName->name($parts[2]);
167
            $resolvedName->fullName(
168
                (string) $resolvedName->getCatalogName() . '.' .
169
                (string) $resolvedName->getSchemaName() . '.' .
170
                $resolvedName->getName()
171
            );
172
        } elseif ($partCount === 2) {
173 84
            /** only schema name and table name passed - not coverage tests */
174
            $resolvedName->schemaName($parts[0]);
175
            $resolvedName->name($parts[1]);
176
            $resolvedName->fullName(
177
                (
178
                    $resolvedName->getSchemaName() !== $this->defaultSchema
179
                    ? (string) $resolvedName->getSchemaName() . '.' : ''
180
                ) . $resolvedName->getName()
181
            );
182
        } else {
183
            /** only table name passed */
184
            $resolvedName->schemaName($this->defaultSchema);
185 84
            $resolvedName->name($parts[0]);
186 84
            $resolvedName->fullName($resolvedName->getName());
187 84
        }
188
189
        return $resolvedName;
190 84
    }
191
192
    /**
193
     * Splits full table name into parts.
194
     *
195
     * @param string $name
196
     *
197
     * @psalm-return string[]
198
     */
199
    protected function getTableNameParts(string $name): array
200 163
    {
201
        $parts = [$name];
202 163
203
        preg_match_all('/([^.\[\]]+)|\[([^\[\]]+)]/', $name, $matches);
204 163
205
        if (isset($matches[0]) && !empty($matches[0])) {
206 163
            $parts = $matches[0];
207 163
        }
208
209
        /** @psalm-var string[] */
210
        return str_replace(['[', ']'], '', $parts);
211 163
    }
212
213
    /**
214
     * Returns all schema names in the database, including the default one but not system schemas.
215
     *
216
     * This method should be overridden by child classes in order to support this feature because the default
217
     * implementation simply throws an exception.
218
     *
219
     * @throws Exception|InvalidConfigException|Throwable
220
     *
221
     * @return array all schema names in the database, except system schemas.
222
     *
223
     * {@see https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-database-principals-transact-sql}
224
     */
225
    protected function findSchemaNames(): array
226 1
    {
227
        $sql = <<<SQL
228 1
        SELECT [s].[name]
229
        FROM [sys].[schemas] AS [s]
230
        INNER JOIN [sys].[database_principals] AS [p] ON [p].[principal_id] = [s].[principal_id]
231
        WHERE [p].[is_fixed_role] = 0 AND [p].[sid] IS NOT NULL
232
        ORDER BY [s].[name] ASC
233
        SQL;
234
235
        return $this->db->createCommand($sql)->queryColumn();
236 1
    }
237
238
    /**
239
     * Returns all table names in the database.
240
     *
241
     * This method should be overridden by child classes in order to support this feature because the default
242
     * implementation simply throws an exception.
243
     *
244
     * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
245
     *
246
     * @throws Exception|InvalidConfigException|Throwable
247
     *
248
     * @return array all table names in the database. The names have NO schema name prefix.
249
     */
250
    protected function findTableNames(string $schema = ''): array
251 5
    {
252
        if ($schema === '') {
253 5
            $schema = $this->defaultSchema;
254 5
        }
255
256
        $sql = <<<SQL
257 5
        SELECT [t].[table_name]
258
        FROM [INFORMATION_SCHEMA].[TABLES] AS [t]
259
        WHERE [t].[table_schema] = :schema AND [t].[table_type] IN ('BASE TABLE', 'VIEW')
260
        ORDER BY [t].[table_name]
261
        SQL;
262
263
        $tables = $this->db->createCommand($sql, [':schema' => $schema])->queryColumn();
264 5
265
        return array_map(static fn (string $item): string => '[' . $item . ']', $tables);
266 5
    }
267
268
    /**
269
     * Loads the metadata for the specified table.
270
     *
271
     * @param string $name table name.
272
     *
273
     * @throws Exception|InvalidConfigException|Throwable
274
     *
275
     * @return TableSchema|null DBMS-dependent table metadata, `null` if the table does not exist.
276
     */
277
    protected function loadTableSchema(string $name): ?TableSchema
278 103
    {
279
        $table = new TableSchema();
280 103
281
        $this->resolveTableNames($table, $name);
282 103
        $this->findPrimaryKeys($table);
283 103
284
        if ($this->findColumns($table)) {
285 103
            $this->findForeignKeys($table);
286 94
            return $table;
287 94
        }
288
289
        return null;
290 19
    }
291
292
    /**
293
     * Loads a primary key for the given table.
294
     *
295
     * @param string $tableName table name.
296
     *
297
     * @throws Exception|InvalidConfigException|Throwable
298
     *
299
     * @return Constraint|null The primary key for the given table, `null` if the table has no primary key.
300
     */
301
    protected function loadTablePrimaryKey(string $tableName): ?Constraint
302 31
    {
303
        /** @var mixed */
304
        $tablePrimaryKey = $this->loadTableConstraints($tableName, 'primaryKey');
305 31
        return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null;
306 31
    }
307
308
    /**
309
     * Loads all foreign keys for the given table.
310
     *
311
     * @param string $tableName table name.
312
     *
313
     * @throws Exception|InvalidConfigException|Throwable
314
     *
315
     * @return array The foreign keys for the given table.
316
     */
317
    protected function loadTableForeignKeys(string $tableName): array
318 4
    {
319
        /** @var mixed */
320
        $tableForeingKeys = $this->loadTableConstraints($tableName, 'foreignKeys');
321 4
        return is_array($tableForeingKeys) ? $tableForeingKeys : [];
322 4
    }
323
324
    /**
325
     * Loads all indexes for the given table.
326
     *
327
     * @param string $tableName table name.
328
     *
329
     * @throws Exception|InvalidConfigException|Throwable
330
     *
331
     * @return array indexes for the given table.
332
     */
333
    protected function loadTableIndexes(string $tableName): array
334 29
    {
335
        $sql = <<<SQL
336 29
        SELECT
337
            [i].[name] AS [name],
338
            [iccol].[name] AS [column_name],
339
            [i].[is_unique] AS [index_is_unique],
340
            [i].[is_primary_key] AS [index_is_primary]
341
        FROM [sys].[indexes] AS [i]
342
        INNER JOIN [sys].[index_columns] AS [ic]
343
            ON [ic].[object_id] = [i].[object_id] AND [ic].[index_id] = [i].[index_id]
344
        INNER JOIN [sys].[columns] AS [iccol]
345
            ON [iccol].[object_id] = [ic].[object_id] AND [iccol].[column_id] = [ic].[column_id]
346
        WHERE [i].[object_id] = OBJECT_ID(:fullName)
347
        ORDER BY [ic].[key_ordinal] ASC
348
        SQL;
349
350
        $resolvedName = $this->resolveTableName($tableName);
351 29
        $indexes = $this->db->createCommand($sql, [':fullName' => $resolvedName->getFullName()])->queryAll();
352 29
353
        /** @psalm-var array[] $indexes */
354
        $indexes = $this->normalizePdoRowKeyCase($indexes, true);
355 29
        $indexes = ArrayHelper::index($indexes, null, 'name');
356 29
357
        $result = [];
358 29
359
        /**
360
         * @psalm-var array<
361
         *   string,
362
         *   array<
363
         *     array-key,
364
         *     array{array-key, name: string, column_name: string, index_is_unique: string, index_is_primary: string}
365
         *   >
366
         * > $indexes
367
         */
368
        foreach ($indexes as $name => $index) {
369 29
            $result[] = (new IndexConstraint())
370 26
                ->primary((bool) $index[0]['index_is_primary'])
371 26
                ->unique((bool) $index[0]['index_is_unique'])
372 26
                ->columnNames(ArrayHelper::getColumn($index, 'column_name'))
373 26
                ->name($name);
374 26
        }
375
376
        return $result;
377 29
    }
378
379
    /**
380
     * Loads all unique constraints for the given table.
381
     *
382
     * @param string $tableName table name.
383
     *
384
     * @throws Exception|InvalidConfigException|Throwable
385
     *
386
     * @return array The unique constraints for the given table.
387
     */
388
    protected function loadTableUniques(string $tableName): array
389 13
    {
390
        /** @var mixed */
391
        $tableUniques = $this->loadTableConstraints($tableName, 'uniques');
392 13
        return is_array($tableUniques) ? $tableUniques : [];
393 13
    }
394
395
    /**
396
     * Loads all check constraints for the given table.
397
     *
398
     * @param string $tableName table name.
399
     *
400
     * @throws Exception|InvalidConfigException|Throwable
401
     *
402
     * @return array The check constraints for the given table.
403
     */
404
    protected function loadTableChecks(string $tableName): array
405 13
    {
406
        /** @var mixed */
407
        $tableCheck = $this->loadTableConstraints($tableName, 'checks');
408 13
        return is_array($tableCheck) ? $tableCheck : [];
409 13
    }
410
411
    /**
412
     * Loads all default value constraints for the given table.
413
     *
414
     * @param string $tableName table name.
415
     *
416
     * @throws Exception|InvalidConfigException|Throwable
417
     *
418
     * @return array The default value constraints for the given table.
419
     */
420
    protected function loadTableDefaultValues(string $tableName): array
421 13
    {
422
        /** @var mixed */
423
        $tableDefault = $this->loadTableConstraints($tableName, 'defaults');
424 13
        return is_array($tableDefault) ? $tableDefault : [];
425 13
    }
426
427
    /**
428
     * Creates a new savepoint.
429
     *
430
     * @param string $name the savepoint name.
431
     *
432
     * @throws Exception|InvalidConfigException|Throwable
433
     */
434
    public function createSavepoint(string $name): void
435 1
    {
436
        $this->db->createCommand("SAVE TRANSACTION $name")->execute();
437 1
    }
438
439
    /**
440
     * Releases an existing savepoint.
441
     *
442
     * @param string $name the savepoint name.
443
     */
444
    public function releaseSavepoint(string $name): void
445 1
    {
446
        throw new NotSupportedException(__METHOD__ . ' is not supported.');
447 1
    }
448
449
    /**
450
     * Rolls back to a previously created savepoint.
451
     *
452
     * @param string $name the savepoint name.
453
     *
454
     * @throws Exception|InvalidConfigException|Throwable
455
     */
456
    public function rollBackSavepoint(string $name): void
457 1
    {
458
        $this->db->createCommand("ROLLBACK TRANSACTION $name")->execute();
459 1
    }
460
461
    /**
462
     * Creates a column schema for the database.
463
     *
464
     * This method may be overridden by child classes to create a DBMS-specific column schema.
465
     *
466
     * @return ColumnSchema column schema instance.
467
     */
468
    protected function createColumnSchema(): ColumnSchema
469 94
    {
470
        return new ColumnSchema();
471 94
    }
472
473
    /**
474
     * Resolves the table name and schema name (if any).
475
     *
476
     * @param TableSchema $table the table metadata object.
477
     * @param string $name the table name
478
     *
479
     * @todo Review this method and see if it can be simplified @darkdef.
480
     */
481
    protected function resolveTableNames(TableSchema $table, string $name): void
482 103
    {
483
        $parts = $this->getTableNameParts($name);
484 103
485
        $partCount = count($parts);
486 103
487
        if ($partCount === 4) {
488 103
            /** server name, catalog name, schema name and table name passed - no coverage tests */
489
            $table->catalogName($parts[1]);
490
            $table->schemaName($parts[2]);
491
            $table->name($parts[3]);
492
            $table->fullName(
493
                (string) $table->getCatalogName() . '.' . (string) $table->getSchemaName() . '.' . $table->getName()
494
            );
495
        } elseif ($partCount === 3) {
496 103
            /** catalog name, schema name and table name passed - no coverage tests */
497
            $table->catalogName($parts[0]);
498
            $table->schemaName($parts[1]);
499
            $table->name($parts[2]);
500
            $table->fullName(
501
                (string) $table->getCatalogName() . '.' . (string) $table->getSchemaName() . '.' . $table->getName()
502
            );
503
        } elseif ($partCount === 2) {
504 103
            /** only schema name and table name passed - no coverage tests */
505
            $table->schemaName($parts[0]);
506 3
            $table->name($parts[1]);
507 3
            $table->fullName(
508 3
                $table->getSchemaName() !== $this->defaultSchema
509 3
                ? (string) $table->getSchemaName() . '.' . $table->getName() : $table->getName()
510 3
            );
511
        } else {
512
            /** only table name passed */
513
            $table->schemaName($this->defaultSchema);
514 100
            $table->name($parts[0]);
515 100
            $table->fullName($table->getName());
516 100
        }
517
    }
518
519
    /**
520
     * Loads the column information into a {@see ColumnSchema} object.
521
     *
522
     * @psalm-param ColumnArray $info The column information.
523
     *
524
     * @return ColumnSchema the column schema object.
525
     */
526
    protected function loadColumnSchema(array $info): ColumnSchema
527 94
    {
528
        $column = $this->createColumnSchema();
529 94
530
        $column->name($info['column_name']);
531 94
        $column->allowNull($info['is_nullable'] === 'YES');
532 94
        $column->dbType($info['data_type']);
533 94
        $column->enumValues([]); // mssql has only vague equivalents to enum
534 94
        $column->primaryKey(false); // primary key will be determined in findColumns() method
535 94
        $column->autoIncrement($info['is_identity'] === '1');
536 94
        $column->unsigned(stripos($column->getDbType(), 'unsigned') !== false);
537 94
        $column->comment($info['comment'] ?? '');
538 94
        $column->type(self::TYPE_STRING);
539 94
540
        if (preg_match('/^(\w+)(?:\(([^)]+)\))?/', $column->getDbType(), $matches)) {
541 94
            $type = $matches[1];
542 94
543
            if (isset($this->typeMap[$type])) {
544 94
                $column->type($this->typeMap[$type]);
545 94
            }
546
547
            if (!empty($matches[2])) {
548 94
                $values = explode(',', $matches[2]);
549 78
                $column->precision((int) $values[0]);
550 78
                $column->size((int) $values[0]);
551 78
552
                if (isset($values[1])) {
553 78
                    $column->scale((int) $values[1]);
554
                }
555
556
                if ($column->getSize() === 1 && ($type === 'tinyint' || $type === 'bit')) {
557 78
                    $column->type('boolean');
558
                } elseif ($type === 'bit') {
559 78
                    if ($column->getSize() > 32) {
560
                        $column->type('bigint');
561
                    } elseif ($column->getSize() === 32) {
562
                        $column->type('integer');
563
                    }
564
                }
565
            }
566
        }
567
568
        $column->phpType($this->getColumnPhpType($column));
569 94
570
        if ($info['column_default'] === '(NULL)') {
571 94
            $info['column_default'] = null;
572 4
        }
573
574
        if (!$column->isPrimaryKey() && ($column->getType() !== 'timestamp' || $info['column_default'] !== 'CURRENT_TIMESTAMP')) {
575 94
            $column->defaultValue($column->defaultPhpTypecast($info['column_default']));
576 94
        }
577
578
        return $column;
579 94
    }
580
581
    /**
582
     * Collects the metadata of table columns.
583
     *
584
     * @param TableSchema $table the table metadata.
585
     *
586
     * @throws Throwable
587
     *
588
     * @return bool whether the table exists in the database.
589
     */
590
    protected function findColumns(TableSchema $table): bool
591 103
    {
592
        $columnsTableName = 'INFORMATION_SCHEMA.COLUMNS';
593 103
        $whereSql = '[t1].[table_name] = ' . (string) $this->db->getQuoter()->quoteValue($table->getName());
594 103
595
        if ($table->getCatalogName() !== null) {
596 103
            $columnsTableName = "{$table->getCatalogName()}.$columnsTableName";
597
            $whereSql .= " AND [t1].[table_catalog] = '{$table->getCatalogName()}'";
598
        }
599
600
        if ($table->getSchemaName() !== null) {
601 103
            $whereSql .= " AND [t1].[table_schema] = '{$table->getSchemaName()}'";
602 103
        }
603
604
        $columnsTableName = $this->db->getQuoter()->quoteTableName($columnsTableName);
605 103
606
        $sql = <<<SQL
607 103
        SELECT
608
            [t1].[column_name],
609
            [t1].[is_nullable],
610
        CASE WHEN [t1].[data_type] IN ('char','varchar','nchar','nvarchar','binary','varbinary') THEN
611
        CASE WHEN [t1].[character_maximum_length] = NULL OR [t1].[character_maximum_length] = -1 THEN
612
            [t1].[data_type]
613
        ELSE
614
            [t1].[data_type] + '(' + LTRIM(RTRIM(CONVERT(CHAR,[t1].[character_maximum_length]))) + ')'
615
        END
616
        ELSE
617
            [t1].[data_type]
618
        END AS 'data_type',
619
        [t1].[column_default],
620
        COLUMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsIdentity') AS is_identity,
621
        (
622
        SELECT CONVERT(VARCHAR, [t2].[value])
623
        FROM [sys].[extended_properties] AS [t2]
624
        WHERE
625
        [t2].[class] = 1 AND
626
        [t2].[class_desc] = 'OBJECT_OR_COLUMN' AND
627
        [t2].[name] = 'MS_Description' AND
628
        [t2].[major_id] = OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[table_name]) AND
629
        [t2].[minor_id] = COLUMNPROPERTY(OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[TABLE_NAME]), [t1].[COLUMN_NAME], 'ColumnID')
630
        ) as comment
631
        FROM $columnsTableName AS [t1]
632
        WHERE $whereSql
633
        SQL;
634
635
        try {
636
            /** @psalm-var ColumnArray[] */
637
            $columns = $this->db->createCommand($sql)->queryAll();
638 103
639
            if (empty($columns)) {
640 103
                return false;
641 103
            }
642
        } catch (Exception $e) {
643
            return false;
644
        }
645
646
        foreach ($columns as $column) {
647 94
            $column = $this->loadColumnSchema($column);
648 94
            foreach ($table->getPrimaryKey() as $primaryKey) {
649 94
                if (strcasecmp($column->getName(), $primaryKey) === 0) {
650 61
                    $column->primaryKey(true);
651 61
                    break;
652 61
                }
653
            }
654
655
            if ($column->isPrimaryKey() && $column->isAutoIncrement()) {
656 94
                $table->sequenceName('');
657 60
            }
658
659
            $table->columns($column->getName(), $column);
660 94
        }
661
662
        return true;
663 94
    }
664
665
    /**
666
     * Collects the constraint details for the given table and constraint type.
667
     *
668
     * @param TableSchema $table
669
     * @param string $type either PRIMARY KEY or UNIQUE.
670
     *
671
     * @throws Exception|InvalidConfigException|Throwable
672
     *
673
     * @return array each entry contains index_name and field_name.
674
     */
675
    protected function findTableConstraints(TableSchema $table, string $type): array
676 103
    {
677
        $keyColumnUsageTableName = 'INFORMATION_SCHEMA.KEY_COLUMN_USAGE';
678 103
        $tableConstraintsTableName = 'INFORMATION_SCHEMA.TABLE_CONSTRAINTS';
679 103
680
        if ($table->getCatalogName() !== null) {
681 103
            $keyColumnUsageTableName = $table->getCatalogName() . '.' . $keyColumnUsageTableName;
682
            $tableConstraintsTableName = $table->getCatalogName() . '.' . $tableConstraintsTableName;
683
        }
684
685
        $keyColumnUsageTableName = $this->db->getQuoter()->quoteTableName($keyColumnUsageTableName);
686 103
        $tableConstraintsTableName = $this->db->getQuoter()->quoteTableName($tableConstraintsTableName);
687 103
688
        $sql = <<<SQL
689 103
        SELECT
690
            [kcu].[constraint_name] AS [index_name],
691
            [kcu].[column_name] AS [field_name]
692
        FROM $keyColumnUsageTableName AS [kcu]
693
        LEFT JOIN $tableConstraintsTableName AS [tc] ON
694
            [kcu].[table_schema] = [tc].[table_schema] AND
695
            [kcu].[table_name] = [tc].[table_name] AND
696
            [kcu].[constraint_name] = [tc].[constraint_name]
697
        WHERE
698
            [tc].[constraint_type] = :type AND
699
            [kcu].[table_name] = :tableName AND
700
            [kcu].[table_schema] = :schemaName
701
        SQL;
702
703
        return $this->db->createCommand(
704 103
            $sql,
705
            [
706
                ':tableName' => $table->getName(),
707 103
                ':schemaName' => $table->getSchemaName(),
708 103
                ':type' => $type,
709
            ]
710
        )->queryAll();
711 103
    }
712
713
    /**
714
     * Collects the primary key column details for the given table.
715
     *
716
     * @param TableSchema $table the table metadata
717
     *
718
     * @throws Exception|InvalidConfigException|Throwable
719
     */
720
    protected function findPrimaryKeys(TableSchema $table): void
721 103
    {
722
        /** @psalm-var array<array-key, array{index_name: string, field_name: string}> $primaryKeys */
723
        $primaryKeys = $this->findTableConstraints($table, 'PRIMARY KEY');
724 103
725
        foreach ($primaryKeys as $row) {
726 103
            $table->primaryKey($row['field_name']);
727 61
        }
728
    }
729
730
    /**
731
     * Collects the foreign key column details for the given table.
732
     *
733
     * @param TableSchema $table the table metadata
734
     *
735
     * @throws Exception|InvalidConfigException|Throwable
736
     */
737
    protected function findForeignKeys(TableSchema $table): void
738 94
    {
739
        $catalogName = $table->getCatalogName();
740 94
        $fk = [];
741 94
        $object = $table->getName();
742 94
        $schemaName = $table->getSchemaName();
743 94
744
        if ($schemaName !== null) {
745 94
            $object = $schemaName . '.' . $object;
746 94
        }
747
748
        if ($catalogName !== null) {
749 94
            $object = $catalogName . '.' . $object;
750
        }
751
752
        /**
753
         * Please refer to the following page for more details:
754
         * {@see http://msdn2.microsoft.com/en-us/library/aa175805(SQL.80).aspx}
755
         */
756
        $sql = <<<SQL
757 94
        SELECT
758
        [fk].[name] AS [fk_name],
759
        [cp].[name] AS [fk_column_name],
760
        OBJECT_NAME([fk].[referenced_object_id]) AS [uq_table_name],
761
        [cr].[name] AS [uq_column_name]
762
        FROM [sys].[foreign_keys] AS [fk]
763
        INNER JOIN [sys].[foreign_key_columns] AS [fkc]
764
            ON [fk].[object_id] = [fkc].[constraint_object_id]
765
        INNER JOIN [sys].[columns] AS [cp]
766
            ON [fk].[parent_object_id] = [cp].[object_id] AND [fkc].[parent_column_id] = [cp].[column_id]
767
        INNER JOIN [sys].[columns] AS [cr]
768
            ON [fk].[referenced_object_id] = [cr].[object_id] AND [fkc].[referenced_column_id] = [cr].[column_id]
769
        WHERE [fk].[parent_object_id] = OBJECT_ID(:object)
770
        SQL;
771
772
        /**
773
         * @psalm-var array<
774
         *   array-key,
775
         *   array{fk_name: string, fk_column_name: string, uq_table_name: string, uq_column_name: string}
776
         * > $rows
777
         */
778
        $rows = $this->db->createCommand($sql, [':object' => $object])->queryAll();
779 94
        $table->foreignKeys([]);
780 94
781
        foreach ($rows as $row) {
782 94
            if (!isset($table->getForeignKeys()[$row['fk_name']])) {
783 5
                $fk[$row['fk_name']][] = $row['uq_table_name'];
784 5
                $table->foreignKeys($fk);
785 5
            }
786
787
            $fk[$row['fk_name']][$row['fk_column_name']] = $row['uq_column_name'];
788 5
            $table->foreignKeys($fk);
789 5
        }
790
    }
791
792
    public function findViewNames(string $schema = ''): array
793 3
    {
794
        if ($schema === '') {
795 3
            $schema = $this->defaultSchema;
796 1
        }
797
798
        $sql = <<<SQL
799 3
        SELECT [t].[table_name]
800
        FROM [INFORMATION_SCHEMA].[TABLES] AS [t]
801
        WHERE [t].[table_schema] = :schema AND [t].[table_type] = 'VIEW'
802
        ORDER BY [t].[table_name]
803
        SQL;
804
805
        $views = $this->db->createCommand($sql, [':schema' => $schema])->queryColumn();
806 3
        return array_map(static fn (string $item): string => '[' . $item . ']', $views);
807 3
    }
808
809
    /**
810
     * Returns all unique indexes for the given table.
811
     *
812
     * Each array element is of the following structure:
813
     *
814
     * ```php
815
     * [
816
     *     'IndexName1' => ['col1' [, ...]],
817
     *     'IndexName2' => ['col2' [, ...]],
818
     * ]
819
     * ```
820
     *
821
     * @param TableSchema $table the table metadata.
822
     *
823
     * @throws Exception|InvalidConfigException|Throwable
824
     *
825
     * @return array all unique indexes for the given table.
826
     */
827
    public function findUniqueIndexes(TableSchema $table): array
828 1
    {
829
        $result = [];
830 1
831
        /** @psalm-var array<array-key, array{index_name: string, field_name: string}> $tableUniqueConstraints */
832
        $tableUniqueConstraints = $this->findTableConstraints($table, 'UNIQUE');
833 1
834
        foreach ($tableUniqueConstraints as $row) {
835 1
            $result[$row['index_name']][] = $row['field_name'];
836 1
        }
837
838
        return $result;
839 1
    }
840
841
    /**
842
     * Loads multiple types of constraints and returns the specified ones.
843
     *
844
     * @param string $tableName table name.
845
     * @param string $returnType return type:
846
     * - primaryKey
847
     * - foreignKeys
848
     * - uniques
849
     * - checks
850
     * - defaults
851
     *
852
     * @throws Exception|InvalidConfigException|Throwable
853
     *
854
     * @return mixed constraints.
855
     */
856
    private function loadTableConstraints(string $tableName, string $returnType): mixed
857 74
    {
858
        $sql = <<<SQL
859 74
        SELECT
860
            [o].[name] AS [name],
861
            COALESCE([ccol].[name], [dcol].[name], [fccol].[name], [kiccol].[name]) AS [column_name],
862
            RTRIM([o].[type]) AS [type],
863
            OBJECT_SCHEMA_NAME([f].[referenced_object_id]) AS [foreign_table_schema],
864
            OBJECT_NAME([f].[referenced_object_id]) AS [foreign_table_name],
865
            [ffccol].[name] AS [foreign_column_name],
866
            [f].[update_referential_action_desc] AS [on_update],
867
            [f].[delete_referential_action_desc] AS [on_delete],
868
            [c].[definition] AS [check_expr],
869
            [d].[definition] AS [default_expr]
870
        FROM (SELECT OBJECT_ID(:fullName) AS [object_id]) AS [t]
871
        INNER JOIN [sys].[objects] AS [o]
872
            ON [o].[parent_object_id] = [t].[object_id] AND [o].[type] IN ('PK', 'UQ', 'C', 'D', 'F')
873
        LEFT JOIN [sys].[check_constraints] AS [c]
874
            ON [c].[object_id] = [o].[object_id]
875
        LEFT JOIN [sys].[columns] AS [ccol]
876
            ON [ccol].[object_id] = [c].[parent_object_id] AND [ccol].[column_id] = [c].[parent_column_id]
877
        LEFT JOIN [sys].[default_constraints] AS [d]
878
            ON [d].[object_id] = [o].[object_id]
879
        LEFT JOIN [sys].[columns] AS [dcol]
880
            ON [dcol].[object_id] = [d].[parent_object_id] AND [dcol].[column_id] = [d].[parent_column_id]
881
        LEFT JOIN [sys].[key_constraints] AS [k]
882
            ON [k].[object_id] = [o].[object_id]
883
        LEFT JOIN [sys].[index_columns] AS [kic]
884
            ON [kic].[object_id] = [k].[parent_object_id] AND [kic].[index_id] = [k].[unique_index_id]
885
        LEFT JOIN [sys].[columns] AS [kiccol]
886
            ON [kiccol].[object_id] = [kic].[object_id] AND [kiccol].[column_id] = [kic].[column_id]
887
        LEFT JOIN [sys].[foreign_keys] AS [f]
888
            ON [f].[object_id] = [o].[object_id]
889
        LEFT JOIN [sys].[foreign_key_columns] AS [fc]
890
            ON [fc].[constraint_object_id] = [o].[object_id]
891
        LEFT JOIN [sys].[columns] AS [fccol]
892
            ON [fccol].[object_id] = [fc].[parent_object_id] AND [fccol].[column_id] = [fc].[parent_column_id]
893
        LEFT JOIN [sys].[columns] AS [ffccol]
894
            ON [ffccol].[object_id] = [fc].[referenced_object_id] AND [ffccol].[column_id] = [fc].[referenced_column_id]
895
        ORDER BY [kic].[key_ordinal] ASC, [fc].[constraint_column_id] ASC
896
        SQL;
897
898
        $resolvedName = $this->resolveTableName($tableName);
899 74
        $constraints = $this->db->createCommand($sql, [':fullName' => $resolvedName->getFullName()])->queryAll();
900 74
901
        /** @psalm-var array[] $constraints */
902
        $constraints = $this->normalizePdoRowKeyCase($constraints, true);
903 74
        $constraints = ArrayHelper::index($constraints, null, ['type', 'name']);
904 74
905
        $result = [
906 74
            'primaryKey' => null,
907
            'foreignKeys' => [],
908
            'uniques' => [],
909
            'checks' => [],
910
            'defaults' => [],
911
        ];
912
913
        /** @psalm-var array<array-key, array> $constraints */
914
        foreach ($constraints as $type => $names) {
915 74
            /**
916
             * @psalm-var object|string|null $name
917
             * @psalm-var ConstraintArray $constraint
918
             */
919
            foreach ($names as $name => $constraint) {
920 74
                switch ($type) {
921 74
                    case 'PK':
922 74
                        /** @var Constraint */
923
                        $result['primaryKey'] = (new Constraint())
924 53
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
925 53
                            ->name($name);
926 53
                        break;
927 53
                    case 'F':
928 74
                        $result['foreignKeys'][] = (new ForeignKeyConstraint())
929 16
                            ->foreignSchemaName($constraint[0]['foreign_table_schema'])
930 16
                            ->foreignTableName($constraint[0]['foreign_table_name'])
931 16
                            ->foreignColumnNames(ArrayHelper::getColumn($constraint, 'foreign_column_name'))
932 16
                            ->onDelete(str_replace('_', '', $constraint[0]['on_delete']))
933 16
                            ->onUpdate(str_replace('_', '', $constraint[0]['on_update']))
934 16
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
935 16
                            ->name($name);
936 16
                        break;
937 16
                    case 'UQ':
938 59
                        $result['uniques'][] = (new Constraint())
939 57
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
940 57
                            ->name($name);
941 57
                        break;
942 57
                    case 'C':
943 31
                        $result['checks'][] = (new CheckConstraint())
944 13
                            ->expression($constraint[0]['check_expr'])
945 13
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
946 13
                            ->name($name);
947 13
                        break;
948 13
                    case 'D':
949 30
                        $result['defaults'][] = (new DefaultValueConstraint())
950 30
                            ->value($constraint[0]['default_expr'])
951 30
                            ->columnNames(ArrayHelper::getColumn($constraint, 'column_name'))
952 30
                            ->name($name);
953 30
                        break;
954 30
                }
955
            }
956
        }
957
958
        foreach ($result as $type => $data) {
959 74
            $this->setTableMetadata($tableName, $type, $data);
960 74
        }
961
962
        return $result[$returnType];
963 74
    }
964
965
    /**
966
     * Create a column schema builder instance giving the type and value precision.
967
     *
968
     * This method may be overridden by child classes to create a DBMS-specific column schema builder.
969
     *
970
     * @param string $type type of the column. See {@see ColumnSchemaBuilder::$type}.
971
     * @param array|int|string|null $length length or precision of the column. See {@see ColumnSchemaBuilder::$length}.
972
     *
973
     * @return ColumnSchemaBuilder column schema builder instance
974
     */
975
    public function createColumnSchemaBuilder(string $type, array|int|string $length = null): ColumnSchemaBuilder
976
    {
977
        return new ColumnSchemaBuilder($type, $length);
978 1
    }
979
980 1
    public function setTransactionIsolationLevel(string $level): void
981
    {
982 1
        $this->db->createCommand("SET TRANSACTION ISOLATION LEVEL $level")->execute();
983
    }
984
985
    /**
986 1
     * Returns the actual name of a given table name.
987
     *
988 1
     * This method will strip off curly brackets from the given table name and replace the percentage character '%' with
989 1
     * {@see ConnectionInterface::tablePrefix}.
990
     *
991 1
     * @param string $name the table name to be converted.
992
     *
993 1
     * @return string the real name of the given table name.
994 1
     */
995 1
    public function getRawTableName(string $name): string
996
    {
997 1
        if (str_contains($name, '{{')) {
998
            $name = preg_replace('/{{(.*?)}}/', '\1', $name);
999
1000
            return str_replace('%', $this->db->getTablePrefix(), $name);
1001
        }
1002 1
1003
        return $name;
1004 1
    }
1005
1006
    /**
1007
     * Returns the cache key for the specified table name.
1008
     *
1009
     * @param string $name the table name.
1010
     *
1011
     * @return array the cache key.
1012
     */
1013
    protected function getCacheKey(string $name): array
1014
    {
1015
        return [
1016
            __CLASS__,
1017
            $this->db->getDriver()->getDsn(),
1018 1
            $this->db->getDriver()->getUsername(),
1019
            $this->getRawTableName($name),
1020
        ];
1021
    }
1022
1023
    /**
1024
     * Returns the cache tag name.
1025
     *
1026
     * This allows {@see refresh()} to invalidate all cached table schemas.
1027
     *
1028
     * @return string the cache tag name.
1029
     */
1030
    protected function getCacheTag(): string
1031 3
    {
1032
        return md5(serialize([
1033 3
            __CLASS__,
1034
            $this->db->getDriver()->getDsn(),
1035
            $this->db->getDriver()->getUsername(),
1036 2
        ]));
1037
    }
1038 2
1039
    /**
1040
     * @return bool whether this DBMS supports [savepoint](http://en.wikipedia.org/wiki/Savepoint).
1041
     */
1042
    public function supportsSavepoint(): bool
1043
    {
1044
        return $this->db->isSavepointEnabled();
1045
    }
1046
1047
    /**
1048
     * Changes row's array key case to lower if PDO one is set to uppercase.
1049
     *
1050
     * @param array $row row's array or an array of row's arrays.
1051 163
     * @param bool $multiple whether multiple rows or a single row passed.
1052
     *
1053 163
     * @throws Exception
1054 23
     *
1055
     * @return array normalized row or rows.
1056 23
     */
1057
    protected function normalizePdoRowKeyCase(array $row, bool $multiple): array
1058
    {
1059 163
        if ($this->db->getSlavePdo()?->getAttribute(PDO::ATTR_CASE) !== PDO::CASE_UPPER) {
1060
            return $row;
1061
        }
1062
1063
        if ($multiple) {
1064
            return array_map(static function (array $row) {
1065
                return array_change_key_case($row, CASE_LOWER);
1066
            }, $row);
1067
        }
1068
1069 163
        return array_change_key_case($row, CASE_LOWER);
1070
    }
1071
1072
    public function getLastInsertID(string $sequenceName = ''): string
1073 163
    {
1074 163
        $pdo = $this->db->getPDO();
1075 163
1076
        if ($pdo !== null && $this->db->isActive()) {
1077
            return $pdo->lastInsertId(
1078
                $sequenceName === '' ? null : $this->db->getQuoter()->quoteTableName($sequenceName)
1079
            );
1080
        }
1081
1082
        throw new InvalidCallException('DB Connection is not active.');
1083
    }
1084
1085
    public function getViewNames(string $schema = '', bool $refresh = false): array
1086 163
    {
1087
        if ($this->viewNames === [] || $refresh) {
1088 163
            $this->viewNames[$schema] = $this->findViewNames($schema);
1089
        }
1090 163
1091 163
        return is_array($this->viewNames[$schema]) ? $this->viewNames[$schema] : [];
1092
    }
1093
}
1094