1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Schema; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
use Throwable; |
9
|
|
|
use Yiisoft\Cache\Dependency\TagDependency; |
10
|
|
|
use Yiisoft\Db\Cache\SchemaCache; |
11
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
12
|
|
|
use Yiisoft\Db\Constraint\Constraint; |
13
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
14
|
|
|
|
15
|
|
|
use function gettype; |
16
|
|
|
use function is_array; |
17
|
|
|
use function preg_match; |
18
|
|
|
|
19
|
|
|
abstract class Schema implements SchemaInterface |
20
|
|
|
{ |
21
|
|
|
public const SCHEMA = 'schema'; |
22
|
|
|
public const PRIMARY_KEY = 'primaryKey'; |
23
|
|
|
public const INDEXES = 'indexes'; |
24
|
|
|
public const CHECKS = 'checks'; |
25
|
|
|
public const FOREIGN_KEYS = 'foreignKeys'; |
26
|
|
|
public const DEFAULT_VALUES = 'defaultValues'; |
27
|
|
|
public const UNIQUES = 'uniques'; |
28
|
|
|
|
29
|
|
|
public const TYPE_PK = 'pk'; |
30
|
|
|
public const TYPE_UPK = 'upk'; |
31
|
|
|
public const TYPE_BIGPK = 'bigpk'; |
32
|
|
|
public const TYPE_UBIGPK = 'ubigpk'; |
33
|
|
|
public const TYPE_CHAR = 'char'; |
34
|
|
|
public const TYPE_STRING = 'string'; |
35
|
|
|
public const TYPE_TEXT = 'text'; |
36
|
|
|
public const TYPE_TINYINT = 'tinyint'; |
37
|
|
|
public const TYPE_SMALLINT = 'smallint'; |
38
|
|
|
public const TYPE_INTEGER = 'integer'; |
39
|
|
|
public const TYPE_BIGINT = 'bigint'; |
40
|
|
|
public const TYPE_FLOAT = 'float'; |
41
|
|
|
public const TYPE_DOUBLE = 'double'; |
42
|
|
|
public const TYPE_DECIMAL = 'decimal'; |
43
|
|
|
public const TYPE_DATETIME = 'datetime'; |
44
|
|
|
public const TYPE_TIMESTAMP = 'timestamp'; |
45
|
|
|
public const TYPE_TIME = 'time'; |
46
|
|
|
public const TYPE_DATE = 'date'; |
47
|
|
|
public const TYPE_BINARY = 'binary'; |
48
|
|
|
public const TYPE_BOOLEAN = 'boolean'; |
49
|
|
|
public const TYPE_MONEY = 'money'; |
50
|
|
|
public const TYPE_JSON = 'json'; |
51
|
|
|
|
52
|
|
|
public const PHP_TYPE_INTEGER = 'integer'; |
53
|
|
|
public const PHP_TYPE_STRING = 'string'; |
54
|
|
|
public const PHP_TYPE_BOOLEAN = 'boolean'; |
55
|
|
|
public const PHP_TYPE_DOUBLE = 'double'; |
56
|
|
|
public const PHP_TYPE_RESOURCE = 'resource'; |
57
|
|
|
public const PHP_TYPE_ARRAY = 'array'; |
58
|
|
|
public const PHP_TYPE_NULL = 'NULL'; |
59
|
|
|
|
60
|
|
|
public const CACHE_VERSION = 'cacheVersion'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Schema cache version, to detect incompatibilities in cached values when the data format of the cache changes. |
64
|
|
|
*/ |
65
|
|
|
protected const SCHEMA_CACHE_VERSION = 1; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string|null the default schema name used for the current session. |
69
|
|
|
*/ |
70
|
|
|
protected ?string $defaultSchema = null; |
71
|
|
|
private array $schemaNames = []; |
72
|
|
|
private array $tableNames = []; |
73
|
|
|
protected array $viewNames = []; |
74
|
|
|
private array $tableMetadata = []; |
75
|
|
|
|
76
|
|
|
public function __construct(protected ConnectionInterface $db, private SchemaCache $schemaCache) |
77
|
|
|
{ |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the cache key for the specified table name. |
82
|
|
|
* |
83
|
|
|
* @param string $name the table name. |
84
|
|
|
* |
85
|
|
|
* @return array the cache key. |
86
|
|
|
*/ |
87
|
|
|
abstract protected function getCacheKey(string $name): array; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the cache tag name. |
91
|
|
|
* |
92
|
|
|
* This allows {@see refresh()} to invalidate all cached table schemas. |
93
|
|
|
* |
94
|
|
|
* @return string the cache tag name. |
95
|
|
|
*/ |
96
|
|
|
abstract protected function getCacheTag(): string; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Loads all check constraints for the given table. |
100
|
|
|
* |
101
|
|
|
* @param string $tableName table name. |
102
|
|
|
* |
103
|
|
|
* @return array check constraints for the given table. |
104
|
|
|
*/ |
105
|
|
|
abstract protected function loadTableChecks(string $tableName): array; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Loads all default value constraints for the given table. |
109
|
|
|
* |
110
|
2532 |
|
* @param string $tableName table name. |
111
|
|
|
* |
112
|
2532 |
|
* @return array default value constraints for the given table. |
113
|
2532 |
|
*/ |
114
|
2532 |
|
abstract protected function loadTableDefaultValues(string $tableName): array; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Loads all foreign keys for the given table. |
118
|
|
|
* |
119
|
|
|
* @param string $tableName table name. |
120
|
|
|
* |
121
|
963 |
|
* @return array foreign keys for the given table. |
122
|
|
|
*/ |
123
|
963 |
|
abstract protected function loadTableForeignKeys(string $tableName): array; |
124
|
963 |
|
|
125
|
|
|
/** |
126
|
|
|
* Loads all indexes for the given table. |
127
|
963 |
|
* |
128
|
|
|
* @param string $tableName table name. |
129
|
|
|
* |
130
|
1665 |
|
* @return array indexes for the given table. |
131
|
|
|
*/ |
132
|
1665 |
|
abstract protected function loadTableIndexes(string $tableName): array; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Loads a primary key for the given table. |
136
|
|
|
* |
137
|
|
|
* @param string $tableName table name. |
138
|
|
|
* |
139
|
|
|
* @return Constraint|null primary key for the given table, `null` if the table has no primary key. |
140
|
18 |
|
*/ |
141
|
|
|
abstract protected function loadTablePrimaryKey(string $tableName): ?Constraint; |
142
|
18 |
|
|
143
|
|
|
/** |
144
|
|
|
* Loads all unique constraints for the given table. |
145
|
|
|
* |
146
|
|
|
* @param string $tableName table name. |
147
|
|
|
* |
148
|
1322 |
|
* @return array unique constraints for the given table. |
149
|
|
|
*/ |
150
|
1322 |
|
abstract protected function loadTableUniques(string $tableName): array; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Loads the metadata for the specified table. |
154
|
|
|
* |
155
|
|
|
* @param string $name table name. |
156
|
13 |
|
* |
157
|
|
|
* @return TableSchemaInterface|null DBMS-dependent table metadata, `null` if the table does not exist. |
158
|
13 |
|
*/ |
159
|
|
|
abstract protected function loadTableSchema(string $name): ?TableSchemaInterface; |
160
|
|
|
|
161
|
|
|
public function getDefaultSchema(): ?string |
162
|
|
|
{ |
163
|
|
|
return $this->defaultSchema; |
164
|
4 |
|
} |
165
|
|
|
|
166
|
4 |
|
public function getPdoType(mixed $data): int |
167
|
4 |
|
{ |
168
|
|
|
/** @psalm-var array<string, int> */ |
169
|
|
|
$typeMap = [ |
170
|
4 |
|
// php type => PDO type |
171
|
|
|
self::PHP_TYPE_BOOLEAN => PDO::PARAM_BOOL, |
172
|
|
|
self::PHP_TYPE_INTEGER => PDO::PARAM_INT, |
173
|
|
|
self::PHP_TYPE_STRING => PDO::PARAM_STR, |
174
|
|
|
self::PHP_TYPE_RESOURCE => PDO::PARAM_LOB, |
175
|
|
|
self::PHP_TYPE_NULL => PDO::PARAM_NULL, |
176
|
23 |
|
]; |
177
|
|
|
|
178
|
23 |
|
$type = gettype($data); |
179
|
23 |
|
|
180
|
|
|
return $typeMap[$type] ?? PDO::PARAM_STR; |
181
|
|
|
} |
182
|
23 |
|
|
183
|
|
|
public function getRawTableName(string $name): string |
184
|
|
|
{ |
185
|
|
|
if (str_contains($name, '{{')) { |
186
|
|
|
$name = preg_replace('/{{(.*?)}}/', '\1', $name); |
187
|
|
|
|
188
|
1360 |
|
return str_replace('%', $this->db->getTablePrefix(), $name); |
189
|
|
|
} |
190
|
1360 |
|
|
191
|
|
|
return $name; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function getSchemaCache(): SchemaCache |
195
|
|
|
{ |
196
|
|
|
return $this->schemaCache; |
197
|
|
|
} |
198
|
|
|
|
199
|
1360 |
|
/** |
200
|
|
|
* @throws NotSupportedException |
201
|
1360 |
|
*/ |
202
|
|
|
public function getSchemaChecks(string $schema = '', bool $refresh = false): array |
203
|
|
|
{ |
204
|
|
|
return $this->getSchemaMetadata($schema, self::CHECKS, $refresh); |
205
|
|
|
} |
206
|
|
|
|
207
|
96 |
|
/** |
208
|
|
|
* @throws NotSupportedException |
209
|
96 |
|
*/ |
210
|
96 |
|
public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): array |
211
|
|
|
{ |
212
|
|
|
return $this->getSchemaMetadata($schema, self::DEFAULT_VALUES, $refresh); |
213
|
96 |
|
} |
214
|
96 |
|
|
215
|
96 |
|
/** |
216
|
|
|
* @throws NotSupportedException |
217
|
|
|
*/ |
218
|
|
|
public function getSchemaForeignKeys(string $schema = '', bool $refresh = false): array |
219
|
|
|
{ |
220
|
103 |
|
return $this->getSchemaMetadata($schema, self::FOREIGN_KEYS, $refresh); |
221
|
|
|
} |
222
|
103 |
|
|
223
|
|
|
/** |
224
|
103 |
|
* @throws NotSupportedException |
225
|
|
|
*/ |
226
|
103 |
|
public function getSchemaIndexes(string $schema = '', bool $refresh = false): array |
227
|
|
|
{ |
228
|
103 |
|
return $this->getSchemaMetadata($schema, self::INDEXES, $refresh); |
229
|
103 |
|
} |
230
|
|
|
|
231
|
103 |
|
public function getSchemaNames(bool $refresh = false): array |
232
|
|
|
{ |
233
|
|
|
if (empty($this->schemaNames) || $refresh) { |
234
|
|
|
$this->schemaNames = $this->findSchemaNames(); |
235
|
|
|
} |
236
|
36 |
|
|
237
|
|
|
return $this->schemaNames; |
238
|
36 |
|
} |
239
|
36 |
|
|
240
|
36 |
|
/** |
241
|
|
|
* @throws NotSupportedException |
242
|
|
|
*/ |
243
|
|
|
public function getSchemaPrimaryKeys(string $schema = '', bool $refresh = false): array |
244
|
|
|
{ |
245
|
|
|
return $this->getSchemaMetadata($schema, self::PRIMARY_KEY, $refresh); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @throws NotSupportedException |
250
|
10 |
|
*/ |
251
|
|
|
public function getSchemaUniques(string $schema = '', bool $refresh = false): array |
252
|
10 |
|
{ |
253
|
|
|
return $this->getSchemaMetadata($schema, self::UNIQUES, $refresh); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function getTableChecks(string $name, bool $refresh = false): array |
257
|
|
|
{ |
258
|
4 |
|
/** @var mixed */ |
259
|
|
|
$tableChecks = $this->getTableMetadata($name, self::CHECKS, $refresh); |
260
|
4 |
|
return is_array($tableChecks) ? $tableChecks : []; |
261
|
4 |
|
} |
262
|
|
|
|
263
|
|
|
public function getTableDefaultValues(string $name, bool $refresh = false): array |
264
|
|
|
{ |
265
|
|
|
/** @var mixed */ |
266
|
|
|
$tableDefaultValues = $this->getTableMetadata($name, self::DEFAULT_VALUES, $refresh); |
267
|
|
|
return is_array($tableDefaultValues) ? $tableDefaultValues : []; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function getTableForeignKeys(string $name, bool $refresh = false): array |
271
|
|
|
{ |
272
|
|
|
/** @var mixed */ |
273
|
|
|
$tableForeignKeys = $this->getTableMetadata($name, self::FOREIGN_KEYS, $refresh); |
274
|
4 |
|
return is_array($tableForeignKeys) ? $tableForeignKeys : []; |
275
|
|
|
} |
276
|
4 |
|
|
277
|
4 |
|
public function getTableIndexes(string $name, bool $refresh = false): array |
278
|
|
|
{ |
279
|
|
|
/** @var mixed */ |
280
|
|
|
$tableIndexes = $this->getTableMetadata($name, self::INDEXES, $refresh); |
281
|
|
|
return is_array($tableIndexes) ? $tableIndexes : []; |
282
|
8 |
|
} |
283
|
|
|
|
284
|
8 |
|
public function getTableNames(string $schema = '', bool $refresh = false): array |
285
|
8 |
|
{ |
286
|
|
|
if (!isset($this->tableNames[$schema]) || $refresh) { |
287
|
|
|
/** @psalm-var string[] */ |
288
|
|
|
$this->tableNames[$schema] = $this->findTableNames($schema); |
289
|
|
|
} |
290
|
28 |
|
|
291
|
|
|
return is_array($this->tableNames[$schema]) ? $this->tableNames[$schema] : []; |
292
|
28 |
|
} |
293
|
|
|
|
294
|
28 |
|
public function getTablePrimaryKey(string $name, bool $refresh = false): ?Constraint |
295
|
|
|
{ |
296
|
|
|
/** @var mixed */ |
297
|
|
|
$tablePrimaryKey = $this->getTableMetadata($name, self::PRIMARY_KEY, $refresh); |
298
|
28 |
|
return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null; |
299
|
28 |
|
} |
300
|
|
|
|
301
|
28 |
|
public function getTableSchema(string $name, bool $refresh = false): ?TableSchemaInterface |
302
|
26 |
|
{ |
303
|
24 |
|
/** @var mixed */ |
304
|
24 |
|
$tableSchema = $this->getTableMetadata($name, self::SCHEMA, $refresh); |
305
|
|
|
return $tableSchema instanceof TableSchemaInterface ? $tableSchema : null; |
306
|
|
|
} |
307
|
4 |
|
|
308
|
|
|
public function getTableSchemas(string $schema = '', bool $refresh = false): array |
309
|
|
|
{ |
310
|
28 |
|
/** @var mixed */ |
311
|
|
|
$tableSchemas = $this->getSchemaMetadata($schema, self::SCHEMA, $refresh); |
312
|
|
|
return is_array($tableSchemas) ? $tableSchemas : []; |
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function getTableUniques(string $name, bool $refresh = false): array |
316
|
1075 |
|
{ |
317
|
|
|
/** @var mixed */ |
318
|
1075 |
|
$tableUniques = $this->getTableMetadata($name, self::UNIQUES, $refresh); |
319
|
6 |
|
return is_array($tableUniques) ? $tableUniques : []; |
320
|
|
|
} |
321
|
|
|
|
322
|
1075 |
|
/** |
323
|
1075 |
|
* Returns a value indicating whether a SQL statement is for read purpose. |
324
|
|
|
* |
325
|
|
|
* @param string $sql the SQL statement. |
326
|
|
|
* |
327
|
|
|
* @return bool whether a SQL statement is for read purpose. |
328
|
|
|
*/ |
329
|
|
|
public function isReadQuery(string $sql): bool |
330
|
|
|
{ |
331
|
|
|
$pattern = '/^\s*(SELECT|SHOW|DESCRIBE)\b/i'; |
332
|
|
|
|
333
|
1574 |
|
return preg_match($pattern, $sql) > 0; |
334
|
|
|
} |
335
|
1574 |
|
|
336
|
4 |
|
/** |
337
|
|
|
* Refreshes the schema. |
338
|
|
|
* |
339
|
1574 |
|
* This method cleans up all cached table schemas so that they can be re-created later to reflect the database |
340
|
146 |
|
* schema change. |
341
|
|
|
*/ |
342
|
|
|
public function refresh(): void |
343
|
1550 |
|
{ |
344
|
1467 |
|
if ($this->schemaCache->isEnabled()) { |
345
|
|
|
$this->schemaCache->invalidate($this->getCacheTag()); |
346
|
|
|
} |
347
|
287 |
|
|
348
|
|
|
$this->tableNames = []; |
349
|
287 |
|
$this->tableMetadata = []; |
350
|
287 |
|
} |
351
|
|
|
|
352
|
|
|
public function refreshTableSchema(string $name): void |
353
|
287 |
|
{ |
354
|
|
|
$rawName = $this->getRawTableName($name); |
355
|
|
|
|
356
|
|
|
unset($this->tableMetadata[$rawName]); |
357
|
|
|
|
358
|
|
|
$this->tableNames = []; |
359
|
1698 |
|
|
360
|
|
|
if ($this->schemaCache->isEnabled()) { |
361
|
1698 |
|
$this->schemaCache->remove($this->getCacheKey($rawName)); |
362
|
149 |
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
1683 |
|
/** |
366
|
232 |
|
* Returns all schema names in the database, including the default one but not system schemas. |
367
|
232 |
|
* |
368
|
|
|
* This method should be overridden by child classes in order to support this feature because the default |
369
|
1673 |
|
* implementation simply throws an exception. |
370
|
|
|
* |
371
|
|
|
* @throws NotSupportedException if this method is not supported by the DBMS. |
372
|
1683 |
|
* |
373
|
4 |
|
* @return array all schema names in the database, except system schemas. |
374
|
|
|
*/ |
375
|
|
|
protected function findSchemaNames(): array |
376
|
1683 |
|
{ |
377
|
|
|
throw new NotSupportedException(static::class . ' does not support fetching all schema names.'); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Returns all table names in the database. |
382
|
1344 |
|
* |
383
|
|
|
* This method should be overridden by child classes in order to support this feature because the default |
384
|
1344 |
|
* implementation simply throws an exception. |
385
|
987 |
|
* |
386
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
387
|
357 |
|
* |
388
|
|
|
* @throws NotSupportedException if this method is not supported by the DBMS. |
389
|
|
|
* |
390
|
1344 |
|
* @return array all table names in the database. The names have NO schema name prefix. |
391
|
|
|
*/ |
392
|
|
|
protected function findTableNames(string $schema): array |
|
|
|
|
393
|
|
|
{ |
394
|
|
|
throw new NotSupportedException(static::class . ' does not support fetching all table names.'); |
395
|
|
|
} |
396
|
1683 |
|
|
397
|
|
|
/** |
398
|
1683 |
|
* Extracts the PHP type from abstract DB type. |
399
|
1357 |
|
* |
400
|
|
|
* @param ColumnSchemaInterface $column the column schema information. |
401
|
326 |
|
* |
402
|
|
|
* @return string PHP type name. |
403
|
|
|
*/ |
404
|
1683 |
|
protected function getColumnPhpType(ColumnSchemaInterface $column): string |
405
|
1683 |
|
{ |
406
|
|
|
/** @psalm-var string[] */ |
407
|
|
|
$typeMap = [ |
408
|
|
|
// abstract type => php type |
409
|
|
|
self::TYPE_TINYINT => self::PHP_TYPE_INTEGER, |
410
|
|
|
self::TYPE_SMALLINT => self::PHP_TYPE_INTEGER, |
411
|
5 |
|
self::TYPE_INTEGER => self::PHP_TYPE_INTEGER, |
412
|
|
|
self::TYPE_BIGINT => self::PHP_TYPE_INTEGER, |
413
|
5 |
|
self::TYPE_BOOLEAN => self::PHP_TYPE_BOOLEAN, |
414
|
5 |
|
self::TYPE_FLOAT => self::PHP_TYPE_DOUBLE, |
415
|
|
|
self::TYPE_DOUBLE => self::PHP_TYPE_DOUBLE, |
416
|
|
|
self::TYPE_BINARY => self::PHP_TYPE_RESOURCE, |
417
|
|
|
self::TYPE_JSON => self::PHP_TYPE_ARRAY, |
418
|
|
|
]; |
419
|
5 |
|
|
420
|
|
|
if (isset($typeMap[$column->getType()])) { |
421
|
|
|
if ($column->getType() === self::TYPE_BIGINT) { |
422
|
|
|
return PHP_INT_SIZE === 8 && !$column->isUnsigned() ? self::PHP_TYPE_INTEGER : self::PHP_TYPE_STRING; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
if ($column->getType() === self::TYPE_INTEGER) { |
426
|
|
|
return PHP_INT_SIZE === 4 && $column->isUnsigned() ? self::PHP_TYPE_STRING : self::PHP_TYPE_INTEGER; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
return $typeMap[$column->getType()]; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
return self::PHP_TYPE_STRING; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Returns the metadata of the given type for all tables in the given schema. |
437
|
|
|
* |
438
|
|
|
* @param string $schema the schema of the metadata. Defaults to empty string, meaning the current or default schema |
439
|
1622 |
|
* name. |
440
|
|
|
* @param string $type metadata type. |
441
|
1622 |
|
* @param bool $refresh whether to fetch the latest available table metadata. If this is `false`, cached data may be |
442
|
130 |
|
* returned if available. |
443
|
|
|
* |
444
|
130 |
|
* @throws NotSupportedException |
445
|
|
|
* |
446
|
|
|
* @return array of metadata. |
447
|
1622 |
|
* |
448
|
|
|
* @psalm-return list<Constraint|TableSchemaInterface|array> |
449
|
|
|
*/ |
450
|
|
|
protected function getSchemaMetadata(string $schema, string $type, bool $refresh): array |
451
|
|
|
{ |
452
|
|
|
$metadata = []; |
453
|
50 |
|
/** @psalm-var string[] */ |
454
|
|
|
$tableNames = $this->getTableNames($schema, $refresh); |
455
|
50 |
|
|
456
|
|
|
foreach ($tableNames as $name) { |
457
|
|
|
if ($schema !== '') { |
458
|
|
|
$name = $schema . '.' . $name; |
459
|
50 |
|
} |
460
|
|
|
|
461
|
50 |
|
$tableMetadata = $this->getTableTypeMetadata($type, $name, $refresh); |
462
|
50 |
|
|
463
|
13 |
|
if ($tableMetadata !== null) { |
464
|
|
|
$metadata[] = $tableMetadata; |
465
|
|
|
} |
466
|
|
|
} |
467
|
50 |
|
|
468
|
50 |
|
return $metadata; |
469
|
|
|
} |
470
|
50 |
|
|
471
|
|
|
/** |
472
|
|
|
* Returns the metadata of the given type for the given table. |
473
|
|
|
* |
474
|
|
|
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
475
|
|
|
* @param string $type metadata type. |
476
|
12 |
|
* @param bool $refresh whether to reload the table metadata even if it is found in the cache. |
477
|
|
|
* |
478
|
12 |
|
* @return mixed metadata. |
479
|
|
|
* |
480
|
12 |
|
* @psalm-suppress MixedArrayAccess |
481
|
|
|
* @psalm-suppress MixedArrayAssignment |
482
|
|
|
*/ |
483
|
|
|
protected function getTableMetadata(string $name, string $type, bool $refresh = false): mixed |
484
|
|
|
{ |
485
|
|
|
$rawName = $this->getRawTableName($name); |
486
|
375 |
|
|
487
|
|
|
if (!isset($this->tableMetadata[$rawName])) { |
488
|
375 |
|
$this->loadTableMetadataFromCache($rawName); |
489
|
375 |
|
} |
490
|
|
|
|
491
|
|
|
if ($refresh || !isset($this->tableMetadata[$rawName][$type])) { |
492
|
375 |
|
$this->tableMetadata[$rawName][$type] = $this->loadTableTypeMetadata($type, $rawName); |
493
|
|
|
$this->saveTableMetadataToCache($rawName); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
return $this->tableMetadata[$rawName][$type]; |
497
|
|
|
} |
498
|
153 |
|
|
499
|
|
|
/** |
500
|
153 |
|
* This method returns the desired metadata type for the table name. |
501
|
|
|
* |
502
|
|
|
* @param string $type |
503
|
|
|
* @param string $name |
504
|
|
|
* |
505
|
|
|
* @return array|Constraint|TableSchemaInterface|null |
506
|
|
|
*/ |
507
|
|
|
protected function loadTableTypeMetadata(string $type, string $name): Constraint|array|TableSchemaInterface|null |
508
|
|
|
{ |
509
|
|
|
return match ($type) { |
510
|
|
|
self::SCHEMA => $this->loadTableSchema($name), |
511
|
|
|
self::PRIMARY_KEY => $this->loadTablePrimaryKey($name), |
512
|
|
|
self::UNIQUES => $this->loadTableUniques($name), |
513
|
|
|
self::FOREIGN_KEYS => $this->loadTableForeignKeys($name), |
514
|
20 |
|
self::INDEXES => $this->loadTableIndexes($name), |
515
|
|
|
self::DEFAULT_VALUES => $this->loadTableDefaultValues($name), |
516
|
20 |
|
self::CHECKS => $this->loadTableChecks($name), |
517
|
|
|
default => null, |
518
|
|
|
}; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* This method returns the desired metadata type for table name (with refresh if needed) |
523
|
|
|
* |
524
|
|
|
* @param string $type |
525
|
|
|
* @param string $name |
526
|
|
|
* @param bool $refresh |
527
|
|
|
* |
528
|
|
|
* @return array|Constraint|TableSchemaInterface|null |
529
|
|
|
*/ |
530
|
141 |
|
protected function getTableTypeMetadata( |
531
|
|
|
string $type, |
532
|
141 |
|
string $name, |
533
|
|
|
bool $refresh = false |
534
|
|
|
): Constraint|array|null|TableSchemaInterface { |
535
|
|
|
return match ($type) { |
536
|
|
|
self::SCHEMA => $this->getTableSchema($name, $refresh), |
537
|
|
|
self::PRIMARY_KEY => $this->getTablePrimaryKey($name, $refresh), |
538
|
|
|
self::UNIQUES => $this->getTableUniques($name, $refresh), |
539
|
|
|
self::FOREIGN_KEYS => $this->getTableForeignKeys($name, $refresh), |
540
|
|
|
self::INDEXES => $this->getTableIndexes($name, $refresh), |
541
|
|
|
self::DEFAULT_VALUES => $this->getTableDefaultValues($name, $refresh), |
542
|
|
|
self::CHECKS => $this->getTableChecks($name, $refresh), |
543
|
|
|
default => null, |
544
|
|
|
}; |
545
|
|
|
} |
546
|
156 |
|
|
547
|
|
|
/** |
548
|
156 |
|
* Resolves the table name and schema name (if any). |
549
|
|
|
* |
550
|
|
|
* @param string $name the table name. |
551
|
|
|
* |
552
|
|
|
* @throws NotSupportedException if this method is not supported by the DBMS. |
553
|
|
|
* |
554
|
|
|
* @return TableSchemaInterface with resolved table, schema, etc. names. |
555
|
|
|
* |
556
|
|
|
* {@see \Yiisoft\Db\Schema\TableSchemaInterface} |
557
|
|
|
*/ |
558
|
|
|
protected function resolveTableName(string $name): TableSchemaInterface |
|
|
|
|
559
|
|
|
{ |
560
|
|
|
throw new NotSupportedException(static::class . ' does not support resolving table names.'); |
561
|
|
|
} |
562
|
63 |
|
|
563
|
|
|
/** |
564
|
63 |
|
* Sets the metadata of the given type for the given table. |
565
|
|
|
* |
566
|
|
|
* @param string $name table name. |
567
|
|
|
* @param string $type metadata type. |
568
|
|
|
* @param mixed $data metadata. |
569
|
|
|
* |
570
|
|
|
* @psalm-suppress MixedArrayAssignment |
571
|
|
|
*/ |
572
|
|
|
protected function setTableMetadata(string $name, string $type, mixed $data): void |
573
|
|
|
{ |
574
|
|
|
$this->tableMetadata[$this->getRawTableName($name)][$type] = $data; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
61 |
|
* Tries to load and populate table metadata from cache. |
579
|
|
|
* |
580
|
61 |
|
* @param string $rawName |
581
|
|
|
*/ |
582
|
|
|
private function loadTableMetadataFromCache(string $rawName): void |
583
|
|
|
{ |
584
|
|
|
if (!$this->schemaCache->isEnabled() || $this->schemaCache->isExcluded($rawName)) { |
585
|
|
|
$this->tableMetadata[$rawName] = []; |
586
|
|
|
return; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
$metadata = $this->schemaCache->getOrSet( |
590
|
|
|
$this->getCacheKey($rawName), |
591
|
|
|
null, |
592
|
|
|
$this->schemaCache->getDuration(), |
593
|
|
|
new TagDependency($this->getCacheTag()), |
594
|
|
|
); |
595
|
|
|
|
596
|
|
|
if ( |
597
|
|
|
!is_array($metadata) || |
598
|
|
|
!isset($metadata[self::CACHE_VERSION]) || |
599
|
|
|
$metadata[self::CACHE_VERSION] !== static::SCHEMA_CACHE_VERSION |
600
|
|
|
) { |
601
|
|
|
$this->tableMetadata[$rawName] = []; |
602
|
|
|
return; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
unset($metadata[self::CACHE_VERSION]); |
606
|
|
|
$this->tableMetadata[$rawName] = $metadata; |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* Saves table metadata to cache. |
611
|
|
|
* |
612
|
|
|
* @param string $rawName |
613
|
|
|
*/ |
614
|
|
|
private function saveTableMetadataToCache(string $rawName): void |
615
|
|
|
{ |
616
|
|
|
if ($this->schemaCache->isEnabled() === false || $this->schemaCache->isExcluded($rawName) === true) { |
617
|
|
|
return; |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
/** @psalm-var array<string, array<TableSchemaInterface|int>> */ |
621
|
|
|
$metadata = $this->tableMetadata[$rawName]; |
622
|
|
|
/** @var int */ |
623
|
|
|
$metadata[self::CACHE_VERSION] = static::SCHEMA_CACHE_VERSION; |
624
|
|
|
|
625
|
|
|
$this->schemaCache->set( |
626
|
|
|
$this->getCacheKey($rawName), |
627
|
|
|
$metadata, |
628
|
|
|
$this->schemaCache->getDuration(), |
629
|
|
|
new TagDependency($this->getCacheTag()), |
630
|
|
|
); |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
protected function findViewNames(string $schema = ''): array |
|
|
|
|
634
|
|
|
{ |
635
|
|
|
return []; |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* @throws Throwable |
640
|
|
|
*/ |
641
|
|
|
public function getViewNames(string $schema = '', bool $refresh = false): array |
642
|
|
|
{ |
643
|
|
|
if (!isset($this->viewNames[$schema]) || $refresh) { |
644
|
|
|
$this->viewNames[$schema] = $this->findViewNames($schema); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
return (array) $this->viewNames[$schema]; |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
|