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