| Total Complexity | 90 |
| Total Lines | 743 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SchemaPDOSqlite 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 SchemaPDOSqlite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 89 | final class SchemaPDOSqlite extends Schema |
||
| 90 | { |
||
| 91 | /** |
||
| 92 | * @var array mapping from physical column types (keys) to abstract column types (values) |
||
| 93 | * |
||
| 94 | * @psalm-var array<array-key, string> $typeMap |
||
| 95 | */ |
||
| 96 | private array $typeMap = [ |
||
| 97 | 'tinyint' => self::TYPE_TINYINT, |
||
| 98 | 'bit' => self::TYPE_SMALLINT, |
||
| 99 | 'boolean' => self::TYPE_BOOLEAN, |
||
| 100 | 'bool' => self::TYPE_BOOLEAN, |
||
| 101 | 'smallint' => self::TYPE_SMALLINT, |
||
| 102 | 'mediumint' => self::TYPE_INTEGER, |
||
| 103 | 'int' => self::TYPE_INTEGER, |
||
| 104 | 'integer' => self::TYPE_INTEGER, |
||
| 105 | 'bigint' => self::TYPE_BIGINT, |
||
| 106 | 'float' => self::TYPE_FLOAT, |
||
| 107 | 'double' => self::TYPE_DOUBLE, |
||
| 108 | 'real' => self::TYPE_FLOAT, |
||
| 109 | 'decimal' => self::TYPE_DECIMAL, |
||
| 110 | 'numeric' => self::TYPE_DECIMAL, |
||
| 111 | 'tinytext' => self::TYPE_TEXT, |
||
| 112 | 'mediumtext' => self::TYPE_TEXT, |
||
| 113 | 'longtext' => self::TYPE_TEXT, |
||
| 114 | 'text' => self::TYPE_TEXT, |
||
| 115 | 'varchar' => self::TYPE_STRING, |
||
| 116 | 'string' => self::TYPE_STRING, |
||
| 117 | 'char' => self::TYPE_CHAR, |
||
| 118 | 'blob' => self::TYPE_BINARY, |
||
| 119 | 'datetime' => self::TYPE_DATETIME, |
||
| 120 | 'year' => self::TYPE_DATE, |
||
| 121 | 'date' => self::TYPE_DATE, |
||
| 122 | 'time' => self::TYPE_TIME, |
||
| 123 | 'timestamp' => self::TYPE_TIMESTAMP, |
||
| 124 | 'enum' => self::TYPE_STRING, |
||
| 125 | ]; |
||
| 126 | |||
| 127 | public function __construct(private ConnectionPDOInterface $db, SchemaCache $schemaCache) |
||
| 128 | { |
||
| 129 | parent::__construct($schemaCache); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns all table names in the database. |
||
| 134 | * |
||
| 135 | * This method should be overridden by child classes in order to support this feature because the default |
||
| 136 | * implementation simply throws an exception. |
||
| 137 | * |
||
| 138 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
| 139 | * |
||
| 140 | * @throws Exception|InvalidConfigException|Throwable |
||
| 141 | * |
||
| 142 | * @return array all table names in the database. The names have NO schema name prefix. |
||
| 143 | */ |
||
| 144 | protected function findTableNames(string $schema = ''): array |
||
| 145 | { |
||
| 146 | return $this->db->createCommand( |
||
| 147 | "SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence' ORDER BY tbl_name" |
||
| 148 | )->queryColumn(); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function insert(string $table, array $columns): bool|array |
||
| 152 | { |
||
| 153 | $command = $this->db->createCommand()->insert($table, $columns); |
||
| 154 | $tablePrimaryKey = []; |
||
| 155 | |||
| 156 | if (!$command->execute()) { |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | |||
| 160 | $tableSchema = $this->getTableSchema($table); |
||
| 161 | $result = []; |
||
| 162 | |||
| 163 | if ($tableSchema !== null) { |
||
| 164 | $tablePrimaryKey = $tableSchema->getPrimaryKey(); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** @var string $name */ |
||
| 168 | foreach ($tablePrimaryKey as $name) { |
||
| 169 | if ($tableSchema?->getColumn($name)?->isAutoIncrement()) { |
||
| 170 | $result[$name] = $this->getLastInsertID((string) $tableSchema?->getSequenceName()); |
||
| 171 | break; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** @var mixed */ |
||
| 175 | $result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue(); |
||
| 176 | } |
||
| 177 | |||
| 178 | return $result; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Loads the metadata for the specified table. |
||
| 183 | * |
||
| 184 | * @param string $name table name. |
||
| 185 | * |
||
| 186 | * @throws Exception|InvalidArgumentException|InvalidConfigException|Throwable |
||
| 187 | * |
||
| 188 | * @return TableSchema|null DBMS-dependent table metadata, `null` if the table does not exist. |
||
| 189 | */ |
||
| 190 | protected function loadTableSchema(string $name): ?TableSchema |
||
| 191 | { |
||
| 192 | $table = new TableSchema(); |
||
| 193 | |||
| 194 | $table->name($name); |
||
| 195 | $table->fullName($name); |
||
| 196 | |||
| 197 | if ($this->findColumns($table)) { |
||
| 198 | $this->findConstraints($table); |
||
| 199 | |||
| 200 | return $table; |
||
| 201 | } |
||
| 202 | |||
| 203 | return null; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Loads a primary key for the given table. |
||
| 208 | * |
||
| 209 | * @param string $tableName table name. |
||
| 210 | * |
||
| 211 | * @throws Exception|InvalidArgumentException|InvalidConfigException|Throwable |
||
| 212 | * |
||
| 213 | * @return Constraint|null primary key for the given table, `null` if the table has no primary key. |
||
| 214 | */ |
||
| 215 | protected function loadTablePrimaryKey(string $tableName): ?Constraint |
||
| 216 | { |
||
| 217 | $tablePrimaryKey = $this->loadTableConstraints($tableName, 'primaryKey'); |
||
| 218 | |||
| 219 | return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Loads all foreign keys for the given table. |
||
| 224 | * |
||
| 225 | * @param string $tableName table name. |
||
| 226 | * |
||
| 227 | * @throws Exception|InvalidConfigException|Throwable |
||
| 228 | * |
||
| 229 | * @return ForeignKeyConstraint[] foreign keys for the given table. |
||
| 230 | */ |
||
| 231 | protected function loadTableForeignKeys(string $tableName): array |
||
| 232 | { |
||
| 233 | $result = []; |
||
| 234 | /** @psalm-var PragmaForeignKeyList */ |
||
| 235 | $foreignKeysList = $this->getPragmaForeignKeyList($tableName); |
||
| 236 | /** @psalm-var NormalizePragmaForeignKeyList */ |
||
| 237 | $foreignKeysList = $this->normalizePdoRowKeyCase($foreignKeysList, true); |
||
| 238 | /** @psalm-var NormalizePragmaForeignKeyList */ |
||
| 239 | $foreignKeysList = ArrayHelper::index($foreignKeysList, null, 'table'); |
||
| 240 | ArraySorter::multisort($foreignKeysList, 'seq', SORT_ASC, SORT_NUMERIC); |
||
| 241 | |||
| 242 | /** @psalm-var NormalizePragmaForeignKeyList $foreignKeysList */ |
||
| 243 | foreach ($foreignKeysList as $table => $foreignKey) { |
||
| 244 | $fk = (new ForeignKeyConstraint()) |
||
| 245 | ->columnNames(ArrayHelper::getColumn($foreignKey, 'from')) |
||
| 246 | ->foreignTableName($table) |
||
| 247 | ->foreignColumnNames(ArrayHelper::getColumn($foreignKey, 'to')) |
||
| 248 | ->onDelete($foreignKey[0]['on_delete'] ?? null) |
||
| 249 | ->onUpdate($foreignKey[0]['on_update'] ?? null); |
||
| 250 | |||
| 251 | $result[] = $fk; |
||
| 252 | } |
||
| 253 | |||
| 254 | return $result; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Loads all indexes for the given table. |
||
| 259 | * |
||
| 260 | * @param string $tableName table name. |
||
| 261 | * |
||
| 262 | * @throws Exception|InvalidArgumentException|InvalidConfigException|Throwable |
||
| 263 | * |
||
| 264 | * @return array|IndexConstraint[] indexes for the given table. |
||
| 265 | */ |
||
| 266 | protected function loadTableIndexes(string $tableName): array |
||
| 267 | { |
||
| 268 | $tableIndexes = $this->loadTableConstraints($tableName, 'indexes'); |
||
| 269 | |||
| 270 | return is_array($tableIndexes) ? $tableIndexes : []; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Loads all unique constraints for the given table. |
||
| 275 | * |
||
| 276 | * @param string $tableName table name. |
||
| 277 | * |
||
| 278 | * @throws Exception|InvalidArgumentException|InvalidConfigException|Throwable |
||
| 279 | * |
||
| 280 | * @return array|Constraint[] unique constraints for the given table. |
||
| 281 | */ |
||
| 282 | protected function loadTableUniques(string $tableName): array |
||
| 283 | { |
||
| 284 | $tableUniques = $this->loadTableConstraints($tableName, 'uniques'); |
||
| 285 | |||
| 286 | return is_array($tableUniques) ? $tableUniques : []; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Loads all check constraints for the given table. |
||
| 291 | * |
||
| 292 | * @param string $tableName table name. |
||
| 293 | * |
||
| 294 | * @throws Exception|InvalidArgumentException|InvalidConfigException|Throwable |
||
| 295 | * |
||
| 296 | * @return CheckConstraint[] check constraints for the given table. |
||
| 297 | */ |
||
| 298 | protected function loadTableChecks(string $tableName): array |
||
| 299 | { |
||
| 300 | $sql = $this->db->createCommand( |
||
| 301 | 'SELECT `sql` FROM `sqlite_master` WHERE name = :tableName', |
||
| 302 | [':tableName' => $tableName], |
||
| 303 | )->queryScalar(); |
||
| 304 | |||
| 305 | $sql = ($sql === false || $sql === null) ? '' : (string) $sql; |
||
| 306 | |||
| 307 | /** @var SqlToken[]|SqlToken[][]|SqlToken[][][] $code */ |
||
| 308 | $code = (new SqlTokenizer($sql))->tokenize(); |
||
| 309 | $pattern = (new SqlTokenizer('any CREATE any TABLE any()'))->tokenize(); |
||
| 310 | $result = []; |
||
| 311 | |||
| 312 | if ($code[0] instanceof SqlToken && $code[0]->matches($pattern, 0, $firstMatchIndex, $lastMatchIndex)) { |
||
| 313 | $offset = 0; |
||
| 314 | $createTableToken = $code[0][(int) $lastMatchIndex - 1]; |
||
| 315 | $sqlTokenizerAnyCheck = new SqlTokenizer('any CHECK()'); |
||
| 316 | |||
| 317 | while ( |
||
| 318 | $createTableToken instanceof SqlToken && |
||
| 319 | $createTableToken->matches($sqlTokenizerAnyCheck->tokenize(), (int) $offset, $firstMatchIndex, $offset) |
||
| 320 | ) { |
||
| 321 | $name = null; |
||
| 322 | $checkSql = (string) $createTableToken[(int) $offset - 1]; |
||
| 323 | $pattern = (new SqlTokenizer('CONSTRAINT any'))->tokenize(); |
||
| 324 | |||
| 325 | if ( |
||
| 326 | isset($createTableToken[(int) $firstMatchIndex - 2]) |
||
| 327 | && $createTableToken->matches($pattern, (int) $firstMatchIndex - 2) |
||
| 328 | ) { |
||
| 329 | $sqlToken = $createTableToken[(int) $firstMatchIndex - 1]; |
||
| 330 | $name = $sqlToken?->getContent(); |
||
| 331 | } |
||
| 332 | |||
| 333 | $result[] = (new CheckConstraint())->name($name)->expression($checkSql); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | return $result; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Loads all default value constraints for the given table. |
||
| 342 | * |
||
| 343 | * @param string $tableName table name. |
||
| 344 | * |
||
| 345 | * @throws NotSupportedException |
||
| 346 | * |
||
| 347 | * @return array default value constraints for the given table. |
||
| 348 | */ |
||
| 349 | protected function loadTableDefaultValues(string $tableName): array |
||
| 350 | { |
||
| 351 | throw new NotSupportedException('SQLite does not support default value constraints.'); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Create a column schema builder instance giving the type and value precision. |
||
| 356 | * |
||
| 357 | * This method may be overridden by child classes to create a DBMS-specific column schema builder. |
||
| 358 | * |
||
| 359 | * @param string $type type of the column. See {@see ColumnSchemaBuilder::$type}. |
||
| 360 | * @param array|int|string|null $length length or precision of the column. See {@see ColumnSchemaBuilder::$length}. |
||
| 361 | * |
||
| 362 | * @return ColumnSchemaBuilder column schema builder instance. |
||
| 363 | */ |
||
| 364 | public function createColumnSchemaBuilder(string $type, $length = null): ColumnSchemaBuilder |
||
| 365 | { |
||
| 366 | return new ColumnSchemaBuilder($type, $length); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Collects the table column metadata. |
||
| 371 | * |
||
| 372 | * @param TableSchema $table the table metadata. |
||
| 373 | * |
||
| 374 | * @throws Exception|InvalidConfigException|Throwable |
||
| 375 | * |
||
| 376 | * @return bool whether the table exists in the database. |
||
| 377 | */ |
||
| 378 | protected function findColumns(TableSchema $table): bool |
||
| 379 | { |
||
| 380 | /** @psalm-var PragmaTableInfo */ |
||
| 381 | $columns = $this->getPragmaTableInfo($table->getName()); |
||
| 382 | |||
| 383 | foreach ($columns as $info) { |
||
| 384 | $column = $this->loadColumnSchema($info); |
||
| 385 | $table->columns($column->getName(), $column); |
||
| 386 | |||
| 387 | if ($column->isPrimaryKey()) { |
||
| 388 | $table->primaryKey($column->getName()); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | $column = count($table->getPrimaryKey()) === 1 ? $table->getColumn((string) $table->getPrimaryKey()[0]) : null; |
||
| 393 | |||
| 394 | if ($column !== null && !strncasecmp($column->getDbType(), 'int', 3)) { |
||
| 395 | $table->sequenceName(''); |
||
| 396 | $column->autoIncrement(true); |
||
| 397 | } |
||
| 398 | |||
| 399 | return !empty($columns); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Collects the foreign key column details for the given table. |
||
| 404 | * |
||
| 405 | * @param TableSchema $table the table metadata. |
||
| 406 | * |
||
| 407 | * @throws Exception|InvalidConfigException|Throwable |
||
| 408 | */ |
||
| 409 | protected function findConstraints(TableSchema $table): void |
||
| 410 | { |
||
| 411 | /** @psalm-var PragmaForeignKeyList */ |
||
| 412 | $foreignKeysList = $this->getPragmaForeignKeyList($table->getName()); |
||
| 413 | |||
| 414 | foreach ($foreignKeysList as $foreignKey) { |
||
| 415 | $id = (int) $foreignKey['id']; |
||
| 416 | $fk = $table->getForeignKeys(); |
||
| 417 | |||
| 418 | if (!isset($fk[$id])) { |
||
| 419 | $table->foreignKey($id, ([$foreignKey['table'], $foreignKey['from'] => $foreignKey['to']])); |
||
| 420 | } else { |
||
| 421 | /** composite FK */ |
||
| 422 | $table->compositeFK($id, $foreignKey['from'], $foreignKey['to']); |
||
| 423 | } |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Returns all unique indexes for the given table. |
||
| 429 | * |
||
| 430 | * Each array element is of the following structure: |
||
| 431 | * |
||
| 432 | * ```php |
||
| 433 | * [ |
||
| 434 | * 'IndexName1' => ['col1' [, ...]], |
||
| 435 | * 'IndexName2' => ['col2' [, ...]], |
||
| 436 | * ] |
||
| 437 | * ``` |
||
| 438 | * |
||
| 439 | * @param TableSchema $table the table metadata. |
||
| 440 | * |
||
| 441 | * @throws Exception|InvalidConfigException|Throwable |
||
| 442 | * |
||
| 443 | * @return array all unique indexes for the given table. |
||
| 444 | */ |
||
| 445 | public function findUniqueIndexes(TableSchema $table): array |
||
| 446 | { |
||
| 447 | /** @psalm-var PragmaIndexList */ |
||
| 448 | $indexList = $this->getPragmaIndexList($table->getName()); |
||
| 449 | $uniqueIndexes = []; |
||
| 450 | |||
| 451 | foreach ($indexList as $index) { |
||
| 452 | $indexName = $index['name']; |
||
| 453 | /** @psalm-var PragmaIndexInfo */ |
||
| 454 | $indexInfo = $this->getPragmaIndexInfo($index['name']); |
||
| 455 | |||
| 456 | if ($index['unique']) { |
||
| 457 | $uniqueIndexes[$indexName] = []; |
||
| 458 | foreach ($indexInfo as $row) { |
||
| 459 | $uniqueIndexes[$indexName][] = $row['name']; |
||
| 460 | } |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | return $uniqueIndexes; |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Loads the column information into a {@see ColumnSchema} object. |
||
| 469 | * |
||
| 470 | * @param array $info column information. |
||
| 471 | * |
||
| 472 | * @return ColumnSchema the column schema object. |
||
| 473 | * |
||
| 474 | * @psalm-param array{cid:string, name:string, type:string, notnull:string, dflt_value:string|null, pk:string} $info |
||
| 475 | */ |
||
| 476 | protected function loadColumnSchema(array $info): ColumnSchema |
||
| 477 | { |
||
| 478 | $column = $this->createColumnSchema(); |
||
| 479 | $column->name($info['name']); |
||
| 480 | $column->allowNull(!$info['notnull']); |
||
| 481 | $column->primaryKey($info['pk'] !== '0'); |
||
| 482 | $column->dbType(strtolower($info['type'])); |
||
| 483 | $column->unsigned(str_contains($column->getDbType(), 'unsigned')); |
||
| 484 | $column->type(self::TYPE_STRING); |
||
| 485 | |||
| 486 | if (preg_match('/^(\w+)(?:\(([^)]+)\))?/', $column->getDbType(), $matches)) { |
||
| 487 | $type = strtolower($matches[1]); |
||
| 488 | |||
| 489 | if (isset($this->typeMap[$type])) { |
||
| 490 | $column->type($this->typeMap[$type]); |
||
| 491 | } |
||
| 492 | |||
| 493 | if (!empty($matches[2])) { |
||
| 494 | $values = explode(',', $matches[2]); |
||
| 495 | $column->precision((int) $values[0]); |
||
| 496 | $column->size((int) $values[0]); |
||
| 497 | |||
| 498 | if (isset($values[1])) { |
||
| 499 | $column->scale((int) $values[1]); |
||
| 500 | } |
||
| 501 | |||
| 502 | if ($column->getSize() === 1 && ($type === 'tinyint' || $type === 'bit')) { |
||
| 503 | $column->type('boolean'); |
||
| 504 | } elseif ($type === 'bit') { |
||
| 505 | if ($column->getSize() > 32) { |
||
| 506 | $column->type('bigint'); |
||
| 507 | } elseif ($column->getSize() === 32) { |
||
| 508 | $column->type('integer'); |
||
| 509 | } |
||
| 510 | } |
||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 514 | $column->phpType($this->getColumnPhpType($column)); |
||
| 515 | |||
| 516 | if (!$column->isPrimaryKey()) { |
||
| 517 | if ($info['dflt_value'] === 'null' || $info['dflt_value'] === '' || $info['dflt_value'] === null) { |
||
| 518 | $column->defaultValue(null); |
||
| 519 | } elseif ($column->getType() === 'timestamp' && $info['dflt_value'] === 'CURRENT_TIMESTAMP') { |
||
| 520 | $column->defaultValue(new Expression('CURRENT_TIMESTAMP')); |
||
| 521 | } else { |
||
| 522 | $value = trim($info['dflt_value'], "'\""); |
||
| 523 | $column->defaultValue($column->phpTypecast($value)); |
||
| 524 | } |
||
| 525 | } |
||
| 526 | |||
| 527 | return $column; |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Sets the isolation level of the current transaction. |
||
| 532 | * |
||
| 533 | * @param string $level The transaction isolation level to use for this transaction. This can be either |
||
| 534 | * {@see TransactionPDOSqlite::READ_UNCOMMITTED} or {@see TransactionPDOSqlite::SERIALIZABLE}. |
||
| 535 | * |
||
| 536 | * @throws Exception|InvalidConfigException|NotSupportedException|Throwable when unsupported isolation levels are |
||
| 537 | * used. SQLite only supports SERIALIZABLE and READ UNCOMMITTED. |
||
| 538 | * |
||
| 539 | * {@see http://www.sqlite.org/pragma.html#pragma_read_uncommitted} |
||
| 540 | */ |
||
| 541 | public function setTransactionIsolationLevel(string $level): void |
||
| 542 | { |
||
| 543 | switch ($level) { |
||
| 544 | case TransactionPDOSqlite::SERIALIZABLE: |
||
| 545 | $this->db->createCommand('PRAGMA read_uncommitted = False;')->execute(); |
||
| 546 | break; |
||
| 547 | case TransactionPDOSqlite::READ_UNCOMMITTED: |
||
| 548 | $this->db->createCommand('PRAGMA read_uncommitted = True;')->execute(); |
||
| 549 | break; |
||
| 550 | default: |
||
| 551 | throw new NotSupportedException( |
||
| 552 | self::class . ' only supports transaction isolation levels READ UNCOMMITTED and SERIALIZABLE.' |
||
| 553 | ); |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Returns table columns info. |
||
| 559 | * |
||
| 560 | * @param string $tableName table name. |
||
| 561 | * |
||
| 562 | * @throws Exception|InvalidConfigException|Throwable |
||
| 563 | * |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | private function loadTableColumnsInfo(string $tableName): array |
||
| 567 | { |
||
| 568 | $tableColumns = $this->getPragmaTableInfo($tableName); |
||
| 569 | /** @psalm-var PragmaTableInfo */ |
||
| 570 | $tableColumns = $this->normalizePdoRowKeyCase($tableColumns, true); |
||
| 571 | |||
| 572 | return ArrayHelper::index($tableColumns, 'cid'); |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Loads multiple types of constraints and returns the specified ones. |
||
| 577 | * |
||
| 578 | * @param string $tableName table name. |
||
| 579 | * @param string $returnType return type: (primaryKey, indexes, uniques). |
||
| 580 | * |
||
| 581 | * @throws Exception|InvalidConfigException|Throwable |
||
| 582 | * |
||
| 583 | * @return (Constraint|IndexConstraint)[]|Constraint|null |
||
| 584 | */ |
||
| 585 | private function loadTableConstraints(string $tableName, string $returnType) |
||
| 586 | { |
||
| 587 | $indexList = $this->getPragmaIndexList($tableName); |
||
| 588 | /** @psalm-var PragmaIndexList $indexes */ |
||
| 589 | $indexes = $this->normalizePdoRowKeyCase($indexList, true); |
||
| 590 | $result = ['primaryKey' => null, 'indexes' => [], 'uniques' => []]; |
||
| 591 | |||
| 592 | foreach ($indexes as $index) { |
||
| 593 | /** @psalm-var Column $columns */ |
||
| 594 | $columns = $this->getPragmaIndexInfo($index['name']); |
||
| 595 | |||
| 596 | $result['indexes'][] = (new IndexConstraint()) |
||
| 597 | ->primary($index['origin'] === 'pk') |
||
| 598 | ->unique((bool) $index['unique']) |
||
| 599 | ->name($index['name']) |
||
| 600 | ->columnNames(ArrayHelper::getColumn($columns, 'name')); |
||
| 601 | |||
| 602 | if ($index['origin'] === 'u') { |
||
| 603 | $result['uniques'][] = (new Constraint()) |
||
| 604 | ->name($index['name']) |
||
| 605 | ->columnNames(ArrayHelper::getColumn($columns, 'name')); |
||
| 606 | } |
||
| 607 | |||
| 608 | if ($index['origin'] === 'pk') { |
||
| 609 | $result['primaryKey'] = (new Constraint()) |
||
| 610 | ->columnNames(ArrayHelper::getColumn($columns, 'name')); |
||
| 611 | } |
||
| 612 | } |
||
| 613 | |||
| 614 | if (!isset($result['primaryKey'])) { |
||
| 615 | /** |
||
| 616 | * Additional check for PK in case of INTEGER PRIMARY KEY with ROWID. |
||
| 617 | * |
||
| 618 | * {@See https://www.sqlite.org/lang_createtable.html#primkeyconst} |
||
| 619 | * |
||
| 620 | * @psalm-var PragmaTableInfo |
||
| 621 | */ |
||
| 622 | $tableColumns = $this->loadTableColumnsInfo($tableName); |
||
| 623 | |||
| 624 | foreach ($tableColumns as $tableColumn) { |
||
| 625 | if ($tableColumn['pk'] > 0) { |
||
| 626 | $result['primaryKey'] = (new Constraint())->columnNames([$tableColumn['name']]); |
||
| 627 | break; |
||
| 628 | } |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | foreach ($result as $type => $data) { |
||
| 633 | $this->setTableMetadata($tableName, $type, $data); |
||
| 634 | } |
||
| 635 | |||
| 636 | return $result[$returnType]; |
||
| 637 | } |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Creates a column schema for the database. |
||
| 641 | * |
||
| 642 | * This method may be overridden by child classes to create a DBMS-specific column schema. |
||
| 643 | * |
||
| 644 | * @return ColumnSchema column schema instance. |
||
| 645 | */ |
||
| 646 | private function createColumnSchema(): ColumnSchema |
||
| 647 | { |
||
| 648 | return new ColumnSchema(); |
||
| 649 | } |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @throws Exception|InvalidConfigException|Throwable |
||
| 653 | */ |
||
| 654 | private function getPragmaForeignKeyList(string $tableName): array |
||
| 655 | { |
||
| 656 | return $this->db->createCommand( |
||
| 657 | 'PRAGMA FOREIGN_KEY_LIST(' . $this->db->getQuoter()->quoteSimpleTableName(($tableName)) . ')' |
||
| 658 | )->queryAll(); |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @throws Exception|InvalidConfigException|Throwable |
||
| 663 | */ |
||
| 664 | private function getPragmaIndexInfo(string $name): array |
||
| 665 | { |
||
| 666 | $column = $this->db |
||
| 667 | ->createCommand('PRAGMA INDEX_INFO(' . $this->db->getQuoter()->quoteValue($name) . ')')->queryAll(); |
||
| 668 | /** @psalm-var Column */ |
||
| 669 | $column = $this->normalizePdoRowKeyCase($column, true); |
||
| 670 | ArraySorter::multisort($column, 'seqno', SORT_ASC, SORT_NUMERIC); |
||
| 671 | |||
| 672 | return $column; |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @throws Exception|InvalidConfigException|Throwable |
||
| 677 | */ |
||
| 678 | private function getPragmaIndexList(string $tableName): array |
||
| 679 | { |
||
| 680 | return $this->db |
||
| 681 | ->createCommand('PRAGMA INDEX_LIST(' . $this->db->getQuoter()->quoteValue($tableName) . ')')->queryAll(); |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @throws Exception|InvalidConfigException|Throwable |
||
| 686 | */ |
||
| 687 | private function getPragmaTableInfo(string $tableName): array |
||
| 688 | { |
||
| 689 | return $this->db->createCommand( |
||
| 690 | 'PRAGMA TABLE_INFO(' . $this->db->getQuoter()->quoteSimpleTableName($tableName) . ')' |
||
| 691 | )->queryAll(); |
||
| 692 | } |
||
| 693 | |||
| 694 | public function rollBackSavepoint(string $name): void |
||
| 695 | { |
||
| 696 | $this->db->createCommand("ROLLBACK TO SAVEPOINT $name")->execute(); |
||
| 697 | } |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Returns the actual name of a given table name. |
||
| 701 | * |
||
| 702 | * This method will strip off curly brackets from the given table name and replace the percentage character '%' with |
||
| 703 | * {@see ConnectionInterface::tablePrefix}. |
||
| 704 | * |
||
| 705 | * @param string $name the table name to be converted. |
||
| 706 | * |
||
| 707 | * @return string the real name of the given table name. |
||
| 708 | */ |
||
| 709 | public function getRawTableName(string $name): string |
||
| 710 | { |
||
| 711 | if (str_contains($name, '{{')) { |
||
| 712 | $name = preg_replace('/{{(.*?)}}/', '\1', $name); |
||
| 713 | |||
| 714 | return str_replace('%', $this->db->getTablePrefix(), $name); |
||
| 715 | } |
||
| 716 | |||
| 717 | return $name; |
||
| 718 | } |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Returns the cache key for the specified table name. |
||
| 722 | * |
||
| 723 | * @param string $name the table name. |
||
| 724 | * |
||
| 725 | * @return array the cache key. |
||
| 726 | */ |
||
| 727 | protected function getCacheKey(string $name): array |
||
| 728 | { |
||
| 729 | return [ |
||
| 730 | __CLASS__, |
||
| 731 | $this->db->getDriver()->getDsn(), |
||
| 732 | $this->db->getDriver()->getUsername(), |
||
| 733 | $this->getRawTableName($name), |
||
| 734 | ]; |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Returns the cache tag name. |
||
| 739 | * |
||
| 740 | * This allows {@see refresh()} to invalidate all cached table schemas. |
||
| 741 | * |
||
| 742 | * @return string the cache tag name. |
||
| 743 | */ |
||
| 744 | protected function getCacheTag(): string |
||
| 745 | { |
||
| 746 | return md5(serialize([ |
||
| 747 | __CLASS__, |
||
| 748 | $this->db->getDriver()->getDsn(), |
||
| 749 | $this->db->getDriver()->getUsername(), |
||
| 750 | ])); |
||
| 751 | } |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Changes row's array key case to lower if PDO one is set to uppercase. |
||
| 755 | * |
||
| 756 | * @param array $row row's array or an array of row's arrays. |
||
| 757 | * @param bool $multiple whether multiple rows or a single row passed. |
||
| 758 | * |
||
| 759 | * @throws Exception |
||
| 760 | * |
||
| 761 | * @return array normalized row or rows. |
||
| 762 | */ |
||
| 763 | protected function normalizePdoRowKeyCase(array $row, bool $multiple): array |
||
| 776 | } |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @return bool whether this DBMS supports [savepoint](http://en.wikipedia.org/wiki/Savepoint). |
||
| 780 | */ |
||
| 781 | public function supportsSavepoint(): bool |
||
| 782 | { |
||
| 783 | return $this->db->isSavepointEnabled(); |
||
| 784 | } |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Creates a new savepoint. |
||
| 788 | * |
||
| 789 | * @param string $name the savepoint name |
||
| 790 | * |
||
| 791 | * @throws Exception|InvalidConfigException|Throwable |
||
| 792 | */ |
||
| 793 | public function createSavepoint(string $name): void |
||
| 794 | { |
||
| 795 | $this->db->createCommand("SAVEPOINT $name")->execute(); |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Returns the ID of the last inserted row or sequence value. |
||
| 800 | * |
||
| 801 | * @param string $sequenceName name of the sequence object (required by some DBMS) |
||
| 802 | * |
||
| 803 | * @throws InvalidCallException if the DB connection is not active |
||
| 804 | * |
||
| 805 | * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
||
| 806 | * |
||
| 807 | * @see http://www.php.net/manual/en/function.PDO-lastInsertId.php |
||
| 808 | */ |
||
| 809 | public function getLastInsertID(string $sequenceName = ''): string |
||
| 820 | } |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Releases an existing savepoint. |
||
| 824 | * |
||
| 825 | * @param string $name the savepoint name |
||
| 826 | * |
||
| 827 | * @throws Exception|InvalidConfigException|Throwable |
||
| 828 | */ |
||
| 829 | public function releaseSavepoint(string $name): void |
||
| 830 | { |
||
| 831 | $this->db->createCommand("RELEASE SAVEPOINT $name")->execute(); |
||
| 832 | } |
||
| 833 | } |
||
| 834 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths