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