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