| Total Complexity | 73 |
| Total Lines | 619 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractSchema often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractSchema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 getDataType(mixed $data): int |
||
| 135 | { |
||
| 136 | /** @psalm-var array<string, int> $typeMap */ |
||
| 137 | $typeMap = [ |
||
| 138 | // php type => SQL data type |
||
| 139 | SchemaInterface::PHP_TYPE_BOOLEAN => DataType::BOOLEAN, |
||
| 140 | SchemaInterface::PHP_TYPE_INTEGER => DataType::INTEGER, |
||
| 141 | SchemaInterface::PHP_TYPE_STRING => DataType::STRING, |
||
| 142 | SchemaInterface::PHP_TYPE_RESOURCE => DataType::LOB, |
||
| 143 | SchemaInterface::PHP_TYPE_NULL => DataType::NULL, |
||
| 144 | ]; |
||
| 145 | |||
| 146 | $type = gettype($data); |
||
| 147 | |||
| 148 | return $typeMap[$type] ?? DataType::STRING; |
||
| 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 | public function getSchemaChecks(string $schema = '', bool $refresh = false): array |
||
| 167 | { |
||
| 168 | return $this->getSchemaMetadata($schema, SchemaInterface::CHECKS, $refresh); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @throws InvalidArgumentException |
||
| 173 | * @throws NotSupportedException |
||
| 174 | */ |
||
| 175 | public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): array |
||
| 176 | { |
||
| 177 | return $this->getSchemaMetadata($schema, SchemaInterface::DEFAULT_VALUES, $refresh); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @throws InvalidArgumentException |
||
| 182 | * @throws NotSupportedException |
||
| 183 | */ |
||
| 184 | public function getSchemaForeignKeys(string $schema = '', bool $refresh = false): array |
||
| 185 | { |
||
| 186 | return $this->getSchemaMetadata($schema, SchemaInterface::FOREIGN_KEYS, $refresh); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @throws InvalidArgumentException |
||
| 191 | * @throws NotSupportedException |
||
| 192 | */ |
||
| 193 | public function getSchemaIndexes(string $schema = '', bool $refresh = false): array |
||
| 194 | { |
||
| 195 | return $this->getSchemaMetadata($schema, SchemaInterface::INDEXES, $refresh); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @throws NotSupportedException If this method isn't supported by the underlying DBMS. |
||
| 200 | */ |
||
| 201 | public function getSchemaNames(bool $refresh = false): array |
||
| 202 | { |
||
| 203 | if (empty($this->schemaNames) || $refresh) { |
||
| 204 | $this->schemaNames = $this->findSchemaNames(); |
||
| 205 | } |
||
| 206 | |||
| 207 | return $this->schemaNames; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @throws InvalidArgumentException |
||
| 212 | * @throws NotSupportedException |
||
| 213 | */ |
||
| 214 | public function getSchemaPrimaryKeys(string $schema = '', bool $refresh = false): array |
||
| 215 | { |
||
| 216 | /** @psalm-var list<Constraint> */ |
||
| 217 | return $this->getSchemaMetadata($schema, SchemaInterface::PRIMARY_KEY, $refresh); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @throws NotSupportedException |
||
| 222 | * @throws InvalidArgumentException |
||
| 223 | */ |
||
| 224 | public function getSchemaUniques(string $schema = '', bool $refresh = false): array |
||
| 225 | { |
||
| 226 | return $this->getSchemaMetadata($schema, SchemaInterface::UNIQUES, $refresh); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @throws InvalidArgumentException |
||
| 231 | */ |
||
| 232 | public function getTableChecks(string $name, bool $refresh = false): array |
||
| 233 | { |
||
| 234 | /** @psalm-var mixed $tableChecks */ |
||
| 235 | $tableChecks = $this->getTableMetadata($name, SchemaInterface::CHECKS, $refresh); |
||
| 236 | return is_array($tableChecks) ? $tableChecks : []; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @throws InvalidArgumentException |
||
| 241 | */ |
||
| 242 | public function getTableDefaultValues(string $name, bool $refresh = false): array |
||
| 243 | { |
||
| 244 | /** @psalm-var mixed $tableDefaultValues */ |
||
| 245 | $tableDefaultValues = $this->getTableMetadata($name, SchemaInterface::DEFAULT_VALUES, $refresh); |
||
| 246 | return is_array($tableDefaultValues) ? $tableDefaultValues : []; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @throws InvalidArgumentException |
||
| 251 | */ |
||
| 252 | public function getTableForeignKeys(string $name, bool $refresh = false): array |
||
| 253 | { |
||
| 254 | /** @psalm-var mixed $tableForeignKeys */ |
||
| 255 | $tableForeignKeys = $this->getTableMetadata($name, SchemaInterface::FOREIGN_KEYS, $refresh); |
||
| 256 | return is_array($tableForeignKeys) ? $tableForeignKeys : []; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @throws InvalidArgumentException |
||
| 261 | */ |
||
| 262 | public function getTableIndexes(string $name, bool $refresh = false): array |
||
| 263 | { |
||
| 264 | /** @psalm-var mixed $tableIndexes */ |
||
| 265 | $tableIndexes = $this->getTableMetadata($name, SchemaInterface::INDEXES, $refresh); |
||
| 266 | return is_array($tableIndexes) ? $tableIndexes : []; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @throws NotSupportedException If this method isn't supported by the underlying DBMS. |
||
| 271 | */ |
||
| 272 | public function getTableNames(string $schema = '', bool $refresh = false): array |
||
| 273 | { |
||
| 274 | if (!isset($this->tableNames[$schema]) || $refresh) { |
||
| 275 | $this->tableNames[$schema] = $this->findTableNames($schema); |
||
| 276 | } |
||
| 277 | |||
| 278 | return is_array($this->tableNames[$schema]) ? $this->tableNames[$schema] : []; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @throws InvalidArgumentException |
||
| 283 | */ |
||
| 284 | public function getTablePrimaryKey(string $name, bool $refresh = false): Constraint|null |
||
| 285 | { |
||
| 286 | /** @psalm-var mixed $tablePrimaryKey */ |
||
| 287 | $tablePrimaryKey = $this->getTableMetadata($name, SchemaInterface::PRIMARY_KEY, $refresh); |
||
| 288 | return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @throws InvalidArgumentException |
||
| 293 | */ |
||
| 294 | public function getTableSchema(string $name, bool $refresh = false): TableSchemaInterface|null |
||
| 295 | { |
||
| 296 | /** @psalm-var mixed $tableSchema */ |
||
| 297 | $tableSchema = $this->getTableMetadata($name, SchemaInterface::SCHEMA, $refresh); |
||
| 298 | return $tableSchema instanceof TableSchemaInterface ? $tableSchema : null; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @throws NotSupportedException |
||
| 303 | * @throws InvalidArgumentException |
||
| 304 | */ |
||
| 305 | public function getTableSchemas(string $schema = '', bool $refresh = false): array |
||
| 306 | { |
||
| 307 | /** @psalm-var list<TableSchemaInterface> */ |
||
| 308 | return $this->getSchemaMetadata($schema, SchemaInterface::SCHEMA, $refresh); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @throws InvalidArgumentException |
||
| 313 | * |
||
| 314 | * @return array The metadata for table unique constraints. |
||
| 315 | */ |
||
| 316 | public function getTableUniques(string $name, bool $refresh = false): array |
||
| 321 | } |
||
| 322 | |||
| 323 | public function isReadQuery(string $sql): bool |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @throws InvalidArgumentException |
||
| 332 | */ |
||
| 333 | public function refresh(): void |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @throws InvalidArgumentException |
||
| 345 | */ |
||
| 346 | public function refreshTableSchema(string $name): void |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | public function enableCache(bool $value): void |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns all schema names in the database, including the default one but not system schemas. |
||
| 366 | * |
||
| 367 | * This method should be overridden by child classes to support this feature because the default |
||
| 368 | * implementation simply throws an exception. |
||
| 369 | * |
||
| 370 | * @throws NotSupportedException If the DBMS doesn't support this method. |
||
| 371 | * |
||
| 372 | * @return array All schemas name in the database, except system schemas. |
||
| 373 | */ |
||
| 374 | protected function findSchemaNames(): array |
||
| 375 | { |
||
| 376 | throw new NotSupportedException(static::class . ' does not support fetching all schema names.'); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Returns all table names in the database. |
||
| 381 | * |
||
| 382 | * This method should be overridden by child classes to support this feature because the default |
||
| 383 | * implementation simply throws an exception. |
||
| 384 | * |
||
| 385 | * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
| 386 | * |
||
| 387 | * @throws NotSupportedException If the DBMS doesn't support this method. |
||
| 388 | * |
||
| 389 | * @return array All tables name in the database. The names have NO schema name prefix. |
||
| 390 | */ |
||
| 391 | protected function findTableNames(string $schema): array |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Extracts the PHP type from an abstract DB type. |
||
| 398 | * |
||
| 399 | * @param ColumnSchemaInterface $column The column schema information. |
||
| 400 | * |
||
| 401 | * @return string The PHP type name. |
||
| 402 | */ |
||
| 403 | protected function getColumnPhpType(ColumnSchemaInterface $column): string |
||
| 404 | { |
||
| 405 | return match ($column->getType()) { |
||
| 406 | // abstract type => php type |
||
| 407 | SchemaInterface::TYPE_TINYINT => SchemaInterface::PHP_TYPE_INTEGER, |
||
| 408 | SchemaInterface::TYPE_SMALLINT => SchemaInterface::PHP_TYPE_INTEGER, |
||
| 409 | SchemaInterface::TYPE_INTEGER => PHP_INT_SIZE === 4 && $column->isUnsigned() |
||
| 410 | ? SchemaInterface::PHP_TYPE_STRING |
||
| 411 | : SchemaInterface::PHP_TYPE_INTEGER, |
||
| 412 | SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE === 8 && !$column->isUnsigned() |
||
| 413 | ? SchemaInterface::PHP_TYPE_INTEGER |
||
| 414 | : SchemaInterface::PHP_TYPE_STRING, |
||
| 415 | SchemaInterface::TYPE_BOOLEAN => SchemaInterface::PHP_TYPE_BOOLEAN, |
||
| 416 | SchemaInterface::TYPE_DECIMAL => SchemaInterface::PHP_TYPE_DOUBLE, |
||
| 417 | SchemaInterface::TYPE_FLOAT => SchemaInterface::PHP_TYPE_DOUBLE, |
||
| 418 | SchemaInterface::TYPE_DOUBLE => SchemaInterface::PHP_TYPE_DOUBLE, |
||
| 419 | SchemaInterface::TYPE_BINARY => SchemaInterface::PHP_TYPE_RESOURCE, |
||
| 420 | SchemaInterface::TYPE_JSON => SchemaInterface::PHP_TYPE_ARRAY, |
||
| 421 | default => SchemaInterface::PHP_TYPE_STRING, |
||
| 422 | }; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Returns the metadata of the given type for all tables in the given schema. |
||
| 427 | * |
||
| 428 | * @param string $schema The schema of the metadata. Defaults to empty string, meaning the current or default schema |
||
| 429 | * name. |
||
| 430 | * @param string $type The metadata type. |
||
| 431 | * @param bool $refresh Whether to fetch the latest available table metadata. If this is `false`, cached data may be |
||
| 432 | * returned if available. |
||
| 433 | * |
||
| 434 | * @throws InvalidArgumentException |
||
| 435 | * @throws NotSupportedException |
||
| 436 | * |
||
| 437 | * @return array The metadata of the given type for all tables in the given schema. |
||
| 438 | * |
||
| 439 | * @psalm-return list<Constraint|TableSchemaInterface|array> |
||
| 440 | */ |
||
| 441 | protected function getSchemaMetadata(string $schema, string $type, bool $refresh): array |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Returns the metadata of the given type for the given table. |
||
| 466 | * |
||
| 467 | * @param string $name The table name. The table name may contain a schema name if any. |
||
| 468 | * Don't quote the table name. |
||
| 469 | * @param string $type The metadata type. |
||
| 470 | * @param bool $refresh whether to reload the table metadata even if it's found in the cache. |
||
| 471 | * |
||
| 472 | * @throws InvalidArgumentException |
||
| 473 | * |
||
| 474 | * @return mixed The metadata of the given type for the given table. |
||
| 475 | */ |
||
| 476 | protected function getTableMetadata(string $name, string $type, bool $refresh = false): mixed |
||
| 477 | { |
||
| 478 | $rawName = $this->getRawTableName($name); |
||
| 479 | |||
| 480 | if (!isset($this->tableMetadata[$rawName])) { |
||
| 481 | $this->loadTableMetadataFromCache($rawName); |
||
| 482 | } |
||
| 483 | |||
| 484 | if ($refresh || !isset($this->tableMetadata[$rawName][$type])) { |
||
| 485 | /** @psalm-suppress MixedArrayAssignment */ |
||
| 486 | $this->tableMetadata[$rawName][$type] = $this->loadTableTypeMetadata($type, $rawName); |
||
| 487 | $this->saveTableMetadataToCache($rawName); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** @psalm-suppress MixedArrayAccess */ |
||
| 491 | return $this->tableMetadata[$rawName][$type]; |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * This method returns the desired metadata type for the table name. |
||
| 496 | */ |
||
| 497 | protected function loadTableTypeMetadata(string $type, string $name): Constraint|array|TableSchemaInterface|null |
||
| 498 | { |
||
| 499 | return match ($type) { |
||
| 500 | SchemaInterface::SCHEMA => $this->loadTableSchema($name), |
||
| 501 | SchemaInterface::PRIMARY_KEY => $this->loadTablePrimaryKey($name), |
||
| 502 | SchemaInterface::UNIQUES => $this->loadTableUniques($name), |
||
| 503 | SchemaInterface::FOREIGN_KEYS => $this->loadTableForeignKeys($name), |
||
| 504 | SchemaInterface::INDEXES => $this->loadTableIndexes($name), |
||
| 505 | SchemaInterface::DEFAULT_VALUES => $this->loadTableDefaultValues($name), |
||
| 506 | SchemaInterface::CHECKS => $this->loadTableChecks($name), |
||
| 507 | default => null, |
||
| 508 | }; |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * This method returns the desired metadata type for table name (with refresh if needed). |
||
| 513 | * |
||
| 514 | * @throws InvalidArgumentException |
||
| 515 | */ |
||
| 516 | protected function getTableTypeMetadata( |
||
| 517 | string $type, |
||
| 518 | string $name, |
||
| 519 | bool $refresh = false |
||
| 520 | ): Constraint|array|null|TableSchemaInterface { |
||
| 521 | return match ($type) { |
||
| 522 | SchemaInterface::SCHEMA => $this->getTableSchema($name, $refresh), |
||
| 523 | SchemaInterface::PRIMARY_KEY => $this->getTablePrimaryKey($name, $refresh), |
||
| 524 | SchemaInterface::UNIQUES => $this->getTableUniques($name, $refresh), |
||
| 525 | SchemaInterface::FOREIGN_KEYS => $this->getTableForeignKeys($name, $refresh), |
||
| 526 | SchemaInterface::INDEXES => $this->getTableIndexes($name, $refresh), |
||
| 527 | SchemaInterface::DEFAULT_VALUES => $this->getTableDefaultValues($name, $refresh), |
||
| 528 | SchemaInterface::CHECKS => $this->getTableChecks($name, $refresh), |
||
| 529 | default => null, |
||
| 530 | }; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Change row's array key case to lower. |
||
| 535 | * |
||
| 536 | * @param array $row Thew row's array or an array of row arrays. |
||
| 537 | * @param bool $multiple Whether many rows or a single row passed. |
||
| 538 | * |
||
| 539 | * @return array The normalized row or rows. |
||
| 540 | */ |
||
| 541 | protected function normalizeRowKeyCase(array $row, bool $multiple): array |
||
| 542 | { |
||
| 543 | if ($multiple) { |
||
| 544 | return array_map(static fn (array $row) => array_change_key_case($row), $row); |
||
| 545 | } |
||
| 546 | |||
| 547 | return array_change_key_case($row); |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Resolves the table name and schema name (if any). |
||
| 552 | * |
||
| 553 | * @param string $name The table name. |
||
| 554 | * |
||
| 555 | * @throws NotSupportedException If the DBMS doesn't support this method. |
||
| 556 | * |
||
| 557 | * @return TableSchemaInterface The with resolved table, schema, etc. names. |
||
| 558 | * |
||
| 559 | * @see TableSchemaInterface |
||
| 560 | */ |
||
| 561 | protected function resolveTableName(string $name): TableSchemaInterface |
||
| 562 | { |
||
| 563 | throw new NotSupportedException(static::class . ' does not support resolving table names.'); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Sets the metadata of the given type for the given table. |
||
| 568 | * |
||
| 569 | * @param string $name The table name. |
||
| 570 | * @param string $type The metadata type. |
||
| 571 | * @param mixed $data The metadata to set. |
||
| 572 | */ |
||
| 573 | protected function setTableMetadata(string $name, string $type, mixed $data): void |
||
| 574 | { |
||
| 575 | /** @psalm-suppress MixedArrayAssignment */ |
||
| 576 | $this->tableMetadata[$this->getRawTableName($name)][$type] = $data; |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Tries to load and populate table metadata from cache. |
||
| 581 | * |
||
| 582 | * @throws InvalidArgumentException |
||
| 583 | */ |
||
| 584 | private function loadTableMetadataFromCache(string $rawName): void |
||
| 585 | { |
||
| 586 | if (!$this->schemaCache->isEnabled() || $this->schemaCache->isExcluded($rawName)) { |
||
| 587 | $this->tableMetadata[$rawName] = []; |
||
| 588 | return; |
||
| 589 | } |
||
| 590 | |||
| 591 | $metadata = $this->schemaCache->get($this->getCacheKey($rawName)); |
||
| 592 | |||
| 593 | if ( |
||
| 594 | !is_array($metadata) || |
||
| 595 | !isset($metadata[self::CACHE_VERSION]) || |
||
| 596 | $metadata[self::CACHE_VERSION] !== static::SCHEMA_CACHE_VERSION |
||
| 597 | ) { |
||
| 598 | $this->tableMetadata[$rawName] = []; |
||
| 599 | return; |
||
| 600 | } |
||
| 601 | |||
| 602 | unset($metadata[self::CACHE_VERSION]); |
||
| 603 | $this->tableMetadata[$rawName] = $metadata; |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Saves table metadata to cache. |
||
| 608 | * |
||
| 609 | * @throws InvalidArgumentException |
||
| 610 | */ |
||
| 611 | private function saveTableMetadataToCache(string $rawName): void |
||
| 612 | { |
||
| 613 | if ($this->schemaCache->isEnabled() === false || $this->schemaCache->isExcluded($rawName) === true) { |
||
| 614 | return; |
||
| 615 | } |
||
| 616 | |||
| 617 | /** @psalm-var array<string, array<TableSchemaInterface|int>> $metadata */ |
||
| 618 | $metadata = $this->tableMetadata[$rawName]; |
||
| 619 | /** @psalm-var int */ |
||
| 620 | $metadata[self::CACHE_VERSION] = static::SCHEMA_CACHE_VERSION; |
||
| 621 | |||
| 622 | $this->schemaCache->set($this->getCacheKey($rawName), $metadata, $this->getCacheTag()); |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Find the view names for the database. |
||
| 627 | * |
||
| 628 | * @param string $schema The schema of the views. |
||
| 629 | * Defaults to empty string, meaning the current or default schema. |
||
| 630 | * |
||
| 631 | * @return array The names of all views in the database. |
||
| 632 | */ |
||
| 633 | protected function findViewNames(string $schema = ''): array |
||
| 636 | } |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @throws Throwable |
||
| 640 | * |
||
| 641 | * @return array The view names for the database. |
||
| 642 | */ |
||
| 643 | public function getViewNames(string $schema = '', bool $refresh = false): array |
||
| 650 | } |
||
| 651 | } |
||
| 652 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.