Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like OracleAdapter 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 OracleAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class OracleAdapter extends PdoAdapter implements AdapterInterface |
||
|
|
|||
| 43 | { |
||
| 44 | protected $schema = 'dbo'; |
||
| 45 | |||
| 46 | protected $signedColumnTypes = ['integer' => true, 'biginteger' => true, 'float' => true, 'decimal' => true]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function connect() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function disconnect() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @codeCoverageIgnore |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | public function hasTransactions() |
||
| 96 | { |
||
| 97 | return true; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | * @codeCoverageIgnore |
||
| 103 | */ |
||
| 104 | public function beginTransaction() |
||
| 105 | { |
||
| 106 | // $this->execute('BEGIN TRANSACTION'); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | * @codeCoverageIgnore |
||
| 112 | */ |
||
| 113 | public function commitTransaction() |
||
| 114 | { |
||
| 115 | // $this->execute('COMMIT TRANSACTION'); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | * @codeCoverageIgnore |
||
| 121 | */ |
||
| 122 | public function rollbackTransaction() |
||
| 123 | { |
||
| 124 | // $this->execute('ROLLBACK TRANSACTION'); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | public function quoteTableName($tableName) |
||
| 131 | { |
||
| 132 | return str_replace('.', '].[', $this->quoteColumnName($tableName)); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | public function quoteColumnName($columnName) |
||
| 139 | { |
||
| 140 | return '"' . str_replace(']', '"', $columnName) . '"'; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public function hasTable($tableName) |
||
| 147 | { |
||
| 148 | $result = $this->fetchRow( |
||
| 149 | sprintf( |
||
| 150 | 'SELECT count(*) as count FROM ALL_TABLES WHERE table_name = \'%s\'', |
||
| 151 | $tableName |
||
| 152 | ) |
||
| 153 | ); |
||
| 154 | |||
| 155 | return $result['COUNT'] > 0; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | public function createTable(Table $table) |
||
| 162 | { |
||
| 163 | $options = $table->getOptions(); |
||
| 164 | |||
| 165 | // Add the default primary key |
||
| 166 | $columns = $table->getPendingColumns(); |
||
| 167 | |||
| 168 | if (!isset($options['id']) || (isset($options['id']) && $options['id'] === true)) { |
||
| 169 | $column = new Column(); |
||
| 170 | $column->setName('id') |
||
| 171 | ->setType('integer') |
||
| 172 | ->setIdentity(true); |
||
| 173 | |||
| 174 | array_unshift($columns, $column); |
||
| 175 | $options['primary_key'] = 'id'; |
||
| 176 | } elseif (isset($options['id']) && is_string($options['id'])) { |
||
| 177 | // Handle id => "field_name" to support AUTO_INCREMENT |
||
| 178 | $column = new Column(); |
||
| 179 | $column->setName($options['id']) |
||
| 180 | ->setType('integer') |
||
| 181 | ->setIdentity(true); |
||
| 182 | |||
| 183 | array_unshift($columns, $column); |
||
| 184 | $options['primary_key'] = $options['id']; |
||
| 185 | } |
||
| 186 | |||
| 187 | $sql = 'CREATE TABLE '; |
||
| 188 | $sql .= $this->quoteTableName($table->getName()) . ' ('; |
||
| 189 | $sqlBuffer = []; |
||
| 190 | $columnsWithComments = []; |
||
| 191 | |||
| 192 | View Code Duplication | foreach ($columns as $column) { |
|
| 193 | $sqlBuffer[] = $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column); |
||
| 194 | |||
| 195 | // set column comments, if needed |
||
| 196 | if ($column->getComment()) { |
||
| 197 | $columnsWithComments[] = $column; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | // set the primary key(s) |
||
| 202 | View Code Duplication | if (isset($options['primary_key'])) { |
|
| 203 | $pkSql = sprintf('CONSTRAINT PK_%s PRIMARY KEY (', substr($table->getName(), 0, 28)); |
||
| 204 | if (is_string($options['primary_key'])) { // handle primary_key => 'id' |
||
| 205 | $pkSql .= $this->quoteColumnName($options['primary_key']); |
||
| 206 | } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') |
||
| 207 | $pkSql .= implode(',', array_map([$this, 'quoteColumnName'], $options['primary_key'])); |
||
| 208 | } |
||
| 209 | $pkSql .= ')'; |
||
| 210 | $sqlBuffer[] = $pkSql; |
||
| 211 | } |
||
| 212 | |||
| 213 | // set the foreign keys |
||
| 214 | $foreignKeys = $table->getForeignKeys(); |
||
| 215 | foreach ($foreignKeys as $key => $foreignKey) { |
||
| 216 | $sqlBuffer[] = $this->getForeignKeySqlDefinition($foreignKey, $table->getName()); |
||
| 217 | } |
||
| 218 | |||
| 219 | $sql .= implode(', ', $sqlBuffer); |
||
| 220 | $sql .= ')'; |
||
| 221 | |||
| 222 | $this->execute($sql); |
||
| 223 | // process column comments |
||
| 224 | foreach ($columnsWithComments as $key => $column) { |
||
| 225 | $sql = $this->getColumnCommentSqlDefinition($column, $table->getName()); |
||
| 226 | $this->execute($sql); |
||
| 227 | } |
||
| 228 | // set the indexes |
||
| 229 | $indexes = $table->getIndexes(); |
||
| 230 | |||
| 231 | if (!empty($indexes)) { |
||
| 232 | foreach ($indexes as $index) { |
||
| 233 | $sql = $this->getIndexSqlDefinition($index, $table->getName()); |
||
| 234 | $this->execute($sql); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | if (!$this->hasSequence($table->getName())) { |
||
| 239 | $sql = "CREATE SEQUENCE SQ_" . $table->getName() . " MINVALUE 1 MAXVALUE 99999999999999999 INCREMENT BY 1"; |
||
| 240 | $this->execute($sql); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Verify if the table has a Sequence for primary Key |
||
| 246 | * |
||
| 247 | * @param string $tableName Table name |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function hasSequence($tableName) |
||
| 252 | { |
||
| 253 | $sql = sprintf( |
||
| 254 | "SELECT COUNT(*) as COUNT FROM user_sequences WHERE sequence_name = '%s'", |
||
| 255 | strtoupper("SQ_" . $tableName) |
||
| 256 | ); |
||
| 257 | $result = $this->fetchRow($sql); |
||
| 258 | |||
| 259 | return $result['COUNT'] > 0; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Gets the Oracle Column Comment Defininition for a column object. |
||
| 264 | * |
||
| 265 | * @param \Phinx\Db\Table\Column $column Column |
||
| 266 | * @param string $tableName Table name |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | protected function getColumnCommentSqlDefinition(Column $column, $tableName) |
||
| 271 | { |
||
| 272 | $comment = (strcasecmp($column->getComment(), 'NULL') !== 0) ? $column->getComment() : ''; |
||
| 273 | |||
| 274 | |||
| 275 | return sprintf( |
||
| 276 | "COMMENT ON COLUMN \"%s\".\"%s\" IS '%s'", |
||
| 277 | $tableName, |
||
| 278 | $column->getName(), |
||
| 279 | str_replace("'", "", $comment) |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | public function renameTable($tableName, $newTableName) |
||
| 287 | { |
||
| 288 | $this->execute(sprintf('alter table "%s" rename to "%s"', $tableName, $newTableName)); |
||
| 289 | |||
| 290 | if (!$this->hasSequence("SQ_" . strtoupper($newTableName))) { |
||
| 291 | $this->renameSequence("SQ_" . strtoupper($tableName), "SQ_" . strtoupper($newTableName)); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Rename an Oracle Sequence Object. |
||
| 297 | * |
||
| 298 | * @param string $sequenceName Old Sequence Name |
||
| 299 | * @param string $newSequenceName New Sequence Name |
||
| 300 | * |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | public function renameSequence($sequenceName, $newSequenceName) |
||
| 304 | { |
||
| 305 | $this->execute(sprintf('rename "%s" to "%s"', $sequenceName, $newSequenceName)); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function dropTable($tableName) |
||
| 312 | { |
||
| 313 | $this->execute(sprintf('DROP TABLE %s', $this->quoteTableName($tableName))); |
||
| 314 | $this->execute(sprintf('DROP SEQUENCE %s', $this->quoteTableName(strtoupper("SQ_" . $tableName)))); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritdoc} |
||
| 319 | */ |
||
| 320 | public function truncateTable($tableName) |
||
| 321 | { |
||
| 322 | $sql = sprintf( |
||
| 323 | 'TRUNCATE TABLE %s', |
||
| 324 | $this->quoteTableName($tableName) |
||
| 325 | ); |
||
| 326 | |||
| 327 | $this->execute($sql); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the comment for a column |
||
| 332 | * |
||
| 333 | * @param string $tableName Table Name |
||
| 334 | * @param string $columnName Column Name |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function getColumnComment($tableName, $columnName) |
||
| 339 | { |
||
| 340 | $sql = sprintf( |
||
| 341 | "select COMMENTS from ALL_COL_COMMENTS WHERE COLUMN_NAME = '%s' and TABLE_NAME = '%s'", |
||
| 342 | $columnName, |
||
| 343 | $tableName |
||
| 344 | ); |
||
| 345 | $row = $this->fetchRow($sql); |
||
| 346 | |||
| 347 | return $row['COMMENTS']; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | public function getColumns($tableName) |
||
| 354 | { |
||
| 355 | $columns = []; |
||
| 356 | |||
| 357 | $sql = sprintf( |
||
| 358 | "select TABLE_NAME \"TABLE_NAME\", COLUMN_NAME \"NAME\", DATA_TYPE \"TYPE\", NULLABLE \"NULL\", |
||
| 359 | DATA_DEFAULT \"DEFAULT\", DATA_LENGTH \"CHAR_LENGTH\", DATA_PRECISION \"PRECISION\", DATA_SCALE \"SCALE\", |
||
| 360 | COLUMN_ID \"ORDINAL_POSITION\" FROM ALL_TAB_COLUMNS WHERE table_name = '%s'", |
||
| 361 | $tableName |
||
| 362 | ); |
||
| 363 | |||
| 364 | $rows = $this->fetchAll($sql); |
||
| 365 | |||
| 366 | foreach ($rows as $columnInfo) { |
||
| 367 | $default = null; |
||
| 368 | if (trim($columnInfo['DEFAULT']) != 'NULL') { |
||
| 369 | $default = trim($columnInfo['DEFAULT']); |
||
| 370 | } |
||
| 371 | |||
| 372 | $column = new Column(); |
||
| 373 | $column->setName($columnInfo['NAME']) |
||
| 374 | ->setType($this->getPhinxType($columnInfo['TYPE'], $columnInfo['PRECISION'])) |
||
| 375 | ->setNull($columnInfo['NULL'] !== 'N') |
||
| 376 | ->setDefault($default) |
||
| 377 | ->setComment($this->getColumnComment($columnInfo['TABLE_NAME'], $columnInfo['NAME'])); |
||
| 378 | |||
| 379 | if (!empty($columnInfo['CHAR_LENGTH'])) { |
||
| 380 | $column->setLimit($columnInfo['CHAR_LENGTH']); |
||
| 381 | } |
||
| 382 | |||
| 383 | $columns[$columnInfo['NAME']] = $column; |
||
| 384 | } |
||
| 385 | |||
| 386 | return $columns; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritdoc} |
||
| 391 | */ |
||
| 392 | View Code Duplication | public function hasColumn($tableName, $columnName) |
|
| 393 | { |
||
| 394 | $sql = sprintf( |
||
| 395 | "select count(*) as count from ALL_TAB_COLUMNS |
||
| 396 | where table_name = '%s' and column_name = '%s'", |
||
| 397 | $tableName, |
||
| 398 | $columnName |
||
| 399 | ); |
||
| 400 | |||
| 401 | $result = $this->fetchRow($sql); |
||
| 402 | |||
| 403 | return $result['COUNT'] > 0; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * {@inheritdoc} |
||
| 408 | */ |
||
| 409 | public function addColumn(Table $table, Column $column) |
||
| 410 | { |
||
| 411 | $sql = sprintf( |
||
| 412 | 'ALTER TABLE %s ADD %s %s', |
||
| 413 | $this->quoteTableName($table->getName()), |
||
| 414 | $this->quoteColumnName($column->getName()), |
||
| 415 | $this->getColumnSqlDefinition($column) |
||
| 416 | ); |
||
| 417 | |||
| 418 | $this->execute($sql); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * {@inheritdoc} |
||
| 423 | */ |
||
| 424 | public function renameColumn($tableName, $columnName, $newColumnName) |
||
| 425 | { |
||
| 426 | if (!$this->hasColumn($tableName, $columnName)) { |
||
| 427 | throw new \InvalidArgumentException("The specified column does not exist: $columnName"); |
||
| 428 | } |
||
| 429 | |||
| 430 | $this->execute( |
||
| 431 | sprintf( |
||
| 432 | "alter table \"%s\" rename column \"%s\" TO \"%s\"", |
||
| 433 | $tableName, |
||
| 434 | $columnName, |
||
| 435 | $newColumnName |
||
| 436 | ) |
||
| 437 | ); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * {@inheritdoc} |
||
| 442 | */ |
||
| 443 | public function changeColumn($tableName, $columnName, Column $newColumn) |
||
| 444 | { |
||
| 445 | $columns = $this->getColumns($tableName); |
||
| 446 | |||
| 447 | if ($columnName !== $newColumn->getName()) { |
||
| 448 | $this->renameColumn($tableName, $columnName, $newColumn->getName()); |
||
| 449 | } |
||
| 450 | |||
| 451 | $setNullSql = ($newColumn->isNull() == $columns[$columnName]->isNull() ? false : true); |
||
| 452 | |||
| 453 | $this->execute( |
||
| 454 | sprintf( |
||
| 455 | 'ALTER TABLE %s MODIFY(%s %s)', |
||
| 456 | $this->quoteTableName($tableName), |
||
| 457 | $this->quoteColumnName($newColumn->getName()), |
||
| 458 | $this->getColumnSqlDefinition($newColumn, $setNullSql) |
||
| 459 | ) |
||
| 460 | ); |
||
| 461 | // change column comment if needed |
||
| 462 | if ($newColumn->getComment()) { |
||
| 463 | $sql = $this->getColumnCommentSqlDefinition($newColumn, $tableName); |
||
| 464 | $this->execute($sql); |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * {@inheritdoc} |
||
| 470 | */ |
||
| 471 | public function dropColumn($tableName, $columnName) |
||
| 472 | { |
||
| 473 | $this->execute( |
||
| 474 | sprintf( |
||
| 475 | 'ALTER TABLE %s DROP COLUMN %s', |
||
| 476 | $this->quoteTableName($tableName), |
||
| 477 | $this->quoteColumnName($columnName) |
||
| 478 | ) |
||
| 479 | ); |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Get an array of indexes from a particular table. |
||
| 484 | * |
||
| 485 | * @param string $tableName Table Name |
||
| 486 | * @return array |
||
| 487 | */ |
||
| 488 | View Code Duplication | public function getIndexes($tableName) |
|
| 489 | { |
||
| 490 | $indexes = []; |
||
| 491 | $sql = "SELECT index_owner as owner,index_name,column_name FROM ALL_IND_COLUMNS |
||
| 492 | WHERE TABLE_NAME = '$tableName'"; |
||
| 493 | |||
| 494 | $rows = $this->fetchAll($sql); |
||
| 495 | foreach ($rows as $row) { |
||
| 496 | if (!isset($indexes[$row['INDEX_NAME']])) { |
||
| 497 | $indexes[$row['INDEX_NAME']] = ['columns' => []]; |
||
| 498 | } |
||
| 499 | $indexes[$row['INDEX_NAME']]['columns'][] = strtoupper($row['COLUMN_NAME']); |
||
| 500 | } |
||
| 501 | |||
| 502 | return $indexes; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * {@inheritdoc} |
||
| 507 | */ |
||
| 508 | View Code Duplication | public function hasIndex($tableName, $columns) |
|
| 509 | { |
||
| 510 | if (is_string($columns)) { |
||
| 511 | $columns = [$columns]; // str to array |
||
| 512 | } |
||
| 513 | |||
| 514 | $indexes = $this->getIndexes($tableName); |
||
| 515 | foreach ($indexes as $index) { |
||
| 516 | $a = array_diff($columns, $index['columns']); |
||
| 517 | |||
| 518 | if (empty($a)) { |
||
| 519 | return true; |
||
| 520 | } |
||
| 521 | } |
||
| 522 | |||
| 523 | return false; |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * {@inheritdoc} |
||
| 528 | */ |
||
| 529 | View Code Duplication | public function hasIndexByName($tableName, $indexName) |
|
| 530 | { |
||
| 531 | $indexes = $this->getIndexes($tableName); |
||
| 532 | |||
| 533 | foreach ($indexes as $name => $index) { |
||
| 534 | if ($name === $indexName) { |
||
| 535 | return true; |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | return false; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * {@inheritdoc} |
||
| 544 | */ |
||
| 545 | public function addIndex(Table $table, Index $index) |
||
| 546 | { |
||
| 547 | $sql = $this->getIndexSqlDefinition($index, $table->getName()); |
||
| 548 | $this->execute($sql); |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * {@inheritdoc} |
||
| 553 | */ |
||
| 554 | public function dropIndex($tableName, $columns) |
||
| 555 | { |
||
| 556 | if (is_string($columns)) { |
||
| 557 | $columns = [$columns]; // str to array |
||
| 558 | } |
||
| 559 | |||
| 560 | $indexes = $this->getIndexes($tableName); |
||
| 561 | $columns = array_map('strtoupper', $columns); |
||
| 562 | |||
| 563 | View Code Duplication | foreach ($indexes as $indexName => $index) { |
|
| 564 | $a = array_diff($columns, $index['columns']); |
||
| 565 | if (empty($a)) { |
||
| 566 | $this->execute( |
||
| 567 | sprintf( |
||
| 568 | 'DROP INDEX %s', |
||
| 569 | $this->quoteColumnName($indexName) |
||
| 570 | ) |
||
| 571 | ); |
||
| 572 | |||
| 573 | break; |
||
| 574 | } |
||
| 575 | } |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * {@inheritdoc} |
||
| 580 | */ |
||
| 581 | public function dropIndexByName($tableName, $indexName) |
||
| 582 | { |
||
| 583 | $indexes = $this->getIndexes($tableName); |
||
| 584 | |||
| 585 | View Code Duplication | foreach ($indexes as $name => $index) { |
|
| 586 | if ($name === $indexName) { |
||
| 587 | $this->execute( |
||
| 588 | sprintf( |
||
| 589 | 'DROP INDEX %s', |
||
| 590 | $this->quoteColumnName($indexName) |
||
| 591 | ) |
||
| 592 | ); |
||
| 593 | |||
| 594 | break; |
||
| 595 | } |
||
| 596 | } |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * {@inheritdoc} |
||
| 601 | */ |
||
| 602 | View Code Duplication | public function hasForeignKey($tableName, $columns, $constraint = null) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Get an array of foreign keys from a particular table. |
||
| 629 | * |
||
| 630 | * @param string $tableName Table Name |
||
| 631 | * @param string $type Type of Constraint Type (R, P) |
||
| 632 | * @return array |
||
| 633 | */ |
||
| 634 | View Code Duplication | protected function getForeignKeys($tableName, $type = 'R') |
|
| 659 | |||
| 660 | /** |
||
| 661 | * {@inheritdoc} |
||
| 662 | */ |
||
| 663 | public function addForeignKey(Table $table, ForeignKey $foreignKey) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * {@inheritdoc} |
||
| 676 | */ |
||
| 677 | public function dropForeignKey($tableName, $columns, $constraint = null) |
||
| 678 | { |
||
| 679 | if (is_string($columns)) { |
||
| 680 | $columns = [$columns]; // str to array |
||
| 681 | } |
||
| 682 | |||
| 683 | if ($constraint) { |
||
| 684 | $this->execute( |
||
| 685 | sprintf( |
||
| 686 | 'ALTER TABLE %s DROP CONSTRAINT %s', |
||
| 687 | $this->quoteTableName($tableName), |
||
| 688 | $constraint |
||
| 689 | ) |
||
| 690 | ); |
||
| 691 | |||
| 692 | return; |
||
| 693 | } else { |
||
| 694 | foreach ($columns as $column) { |
||
| 695 | $rows = $this->fetchAll(sprintf( |
||
| 696 | "SELECT a.CONSTRAINT_NAME, a.TABLE_NAME, b.COLUMN_NAME, |
||
| 697 | (SELECT c.TABLE_NAME from ALL_CONS_COLUMNS c |
||
| 698 | WHERE c.CONSTRAINT_NAME = a.R_CONSTRAINT_NAME) referenced_table_name, |
||
| 699 | (SELECT c.COLUMN_NAME from ALL_CONS_COLUMNS c |
||
| 700 | WHERE c.CONSTRAINT_NAME = a.R_CONSTRAINT_NAME) referenced_column_name |
||
| 701 | FROM all_constraints a JOIN ALL_CONS_COLUMNS b ON a.CONSTRAINT_NAME = b.CONSTRAINT_NAME |
||
| 702 | WHERE a.table_name = '%s' |
||
| 703 | AND CONSTRAINT_TYPE = 'R' |
||
| 704 | AND COLUMN_NAME = '%s'", |
||
| 705 | $tableName, |
||
| 706 | $column |
||
| 707 | )); |
||
| 708 | foreach ($rows as $row) { |
||
| 709 | $this->dropForeignKey($tableName, $columns, $row['CONSTRAINT_NAME']); |
||
| 710 | } |
||
| 711 | } |
||
| 712 | } |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * {@inheritdoc} |
||
| 717 | */ |
||
| 718 | public function getSqlType($type, $limit = null) |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Returns Phinx type by SQL type |
||
| 771 | * |
||
| 772 | * @param string $sqlType SQL Type definition |
||
| 773 | * @param int $precision Precision of NUMBER type to define Phinx Type. |
||
| 774 | * @throws \RuntimeException |
||
| 775 | * @internal param string $sqlType SQL type |
||
| 776 | * @return string Phinx type |
||
| 777 | */ |
||
| 778 | public function getPhinxType($sqlType, $precision = null) |
||
| 814 | |||
| 815 | /** |
||
| 816 | * {@inheritdoc} |
||
| 817 | */ |
||
| 818 | View Code Duplication | public function createDatabase($name, $options = []) |
|
| 827 | |||
| 828 | /** |
||
| 829 | * {@inheritdoc} |
||
| 830 | */ |
||
| 831 | public function hasDatabase($name) |
||
| 842 | |||
| 843 | /** |
||
| 844 | * {@inheritdoc} |
||
| 845 | */ |
||
| 846 | public function dropDatabase($name) |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Get the defintion for a `DEFAULT` statement. |
||
| 859 | * |
||
| 860 | * @param mixed $default Default value for column |
||
| 861 | * @return string |
||
| 862 | */ |
||
| 863 | protected function getDefaultValueDefinition($default) |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Gets the Oracle Column Definition for a Column object. |
||
| 876 | * |
||
| 877 | * @param \Phinx\Db\Table\Column $column Column |
||
| 878 | * @param bool $setNullSql Set column nullable |
||
| 879 | * @return string |
||
| 880 | */ |
||
| 881 | protected function getColumnSqlDefinition(Column $column, $setNullSql = true) |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Gets the Oracle Index Definition for an Index object. |
||
| 918 | * |
||
| 919 | * @param \Phinx\Db\Table\Index $index Index |
||
| 920 | * @param string $tableName Table Name |
||
| 921 | * @return string |
||
| 922 | */ |
||
| 923 | protected function getIndexSqlDefinition(Index $index, $tableName) |
||
| 924 | { |
||
| 925 | if (is_string($index->getName())) { |
||
| 926 | $indexName = $index->getName(); |
||
| 927 | View Code Duplication | } else { |
|
| 928 | $columnNames = $index->getColumns(); |
||
| 929 | if (is_string($columnNames)) { |
||
| 930 | $columnNames = [$columnNames]; |
||
| 931 | } |
||
| 932 | $indexName = sprintf('%s_%s', $tableName, implode('_', $columnNames)); |
||
| 933 | } |
||
| 934 | $def = sprintf( |
||
| 935 | "CREATE %s INDEX %s ON %s (%s)", |
||
| 936 | ($index->getType() === Index::UNIQUE ? 'UNIQUE' : ''), |
||
| 937 | $indexName, |
||
| 938 | $this->quoteTableName($tableName), |
||
| 939 | '"' . implode('","', $index->getColumns()) . '"' |
||
| 940 | ); |
||
| 941 | |||
| 942 | return $def; |
||
| 943 | } |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Gets the Oracle Foreign Key Definition for an ForeignKey object. |
||
| 947 | * |
||
| 948 | * @param \Phinx\Db\Table\ForeignKey $foreignKey Foreign Key Object |
||
| 949 | * @param string $tableName Table Name |
||
| 950 | * @return string |
||
| 951 | */ |
||
| 952 | protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, $tableName) |
||
| 968 | |||
| 969 | /** |
||
| 970 | * {@inheritdoc} |
||
| 971 | */ |
||
| 972 | public function getColumnTypes() |
||
| 976 | |||
| 977 | /** |
||
| 978 | * Records a migration being run. |
||
| 979 | * |
||
| 980 | * @param \Phinx\Migration\MigrationInterface $migration Migration |
||
| 981 | * @param string $direction Direction |
||
| 982 | * @param int $startTime Start Time |
||
| 983 | * @param int $endTime End Time |
||
| 984 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
| 985 | */ |
||
| 986 | public function migrated(\Phinx\Migration\MigrationInterface $migration, $direction, $startTime, $endTime) |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * {@inheritdoc} |
||
| 1026 | */ |
||
| 1027 | public function bulkinsert(Table $table, $rows) |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Get Next Auto Increment Value Sequence |
||
| 1066 | * |
||
| 1067 | * |
||
| 1068 | * @param string $sequence Sequence Name |
||
| 1069 | * @return int |
||
| 1070 | */ |
||
| 1071 | protected function getNextValSequence($sequence) |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * {@inheritdoc} |
||
| 1081 | */ |
||
| 1082 | View Code Duplication | public function getVersionLog() |
|
| 1106 | } |
||
| 1107 |