Passed
Pull Request — master (#682)
by Alexander
02:11
created

AbstractSchema::enableCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema;
6
7
use PDO;
8
use Psr\SimpleCache\InvalidArgumentException;
9
use Throwable;
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
use function preg_replace;
21
use function str_contains;
22
use function str_replace;
23
24
/**
25
 * Provides a set of methods for working with database schemas such as creating, modifying, and inspecting tables,
26
 * columns, and other database objects.
27
 *
28
 * It's a powerful and flexible tool that allows you to perform a wide range of database operations in a
29
 * database-agnostic way.
30
 */
31
abstract class AbstractSchema implements SchemaInterface
32
{
33
    /**
34
     * Schema cache version, to detect incompatibilities in cached values when the data format of the cache changes.
35
     */
36
    protected const SCHEMA_CACHE_VERSION = 1;
37
    protected const CACHE_VERSION = 'cacheVersion';
38
    /**
39
     * @var string|null $defaultSchema The default schema name used for the current session.
40
     */
41
    protected string|null $defaultSchema = null;
42
    protected array $viewNames = [];
43
    private array $schemaNames = [];
44
    /** @psalm-var string[]|array */
45
    private array $tableNames = [];
46
    private array $tableMetadata = [];
47
48
    public function __construct(protected ConnectionInterface $db, private SchemaCache $schemaCache)
49
    {
50
    }
51
52
    /**
53
     * @param string $name The table name.
54
     *
55
     * @return array The cache key for the specified table name.
56
     */
57
    abstract protected function getCacheKey(string $name): array;
58
59
    /**
60
     * @return string The cache tag name.
61
     *
62
     * This allows {@see refresh()} to invalidate all cached table schemas.
63
     */
64
    abstract protected function getCacheTag(): string;
65
66
    /**
67
     * Loads all check constraints for the given table.
68
     *
69
     * @param string $tableName The table name.
70
     *
71
     * @return array The check constraints for the given table.
72
     */
73
    abstract protected function loadTableChecks(string $tableName): array;
74
75
    /**
76
     * Loads all default value constraints for the given table.
77
     *
78
     * @param string $tableName The table name.
79
     *
80
     * @return array The default value constraints for the given table.
81
     */
82
    abstract protected function loadTableDefaultValues(string $tableName): array;
83
84
    /**
85
     * Loads all foreign keys for the given table.
86
     *
87
     * @param string $tableName The table name.
88
     *
89
     * @return array The foreign keys for the given table.
90
     */
91
    abstract protected function loadTableForeignKeys(string $tableName): array;
92
93
    /**
94
     * Loads all indexes for the given table.
95
     *
96
     * @param string $tableName The table name.
97
     *
98
     * @return array The indexes for the given table.
99
     */
100
    abstract protected function loadTableIndexes(string $tableName): array;
101
102
    /**
103
     * Loads a primary key for the given table.
104
     *
105
     * @param string $tableName The table name.
106
     *
107
     * @return Constraint|null The primary key for the given table. `null` if the table has no primary key.
108
     */
109
    abstract protected function loadTablePrimaryKey(string $tableName): Constraint|null;
110
111
    /**
112
     * Loads all unique constraints for the given table.
113
     *
114
     * @param string $tableName The table name.
115
     *
116
     * @return array The unique constraints for the given table.
117
     */
118
    abstract protected function loadTableUniques(string $tableName): array;
119
120
    /**
121
     * Loads the metadata for the specified table.
122
     *
123
     * @param string $name The table name.
124
     *
125
     * @return TableSchemaInterface|null DBMS-dependent table metadata, `null` if the table doesn't exist.
126
     */
127
    abstract protected function loadTableSchema(string $name): TableSchemaInterface|null;
128
129
    public function getDefaultSchema(): string|null
130
    {
131
        return $this->defaultSchema;
132
    }
133
134
    public function getPdoType(mixed $data): int
135
    {
136
        /** @psalm-var array<string, int> $typeMap */
137
        $typeMap = [
138
            // php type => PDO type
139
            SchemaInterface::PHP_TYPE_BOOLEAN => PDO::PARAM_BOOL,
140
            SchemaInterface::PHP_TYPE_INTEGER => PDO::PARAM_INT,
141
            SchemaInterface::PHP_TYPE_STRING => PDO::PARAM_STR,
142
            SchemaInterface::PHP_TYPE_RESOURCE => PDO::PARAM_LOB,
143
            SchemaInterface::PHP_TYPE_NULL => PDO::PARAM_NULL,
144
        ];
145
146
        $type = gettype($data);
147
148
        return $typeMap[$type] ?? PDO::PARAM_STR;
149
    }
150
151
    public function getRawTableName(string $name): string
152
    {
153
        if (str_contains($name, '{{')) {
154
            $name = preg_replace('/{{(.*?)}}/', '\1', $name);
155
156
            return str_replace('%', $this->db->getTablePrefix(), $name);
157
        }
158
159
        return $name;
160
    }
161
162
    /**
163
     * @throws InvalidArgumentException
164
     * @throws NotSupportedException
165
     *
166
     * @return array The metadata for check constraints.
167
     */
168
    public function getSchemaChecks(string $schema = '', bool $refresh = false): array
169
    {
170
        return $this->getSchemaMetadata($schema, SchemaInterface::CHECKS, $refresh);
171
    }
172
173
    /**
174
     * @throws InvalidArgumentException
175
     * @throws NotSupportedException
176
     *
177
     * @return array The metadata for default values constraints.
178
     */
179
    public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): array
180
    {
181
        return $this->getSchemaMetadata($schema, SchemaInterface::DEFAULT_VALUES, $refresh);
182
    }
183
184
    /**
185
     * @throws InvalidArgumentException
186
     * @throws NotSupportedException
187
     *
188
     * @return array The metadata for foreign keys constraints.
189
     */
190
    public function getSchemaForeignKeys(string $schema = '', bool $refresh = false): array
191
    {
192
        return $this->getSchemaMetadata($schema, SchemaInterface::FOREIGN_KEYS, $refresh);
193
    }
194
195
    /**
196
     * @throws InvalidArgumentException
197
     * @throws NotSupportedException
198
     *
199
     * @return array The metadata for indexes constraints.
200
     */
201
    public function getSchemaIndexes(string $schema = '', bool $refresh = false): array
202
    {
203
        return $this->getSchemaMetadata($schema, SchemaInterface::INDEXES, $refresh);
204
    }
205
206
    /**
207
     * @throws NotSupportedException If this method isn't supported by the underlying DBMS.
208
     *
209
     * @return array The schema names in the database, except system schemas.
210
     */
211
    public function getSchemaNames(bool $refresh = false): array
212
    {
213
        if (empty($this->schemaNames) || $refresh) {
214
            $this->schemaNames = $this->findSchemaNames();
215
        }
216
217
        return $this->schemaNames;
218
    }
219
220
    /**
221
     * @throws InvalidArgumentException
222
     * @throws NotSupportedException
223
     *
224
     * @return array The metadata for primary keys constraints.
225
     */
226
    public function getSchemaPrimaryKeys(string $schema = '', bool $refresh = false): array
227
    {
228
        return $this->getSchemaMetadata($schema, SchemaInterface::PRIMARY_KEY, $refresh);
229
    }
230
231
    /**
232
     * @throws InvalidArgumentException
233
     * @throws NotSupportedException
234
     *
235
     * @return array The metadata for unique constraints.
236
     */
237
    public function getSchemaUniques(string $schema = '', bool $refresh = false): array
238
    {
239
        return $this->getSchemaMetadata($schema, SchemaInterface::UNIQUES, $refresh);
240
    }
241
242
    /**
243
     * @throws InvalidArgumentException
244
     *
245
     * @return array The metadata for table checks constraints.
246
     */
247
    public function getTableChecks(string $name, bool $refresh = false): array
248
    {
249
        /** @psalm-var mixed $tableChecks */
250
        $tableChecks = $this->getTableMetadata($name, SchemaInterface::CHECKS, $refresh);
251
        return is_array($tableChecks) ? $tableChecks : [];
252
    }
253
254
    /**
255
     * @throws InvalidArgumentException
256
     *
257
     * @return array The metadata for table default values constraints.
258
     */
259
    public function getTableDefaultValues(string $name, bool $refresh = false): array
260
    {
261
        /** @psalm-var mixed $tableDefaultValues */
262
        $tableDefaultValues = $this->getTableMetadata($name, SchemaInterface::DEFAULT_VALUES, $refresh);
263
        return is_array($tableDefaultValues) ? $tableDefaultValues : [];
264
    }
265
266
    /**
267
     * @throws InvalidArgumentException
268
     *
269
     * @return array The metadata for table foreign keys constraints.
270
     */
271
    public function getTableForeignKeys(string $name, bool $refresh = false): array
272
    {
273
        /** @psalm-var mixed $tableForeignKeys */
274
        $tableForeignKeys = $this->getTableMetadata($name, SchemaInterface::FOREIGN_KEYS, $refresh);
275
        return is_array($tableForeignKeys) ? $tableForeignKeys : [];
276
    }
277
278
    /**
279
     * @throws InvalidArgumentException
280
     *
281
     * @return array The metadata for table indexes constraints.
282
     */
283
    public function getTableIndexes(string $name, bool $refresh = false): array
284
    {
285
        /** @psalm-var mixed $tableIndexes */
286
        $tableIndexes = $this->getTableMetadata($name, SchemaInterface::INDEXES, $refresh);
287
        return is_array($tableIndexes) ? $tableIndexes : [];
288
    }
289
290
    /**
291
     * @throws NotSupportedException If this method isn't supported by the underlying DBMS.
292
     *
293
     * @return array The table names in the database.
294
     */
295
    public function getTableNames(string $schema = '', bool $refresh = false): array
296
    {
297
        if (!isset($this->tableNames[$schema]) || $refresh) {
298
            $this->tableNames[$schema] = $this->findTableNames($schema);
299
        }
300
301
        return is_array($this->tableNames[$schema]) ? $this->tableNames[$schema] : [];
302
    }
303
304
    /**
305
     * @throws InvalidArgumentException
306
     *
307
     * @return Constraint|null The metadata for table primary key constraint.
308
     */
309
    public function getTablePrimaryKey(string $name, bool $refresh = false): Constraint|null
310
    {
311
        /** @psalm-var mixed $tablePrimaryKey */
312
        $tablePrimaryKey = $this->getTableMetadata($name, SchemaInterface::PRIMARY_KEY, $refresh);
313
        return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null;
314
    }
315
316
    /**
317
     * @throws InvalidArgumentException
318
     *
319
     * @return TableSchemaInterface|null The table schema information. Null if the named table doesn't exist.
320
     */
321
    public function getTableSchema(string $name, bool $refresh = false): TableSchemaInterface|null
322
    {
323
        /** @psalm-var mixed $tableSchema */
324
        $tableSchema = $this->getTableMetadata($name, SchemaInterface::SCHEMA, $refresh);
325
        return $tableSchema instanceof TableSchemaInterface ? $tableSchema : null;
326
    }
327
328
    /**
329
     * @throws NotSupportedException
330
     * @throws InvalidArgumentException
331
     *
332
     * @return array The list of table schemas in the database.
333
     */
334
    public function getTableSchemas(string $schema = '', bool $refresh = false): array
335
    {
336
        /** @psalm-var mixed $tableSchemas */
337
        $tableSchemas = $this->getSchemaMetadata($schema, SchemaInterface::SCHEMA, $refresh);
338
339
        return is_array($tableSchemas) ? $tableSchemas : [];
0 ignored issues
show
introduced by
The condition is_array($tableSchemas) is always true.
Loading history...
340
    }
341
342
    /**
343
     * @throws InvalidArgumentException
344
     *
345
     * @return array The metadata for table unique constraints.
346
     */
347
    public function getTableUniques(string $name, bool $refresh = false): array
348
    {
349
        /** @psalm-var mixed $tableUniques */
350
        $tableUniques = $this->getTableMetadata($name, SchemaInterface::UNIQUES, $refresh);
351
        return is_array($tableUniques) ? $tableUniques : [];
352
    }
353
354
    public function isReadQuery(string $sql): bool
355
    {
356
        $pattern = '/^\s*(SELECT|SHOW|DESCRIBE)\b/i';
357
358
        return preg_match($pattern, $sql) > 0;
359
    }
360
361
    /**
362
     * @throws InvalidArgumentException
363
     */
364
    public function refresh(): void
365
    {
366
        if ($this->schemaCache->isEnabled()) {
367
            $this->schemaCache->invalidate($this->getCacheTag());
368
        }
369
370
        $this->tableNames = [];
371
        $this->tableMetadata = [];
372
    }
373
374
    /**
375
     * @throws InvalidArgumentException
376
     */
377
    public function refreshTableSchema(string $name): void
378
    {
379
        $rawName = $this->getRawTableName($name);
380
381
        unset($this->tableMetadata[$rawName]);
382
383
        $this->tableNames = [];
384
385
        if ($this->schemaCache->isEnabled()) {
386
            $this->schemaCache->remove($this->getCacheKey($rawName));
387
        }
388
    }
389
390
    public function enableCache(bool $value): void
391
    {
392
        $this->schemaCache->setEnable($value);
393
    }
394
395
    /**
396
     * Returns all schema names in the database, including the default one but not system schemas.
397
     *
398
     * This method should be overridden by child classes to support this feature because the default
399
     * implementation simply throws an exception.
400
     *
401
     * @throws NotSupportedException If the DBMS doesn't support this method.
402
     *
403
     * @return array All schemas name in the database, except system schemas.
404
     */
405
    protected function findSchemaNames(): array
406
    {
407
        throw new NotSupportedException(static::class . ' does not support fetching all schema names.');
408
    }
409
410
    /**
411
     * Returns all table names in the database.
412
     *
413
     * This method should be overridden by child classes to support this feature because the default
414
     * implementation simply throws an exception.
415
     *
416
     * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema.
417
     *
418
     * @throws NotSupportedException If the DBMS doesn't support this method.
419
     *
420
     * @return array All tables name in the database. The names have NO schema name prefix.
421
     */
422
    protected function findTableNames(string $schema): array
0 ignored issues
show
Unused Code introduced by
The parameter $schema is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

422
    protected function findTableNames(/** @scrutinizer ignore-unused */ string $schema): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
423
    {
424
        throw new NotSupportedException(static::class . ' does not support fetching all table names.');
425
    }
426
427
    /**
428
     * Extracts the PHP type from an abstract DB type.
429
     *
430
     * @param ColumnSchemaInterface $column The column schema information.
431
     *
432
     * @return string The PHP type name.
433
     */
434
    protected function getColumnPhpType(ColumnSchemaInterface $column): string
435
    {
436
        /** @psalm-var string[] $typeMap */
437
        $typeMap = [
438
            // abstract type => php type
439
            SchemaInterface::TYPE_TINYINT => SchemaInterface::PHP_TYPE_INTEGER,
440
            SchemaInterface::TYPE_SMALLINT => SchemaInterface::PHP_TYPE_INTEGER,
441
            SchemaInterface::TYPE_INTEGER => SchemaInterface::PHP_TYPE_INTEGER,
442
            SchemaInterface::TYPE_BIGINT => SchemaInterface::PHP_TYPE_INTEGER,
443
            SchemaInterface::TYPE_BOOLEAN => SchemaInterface::PHP_TYPE_BOOLEAN,
444
            SchemaInterface::TYPE_DECIMAL => SchemaInterface::PHP_TYPE_DOUBLE,
445
            SchemaInterface::TYPE_FLOAT => SchemaInterface::PHP_TYPE_DOUBLE,
446
            SchemaInterface::TYPE_DOUBLE => SchemaInterface::PHP_TYPE_DOUBLE,
447
            SchemaInterface::TYPE_BINARY => SchemaInterface::PHP_TYPE_RESOURCE,
448
            SchemaInterface::TYPE_JSON => SchemaInterface::PHP_TYPE_ARRAY,
449
        ];
450
451
        if (isset($typeMap[$column->getType()])) {
452
            if ($column->getType() === SchemaInterface::TYPE_BIGINT) {
453
                return PHP_INT_SIZE === 8 && !$column->isUnsigned()
454
                    ? SchemaInterface::PHP_TYPE_INTEGER : SchemaInterface::PHP_TYPE_STRING;
455
            }
456
457
            if ($column->getType() === SchemaInterface::TYPE_INTEGER) {
458
                return PHP_INT_SIZE === 4 && $column->isUnsigned()
459
                    ? SchemaInterface::PHP_TYPE_STRING : SchemaInterface::PHP_TYPE_INTEGER;
460
            }
461
462
            return $typeMap[$column->getType()];
463
        }
464
465
        return SchemaInterface::PHP_TYPE_STRING;
466
    }
467
468
    /**
469
     * Returns the metadata of the given type for all tables in the given schema.
470
     *
471
     * @param string $schema The schema of the metadata. Defaults to empty string, meaning the current or default schema
472
     * name.
473
     * @param string $type The metadata type.
474
     * @param bool $refresh Whether to fetch the latest available table metadata. If this is `false`, cached data may be
475
     * returned if available.
476
     *
477
     * @throws InvalidArgumentException
478
     * @throws NotSupportedException
479
     *
480
     * @return array The metadata of the given type for all tables in the given schema.
481
     */
482
    protected function getSchemaMetadata(string $schema, string $type, bool $refresh): array
483
    {
484
        $metadata = [];
485
        /** @psalm-var string[] $tableNames */
486
        $tableNames = $this->getTableNames($schema, $refresh);
487
488
        foreach ($tableNames as $name) {
489
            $name = $this->db->getQuoter()->quoteSimpleTableName($name);
490
491
            if ($schema !== '') {
492
                $name = $schema . '.' . $name;
493
            }
494
495
            $tableMetadata = $this->getTableTypeMetadata($type, $name, $refresh);
496
497
            if ($tableMetadata !== null) {
498
                $metadata[] = $tableMetadata;
499
            }
500
        }
501
502
        return $metadata;
503
    }
504
505
    /**
506
     * Returns the metadata of the given type for the given table.
507
     *
508
     * @param string $name The table name. The table name may contain a schema name if any.
509
     * Don't quote the table name.
510
     * @param string $type The metadata type.
511
     * @param bool $refresh whether to reload the table metadata even if it's found in the cache.
512
     *
513
     * @throws InvalidArgumentException
514
     *
515
     * @return mixed The metadata of the given type for the given table.
516
     */
517
    protected function getTableMetadata(string $name, string $type, bool $refresh = false): mixed
518
    {
519
        $rawName = $this->getRawTableName($name);
520
521
        if (!isset($this->tableMetadata[$rawName])) {
522
            $this->loadTableMetadataFromCache($rawName);
523
        }
524
525
        if ($refresh || !isset($this->tableMetadata[$rawName][$type])) {
526
            /** @psalm-suppress MixedArrayAssignment */
527
            $this->tableMetadata[$rawName][$type] = $this->loadTableTypeMetadata($type, $rawName);
528
            $this->saveTableMetadataToCache($rawName);
529
        }
530
531
        /** @psalm-suppress MixedArrayAccess */
532
        return $this->tableMetadata[$rawName][$type];
533
    }
534
535
    /**
536
     * This method returns the desired metadata type for the table name.
537
     */
538
    protected function loadTableTypeMetadata(string $type, string $name): Constraint|array|TableSchemaInterface|null
539
    {
540
        return match ($type) {
541
            SchemaInterface::SCHEMA => $this->loadTableSchema($name),
542
            SchemaInterface::PRIMARY_KEY => $this->loadTablePrimaryKey($name),
543
            SchemaInterface::UNIQUES => $this->loadTableUniques($name),
544
            SchemaInterface::FOREIGN_KEYS => $this->loadTableForeignKeys($name),
545
            SchemaInterface::INDEXES => $this->loadTableIndexes($name),
546
            SchemaInterface::DEFAULT_VALUES => $this->loadTableDefaultValues($name),
547
            SchemaInterface::CHECKS => $this->loadTableChecks($name),
548
            default => null,
549
        };
550
    }
551
552
    /**
553
     * This method returns the desired metadata type for table name (with refresh if needed).
554
     *
555
     * @throws InvalidArgumentException
556
     */
557
    protected function getTableTypeMetadata(
558
        string $type,
559
        string $name,
560
        bool $refresh = false
561
    ): Constraint|array|null|TableSchemaInterface {
562
        return match ($type) {
563
            SchemaInterface::SCHEMA => $this->getTableSchema($name, $refresh),
564
            SchemaInterface::PRIMARY_KEY => $this->getTablePrimaryKey($name, $refresh),
565
            SchemaInterface::UNIQUES => $this->getTableUniques($name, $refresh),
566
            SchemaInterface::FOREIGN_KEYS => $this->getTableForeignKeys($name, $refresh),
567
            SchemaInterface::INDEXES => $this->getTableIndexes($name, $refresh),
568
            SchemaInterface::DEFAULT_VALUES => $this->getTableDefaultValues($name, $refresh),
569
            SchemaInterface::CHECKS => $this->getTableChecks($name, $refresh),
570
            default => null,
571
        };
572
    }
573
574
    /**
575
     * Change row's array key case to lower.
576
     *
577
     * @param array $row Thew row's array or an array of row arrays.
578
     * @param bool $multiple Whether many rows or a single row passed.
579
     *
580
     * @return array The normalized row or rows.
581
     */
582
    protected function normalizeRowKeyCase(array $row, bool $multiple): array
583
    {
584
        if ($multiple) {
585
            return array_map(static fn (array $row) => array_change_key_case($row), $row);
586
        }
587
588
        return array_change_key_case($row);
589
    }
590
591
    /**
592
     * Resolves the table name and schema name (if any).
593
     *
594
     * @param string $name The table name.
595
     *
596
     * @throws NotSupportedException If the DBMS doesn't support this method.
597
     *
598
     * @return TableSchemaInterface The with resolved table, schema, etc. names.
599
     *
600
     * @see TableSchemaInterface
601
     */
602
    protected function resolveTableName(string $name): TableSchemaInterface
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

602
    protected function resolveTableName(/** @scrutinizer ignore-unused */ string $name): TableSchemaInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
603
    {
604
        throw new NotSupportedException(static::class . ' does not support resolving table names.');
605
    }
606
607
    /**
608
     * Sets the metadata of the given type for the given table.
609
     *
610
     * @param string $name The table name.
611
     * @param string $type The metadata type.
612
     * @param mixed $data The metadata to set.
613
     */
614
    protected function setTableMetadata(string $name, string $type, mixed $data): void
615
    {
616
        /** @psalm-suppress MixedArrayAssignment  */
617
        $this->tableMetadata[$this->getRawTableName($name)][$type] = $data;
618
    }
619
620
    /**
621
     * Tries to load and populate table metadata from cache.
622
     *
623
     * @throws InvalidArgumentException
624
     */
625
    private function loadTableMetadataFromCache(string $rawName): void
626
    {
627
        if (!$this->schemaCache->isEnabled() || $this->schemaCache->isExcluded($rawName)) {
628
            $this->tableMetadata[$rawName] = [];
629
            return;
630
        }
631
632
        $metadata = $this->schemaCache->get($this->getCacheKey($rawName));
633
634
        if (
635
            !is_array($metadata) ||
636
            !isset($metadata[self::CACHE_VERSION]) ||
637
            $metadata[self::CACHE_VERSION] !== static::SCHEMA_CACHE_VERSION
638
        ) {
639
            $this->tableMetadata[$rawName] = [];
640
            return;
641
        }
642
643
        unset($metadata[self::CACHE_VERSION]);
644
        $this->tableMetadata[$rawName] = $metadata;
645
    }
646
647
    /**
648
     * Saves table metadata to cache.
649
     *
650
     * @throws InvalidArgumentException
651
     */
652
    private function saveTableMetadataToCache(string $rawName): void
653
    {
654
        if ($this->schemaCache->isEnabled() === false || $this->schemaCache->isExcluded($rawName) === true) {
655
            return;
656
        }
657
658
        /** @psalm-var array<string, array<TableSchemaInterface|int>> $metadata */
659
        $metadata = $this->tableMetadata[$rawName];
660
        /** @psalm-var int */
661
        $metadata[self::CACHE_VERSION] = static::SCHEMA_CACHE_VERSION;
662
663
        $this->schemaCache->set($this->getCacheKey($rawName), $metadata, $this->getCacheTag());
664
    }
665
666
    /**
667
     * Find the view names for the database.
668
     *
669
     * @param string $schema The schema of the views.
670
     * Defaults to empty string, meaning the current or default schema.
671
     *
672
     * @return array The names of all views in the database.
673
     */
674
    protected function findViewNames(string $schema = ''): array
0 ignored issues
show
Unused Code introduced by
The parameter $schema is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

674
    protected function findViewNames(/** @scrutinizer ignore-unused */ string $schema = ''): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
675
    {
676
        return [];
677
    }
678
679
    /**
680
     * @throws Throwable
681
     *
682
     * @return array The view names for the database.
683
     */
684
    public function getViewNames(string $schema = '', bool $refresh = false): array
685
    {
686
        if (!isset($this->viewNames[$schema]) || $refresh) {
687
            $this->viewNames[$schema] = $this->findViewNames($schema);
688
        }
689
690
        return (array) $this->viewNames[$schema];
691
    }
692
}
693