forked from yiisoft/db-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchema.php
849 lines (756 loc) · 27.4 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Oracle;
use Throwable;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constraint\CheckConstraint;
use Yiisoft\Db\Constraint\Constraint;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
use Yiisoft\Db\Constraint\IndexConstraint;
use Yiisoft\Db\Driver\Pdo\AbstractPdoSchema;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\DbArrayHelper;
use Yiisoft\Db\Oracle\Column\BinaryColumnSchema;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;
use function array_change_key_case;
use function array_map;
use function array_merge;
use function array_reverse;
use function implode;
use function is_array;
use function md5;
use function preg_match;
use function preg_replace;
use function serialize;
use function str_replace;
use function strtolower;
use function trim;
/**
* Implements the Oracle Server specific schema, supporting Oracle Server 11C and above.
*
* @psalm-type ColumnInfoArray = array{
* column_name: string,
* data_type: string,
* data_precision: string|null,
* data_scale: string|null,
* data_length: string,
* nullable: string,
* data_default: string|null,
* is_pk: string|null,
* identity_column: string,
* column_comment: string|null
* }
*
* @psalm-type ConstraintArray = array<
* array-key,
* array {
* name: string,
* column_name: string,
* type: string,
* foreign_table_schema: string|null,
* foreign_table_name: string|null,
* foreign_column_name: string|null,
* on_update: string,
* on_delete: string,
* check_expr: string
* }
* >
*/
final class Schema extends AbstractPdoSchema
{
/**
* The mapping from physical column types (keys) to abstract column types (values).
*
* @link https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/Data-Types.html
*
* @var string[]
*/
private const TYPE_MAP = [
'char' => self::TYPE_CHAR,
'nchar' => self::TYPE_CHAR,
'varchar2' => self::TYPE_STRING,
'nvarchar2' => self::TYPE_STRING,
'clob' => self::TYPE_TEXT,
'nclob' => self::TYPE_TEXT,
'blob' => self::TYPE_BINARY,
'bfile' => self::TYPE_BINARY,
'long raw' => self::TYPE_BINARY,
'raw' => self::TYPE_BINARY,
'number' => self::TYPE_DECIMAL,
'binary_float' => self::TYPE_FLOAT, // 32 bit
'binary_double' => self::TYPE_DOUBLE, // 64 bit
'float' => self::TYPE_DOUBLE, // 126 bit
'timestamp' => self::TYPE_TIMESTAMP,
'timestamp with time zone' => self::TYPE_TIMESTAMP,
'timestamp with local time zone' => self::TYPE_TIMESTAMP,
'date' => self::TYPE_DATE,
'interval day to second' => self::TYPE_TIME,
/** Deprecated */
'long' => self::TYPE_TEXT,
];
public function __construct(protected ConnectionInterface $db, SchemaCache $schemaCache, string $defaultSchema)
{
$this->defaultSchema = $defaultSchema;
parent::__construct($db, $schemaCache);
}
public function createColumn(string $type, array|int|string $length = null): ColumnInterface
{
return new Column($type, $length);
}
protected function resolveTableName(string $name): TableSchemaInterface
{
$resolvedName = new TableSchema();
$parts = array_reverse(
$this->db->getQuoter()->getTableNameParts($name)
);
$resolvedName->name($parts[0] ?? '');
$resolvedName->schemaName($parts[1] ?? $this->defaultSchema);
$resolvedName->fullName(
$resolvedName->getSchemaName() !== $this->defaultSchema ?
implode('.', array_reverse($parts)) : $resolvedName->getName()
);
return $resolvedName;
}
/**
* @link https://docs.oracle.com/cd/B28359_01/server.111/b28337/tdpsg_user_accounts.htm
*
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Throwable
*/
protected function findSchemaNames(): array
{
$sql = <<<SQL
SELECT "u"."USERNAME"
FROM "DBA_USERS" "u"
WHERE "u"."DEFAULT_TABLESPACE" NOT IN ('SYSTEM', 'SYSAUX')
ORDER BY "u"."USERNAME" ASC
SQL;
return $this->db->createCommand($sql)->queryColumn();
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findTableComment(TableSchemaInterface $tableSchema): void
{
$sql = <<<SQL
SELECT "COMMENTS"
FROM ALL_TAB_COMMENTS
WHERE
"OWNER" = :schemaName AND
"TABLE_NAME" = :tableName
SQL;
$comment = $this->db->createCommand($sql, [
':schemaName' => $tableSchema->getSchemaName(),
':tableName' => $tableSchema->getName(),
])->queryScalar();
$tableSchema->comment(is_string($comment) ? $comment : null);
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findTableNames(string $schema = ''): array
{
if ($schema === '') {
$sql = <<<SQL
SELECT TABLE_NAME
FROM USER_TABLES
UNION ALL
SELECT VIEW_NAME AS TABLE_NAME
FROM USER_VIEWS
UNION ALL
SELECT MVIEW_NAME AS TABLE_NAME
FROM USER_MVIEWS
ORDER BY TABLE_NAME
SQL;
$command = $this->db->createCommand($sql);
} else {
$sql = <<<SQL
SELECT OBJECT_NAME AS TABLE_NAME
FROM ALL_OBJECTS
WHERE OBJECT_TYPE IN ('TABLE', 'VIEW', 'MATERIALIZED VIEW') AND OWNER = :schema
ORDER BY OBJECT_NAME
SQL;
$command = $this->db->createCommand($sql, [':schema' => $schema]);
}
$rows = $command->queryAll();
$names = [];
/** @psalm-var string[][] $rows */
foreach ($rows as $row) {
/** @psalm-var string[] $row */
$row = array_change_key_case($row);
$names[] = $row['table_name'];
}
return $names;
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function loadTableSchema(string $name): TableSchemaInterface|null
{
$table = $this->resolveTableName($name);
$this->findTableComment($table);
if ($this->findColumns($table)) {
$this->findConstraints($table);
return $table;
}
return null;
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Throwable
*/
protected function loadTablePrimaryKey(string $tableName): Constraint|null
{
/** @psalm-var mixed $tablePrimaryKey */
$tablePrimaryKey = $this->loadTableConstraints($tableName, self::PRIMARY_KEY);
return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null;
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Throwable
*/
protected function loadTableForeignKeys(string $tableName): array
{
/** @psalm-var mixed $tableForeignKeys */
$tableForeignKeys = $this->loadTableConstraints($tableName, self::FOREIGN_KEYS);
return is_array($tableForeignKeys) ? $tableForeignKeys : [];
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Throwable
*/
protected function loadTableIndexes(string $tableName): array
{
$sql = <<<SQL
SELECT "ui"."INDEX_NAME" AS "name", "uicol"."COLUMN_NAME" AS "column_name",
CASE "ui"."UNIQUENESS" WHEN 'UNIQUE' THEN 1 ELSE 0 END AS "index_is_unique",
CASE WHEN "uc"."CONSTRAINT_NAME" IS NOT NULL THEN 1 ELSE 0 END AS "index_is_primary"
FROM "SYS"."USER_INDEXES" "ui"
LEFT JOIN "SYS"."USER_IND_COLUMNS" "uicol"
ON "uicol"."INDEX_NAME" = "ui"."INDEX_NAME"
LEFT JOIN "SYS"."USER_CONSTRAINTS" "uc"
ON "uc"."OWNER" = "ui"."TABLE_OWNER" AND "uc"."CONSTRAINT_NAME" = "ui"."INDEX_NAME" AND "uc"."CONSTRAINT_TYPE" = 'P'
WHERE "ui"."TABLE_OWNER" = :schemaName AND "ui"."TABLE_NAME" = :tableName
ORDER BY "uicol"."COLUMN_POSITION" ASC
SQL;
$resolvedName = $this->resolveTableName($tableName);
$indexes = $this->db->createCommand($sql, [
':schemaName' => $resolvedName->getSchemaName(),
':tableName' => $resolvedName->getName(),
])->queryAll();
/** @psalm-var array[] $indexes */
$indexes = array_map('array_change_key_case', $indexes);
$indexes = DbArrayHelper::index($indexes, null, ['name']);
$result = [];
/**
* @psalm-var object|string|null $name
* @psalm-var array[] $index
*/
foreach ($indexes as $name => $index) {
$columnNames = DbArrayHelper::getColumn($index, 'column_name');
if ($columnNames[0] === null) {
$columnNames[0] = '';
}
$result[] = (new IndexConstraint())
->primary((bool) $index[0]['index_is_primary'])
->unique((bool) $index[0]['index_is_unique'])
->name($name)
->columnNames($columnNames);
}
return $result;
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Throwable
*/
protected function loadTableUniques(string $tableName): array
{
/** @psalm-var mixed $tableUniques */
$tableUniques = $this->loadTableConstraints($tableName, self::UNIQUES);
return is_array($tableUniques) ? $tableUniques : [];
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Throwable
*/
protected function loadTableChecks(string $tableName): array
{
/** @psalm-var mixed $tableCheck */
$tableCheck = $this->loadTableConstraints($tableName, self::CHECKS);
return is_array($tableCheck) ? $tableCheck : [];
}
/**
* @throws NotSupportedException If this method is called.
*/
protected function loadTableDefaultValues(string $tableName): array
{
throw new NotSupportedException(__METHOD__ . ' is not supported by Oracle.');
}
/**
* Collects the table column metadata.
*
* @param TableSchemaInterface $table The table schema.
*
* @throws Exception
* @throws Throwable
*
* @return bool Whether the table exists.
*/
protected function findColumns(TableSchemaInterface $table): bool
{
$sql = <<<SQL
SELECT
A.COLUMN_NAME,
A.DATA_TYPE,
A.DATA_PRECISION,
A.DATA_SCALE,
A.IDENTITY_COLUMN,
(
CASE A.CHAR_USED WHEN 'C' THEN A.CHAR_LENGTH
ELSE A.DATA_LENGTH
END
) AS DATA_LENGTH,
A.NULLABLE,
A.DATA_DEFAULT,
(
SELECT COUNT(*)
FROM ALL_CONSTRAINTS AC
INNER JOIN ALL_CONS_COLUMNS ACC ON ACC.CONSTRAINT_NAME=AC.CONSTRAINT_NAME
WHERE
AC.OWNER = A.OWNER
AND AC.TABLE_NAME = B.OBJECT_NAME
AND ACC.COLUMN_NAME = A.COLUMN_NAME
AND AC.CONSTRAINT_TYPE = 'P'
) AS IS_PK,
COM.COMMENTS AS COLUMN_COMMENT
FROM ALL_TAB_COLUMNS A
INNER JOIN ALL_OBJECTS B ON B.OWNER = A.OWNER AND LTRIM(B.OBJECT_NAME) = LTRIM(A.TABLE_NAME)
LEFT JOIN ALL_COL_COMMENTS COM ON (A.OWNER = COM.OWNER AND A.TABLE_NAME = COM.TABLE_NAME AND A.COLUMN_NAME = COM.COLUMN_NAME)
WHERE
A.OWNER = :schemaName
AND B.OBJECT_TYPE IN ('TABLE', 'VIEW', 'MATERIALIZED VIEW')
AND B.OBJECT_NAME = :tableName
ORDER BY A.COLUMN_ID
SQL;
$columns = $this->db->createCommand($sql, [
':tableName' => $table->getName(),
':schemaName' => $table->getSchemaName(),
])->queryAll();
if ($columns === []) {
return false;
}
/** @psalm-var string[][] $info */
foreach ($columns as $info) {
/** @psalm-var ColumnInfoArray $info */
$info = array_change_key_case($info);
$column = $this->loadColumnSchema($info);
$table->column($info['column_name'], $column);
}
return true;
}
/**
* Sequence name of table.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return string|null Whether the sequence exists.
*
* @internal TableSchemaInterface `$table->getName()` The table schema.
*/
protected function getTableSequenceName(string $tableName): string|null
{
$sequenceNameSql = <<<SQL
SELECT
UD.REFERENCED_NAME AS SEQUENCE_NAME
FROM USER_DEPENDENCIES UD
JOIN USER_TRIGGERS UT ON (UT.TRIGGER_NAME = UD.NAME)
WHERE
UT.TABLE_NAME = :tableName
AND UD.TYPE = 'TRIGGER'
AND UD.REFERENCED_TYPE = 'SEQUENCE'
SQL;
$sequenceName = $this->db->createCommand($sequenceNameSql, [':tableName' => $tableName])->queryScalar();
/** @var string|null */
return $sequenceName === false ? null : $sequenceName;
}
/**
* Loads the column information into a {@see ColumnSchemaInterface} object.
*
* @param array $info The column information.
*
* @return ColumnSchemaInterface The column schema object.
*
* @psalm-param ColumnInfoArray $info The column information.
*/
private function loadColumnSchema(array $info): ColumnSchemaInterface
{
$dbType = $info['data_type'];
$type = $this->extractColumnType($dbType, $info);
$column = $this->createColumnSchema($type);
$column->name($info['column_name']);
$column->allowNull($info['nullable'] === 'Y');
$column->comment($info['column_comment']);
$column->primaryKey((bool) $info['is_pk']);
$column->autoIncrement($info['identity_column'] === 'YES');
$column->size((int) $info['data_length']);
$column->precision($info['data_precision'] !== null ? (int) $info['data_precision'] : null);
$column->scale($info['data_scale'] !== null ? (int) $info['data_scale'] : null);
$column->dbType($dbType);
$column->defaultValue($this->normalizeDefaultValue($info['data_default'], $column));
return $column;
}
protected function createColumnSchemaFromPhpType(string $phpType, string $type): ColumnSchemaInterface
{
if ($phpType === self::PHP_TYPE_RESOURCE) {
return new BinaryColumnSchema($type, $phpType);
}
return parent::createColumnSchemaFromPhpType($phpType, $type);
}
/**
* Converts column's default value according to {@see ColumnSchema::phpType} after retrieval from the database.
*
* @param string|null $defaultValue The default value retrieved from the database.
* @param ColumnSchemaInterface $column The column schema object.
*
* @return mixed The normalized default value.
*/
private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed
{
if ($defaultValue === null || $column->isPrimaryKey()) {
return null;
}
$defaultValue = trim($defaultValue);
if ($defaultValue === 'NULL') {
return null;
}
if ($column->getType() === self::TYPE_TIMESTAMP && $defaultValue === 'CURRENT_TIMESTAMP') {
return new Expression($defaultValue);
}
if (preg_match("/^'(.*)'$/s", $defaultValue, $matches) === 1) {
$defaultValue = str_replace("''", "'", $matches[1]);
}
return $column->phpTypecast($defaultValue);
}
/**
* Finds constraints and fills them into TableSchemaInterface object passed.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @psalm-suppress PossiblyNullArrayOffset
*/
protected function findConstraints(TableSchemaInterface $table): void
{
$sql = <<<SQL
SELECT
/*+ PUSH_PRED(C) PUSH_PRED(D) PUSH_PRED(E) */
D.CONSTRAINT_NAME,
D.CONSTRAINT_TYPE,
C.COLUMN_NAME,
C.POSITION,
D.R_CONSTRAINT_NAME,
E.TABLE_NAME AS TABLE_REF,
F.COLUMN_NAME AS COLUMN_REF,
C.TABLE_NAME
FROM ALL_CONS_COLUMNS C
INNER JOIN ALL_CONSTRAINTS D ON D.OWNER = C.OWNER AND D.CONSTRAINT_NAME = C.CONSTRAINT_NAME
LEFT JOIN ALL_CONSTRAINTS E ON E.OWNER = D.R_OWNER AND E.CONSTRAINT_NAME = D.R_CONSTRAINT_NAME
LEFT JOIN ALL_CONS_COLUMNS F ON F.OWNER = E.OWNER AND F.CONSTRAINT_NAME = E.CONSTRAINT_NAME AND F.POSITION = C.POSITION
WHERE
C.OWNER = :schemaName
AND C.TABLE_NAME = :tableName
ORDER BY D.CONSTRAINT_NAME, C.POSITION
SQL;
/**
* @psalm-var array{
* array{
* constraint_name: string,
* constraint_type: string,
* column_name: string,
* position: string|null,
* r_constraint_name: string|null,
* table_ref: string|null,
* column_ref: string|null,
* table_name: string
* }
* } $rows
*/
$rows = $this->db->createCommand(
$sql,
[':tableName' => $table->getName(), ':schemaName' => $table->getSchemaName()]
)->queryAll();
$constraints = [];
foreach ($rows as $row) {
/** @psalm-var string[] $row */
$row = array_change_key_case($row);
if ($row['constraint_type'] === 'P') {
$table->getColumns()[$row['column_name']]->primaryKey(true);
$table->primaryKey($row['column_name']);
if (empty($table->getSequenceName())) {
$table->sequenceName($this->getTableSequenceName($table->getName()));
}
}
if ($row['constraint_type'] !== 'R') {
/**
* This condition isn't checked in `WHERE` because of an Oracle Bug:
*
* @link https://github.com/yiisoft/yii2/pull/8844
*/
continue;
}
$name = $row['constraint_name'];
if (!isset($constraints[$name])) {
$constraints[$name] = [
'tableName' => $row['table_ref'],
'columns' => [],
];
}
$constraints[$name]['columns'][$row['column_name']] = $row['column_ref'];
}
foreach ($constraints as $index => $constraint) {
$table->foreignKey($index, array_merge([$constraint['tableName']], $constraint['columns']));
}
}
/**
* Returns all unique indexes for the given table.
*
* Each array element is of the following structure:.
*
* ```php
* [
* 'IndexName1' => ['col1' [, ...]],
* 'IndexName2' => ['col2' [, ...]],
* ]
* ```
*
* @param TableSchemaInterface $table The table metadata.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return array All unique indexes for the given table.
*/
public function findUniqueIndexes(TableSchemaInterface $table): array
{
$query = <<<SQL
SELECT
DIC.INDEX_NAME,
DIC.COLUMN_NAME
FROM ALL_INDEXES DI
INNER JOIN ALL_IND_COLUMNS DIC ON DI.TABLE_NAME = DIC.TABLE_NAME AND DI.INDEX_NAME = DIC.INDEX_NAME
WHERE
DI.UNIQUENESS = 'UNIQUE'
AND DIC.TABLE_OWNER = :schemaName
AND DIC.TABLE_NAME = :tableName
ORDER BY DIC.TABLE_NAME, DIC.INDEX_NAME, DIC.COLUMN_POSITION
SQL;
$result = [];
$rows = $this->db->createCommand(
$query,
[':tableName' => $table->getName(), ':schemaName' => $table->getschemaName()]
)->queryAll();
/** @psalm-var array<array{INDEX_NAME: string, COLUMN_NAME: string}> $rows */
foreach ($rows as $row) {
$result[$row['INDEX_NAME']][] = $row['COLUMN_NAME'];
}
return $result;
}
/**
* Extracts the data type for the given column.
*
* @param string $dbType The database data type
* @param array $info Column information.
* @psalm-param ColumnInfoArray $info
*
* @return string The abstract column type.
*/
private function extractColumnType(string $dbType, array $info): string
{
$dbType = strtolower($dbType);
if ($dbType === 'number') {
$scale = $info['data_scale'] !== null ? (int) $info['data_scale'] : null;
return match ($scale) {
null => self::TYPE_DOUBLE,
0 => self::TYPE_INTEGER,
default => self::TYPE_DECIMAL,
};
}
$dbType = preg_replace('/\([^)]+\)/', '', $dbType);
if ($dbType === 'interval day to second' && $info['data_precision'] > 0) {
return self::TYPE_STRING;
}
return self::TYPE_MAP[$dbType] ?? self::TYPE_STRING;
}
/**
* Loads multiple types of constraints and returns the specified ones.
*
* @param string $tableName The table name.
* @param string $returnType The return type:
* - primaryKey
* - foreignKeys
* - uniques
* - checks
*
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Throwable
*
* @return mixed Constraints.
*/
private function loadTableConstraints(string $tableName, string $returnType): mixed
{
$sql = <<<SQL
SELECT
"uc"."CONSTRAINT_NAME" AS "name",
"uccol"."COLUMN_NAME" AS "column_name",
"uc"."CONSTRAINT_TYPE" AS "type",
"fuc"."OWNER" AS "foreign_table_schema",
"fuc"."TABLE_NAME" AS "foreign_table_name",
"fuccol"."COLUMN_NAME" AS "foreign_column_name",
"uc"."DELETE_RULE" AS "on_delete",
"uc"."SEARCH_CONDITION" AS "check_expr"
FROM "USER_CONSTRAINTS" "uc"
INNER JOIN "USER_CONS_COLUMNS" "uccol"
ON "uccol"."OWNER" = "uc"."OWNER" AND "uccol"."CONSTRAINT_NAME" = "uc"."CONSTRAINT_NAME"
LEFT JOIN "USER_CONSTRAINTS" "fuc"
ON "fuc"."OWNER" = "uc"."R_OWNER" AND "fuc"."CONSTRAINT_NAME" = "uc"."R_CONSTRAINT_NAME"
LEFT JOIN "USER_CONS_COLUMNS" "fuccol"
ON "fuccol"."OWNER" = "fuc"."OWNER" AND "fuccol"."CONSTRAINT_NAME" = "fuc"."CONSTRAINT_NAME" AND "fuccol"."POSITION" = "uccol"."POSITION"
WHERE "uc"."OWNER" = :schemaName AND "uc"."TABLE_NAME" = :tableName
ORDER BY "uccol"."POSITION" ASC
SQL;
$resolvedName = $this->resolveTableName($tableName);
$constraints = $this->db->createCommand($sql, [
':schemaName' => $resolvedName->getSchemaName(),
':tableName' => $resolvedName->getName(),
])->queryAll();
/** @psalm-var array[] $constraints */
$constraints = array_map('array_change_key_case', $constraints);
$constraints = DbArrayHelper::index($constraints, null, ['type', 'name']);
$result = [
self::PRIMARY_KEY => null,
self::FOREIGN_KEYS => [],
self::UNIQUES => [],
self::CHECKS => [],
];
/**
* @psalm-var string $type
* @psalm-var array $names
*/
foreach ($constraints as $type => $names) {
/**
* @psalm-var object|string|null $name
* @psalm-var ConstraintArray $constraint
*/
foreach ($names as $name => $constraint) {
switch ($type) {
case 'P':
$result[self::PRIMARY_KEY] = (new Constraint())
->name($name)
->columnNames(DbArrayHelper::getColumn($constraint, 'column_name'));
break;
case 'R':
$result[self::FOREIGN_KEYS][] = (new ForeignKeyConstraint())
->name($name)
->columnNames(DbArrayHelper::getColumn($constraint, 'column_name'))
->foreignSchemaName($constraint[0]['foreign_table_schema'])
->foreignTableName($constraint[0]['foreign_table_name'])
->foreignColumnNames(DbArrayHelper::getColumn($constraint, 'foreign_column_name'))
->onDelete($constraint[0]['on_delete'])
->onUpdate(null);
break;
case 'U':
$result[self::UNIQUES][] = (new Constraint())
->name($name)
->columnNames(DbArrayHelper::getColumn($constraint, 'column_name'));
break;
case 'C':
$result[self::CHECKS][] = (new CheckConstraint())
->name($name)
->columnNames(DbArrayHelper::getColumn($constraint, 'column_name'))
->expression($constraint[0]['check_expr']);
break;
}
}
}
foreach ($result as $type => $data) {
$this->setTableMetadata($tableName, $type, $data);
}
return $result[$returnType];
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findViewNames(string $schema = ''): array
{
$sql = match ($schema) {
'' => <<<SQL
SELECT VIEW_NAME FROM USER_VIEWS
SQL,
default => <<<SQL
SELECT VIEW_NAME FROM ALL_VIEWS WHERE OWNER = '$schema'
SQL,
};
/** @psalm-var string[][] $views */
$views = $this->db->createCommand($sql)->queryAll();
foreach ($views as $key => $view) {
$views[$key] = $view['VIEW_NAME'];
}
return $views;
}
/**
* Returns the cache key for the specified table name.
*
* @param string $name The table name.
*
* @return array The cache key.
*
* @psalm-suppress DeprecatedMethod
*/
protected function getCacheKey(string $name): array
{
return array_merge([self::class], $this->generateCacheKey(), [$this->db->getQuoter()->getRawTableName($name)]);
}
/**
* Returns the cache tag name.
*
* This allows {@see refresh()} to invalidate all cached table schemas.
*
* @return string The cache tag name.
*/
protected function getCacheTag(): string
{
return md5(serialize(array_merge([self::class], $this->generateCacheKey())));
}
}