Complex classes like MysqlAdapter 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 MysqlAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class MysqlAdapter extends PdoAdapter |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var string[] |
||
| 31 | */ |
||
| 32 | protected static $specificColumnTypes = [ |
||
| 33 | self::PHINX_TYPE_ENUM, |
||
| 34 | self::PHINX_TYPE_SET, |
||
| 35 | self::PHINX_TYPE_YEAR, |
||
| 36 | self::PHINX_TYPE_JSON, |
||
| 37 | self::PHINX_TYPE_BINARYUUID, |
||
| 38 | self::PHINX_TYPE_TINYBLOB, |
||
| 39 | self::PHINX_TYPE_MEDIUMBLOB, |
||
| 40 | self::PHINX_TYPE_LONGBLOB, |
||
| 41 | ]; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool[] |
||
| 45 | */ |
||
| 46 | protected $signedColumnTypes = [ |
||
| 47 | self::PHINX_TYPE_INTEGER => true, |
||
| 48 | self::PHINX_TYPE_TINY_INTEGER => true, |
||
| 49 | self::PHINX_TYPE_SMALL_INTEGER => true, |
||
| 50 | self::PHINX_TYPE_BIG_INTEGER => true, |
||
| 51 | self::PHINX_TYPE_FLOAT => true, |
||
| 52 | self::PHINX_TYPE_DECIMAL => true, |
||
| 53 | self::PHINX_TYPE_DOUBLE => true, |
||
| 54 | self::PHINX_TYPE_BOOLEAN => true, |
||
| 55 | ]; |
||
| 56 | |||
| 57 | public const TEXT_TINY = 255; |
||
| 58 | public const TEXT_SMALL = 255; /* deprecated, alias of TEXT_TINY */ |
||
| 59 | public const TEXT_REGULAR = 65535; |
||
| 60 | public const TEXT_MEDIUM = 16777215; |
||
| 61 | public const TEXT_LONG = 4294967295; |
||
| 62 | |||
| 63 | // According to https://dev.mysql.com/doc/refman/5.0/en/blob.html BLOB sizes are the same as TEXT |
||
| 64 | public const BLOB_TINY = 255; |
||
| 65 | public const BLOB_SMALL = 255; /* deprecated, alias of BLOB_TINY */ |
||
| 66 | public const BLOB_REGULAR = 65535; |
||
| 67 | public const BLOB_MEDIUM = 16777215; |
||
| 68 | public const BLOB_LONG = 4294967295; |
||
| 69 | |||
| 70 | 80 | public const INT_TINY = 255; |
|
| 71 | public const INT_SMALL = 65535; |
||
| 72 | 80 | public const INT_MEDIUM = 16777215; |
|
| 73 | 80 | public const INT_REGULAR = 4294967295; |
|
| 74 | public const INT_BIG = 18446744073709551615; |
||
| 75 | |||
| 76 | public const BIT = 64; |
||
| 77 | |||
| 78 | public const TYPE_YEAR = 'year'; |
||
| 79 | 80 | ||
| 80 | 80 | /** |
|
| 81 | * {@inheritDoc} |
||
| 82 | 80 | * |
|
| 83 | * @throws \RuntimeException |
||
| 84 | 80 | * @throws \InvalidArgumentException |
|
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | public function connect() |
||
| 89 | 80 | { |
|
| 90 | 80 | if ($this->connection === null) { |
|
| 91 | 80 | if (!class_exists('PDO') || !in_array('mysql', PDO::getAvailableDrivers(), true)) { |
|
| 92 | 80 | // @codeCoverageIgnoreStart |
|
| 93 | throw new RuntimeException('You need to enable the PDO_Mysql extension for Phinx to run properly.'); |
||
| 94 | // @codeCoverageIgnoreEnd |
||
| 95 | 80 | } |
|
| 96 | |||
| 97 | $options = $this->getOptions(); |
||
| 98 | 80 | ||
| 99 | $dsn = 'mysql:'; |
||
| 100 | |||
| 101 | if (!empty($options['unix_socket'])) { |
||
| 102 | 80 | // use socket connection |
|
| 103 | $dsn .= 'unix_socket=' . $options['unix_socket']; |
||
| 104 | } else { |
||
| 105 | // use network connection |
||
| 106 | 80 | $dsn .= 'host=' . $options['host']; |
|
| 107 | 80 | if (!empty($options['port'])) { |
|
| 108 | $dsn .= ';port=' . $options['port']; |
||
| 109 | } |
||
| 110 | 80 | } |
|
| 111 | |||
| 112 | $dsn .= ';dbname=' . $options['name']; |
||
| 113 | 80 | ||
| 114 | 80 | // charset support |
|
| 115 | 1 | if (!empty($options['charset'])) { |
|
| 116 | 1 | $dsn .= ';charset=' . $options['charset']; |
|
| 117 | 1 | } |
|
| 118 | 1 | ||
| 119 | $driverOptions = []; |
||
| 120 | |||
| 121 | 80 | // use custom data fetch mode |
|
| 122 | 80 | if (!empty($options['fetch_mode'])) { |
|
| 123 | 80 | $driverOptions[PDO::ATTR_DEFAULT_FETCH_MODE] = constant('\PDO::FETCH_' . strtoupper($options['fetch_mode'])); |
|
| 124 | } |
||
| 125 | |||
| 126 | // support arbitrary \PDO::MYSQL_ATTR_* driver options and pass them to PDO |
||
| 127 | // http://php.net/manual/en/ref.pdo-mysql.php#pdo-mysql.constants |
||
| 128 | 81 | foreach ($options as $key => $option) { |
|
| 129 | if (strpos($key, 'mysql_attr_') === 0) { |
||
| 130 | 81 | $pdoConstant = '\PDO::' . strtoupper($key); |
|
| 131 | 81 | if (!defined($pdoConstant)) { |
|
| 132 | throw new \UnexpectedValueException('Invalid PDO attribute: ' . $key . ' (' . $pdoConstant . ')'); |
||
| 133 | } |
||
| 134 | $driverOptions[constant($pdoConstant)] = $option; |
||
| 135 | } |
||
| 136 | 6 | } |
|
| 137 | |||
| 138 | 6 | $db = $this->createPdoConnection($dsn, $options['user'] ?? null, $options['pass'] ?? null, $driverOptions); |
|
| 139 | |||
| 140 | $this->setConnection($db); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | 6 | /** |
|
| 145 | * @inheritDoc |
||
| 146 | 6 | */ |
|
| 147 | 6 | public function disconnect() |
|
| 148 | { |
||
| 149 | $this->connection = null; |
||
| 150 | } |
||
| 151 | |||
| 152 | 6 | /** |
|
| 153 | * @inheritDoc |
||
| 154 | 6 | */ |
|
| 155 | 6 | public function hasTransactions() |
|
| 156 | { |
||
| 157 | return true; |
||
| 158 | } |
||
| 159 | |||
| 160 | 1 | /** |
|
| 161 | * @inheritDoc |
||
| 162 | 1 | */ |
|
| 163 | 1 | public function beginTransaction() |
|
| 164 | { |
||
| 165 | $this->execute('START TRANSACTION'); |
||
| 166 | } |
||
| 167 | |||
| 168 | 112 | /** |
|
| 169 | * @inheritDoc |
||
| 170 | 112 | */ |
|
| 171 | public function commitTransaction() |
||
| 172 | { |
||
| 173 | $this->execute('COMMIT'); |
||
| 174 | } |
||
| 175 | |||
| 176 | 112 | /** |
|
| 177 | * @inheritDoc |
||
| 178 | 112 | */ |
|
| 179 | public function rollbackTransaction() |
||
| 180 | { |
||
| 181 | $this->execute('ROLLBACK'); |
||
| 182 | } |
||
| 183 | |||
| 184 | 82 | /** |
|
| 185 | * @inheritDoc |
||
| 186 | 82 | */ |
|
| 187 | public function quoteTableName($tableName) |
||
| 188 | 82 | { |
|
| 189 | return str_replace('.', '`.`', $this->quoteColumnName($tableName)); |
||
| 190 | } |
||
| 191 | 82 | ||
| 192 | 82 | /** |
|
| 193 | * @inheritDoc |
||
| 194 | 82 | */ |
|
| 195 | public function quoteColumnName($columnName) |
||
| 196 | 82 | { |
|
| 197 | return '`' . str_replace('`', '``', $columnName) . '`'; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritDoc |
||
| 202 | 82 | */ |
|
| 203 | public function hasTable($tableName) |
||
| 204 | { |
||
| 205 | if ($this->hasCreatedTable($tableName)) { |
||
| 206 | 82 | return true; |
|
| 207 | } |
||
| 208 | 82 | ||
| 209 | 82 | if (strpos($tableName, '.') !== false) { |
|
| 210 | [$schema, $table] = explode('.', $tableName); |
||
| 211 | $exists = $this->hasTableWithSchema($schema, $table); |
||
| 212 | 82 | // Only break here on success, because it is possible for table names to contain a dot. |
|
| 213 | 82 | if ($exists) { |
|
| 214 | 68 | return true; |
|
| 215 | 68 | } |
|
| 216 | 68 | } |
|
| 217 | 68 | ||
| 218 | 68 | $options = $this->getOptions(); |
|
| 219 | |||
| 220 | 68 | return $this->hasTableWithSchema($options['name'], $tableName); |
|
| 221 | 68 | } |
|
| 222 | 82 | ||
| 223 | /** |
||
| 224 | 2 | * @param string $schema The table schema |
|
| 225 | 2 | * @param string $tableName The table name |
|
| 226 | 2 | * |
|
| 227 | 2 | * @return bool |
|
| 228 | */ |
||
| 229 | 2 | protected function hasTableWithSchema($schema, $tableName) |
|
| 230 | 2 | { |
|
| 231 | 2 | $result = $this->fetchRow(sprintf( |
|
| 232 | "SELECT TABLE_NAME |
||
| 233 | FROM INFORMATION_SCHEMA.TABLES |
||
| 234 | WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", |
||
| 235 | $schema, |
||
| 236 | 82 | $tableName |
|
| 237 | 82 | )); |
|
| 238 | 82 | ||
| 239 | 82 | return !empty($result); |
|
| 240 | } |
||
| 241 | |||
| 242 | 82 | /** |
|
| 243 | 82 | * @inheritDoc |
|
| 244 | 82 | */ |
|
| 245 | 82 | public function createTable(Table $table, array $columns = [], array $indexes = []) |
|
| 337 | 12 | ||
| 338 | 12 | /** |
|
| 339 | 12 | * {@inheritDoc} |
|
| 340 | * |
||
| 341 | 12 | * @throws \InvalidArgumentException |
|
| 342 | 12 | */ |
|
| 343 | 12 | protected function getChangePrimaryKeyInstructions(Table $table, $newColumns) |
|
| 372 | 77 | ||
| 373 | /** |
||
| 374 | 21 | * @inheritDoc |
|
| 375 | */ |
||
| 376 | protected function getChangeCommentInstructions(Table $table, $newComment) |
||
| 389 | 79 | ||
| 390 | 95 | /** |
|
| 391 | * @inheritDoc |
||
| 392 | */ |
||
| 393 | protected function getRenameTableInstructions($tableName, $newTableName) |
||
| 404 | |||
| 405 | 18 | /** |
|
| 406 | 2 | * @inheritDoc |
|
| 407 | 2 | */ |
|
| 408 | protected function getDropTableInstructions($tableName) |
||
| 415 | 7 | ||
| 416 | /** |
||
| 417 | 7 | * @inheritDoc |
|
| 418 | 7 | */ |
|
| 419 | 7 | public function truncateTable($tableName) |
|
| 420 | 5 | { |
|
| 421 | 5 | $sql = sprintf( |
|
| 422 | 5 | 'TRUNCATE TABLE %s', |
|
| 423 | 1 | $this->quoteTableName($tableName) |
|
| 424 | 1 | ); |
|
| 425 | 5 | ||
| 426 | $this->execute($sql); |
||
| 427 | 5 | } |
|
| 428 | 5 | ||
| 429 | 5 | /** |
|
| 430 | 5 | * @inheritDoc |
|
| 431 | 5 | */ |
|
| 432 | 5 | public function getColumns($tableName) |
|
| 433 | { |
||
| 434 | 5 | $columns = []; |
|
| 435 | 5 | $rows = $this->fetchAll(sprintf('SHOW COLUMNS FROM %s', $this->quoteTableName($tableName))); |
|
| 436 | 5 | foreach ($rows as $columnInfo) { |
|
| 437 | $phinxType = $this->getPhinxType($columnInfo['Type']); |
||
| 438 | 6 | ||
| 439 | $column = new Column(); |
||
| 440 | 2 | $column->setName($columnInfo['Field']) |
|
| 441 | ->setNull($columnInfo['Null'] !== 'NO') |
||
| 442 | ->setDefault($columnInfo['Default']) |
||
| 443 | 2 | ->setType($phinxType['name']) |
|
| 444 | ->setSigned(strpos($columnInfo['Type'], 'unsigned') === false) |
||
| 445 | ->setLimit($phinxType['limit']) |
||
| 446 | ->setScale($phinxType['scale']); |
||
| 447 | |||
| 448 | if ($columnInfo['Extra'] === 'auto_increment') { |
||
| 449 | 5 | $column->setIdentity(true); |
|
| 450 | } |
||
| 451 | 5 | ||
| 452 | 5 | if (isset($phinxType['values'])) { |
|
| 453 | 5 | $column->setValues($phinxType['values']); |
|
| 454 | 5 | } |
|
| 455 | 5 | ||
| 456 | 5 | $columns[] = $column; |
|
| 457 | 5 | } |
|
| 458 | 5 | ||
| 459 | return $columns; |
||
| 460 | 5 | } |
|
| 461 | 5 | ||
| 462 | 5 | /** |
|
| 463 | * @inheritDoc |
||
| 464 | */ |
||
| 465 | public function hasColumn($tableName, $columnName) |
||
| 466 | { |
||
| 467 | 5 | $rows = $this->fetchAll(sprintf('SHOW COLUMNS FROM %s', $this->quoteTableName($tableName))); |
|
| 468 | foreach ($rows as $column) { |
||
| 469 | 5 | if (strcasecmp($column['Field'], $columnName) === 0) { |
|
| 470 | 5 | return true; |
|
| 471 | 5 | } |
|
| 472 | 5 | } |
|
| 473 | 5 | ||
| 474 | 5 | return false; |
|
| 475 | 5 | } |
|
| 476 | 5 | ||
| 477 | /** |
||
| 478 | * @inheritDoc |
||
| 479 | */ |
||
| 480 | protected function getAddColumnInstructions(Table $table, Column $column) |
||
| 481 | { |
||
| 482 | $alter = sprintf( |
||
| 483 | 'ADD %s %s', |
||
| 484 | 19 | $this->quoteColumnName($column->getName()), |
|
| 485 | $this->getColumnSqlDefinition($column) |
||
| 486 | 19 | ); |
|
| 487 | 19 | ||
| 488 | 19 | if ($column->getAfter()) { |
|
| 489 | 18 | $alter .= ' AFTER ' . $this->quoteColumnName($column->getAfter()); |
|
| 490 | 18 | } |
|
| 491 | 18 | ||
| 492 | 18 | return new AlterInstructions([$alter]); |
|
| 493 | 19 | } |
|
| 494 | 19 | ||
| 495 | /** |
||
| 496 | * {@inheritDoc} |
||
| 497 | * |
||
| 498 | * @throws \InvalidArgumentException |
||
| 499 | */ |
||
| 500 | 14 | protected function getRenameColumnInstructions($tableName, $columnName, $newColumnName) |
|
| 501 | { |
||
| 502 | 14 | $rows = $this->fetchAll(sprintf('SHOW FULL COLUMNS FROM %s', $this->quoteTableName($tableName))); |
|
| 503 | 6 | ||
| 504 | 6 | foreach ($rows as $row) { |
|
| 505 | if (strcasecmp($row['Field'], $columnName) === 0) { |
||
| 506 | 14 | $null = ($row['Null'] === 'NO') ? 'NOT NULL' : 'NULL'; |
|
| 507 | 14 | $comment = isset($row['Comment']) ? ' COMMENT ' . '\'' . addslashes($row['Comment']) . '\'' : ''; |
|
| 508 | $extra = ' ' . strtoupper($row['Extra']); |
||
| 509 | 14 | if (($row['Default'] !== null)) { |
|
| 510 | 14 | $extra .= $this->getDefaultValueDefinition($row['Default']); |
|
| 511 | 12 | } |
|
| 512 | $definition = $row['Type'] . ' ' . $null . $extra . $comment; |
||
| 513 | 13 | ||
| 514 | $alter = sprintf( |
||
| 515 | 11 | 'CHANGE COLUMN %s %s %s', |
|
| 516 | $this->quoteColumnName($columnName), |
||
| 517 | $this->quoteColumnName($newColumnName), |
||
| 518 | $definition |
||
| 519 | ); |
||
| 520 | |||
| 521 | 1 | return new AlterInstructions([$alter]); |
|
| 522 | } |
||
| 523 | 1 | } |
|
| 524 | |||
| 525 | 1 | throw new InvalidArgumentException(sprintf( |
|
| 526 | 1 | "The specified column doesn't exist: " . |
|
| 527 | 1 | $columnName |
|
| 528 | )); |
||
| 529 | 1 | } |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @inheritDoc |
||
| 533 | */ |
||
| 534 | protected function getChangeColumnInstructions($tableName, $columnName, Column $newColumn) |
||
| 535 | { |
||
| 536 | $after = $newColumn->getAfter() ? ' AFTER ' . $this->quoteColumnName($newColumn->getAfter()) : ''; |
||
| 537 | 4 | $alter = sprintf( |
|
| 538 | 'CHANGE %s %s %s%s', |
||
| 539 | 4 | $this->quoteColumnName($columnName), |
|
| 540 | 4 | $this->quoteColumnName($newColumn->getName()), |
|
| 541 | 4 | $this->getColumnSqlDefinition($newColumn), |
|
| 542 | 4 | $after |
|
| 543 | 4 | ); |
|
| 544 | 4 | ||
| 545 | 4 | return new AlterInstructions([$alter]); |
|
| 546 | 4 | } |
|
| 547 | |||
| 548 | /** |
||
| 549 | * @inheritDoc |
||
| 550 | */ |
||
| 551 | 3 | protected function getDropColumnInstructions($tableName, $columnName) |
|
| 552 | { |
||
| 553 | 3 | $alter = sprintf('DROP COLUMN %s', $this->quoteColumnName($columnName)); |
|
| 554 | 2 | ||
| 555 | 2 | return new AlterInstructions([$alter]); |
|
| 556 | } |
||
| 557 | 3 | ||
| 558 | 3 | /** |
|
| 559 | * Get an array of indexes from a particular table. |
||
| 560 | 3 | * |
|
| 561 | 3 | * @param string $tableName Table name |
|
| 562 | 3 | * |
|
| 563 | 3 | * @return array |
|
| 564 | 3 | */ |
|
| 565 | 3 | protected function getIndexes($tableName) |
|
| 566 | 3 | { |
|
| 567 | 3 | $indexes = []; |
|
| 568 | 3 | $rows = $this->fetchAll(sprintf('SHOW INDEXES FROM %s', $this->quoteTableName($tableName))); |
|
| 569 | 3 | foreach ($rows as $row) { |
|
| 570 | if (!isset($indexes[$row['Key_name']])) { |
||
| 571 | 3 | $indexes[$row['Key_name']] = ['columns' => []]; |
|
| 572 | 1 | } |
|
| 573 | $indexes[$row['Key_name']]['columns'][] = strtolower($row['Column_name']); |
||
| 574 | } |
||
| 575 | |||
| 576 | return $indexes; |
||
| 577 | 2 | } |
|
| 578 | |||
| 579 | 2 | /** |
|
| 580 | * @inheritDoc |
||
| 581 | 2 | */ |
|
| 582 | public function hasIndex($tableName, $columns) |
||
| 583 | 2 | { |
|
| 584 | 2 | if (is_string($columns)) { |
|
| 585 | 2 | $columns = [$columns]; // str to array |
|
| 586 | 2 | } |
|
| 587 | 2 | ||
| 588 | 2 | $columns = array_map('strtolower', $columns); |
|
| 589 | 2 | $indexes = $this->getIndexes($tableName); |
|
| 590 | 2 | ||
| 591 | 2 | foreach ($indexes as $index) { |
|
| 592 | if ($columns == $index['columns']) { |
||
| 593 | 2 | return true; |
|
| 594 | } |
||
| 595 | } |
||
| 596 | |||
| 597 | return false; |
||
| 598 | } |
||
| 599 | 21 | ||
| 600 | /** |
||
| 601 | 21 | * @inheritDoc |
|
| 602 | 5 | */ |
|
| 603 | 5 | public function hasIndexByName($tableName, $indexName) |
|
| 604 | 21 | { |
|
| 605 | 21 | $indexes = $this->getIndexes($tableName); |
|
| 606 | 6 | ||
| 607 | 4 | foreach ($indexes as $name => $index) { |
|
| 608 | if ($name === $indexName) { |
||
| 609 | 4 | return true; |
|
| 610 | } |
||
| 611 | 15 | } |
|
| 612 | 12 | ||
| 613 | 10 | return false; |
|
| 614 | } |
||
| 615 | 11 | ||
| 616 | 11 | /** |
|
| 617 | * @inheritDoc |
||
| 618 | */ |
||
| 619 | protected function getAddIndexInstructions(Table $table, Index $index) |
||
| 620 | { |
||
| 621 | $instructions = new AlterInstructions(); |
||
| 622 | |||
| 623 | if ($index->getType() === Index::FULLTEXT) { |
||
| 624 | // Must be executed separately |
||
| 625 | // SQLSTATE[HY000]: General error: 1795 InnoDB presently supports one FULLTEXT index creation at a time |
||
| 626 | 22 | $alter = sprintf( |
|
| 627 | 'ALTER TABLE %s ADD %s', |
||
| 628 | 22 | $this->quoteTableName($table->getName()), |
|
| 629 | 22 | $this->getIndexSqlDefinition($index) |
|
| 630 | ); |
||
| 631 | |||
| 632 | $instructions->addPostStep($alter); |
||
| 633 | } else { |
||
| 634 | $alter = sprintf( |
||
| 635 | 'ADD %s', |
||
| 636 | $this->getIndexSqlDefinition($index) |
||
| 637 | ); |
||
| 638 | |||
| 639 | $instructions->addAlter($alter); |
||
| 640 | 22 | } |
|
| 641 | |||
| 642 | 22 | return $instructions; |
|
| 643 | 22 | } |
|
| 644 | 19 | ||
| 645 | 19 | /** |
|
| 646 | 19 | * {@inheritDoc} |
|
| 647 | 19 | * |
|
| 648 | 22 | * @throws \InvalidArgumentException |
|
| 649 | 22 | */ |
|
| 650 | protected function getDropIndexByColumnsInstructions($tableName, $columns) |
||
| 651 | { |
||
| 652 | if (is_string($columns)) { |
||
| 653 | $columns = [$columns]; // str to array |
||
| 654 | } |
||
| 655 | 15 | ||
| 656 | $indexes = $this->getIndexes($tableName); |
||
| 657 | 15 | $columns = array_map('strtolower', $columns); |
|
| 658 | 15 | ||
| 659 | 15 | foreach ($indexes as $indexName => $index) { |
|
| 660 | 15 | if ($columns == $index['columns']) { |
|
| 661 | 15 | return new AlterInstructions([sprintf( |
|
| 662 | 15 | 'DROP INDEX %s', |
|
| 663 | 15 | $this->quoteColumnName($indexName) |
|
| 664 | 15 | )]); |
|
| 665 | } |
||
| 666 | } |
||
| 667 | |||
| 668 | throw new InvalidArgumentException(sprintf( |
||
| 669 | 8 | "The specified index on columns '%s' does not exist", |
|
| 670 | implode(',', $columns) |
||
| 671 | 8 | )); |
|
| 672 | 3 | } |
|
| 673 | 3 | ||
| 674 | /** |
||
| 675 | * {@inheritDoc} |
||
| 676 | 8 | * |
|
| 677 | 8 | * @throws \InvalidArgumentException |
|
| 678 | 8 | */ |
|
| 679 | 8 | protected function getDropIndexByNameInstructions($tableName, $indexName) |
|
| 680 | 8 | { |
|
| 681 | $indexes = $this->getIndexes($tableName); |
||
| 682 | 8 | ||
| 683 | 8 | foreach ($indexes as $name => $index) { |
|
| 684 | 8 | if ($name === $indexName) { |
|
| 685 | return new AlterInstructions([sprintf( |
||
| 686 | 7 | 'DROP INDEX %s', |
|
| 687 | 7 | $this->quoteColumnName($indexName) |
|
| 688 | )]); |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | throw new InvalidArgumentException(sprintf( |
||
| 693 | "The specified index name '%s' does not exist", |
||
| 694 | $indexName |
||
| 695 | 7 | )); |
|
| 696 | 7 | } |
|
| 697 | |||
| 698 | 7 | /** |
|
| 699 | 7 | * @inheritDoc |
|
| 700 | 7 | */ |
|
| 701 | 7 | public function hasPrimaryKey($tableName, $columns, $constraint = null) |
|
| 702 | 7 | { |
|
| 703 | $primaryKey = $this->getPrimaryKey($tableName); |
||
| 704 | 7 | ||
| 705 | if (empty($primaryKey['constraint'])) { |
||
| 706 | return false; |
||
| 707 | } |
||
| 708 | |||
| 709 | 96 | if ($constraint) { |
|
| 710 | return ($primaryKey['constraint'] === $constraint); |
||
| 711 | } else { |
||
| 712 | 96 | if (is_string($columns)) { |
|
| 713 | 87 | $columns = [$columns]; // str to array |
|
| 714 | } |
||
| 715 | 96 | $missingColumns = array_diff($columns, $primaryKey['columns']); |
|
| 716 | 4 | ||
| 717 | return empty($missingColumns); |
||
| 718 | 96 | } |
|
| 719 | 9 | } |
|
| 720 | |||
| 721 | /** |
||
| 722 | 6 | * Get the primary key from a particular table. |
|
| 723 | 6 | * |
|
| 724 | 6 | * @param string $tableName Table name |
|
| 725 | 6 | * |
|
| 726 | 6 | * @return array |
|
| 727 | 6 | */ |
|
| 728 | 6 | public function getPrimaryKey($tableName) |
|
| 729 | 6 | { |
|
| 730 | $options = $this->getOptions(); |
||
| 731 | 5 | $rows = $this->fetchAll(sprintf( |
|
| 732 | "SELECT |
||
| 733 | 5 | k.CONSTRAINT_NAME, |
|
| 734 | k.COLUMN_NAME |
||
| 735 | 95 | FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS t |
|
| 736 | 5 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE k |
|
| 737 | USING(CONSTRAINT_NAME,TABLE_NAME) |
||
| 738 | 95 | WHERE t.CONSTRAINT_TYPE='PRIMARY KEY' |
|
| 739 | 3 | AND t.TABLE_SCHEMA='%s' |
|
| 740 | AND t.TABLE_NAME='%s'", |
||
| 741 | 95 | $options['name'], |
|
| 742 | 1 | $tableName |
|
| 743 | )); |
||
| 744 | |||
| 745 | 1 | $primaryKey = [ |
|
| 746 | 1 | 'columns' => [], |
|
| 747 | 1 | ]; |
|
| 748 | 1 | foreach ($rows as $row) { |
|
| 749 | 1 | $primaryKey['constraint'] = $row['CONSTRAINT_NAME']; |
|
| 750 | 1 | $primaryKey['columns'][] = $row['COLUMN_NAME']; |
|
| 751 | 1 | } |
|
| 752 | 1 | ||
| 753 | return $primaryKey; |
||
| 754 | 1 | } |
|
| 755 | |||
| 756 | 1 | /** |
|
| 757 | * @inheritDoc |
||
| 758 | 95 | */ |
|
| 759 | 82 | public function hasForeignKey($tableName, $columns, $constraint = null) |
|
| 781 | 82 | ||
| 782 | 76 | /** |
|
| 783 | 76 | * Get an array of foreign keys from a particular table. |
|
| 784 | 82 | * |
|
| 785 | * @param string $tableName Table name |
||
| 786 | 86 | * |
|
| 787 | 82 | * @return array |
|
| 788 | */ |
||
| 789 | 86 | protected function getForeignKeys($tableName) |
|
| 820 | |||
| 821 | 6 | /** |
|
| 822 | 4 | * @inheritDoc |
|
| 823 | */ |
||
| 824 | 2 | protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreignKey) |
|
| 833 | 2 | ||
| 834 | 2 | /** |
|
| 835 | 2 | * @inheritDoc |
|
| 836 | */ |
||
| 837 | protected function getDropForeignKeyInstructions($tableName, $constraint) |
||
| 846 | 17 | ||
| 847 | /** |
||
| 848 | 17 | * {@inheritDoc} |
|
| 849 | 17 | * |
|
| 850 | 1 | * @throws \InvalidArgumentException |
|
| 851 | */ |
||
| 852 | 16 | protected function getDropForeignKeyByColumnsInstructions($tableName, $columns) |
|
| 884 | 2 | ||
| 885 | 16 | /** |
|
| 886 | 2 | * {@inheritDoc} |
|
| 887 | 2 | * |
|
| 888 | 2 | * @throws \Phinx\Db\Adapter\UnsupportedColumnTypeException |
|
| 889 | 16 | */ |
|
| 890 | 2 | public function getSqlType($type, $limit = null) |
|
| 891 | 2 | { |
|
| 892 | 2 | switch ($type) { |
|
| 893 | 16 | case static::PHINX_TYPE_FLOAT: |
|
| 894 | 15 | case static::PHINX_TYPE_DOUBLE: |
|
| 895 | 15 | case static::PHINX_TYPE_DECIMAL: |
|
| 896 | 12 | case static::PHINX_TYPE_DATE: |
|
| 897 | 12 | case static::PHINX_TYPE_ENUM: |
|
| 898 | 15 | case static::PHINX_TYPE_SET: |
|
| 899 | 11 | case static::PHINX_TYPE_JSON: |
|
| 900 | 6 | // Geospatial database types |
|
| 901 | 4 | case static::PHINX_TYPE_GEOMETRY: |
|
| 902 | 4 | case static::PHINX_TYPE_POINT: |
|
| 903 | 6 | case static::PHINX_TYPE_LINESTRING: |
|
| 904 | 6 | case static::PHINX_TYPE_POLYGON: |
|
| 905 | 10 | return ['name' => $type]; |
|
| 906 | 2 | case static::PHINX_TYPE_DATETIME: |
|
| 907 | 2 | case static::PHINX_TYPE_TIMESTAMP: |
|
| 908 | 10 | case static::PHINX_TYPE_TIME: |
|
| 909 | 1 | return ['name' => $type, 'limit' => $limit]; |
|
| 1036 | |||
| 1037 | /** |
||
| 1038 | 16 | * Returns Phinx type by SQL type |
|
| 1039 | * |
||
| 1040 | 16 | * @internal param string $sqlType SQL type |
|
| 1041 | 16 | * |
|
| 1042 | 16 | * @param string $sqlTypeDef SQL Type definition |
|
| 1043 | 2 | * |
|
| 1044 | 2 | * @throws \Phinx\Db\Adapter\UnsupportedColumnTypeException |
|
| 1045 | * |
||
| 1046 | 16 | * @return array Phinx type |
|
| 1047 | 5 | */ |
|
| 1048 | 5 | public function getPhinxType($sqlTypeDef) |
|
| 1221 | |||
| 1222 | /** |
||
| 1223 | * @inheritDoc |
||
| 1224 | */ |
||
| 1225 | public function createDatabase($name, $options = []) |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * @inheritDoc |
||
| 1243 | */ |
||
| 1244 | public function hasDatabase($name) |
||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * @inheritDoc |
||
| 1264 | */ |
||
| 1265 | public function dropDatabase($name) |
||
| 1270 | |||
| 1271 | /** |
||
| 1272 | * Gets the MySQL Column Definition for a Column object. |
||
| 1273 | * |
||
| 1274 | * @param \Phinx\Db\Table\Column $column Column |
||
| 1275 | * |
||
| 1276 | * @return string |
||
| 1277 | */ |
||
| 1278 | protected function getColumnSqlDefinition(Column $column) |
||
| 1331 | |||
| 1332 | /** |
||
| 1333 | * Gets the MySQL Index Definition for an Index object. |
||
| 1334 | * |
||
| 1335 | * @param \Phinx\Db\Table\Index $index Index |
||
| 1336 | * |
||
| 1337 | * @return string |
||
| 1338 | */ |
||
| 1339 | protected function getIndexSqlDefinition(Index $index) |
||
| 1389 | |||
| 1390 | /** |
||
| 1391 | * Gets the MySQL Foreign Key Definition for an ForeignKey object. |
||
| 1392 | * |
||
| 1393 | * @param \Phinx\Db\Table\ForeignKey $foreignKey Foreign key |
||
| 1394 | * |
||
| 1395 | * @return string |
||
| 1396 | */ |
||
| 1397 | protected function getForeignKeySqlDefinition(ForeignKey $foreignKey) |
||
| 1422 | |||
| 1423 | /** |
||
| 1424 | * Describes a database table. This is a MySQL adapter specific method. |
||
| 1425 | * |
||
| 1426 | * @param string $tableName Table name |
||
| 1427 | * |
||
| 1428 | * @return array |
||
| 1429 | */ |
||
| 1430 | public function describeTable($tableName) |
||
| 1446 | |||
| 1447 | /** |
||
| 1448 | * Returns MySQL column types (inherited and MySQL specified). |
||
| 1449 | * |
||
| 1450 | * @return string[] |
||
| 1451 | */ |
||
| 1452 | public function getColumnTypes() |
||
| 1456 | |||
| 1457 | /** |
||
| 1458 | * @inheritDoc |
||
| 1459 | */ |
||
| 1460 | public function getDecoratedConnection() |
||
| 1475 | } |
||
| 1476 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.