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