Passed
Pull Request — master (#808)
by Sergei
02:31
created

AbstractSchema::getColumnPhpType()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 19
rs 9.4222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema;
6
7
use Psr\SimpleCache\InvalidArgumentException;
8
use Throwable;
9
use Yiisoft\Db\Cache\SchemaCache;
10
use Yiisoft\Db\Command\DataType;
11
use Yiisoft\Db\Connection\ConnectionInterface;
12
use Yiisoft\Db\Constraint\Constraint;
13
use Yiisoft\Db\Constraint\IndexConstraint;
14
use Yiisoft\Db\Exception\NotSupportedException;
15
use Yiisoft\Db\Schema\Column\ColumnFactory;
16
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
17
18
use function array_change_key_case;
19
use function array_map;
20
use function gettype;
21
use function is_array;
22
use function preg_match;
23
use function preg_replace;
24
use function str_contains;
25
use function str_replace;
26
27
/**
28
 * Provides a set of methods for working with database schemas such as creating, modifying, and inspecting tables,
29
 * columns, and other database objects.
30
 *
31
 * It's a powerful and flexible tool that allows you to perform a wide range of database operations in a
32
 * database-agnostic way.
33
 */
34
abstract class AbstractSchema implements SchemaInterface
35
{
36
    /**
37
     * Schema cache version, to detect incompatibilities in cached values when the data format of the cache changes.
38
     */
39
    protected const SCHEMA_CACHE_VERSION = 1;
40
    protected const CACHE_VERSION = 'cacheVersion';
41
    /**
42
     * @var string|null $defaultSchema The default schema name used for the current session.
43
     */
44
    protected string|null $defaultSchema = null;
45
    protected array $viewNames = [];
46
    private array $schemaNames = [];
47
    /** @psalm-var string[]|array */
48
    private array $tableNames = [];
49
    private array $tableMetadata = [];
50
51
    public function __construct(
52
        protected ConnectionInterface $db,
53
        private SchemaCache $schemaCache,
54
        private ColumnFactoryInterface|null $columnFactory = null,
55
    ) {
56
    }
57
58
    /**
59
     * @param string $name The table name.
60
     *
61
     * @return array The cache key for the specified table name.
62
     */
63
    abstract protected function getCacheKey(string $name): array;
64
65
    /**
66
     * @return string The cache tag name.
67
     *
68
     * This allows {@see refresh()} to invalidate all cached table schemas.
69
     */
70
    abstract protected function getCacheTag(): string;
71
72
    /**
73
     * Loads all check constraints for the given table.
74
     *
75
     * @param string $tableName The table name.
76
     *
77
     * @return array The check constraints for the given table.
78
     */
79
    abstract protected function loadTableChecks(string $tableName): array;
80
81
    /**
82
     * Loads all default value constraints for the given table.
83
     *
84
     * @param string $tableName The table name.
85
     *
86
     * @return array The default value constraints for the given table.
87
     */
88
    abstract protected function loadTableDefaultValues(string $tableName): array;
89
90
    /**
91
     * Loads all foreign keys for the given table.
92
     *
93
     * @param string $tableName The table name.
94
     *
95
     * @return array The foreign keys for the given table.
96
     */
97
    abstract protected function loadTableForeignKeys(string $tableName): array;
98
99
    /**
100
     * Loads all indexes for the given table.
101
     *
102
     * @param string $tableName The table name.
103
     *
104
     * @return IndexConstraint[] The indexes for the given table.
105
     */
106
    abstract protected function loadTableIndexes(string $tableName): array;
107
108
    /**
109
     * Loads a primary key for the given table.
110
     *
111
     * @param string $tableName The table name.
112
     *
113
     * @return Constraint|null The primary key for the given table. `null` if the table has no primary key.
114
     */
115
    abstract protected function loadTablePrimaryKey(string $tableName): Constraint|null;
116
117
    /**
118
     * Loads all unique constraints for the given table.
119
     *
120
     * @param string $tableName The table name.
121
     *
122
     * @return array The unique constraints for the given table.
123
     */
124
    abstract protected function loadTableUniques(string $tableName): array;
125
126
    /**
127
     * Loads the metadata for the specified table.
128
     *
129
     * @param string $name The table name.
130
     *
131
     * @return TableSchemaInterface|null DBMS-dependent table metadata, `null` if the table doesn't exist.
132
     */
133
    abstract protected function loadTableSchema(string $name): TableSchemaInterface|null;
134
135
    public function getColumnFactory(): ColumnFactoryInterface
136
    {
137
        return $this->columnFactory ??= new ColumnFactory();
138
    }
139
140
    public function getDefaultSchema(): string|null
141
    {
142
        return $this->defaultSchema;
143
    }
144
145
    public function getDataType(mixed $data): int
146
    {
147
        return match (gettype($data)) {
148
            // php type => SQL data type
149
            SchemaInterface::PHP_TYPE_BOOLEAN => DataType::BOOLEAN,
150
            SchemaInterface::PHP_TYPE_INTEGER => DataType::INTEGER,
151
            SchemaInterface::PHP_TYPE_RESOURCE => DataType::LOB,
152
            SchemaInterface::PHP_TYPE_NULL => DataType::NULL,
153
            default => DataType::STRING,
154
        };
155
    }
156
157
    /** @deprecated Use {@see Quoter::getRawTableName()}. Will be removed in version 2.0.0. */
158
    public function getRawTableName(string $name): string
159
    {
160
        if (str_contains($name, '{{')) {
161
            $name = preg_replace('/{{(.*?)}}/', '\1', $name);
162
163
            return str_replace('%', $this->db->getTablePrefix(), $name);
164
        }
165
166
        return $name;
167
    }
168
169
    /**
170
     * @throws InvalidArgumentException
171
     * @throws NotSupportedException
172
     */
173
    public function getSchemaChecks(string $schema = '', bool $refresh = false): array
174
    {
175
        return $this->getSchemaMetadata($schema, SchemaInterface::CHECKS, $refresh);
176
    }
177
178
    /**
179
     * @throws InvalidArgumentException
180
     * @throws NotSupportedException
181
     */
182
    public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): array
183
    {
184
        return $this->getSchemaMetadata($schema, SchemaInterface::DEFAULT_VALUES, $refresh);
185
    }
186
187
    /**
188
     * @throws InvalidArgumentException
189
     * @throws NotSupportedException
190
     */
191
    public function getSchemaForeignKeys(string $schema = '', bool $refresh = false): array
192
    {
193
        return $this->getSchemaMetadata($schema, SchemaInterface::FOREIGN_KEYS, $refresh);
194
    }
195
196
    /**
197
     * @throws InvalidArgumentException
198
     * @throws NotSupportedException
199
     */
200
    public function getSchemaIndexes(string $schema = '', bool $refresh = false): array
201
    {
202
        return $this->getSchemaMetadata($schema, SchemaInterface::INDEXES, $refresh);
203
    }
204
205
    /**
206
     * @throws NotSupportedException If this method isn't supported by the underlying DBMS.
207
     */
208
    public function getSchemaNames(bool $refresh = false): array
209
    {
210
        if (empty($this->schemaNames) || $refresh) {
211
            $this->schemaNames = $this->findSchemaNames();
212
        }
213
214
        return $this->schemaNames;
215
    }
216
217
    /**
218
     * @throws InvalidArgumentException
219
     * @throws NotSupportedException
220
     */
221
    public function getSchemaPrimaryKeys(string $schema = '', bool $refresh = false): array
222
    {
223
        /** @psalm-var list<Constraint> */
224
        return $this->getSchemaMetadata($schema, SchemaInterface::PRIMARY_KEY, $refresh);
225
    }
226
227
    /**
228
     * @throws NotSupportedException
229
     * @throws InvalidArgumentException
230
     */
231
    public function getSchemaUniques(string $schema = '', bool $refresh = false): array
232
    {
233
        return $this->getSchemaMetadata($schema, SchemaInterface::UNIQUES, $refresh);
234
    }
235
236
    /**
237
     * @throws InvalidArgumentException
238
     */
239
    public function getTableChecks(string $name, bool $refresh = false): array
240
    {
241
        /** @psalm-var mixed $tableChecks */
242
        $tableChecks = $this->getTableMetadata($name, SchemaInterface::CHECKS, $refresh);
243
        return is_array($tableChecks) ? $tableChecks : [];
244
    }
245
246
    /**
247
     * @throws InvalidArgumentException
248
     */
249
    public function getTableDefaultValues(string $name, bool $refresh = false): array
250
    {
251
        /** @psalm-var mixed $tableDefaultValues */
252
        $tableDefaultValues = $this->getTableMetadata($name, SchemaInterface::DEFAULT_VALUES, $refresh);
253
        return is_array($tableDefaultValues) ? $tableDefaultValues : [];
254
    }
255
256
    /**
257
     * @throws InvalidArgumentException
258
     */
259
    public function getTableForeignKeys(string $name, bool $refresh = false): array
260
    {
261
        /** @psalm-var mixed $tableForeignKeys */
262
        $tableForeignKeys = $this->getTableMetadata($name, SchemaInterface::FOREIGN_KEYS, $refresh);
263
        return is_array($tableForeignKeys) ? $tableForeignKeys : [];
264
    }
265
266
    /**
267
     * @throws InvalidArgumentException
268
     */
269
    public function getTableIndexes(string $name, bool $refresh = false): array
270
    {
271
        /** @var IndexConstraint[] */
272
        return $this->getTableMetadata($name, SchemaInterface::INDEXES, $refresh);
273
    }
274
275
    /**
276
     * @throws NotSupportedException If this method isn't supported by the underlying DBMS.
277
     */
278
    public function getTableNames(string $schema = '', bool $refresh = false): array
279
    {
280
        if (!isset($this->tableNames[$schema]) || $refresh) {
281
            $this->tableNames[$schema] = $this->findTableNames($schema);
282
        }
283
284
        return is_array($this->tableNames[$schema]) ? $this->tableNames[$schema] : [];
285
    }
286
287
    /**
288
     * @throws InvalidArgumentException
289
     */
290
    public function getTablePrimaryKey(string $name, bool $refresh = false): Constraint|null
291
    {
292
        /** @psalm-var mixed $tablePrimaryKey */
293
        $tablePrimaryKey = $this->getTableMetadata($name, SchemaInterface::PRIMARY_KEY, $refresh);
294
        return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null;
295
    }
296
297
    /**
298
     * @throws InvalidArgumentException
299
     */
300
    public function getTableSchema(string $name, bool $refresh = false): TableSchemaInterface|null
301
    {
302
        /** @psalm-var mixed $tableSchema */
303
        $tableSchema = $this->getTableMetadata($name, SchemaInterface::SCHEMA, $refresh);
304
        return $tableSchema instanceof TableSchemaInterface ? $tableSchema : null;
305
    }
306
307
    /**
308
     * @throws NotSupportedException
309
     * @throws InvalidArgumentException
310
     */
311
    public function getTableSchemas(string $schema = '', bool $refresh = false): array
312
    {
313
        /** @psalm-var list<TableSchemaInterface> */
314
        return $this->getSchemaMetadata($schema, SchemaInterface::SCHEMA, $refresh);
315
    }
316
317
    /**
318
     * @throws InvalidArgumentException
319
     *
320
     * @return array The metadata for table unique constraints.
321
     */
322
    public function getTableUniques(string $name, bool $refresh = false): array
323
    {
324
        /** @psalm-var mixed $tableUniques */
325
        $tableUniques = $this->getTableMetadata($name, SchemaInterface::UNIQUES, $refresh);
326
        return is_array($tableUniques) ? $tableUniques : [];
327
    }
328
329
    /** @deprecated Use {@see DbStringHelper::isReadQuery()}. Will be removed in version 2.0.0. */
330
    public function isReadQuery(string $sql): bool
331
    {
332
        $pattern = '/^\s*(SELECT|SHOW|DESCRIBE)\b/i';
333
334
        return preg_match($pattern, $sql) > 0;
335
    }
336
337
    /**
338
     * @throws InvalidArgumentException
339
     */
340
    public function refresh(): void
341
    {
342
        if ($this->schemaCache->isEnabled()) {
343
            $this->schemaCache->invalidate($this->getCacheTag());
344
        }
345
346
        $this->tableNames = [];
347
        $this->tableMetadata = [];
348
    }
349
350
    /**
351
     * @throws InvalidArgumentException
352
     */
353
    public function refreshTableSchema(string $name): void
354
    {
355
        /** @psalm-suppress DeprecatedMethod */
356
        $rawName = $this->getRawTableName($name);
0 ignored issues
show
Deprecated Code introduced by
The function Yiisoft\Db\Schema\Abstra...hema::getRawTableName() has been deprecated: Use {@see Quoter::getRawTableName()}. Will be removed in version 2.0.0. ( Ignorable by Annotation )

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

356
        $rawName = /** @scrutinizer ignore-deprecated */ $this->getRawTableName($name);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
357
358
        unset($this->tableMetadata[$rawName]);
359
360
        $this->tableNames = [];
361
362
        if ($this->schemaCache->isEnabled()) {
363
            $this->schemaCache->remove($this->getCacheKey($rawName));
364
        }
365
    }
366
367
    public function enableCache(bool $value): void
368
    {
369
        $this->schemaCache->setEnabled($value);
370
    }
371
372
    /**
373
     * Returns all schema names in the database, including the default one but not system schemas.
374
     *
375
     * This method should be overridden by child classes to support this feature because the default
376
     * implementation simply throws an exception.
377
     *
378
     * @throws NotSupportedException If the DBMS doesn't support this method.
379
     *
380
     * @return array All schemas name in the database, except system schemas.
381
     */
382
    protected function findSchemaNames(): array
383
    {
384
        throw new NotSupportedException(static::class . ' does not support fetching all schema names.');
385
    }
386
387
    /**
388
     * Returns all table names in the database.
389
     *
390
     * This method should be overridden by child classes to support this feature because the default
391
     * implementation simply throws an exception.
392
     *
393
     * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema.
394
     *
395
     * @throws NotSupportedException If the DBMS doesn't support this method.
396
     *
397
     * @return array All tables name in the database. The names have NO schema name prefix.
398
     */
399
    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

399
    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...
400
    {
401
        throw new NotSupportedException(static::class . ' does not support fetching all table names.');
402
    }
403
404
    /**
405
     * Returns the metadata of the given type for all tables in the given schema.
406
     *
407
     * @param string $schema The schema of the metadata. Defaults to empty string, meaning the current or default schema
408
     * name.
409
     * @param string $type The metadata type.
410
     * @param bool $refresh Whether to fetch the latest available table metadata. If this is `false`, cached data may be
411
     * returned if available.
412
     *
413
     * @throws InvalidArgumentException
414
     * @throws NotSupportedException
415
     *
416
     * @return array The metadata of the given type for all tables in the given schema.
417
     *
418
     * @psalm-return list<Constraint|TableSchemaInterface|array>
419
     */
420
    protected function getSchemaMetadata(string $schema, string $type, bool $refresh): array
421
    {
422
        $metadata = [];
423
        /** @psalm-var string[] $tableNames */
424
        $tableNames = $this->getTableNames($schema, $refresh);
425
426
        foreach ($tableNames as $name) {
427
            $name = $this->db->getQuoter()->quoteSimpleTableName($name);
428
429
            if ($schema !== '') {
430
                $name = $schema . '.' . $name;
431
            }
432
433
            $tableMetadata = $this->getTableTypeMetadata($type, $name, $refresh);
434
435
            if ($tableMetadata !== null) {
436
                $metadata[] = $tableMetadata;
437
            }
438
        }
439
440
        return $metadata;
441
    }
442
443
    /**
444
     * Returns the metadata of the given type for the given table.
445
     *
446
     * @param string $name The table name. The table name may contain a schema name if any.
447
     * Don't quote the table name.
448
     * @param string $type The metadata type.
449
     * @param bool $refresh whether to reload the table metadata even if it's found in the cache.
450
     *
451
     * @throws InvalidArgumentException
452
     *
453
     * @return mixed The metadata of the given type for the given table.
454
     */
455
    protected function getTableMetadata(string $name, string $type, bool $refresh = false): mixed
456
    {
457
        /** @psalm-suppress DeprecatedMethod */
458
        $rawName = $this->getRawTableName($name);
0 ignored issues
show
Deprecated Code introduced by
The function Yiisoft\Db\Schema\Abstra...hema::getRawTableName() has been deprecated: Use {@see Quoter::getRawTableName()}. Will be removed in version 2.0.0. ( Ignorable by Annotation )

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

458
        $rawName = /** @scrutinizer ignore-deprecated */ $this->getRawTableName($name);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
459
460
        if (!isset($this->tableMetadata[$rawName])) {
461
            $this->loadTableMetadataFromCache($rawName);
462
        }
463
464
        if ($refresh || !isset($this->tableMetadata[$rawName][$type])) {
465
            /** @psalm-suppress MixedArrayAssignment */
466
            $this->tableMetadata[$rawName][$type] = $this->loadTableTypeMetadata($type, $rawName);
467
            $this->saveTableMetadataToCache($rawName);
468
        }
469
470
        /** @psalm-suppress MixedArrayAccess */
471
        return $this->tableMetadata[$rawName][$type];
472
    }
473
474
    /**
475
     * This method returns the desired metadata type for the table name.
476
     */
477
    protected function loadTableTypeMetadata(string $type, string $name): Constraint|array|TableSchemaInterface|null
478
    {
479
        return match ($type) {
480
            SchemaInterface::SCHEMA => $this->loadTableSchema($name),
481
            SchemaInterface::PRIMARY_KEY => $this->loadTablePrimaryKey($name),
482
            SchemaInterface::UNIQUES => $this->loadTableUniques($name),
483
            SchemaInterface::FOREIGN_KEYS => $this->loadTableForeignKeys($name),
484
            SchemaInterface::INDEXES => $this->loadTableIndexes($name),
485
            SchemaInterface::DEFAULT_VALUES => $this->loadTableDefaultValues($name),
486
            SchemaInterface::CHECKS => $this->loadTableChecks($name),
487
            default => null,
488
        };
489
    }
490
491
    /**
492
     * This method returns the desired metadata type for table name (with refresh if needed).
493
     *
494
     * @throws InvalidArgumentException
495
     */
496
    protected function getTableTypeMetadata(
497
        string $type,
498
        string $name,
499
        bool $refresh = false
500
    ): Constraint|array|null|TableSchemaInterface {
501
        return match ($type) {
502
            SchemaInterface::SCHEMA => $this->getTableSchema($name, $refresh),
503
            SchemaInterface::PRIMARY_KEY => $this->getTablePrimaryKey($name, $refresh),
504
            SchemaInterface::UNIQUES => $this->getTableUniques($name, $refresh),
505
            SchemaInterface::FOREIGN_KEYS => $this->getTableForeignKeys($name, $refresh),
506
            SchemaInterface::INDEXES => $this->getTableIndexes($name, $refresh),
507
            SchemaInterface::DEFAULT_VALUES => $this->getTableDefaultValues($name, $refresh),
508
            SchemaInterface::CHECKS => $this->getTableChecks($name, $refresh),
509
            default => null,
510
        };
511
    }
512
513
    /**
514
     * Change row's array key case to lower.
515
     *
516
     * @param array $row Thew row's array or an array of row arrays.
517
     * @param bool $multiple Whether many rows or a single row passed.
518
     *
519
     * @return array The normalized row or rows.
520
     *
521
     * @deprecated Use `array_change_key_case($row)` or `array_map('array_change_key_case', $row)`.
522
     * Will be removed in version 2.0.0.
523
     */
524
    protected function normalizeRowKeyCase(array $row, bool $multiple): array
525
    {
526
        if ($multiple) {
527
            return array_map(static fn (array $row) => array_change_key_case($row), $row);
528
        }
529
530
        return array_change_key_case($row);
531
    }
532
533
    /**
534
     * Resolves the table name and schema name (if any).
535
     *
536
     * @param string $name The table name.
537
     *
538
     * @throws NotSupportedException If the DBMS doesn't support this method.
539
     *
540
     * @return TableSchemaInterface The with resolved table, schema, etc. names.
541
     *
542
     * @see TableSchemaInterface
543
     */
544
    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

544
    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...
545
    {
546
        throw new NotSupportedException(static::class . ' does not support resolving table names.');
547
    }
548
549
    /**
550
     * Sets the metadata of the given type for the given table.
551
     *
552
     * @param string $name The table name.
553
     * @param string $type The metadata type.
554
     * @param mixed $data The metadata to set.
555
     */
556
    protected function setTableMetadata(string $name, string $type, mixed $data): void
557
    {
558
        /**
559
         * @psalm-suppress MixedArrayAssignment
560
         * @psalm-suppress DeprecatedMethod
561
         */
562
        $this->tableMetadata[$this->getRawTableName($name)][$type] = $data;
0 ignored issues
show
Deprecated Code introduced by
The function Yiisoft\Db\Schema\Abstra...hema::getRawTableName() has been deprecated: Use {@see Quoter::getRawTableName()}. Will be removed in version 2.0.0. ( Ignorable by Annotation )

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

562
        $this->tableMetadata[/** @scrutinizer ignore-deprecated */ $this->getRawTableName($name)][$type] = $data;

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
563
    }
564
565
    /**
566
     * Tries to load and populate table metadata from cache.
567
     *
568
     * @throws InvalidArgumentException
569
     */
570
    private function loadTableMetadataFromCache(string $rawName): void
571
    {
572
        if (!$this->schemaCache->isEnabled() || $this->schemaCache->isExcluded($rawName)) {
573
            $this->tableMetadata[$rawName] = [];
574
            return;
575
        }
576
577
        $metadata = $this->schemaCache->get($this->getCacheKey($rawName));
578
579
        if (
580
            !is_array($metadata) ||
581
            !isset($metadata[self::CACHE_VERSION]) ||
582
            $metadata[self::CACHE_VERSION] !== static::SCHEMA_CACHE_VERSION
583
        ) {
584
            $this->tableMetadata[$rawName] = [];
585
            return;
586
        }
587
588
        unset($metadata[self::CACHE_VERSION]);
589
        $this->tableMetadata[$rawName] = $metadata;
590
    }
591
592
    /**
593
     * Saves table metadata to cache.
594
     *
595
     * @throws InvalidArgumentException
596
     */
597
    private function saveTableMetadataToCache(string $rawName): void
598
    {
599
        if ($this->schemaCache->isEnabled() === false || $this->schemaCache->isExcluded($rawName) === true) {
600
            return;
601
        }
602
603
        /** @psalm-var array<string, array<TableSchemaInterface|int>> $metadata */
604
        $metadata = $this->tableMetadata[$rawName];
605
        /** @psalm-var int */
606
        $metadata[self::CACHE_VERSION] = static::SCHEMA_CACHE_VERSION;
607
608
        $this->schemaCache->set($this->getCacheKey($rawName), $metadata, $this->getCacheTag());
609
    }
610
611
    /**
612
     * Find the view names for the database.
613
     *
614
     * @param string $schema The schema of the views.
615
     * Defaults to empty string, meaning the current or default schema.
616
     *
617
     * @return array The names of all views in the database.
618
     */
619
    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

619
    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...
620
    {
621
        return [];
622
    }
623
624
    /**
625
     * @throws Throwable
626
     *
627
     * @return array The view names for the database.
628
     */
629
    public function getViewNames(string $schema = '', bool $refresh = false): array
630
    {
631
        if (!isset($this->viewNames[$schema]) || $refresh) {
632
            $this->viewNames[$schema] = $this->findViewNames($schema);
633
        }
634
635
        return (array) $this->viewNames[$schema];
636
    }
637
}
638