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 SQLiteAdapter 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 SQLiteAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class SQLiteAdapter extends PdoAdapter implements AdapterInterface |
||
47 | { |
||
48 | protected $definitionsWithLimits = [ |
||
49 | 'CHAR', |
||
50 | 'CHARACTER', |
||
51 | 'VARCHAR', |
||
52 | 'VARYING CHARACTER', |
||
53 | 'NCHAR', |
||
54 | 'NATIVE CHARACTER', |
||
55 | 'NVARCHAR' |
||
56 | 42 | ]; |
|
57 | |||
58 | 42 | protected $suffix = '.sqlite3'; |
|
59 | 42 | ||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function connect() |
||
95 | |||
96 | 48 | /** |
|
97 | 48 | * {@inheritdoc} |
|
98 | */ |
||
99 | public function setOptions(array $options) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function disconnect() |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function hasTransactions() |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 43 | public function beginTransaction() |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 44 | public function commitTransaction() |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 42 | public function rollbackTransaction() |
|
154 | 42 | ||
155 | 12 | /** |
|
156 | 42 | * {@inheritdoc} |
|
157 | */ |
||
158 | 42 | public function quoteTableName($tableName) |
|
162 | |||
163 | /** |
||
164 | 42 | * {@inheritdoc} |
|
165 | */ |
||
166 | public function quoteColumnName($columnName) |
||
170 | 35 | ||
171 | 35 | /** |
|
172 | 35 | * @param string $tableName Table name |
|
173 | 35 | * @param boolean $quoted Whether to return the schema name and table name escaped and quoted. If quoted, the schema (if any) will also be appended with a dot |
|
174 | * @return array |
||
175 | 35 | */ |
|
176 | 42 | protected function getSchemaName($tableName, $quoted = false) |
|
193 | |||
194 | 42 | /** |
|
195 | 42 | * Retrieves information about a given table from one of the SQLite pragmas |
|
196 | 42 | * |
|
197 | 42 | * @param string $tableName The table to query |
|
198 | 42 | * @param string $pragma The pragma to query |
|
199 | 42 | * @return array |
|
200 | */ |
||
201 | protected function getTableInfo($tableName, $pragma = 'table_info') |
||
202 | 1 | { |
|
203 | 1 | $info = $this->getSchemaName($tableName, true); |
|
204 | 1 | return $this->fetchAll(sprintf('PRAGMA %s%s(%s)', $info['schema'], $pragma, $info['table'])); |
|
205 | } |
||
206 | 1 | ||
207 | 1 | /** |
|
208 | 1 | * {@inheritdoc} |
|
209 | 1 | */ |
|
210 | 1 | public function hasTable($tableName) |
|
211 | 1 | { |
|
212 | 42 | $info = $this->getSchemaName($tableName); |
|
213 | 42 | if ($info['schema'] === '') { |
|
214 | 37 | // if no schema is specified we search all schemata |
|
215 | $rows = $this->fetchAll('PRAGMA database_list;'); |
||
216 | $schemata = []; |
||
217 | foreach ($rows as $row) { |
||
218 | 42 | $schemata[] = $row['name']; |
|
219 | 42 | } |
|
220 | 1 | } else { |
|
221 | 1 | // otherwise we search just the specified schema |
|
222 | 1 | $schemata = (array)$info['schema']; |
|
223 | 1 | } |
|
224 | |||
225 | 42 | $table = strtolower($info['table']); |
|
226 | foreach ($schemata as $schema) { |
||
227 | 42 | if (strtolower($schema) === 'temp') { |
|
228 | $master = 'sqlite_temp_master'; |
||
229 | 42 | } else { |
|
230 | 6 | $master = sprintf('%s.%s', $this->quoteColumnName($schema), 'sqlite_master'); |
|
231 | 42 | } |
|
232 | 42 | try { |
|
233 | $rows = $this->fetchAll(sprintf('SELECT name FROM %s WHERE type=\'table\' AND lower(name) = %s', $master, $this->quoteString($table))); |
||
234 | } catch (\PDOException $e) { |
||
235 | // an exception can occur if the schema part of the table refers to a database which is not attached |
||
236 | return false; |
||
237 | 1 | } |
|
238 | |||
239 | 1 | // this somewhat pedantic check with strtolower is performed because the SQL lower function may be redefined, |
|
240 | 1 | // and can act on all Unicode characters if the ICU extension is loaded, while SQL identifiers are only case-insensitive for ASCII |
|
241 | foreach ($rows as $row) { |
||
242 | if (strtolower($row['name']) === $table) { |
||
243 | return true; |
||
244 | } |
||
245 | 1 | } |
|
246 | } |
||
247 | 1 | ||
248 | 1 | return false; |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | 1 | */ |
|
254 | public function createTable(Table $table, array $columns = [], array $indexes = []) |
||
314 | 4 | ||
315 | 4 | /** |
|
316 | 4 | * {@inheritdoc} |
|
317 | 4 | */ |
|
318 | protected function getChangePrimaryKeyInstructions(Table $table, $newColumns) |
||
319 | 4 | { |
|
320 | 4 | $instructions = new AlterInstructions(); |
|
321 | |||
322 | // Drop the existing primary key |
||
323 | $primaryKey = $this->getPrimaryKey($table->getName()); |
||
324 | if (!empty($primaryKey)) { |
||
325 | 2 | $instructions->merge( |
|
326 | // FIXME: array access is a hack to make this incomplete implementation work with a correct getPrimaryKey implementation |
||
327 | 2 | $this->getDropPrimaryKeyInstructions($table, $primaryKey[0]) |
|
328 | ); |
||
329 | 2 | } |
|
330 | |||
331 | 2 | // Add the primary key(s) |
|
332 | 2 | if (!empty($newColumns)) { |
|
333 | 2 | if (!is_string($newColumns)) { |
|
334 | 2 | throw new \InvalidArgumentException(sprintf( |
|
335 | 2 | "Invalid value for primary key: %s", |
|
336 | 2 | json_encode($newColumns) |
|
337 | )); |
||
338 | 2 | } |
|
339 | 2 | ||
340 | 2 | $instructions->merge( |
|
341 | 2 | $this->getAddPrimaryKeyInstructions($table, $newColumns) |
|
342 | 2 | ); |
|
343 | 2 | } |
|
344 | 2 | ||
345 | 2 | return $instructions; |
|
346 | 2 | } |
|
347 | |||
348 | 2 | /** |
|
349 | 1 | * {@inheritdoc} |
|
350 | */ |
||
351 | 1 | protected function getChangeCommentInstructions(Table $table, $newComment) |
|
352 | { |
||
353 | throw new \BadMethodCallException('SQLite does not have table comments'); |
||
354 | 1 | } |
|
355 | |||
356 | 1 | /** |
|
357 | 1 | * {@inheritdoc} |
|
358 | 1 | */ |
|
359 | View Code Duplication | protected function getRenameTableInstructions($tableName, $newTableName) |
|
360 | 1 | { |
|
361 | 1 | $sql = sprintf( |
|
362 | 'ALTER TABLE %s RENAME TO %s', |
||
363 | $this->quoteTableName($tableName), |
||
364 | 1 | $this->quoteTableName($newTableName) |
|
365 | 1 | ); |
|
366 | 1 | ||
367 | 1 | return new AlterInstructions([], [$sql]); |
|
368 | 1 | } |
|
369 | |||
370 | 1 | /** |
|
371 | * {@inheritdoc} |
||
372 | 1 | */ |
|
373 | protected function getDropTableInstructions($tableName) |
||
374 | 1 | { |
|
375 | 1 | $sql = sprintf('DROP TABLE %s', $this->quoteTableName($tableName)); |
|
376 | |||
377 | return new AlterInstructions([], [$sql]); |
||
378 | } |
||
379 | |||
380 | 6 | /** |
|
381 | * {@inheritdoc} |
||
382 | */ |
||
383 | public function truncateTable($tableName) |
||
384 | 6 | { |
|
385 | $sql = sprintf( |
||
386 | 6 | 'DELETE FROM %s', |
|
387 | $this->quoteTableName($tableName) |
||
388 | 6 | ); |
|
389 | 6 | ||
390 | 6 | $this->execute($sql); |
|
391 | 6 | } |
|
392 | 6 | ||
393 | 6 | /** |
|
394 | * {@inheritdoc} |
||
395 | 6 | */ |
|
396 | 6 | public function getColumns($tableName) |
|
397 | 6 | { |
|
398 | 6 | $columns = []; |
|
399 | 6 | $rows = $this->fetchAll(sprintf('pragma table_info(%s)', $this->quoteTableName($tableName))); |
|
400 | 6 | ||
401 | 6 | foreach ($rows as $columnInfo) { |
|
402 | 6 | $column = new Column(); |
|
403 | 6 | $type = strtolower($columnInfo['type']); |
|
404 | $column->setName($columnInfo['name']) |
||
405 | 6 | ->setNull($columnInfo['notnull'] !== '1') |
|
406 | ->setDefault($columnInfo['dflt_value']); |
||
407 | |||
408 | $phinxType = $this->getPhinxType($type); |
||
409 | |||
410 | $column->setType($phinxType['name']) |
||
411 | 6 | ->setLimit($phinxType['limit']) |
|
412 | ->setScale($phinxType['scale']); |
||
413 | 6 | ||
414 | 6 | if ($columnInfo['pk'] == 1) { |
|
415 | 6 | $column->setIdentity(true); |
|
416 | 6 | } |
|
417 | |||
418 | 6 | $columns[] = $column; |
|
419 | } |
||
420 | 6 | ||
421 | return $columns; |
||
422 | 6 | } |
|
423 | 6 | ||
424 | 6 | /** |
|
425 | 6 | * {@inheritdoc} |
|
426 | 6 | */ |
|
427 | View Code Duplication | public function hasColumn($tableName, $columnName) |
|
428 | 6 | { |
|
429 | $rows = $this->fetchAll(sprintf('pragma table_info(%s)', $this->quoteTableName($tableName))); |
||
430 | 6 | foreach ($rows as $column) { |
|
431 | 6 | if (strcasecmp($column['name'], $columnName) === 0) { |
|
432 | 6 | return true; |
|
433 | } |
||
434 | } |
||
435 | |||
436 | return false; |
||
437 | 2 | } |
|
438 | |||
439 | /** |
||
440 | 2 | * {@inheritdoc} |
|
441 | */ |
||
442 | 2 | View Code Duplication | protected function getAddColumnInstructions(Table $table, Column $column) |
443 | { |
||
444 | 2 | $alter = sprintf( |
|
445 | 2 | 'ALTER TABLE %s ADD COLUMN %s %s', |
|
446 | 2 | $this->quoteTableName($table->getName()), |
|
447 | 2 | $this->quoteColumnName($column->getName()), |
|
448 | 2 | $this->getColumnSqlDefinition($column) |
|
449 | 2 | ); |
|
450 | |||
451 | 2 | return new AlterInstructions([], [$alter]); |
|
452 | 2 | } |
|
453 | 2 | ||
454 | 2 | /** |
|
455 | 2 | * Returns the original CREATE statement for the give table |
|
456 | 2 | * |
|
457 | 2 | * @param string $tableName The table name to get the create statement for |
|
458 | 2 | * @return string |
|
459 | 2 | */ |
|
460 | protected function getDeclaringSql($tableName) |
||
461 | 2 | { |
|
462 | $rows = $this->fetchAll('select * from sqlite_master where `type` = \'table\''); |
||
463 | 2 | ||
464 | $sql = ''; |
||
465 | foreach ($rows as $table) { |
||
466 | if ($table['tbl_name'] === $tableName) { |
||
467 | $sql = $table['sql']; |
||
468 | } |
||
469 | 2 | } |
|
470 | |||
471 | 2 | return $sql; |
|
472 | 2 | } |
|
473 | 2 | ||
474 | /** |
||
475 | 2 | * Copies all the data from a tmp table to another table |
|
476 | * |
||
477 | 2 | * @param string $tableName The table name to copy the data to |
|
478 | 2 | * @param string $tmpTableName The tmp table name where the data is stored |
|
479 | 2 | * @param string[] $writeColumns The list of columns in the target table |
|
480 | * @param string[] $selectColumns The list of columns in the tmp table |
||
481 | 2 | * @return void |
|
482 | */ |
||
483 | 2 | protected function copyDataToNewTable($tableName, $tmpTableName, $writeColumns, $selectColumns) |
|
484 | 2 | { |
|
485 | 2 | $sql = sprintf( |
|
486 | 2 | 'INSERT INTO %s(%s) SELECT %s FROM %s', |
|
487 | 2 | $this->quoteTableName($tableName), |
|
488 | implode(', ', $writeColumns), |
||
489 | 2 | implode(', ', $selectColumns), |
|
490 | $this->quoteTableName($tmpTableName) |
||
491 | 2 | ); |
|
492 | 2 | $this->execute($sql); |
|
493 | 2 | } |
|
494 | |||
495 | /** |
||
496 | * Modifies the passed instructions to copy all data from the tmp table into |
||
497 | * the provided table and then drops the tmp table. |
||
498 | * |
||
499 | * @param AlterInstructions $instructions The instructions to modify |
||
500 | * @param string $tableName The table name to copy the data to |
||
501 | 9 | * @return AlterInstructions |
|
502 | */ |
||
503 | 9 | protected function copyAndDropTmpTable($instructions, $tableName) |
|
504 | 9 | { |
|
505 | $instructions->addPostStep(function ($state) use ($tableName) { |
||
506 | 9 | $this->copyDataToNewTable( |
|
507 | 9 | $tableName, |
|
508 | 9 | $state['tmpTableName'], |
|
509 | 9 | $state['writeColumns'], |
|
510 | 9 | $state['selectColumns'] |
|
511 | 9 | ); |
|
512 | 9 | ||
513 | 9 | $this->execute(sprintf('DROP TABLE %s', $this->quoteTableName($state['tmpTableName']))); |
|
514 | 9 | ||
515 | 9 | return $state; |
|
516 | }); |
||
517 | |||
518 | return $instructions; |
||
519 | } |
||
520 | |||
521 | 9 | /** |
|
522 | * Returns the columns and type to use when copying a table to another in the process |
||
523 | 9 | * of altering a table |
|
524 | 4 | * |
|
525 | 4 | * @param string $tableName The table to modify |
|
526 | * @param string $columnName The column name that is about to change |
||
527 | 9 | * @param string|false $newColumnName Optionally the new name for the column |
|
528 | 9 | * @return AlterInstructions |
|
529 | */ |
||
530 | 9 | protected function calculateNewTableColumns($tableName, $columnName, $newColumnName) |
|
531 | 9 | { |
|
532 | 9 | $columns = $this->fetchAll(sprintf('pragma table_info(%s)', $this->quoteTableName($tableName))); |
|
533 | 9 | $selectColumns = []; |
|
534 | $writeColumns = []; |
||
535 | 8 | $columnType = null; |
|
536 | $found = false; |
||
537 | 8 | ||
538 | foreach ($columns as $column) { |
||
539 | $selectName = $column['name']; |
||
540 | $writeName = $selectName; |
||
541 | |||
542 | if ($selectName == $columnName) { |
||
543 | 1 | $writeName = $newColumnName; |
|
544 | $found = true; |
||
545 | 1 | $columnType = $column['type']; |
|
546 | $selectName = $newColumnName === false ? $newColumnName : $selectName; |
||
547 | 1 | } |
|
548 | 1 | ||
549 | 1 | $selectColumns[] = $selectName; |
|
550 | $writeColumns[] = $writeName; |
||
551 | } |
||
552 | |||
553 | $selectColumns = array_filter($selectColumns, 'strlen'); |
||
554 | $writeColumns = array_filter($writeColumns, 'strlen'); |
||
555 | $selectColumns = array_map([$this, 'quoteColumnName'], $selectColumns); |
||
556 | $writeColumns = array_map([$this, 'quoteColumnName'], $writeColumns); |
||
557 | |||
558 | if (!$found) { |
||
559 | 8 | throw new \InvalidArgumentException(sprintf( |
|
560 | 'The specified column doesn\'t exist: ' . $columnName |
||
561 | 8 | )); |
|
562 | 8 | } |
|
563 | 8 | ||
564 | 8 | return compact('writeColumns', 'selectColumns', 'columnType'); |
|
565 | 8 | } |
|
566 | 8 | ||
567 | 8 | /** |
|
568 | 8 | * Returns the initial instructions to alter a table using the |
|
569 | 8 | * rename-alter-copy strategy |
|
570 | 8 | * |
|
571 | * @param string $tableName The table to modify |
||
572 | 8 | * @return AlterInstructions |
|
573 | 8 | */ |
|
574 | 8 | protected function beginAlterByCopyTable($tableName) |
|
575 | { |
||
576 | $instructions = new AlterInstructions(); |
||
577 | $instructions->addPostStep(function ($state) use ($tableName) { |
||
578 | $createSQL = $this->getDeclaringSql($tableName); |
||
579 | 1 | ||
580 | $tmpTableName = 'tmp_' . $tableName; |
||
581 | 1 | $this->execute( |
|
582 | 1 | sprintf( |
|
583 | 1 | 'ALTER TABLE %s RENAME TO %s', |
|
584 | $this->quoteTableName($tableName), |
||
585 | 1 | $this->quoteTableName($tmpTableName) |
|
586 | 1 | ) |
|
587 | ); |
||
588 | 1 | ||
589 | 1 | return compact('createSQL', 'tmpTableName') + $state; |
|
590 | 1 | }); |
|
591 | 1 | ||
592 | 1 | return $instructions; |
|
593 | 1 | } |
|
594 | 1 | ||
595 | 1 | /** |
|
596 | 1 | * {@inheritdoc} |
|
597 | 1 | */ |
|
598 | protected function getRenameColumnInstructions($tableName, $columnName, $newColumnName) |
||
599 | { |
||
600 | $instructions = $this->beginAlterByCopyTable($tableName); |
||
601 | View Code Duplication | $instructions->addPostStep(function ($state) use ($columnName, $newColumnName) { |
|
602 | $newState = $this->calculateNewTableColumns($state['tmpTableName'], $columnName, $newColumnName); |
||
603 | |||
604 | return $newState + $state; |
||
605 | 1 | }); |
|
606 | |||
607 | 1 | $instructions->addPostStep(function ($state) use ($columnName, $newColumnName) { |
|
608 | $sql = str_replace( |
||
609 | 1 | $this->quoteColumnName($columnName), |
|
610 | 1 | $this->quoteColumnName($newColumnName), |
|
611 | 1 | $state['createSQL'] |
|
612 | 1 | ); |
|
613 | 1 | $this->execute($sql); |
|
614 | 1 | ||
615 | 1 | return $state; |
|
616 | 1 | }); |
|
617 | 1 | ||
618 | return $this->copyAndDropTmpTable($instructions, $tableName); |
||
619 | } |
||
620 | |||
621 | /** |
||
622 | * {@inheritdoc} |
||
623 | */ |
||
624 | protected function getChangeColumnInstructions($tableName, $columnName, Column $newColumn) |
||
625 | 5 | { |
|
626 | $instructions = $this->beginAlterByCopyTable($tableName); |
||
627 | 5 | ||
628 | $newColumnName = $newColumn->getName(); |
||
629 | View Code Duplication | $instructions->addPostStep(function ($state) use ($columnName, $newColumnName) { |
|
630 | 5 | $newState = $this->calculateNewTableColumns($state['tmpTableName'], $columnName, $newColumnName); |
|
631 | |||
632 | 5 | return $newState + $state; |
|
633 | 5 | }); |
|
634 | 5 | ||
635 | $instructions->addPostStep(function ($state) use ($columnName, $newColumn) { |
||
636 | 1 | $sql = preg_replace( |
|
637 | sprintf("/%s(?:\/\*.*?\*\/|\([^)]+\)|'[^']*?'|[^,])+([,)])/", $this->quoteColumnName($columnName)), |
||
638 | sprintf('%s %s$1', $this->quoteColumnName($newColumn->getName()), $this->getColumnSqlDefinition($newColumn)), |
||
639 | $state['createSQL'], |
||
640 | 1 |
||
641 | ); |
||
642 | $this->execute($sql); |
||
643 | |||
644 | return $state; |
||
645 | 5 | }); |
|
646 | |||
647 | 5 | return $this->copyAndDropTmpTable($instructions, $tableName); |
|
648 | 5 | } |
|
649 | |||
650 | /** |
||
651 | * {@inheritdoc} |
||
652 | */ |
||
653 | protected function getDropColumnInstructions($tableName, $columnName) |
||
654 | { |
||
655 | $instructions = $this->beginAlterByCopyTable($tableName); |
||
656 | |||
657 | View Code Duplication | $instructions->addPostStep(function ($state) use ($columnName) { |
|
658 | $newState = $this->calculateNewTableColumns($state['tmpTableName'], $columnName, false); |
||
659 | |||
660 | return $newState + $state; |
||
661 | 5 | }); |
|
662 | |||
663 | 5 | $instructions->addPostStep(function ($state) use ($columnName) { |
|
664 | 5 | $sql = preg_replace( |
|
665 | 5 | sprintf("/%s\s%s.*(,\s(?!')|\)$)/U", preg_quote($this->quoteColumnName($columnName)), preg_quote($state['columnType'])), |
|
666 | 5 | "", |
|
667 | 5 | $state['createSQL'] |
|
668 | 5 | ); |
|
669 | 5 | ||
670 | 5 | if (substr($sql, -2) === ', ') { |
|
671 | 5 | $sql = substr($sql, 0, -2) . ')'; |
|
672 | 5 | } |
|
673 | 5 | ||
674 | $this->execute($sql); |
||
675 | |||
676 | return $state; |
||
677 | }); |
||
678 | |||
679 | 4 | return $this->copyAndDropTmpTable($instructions, $tableName); |
|
680 | } |
||
681 | |||
682 | 4 | /** |
|
683 | * Get an array of indexes from a particular table. |
||
684 | 4 | * |
|
685 | 4 | * @param string $tableName Table Name |
|
686 | * @return array |
||
687 | 4 | */ |
|
688 | 4 | protected function getIndexes($tableName) |
|
689 | 4 | { |
|
690 | 4 | $indexes = []; |
|
691 | 4 | $schema = $this->getSchemaName($tableName, true)['schema']; |
|
692 | 4 | $indexList = $this->getTableInfo($tableName, 'index_list'); |
|
693 | |||
694 | 4 | foreach ($indexList as $index) { |
|
695 | 4 | $indexData = $this->fetchAll(sprintf('pragma %sindex_info(%s)', $schema, $this->quoteColumnName($index['name']))); |
|
696 | 4 | $cols = []; |
|
697 | 4 | foreach ($indexData as $indexItem) { |
|
698 | 4 | $cols[] = $indexItem['name']; |
|
699 | } |
||
700 | 4 | $indexes[$index['name']] = $cols; |
|
701 | } |
||
702 | 4 | ||
703 | 4 | return $indexes; |
|
704 | } |
||
705 | 4 | ||
706 | 4 | /** |
|
707 | 4 | * Finds the names of a table's indexes matching the supplied columns |
|
708 | 4 | * |
|
709 | 4 | * @param string $tableName The table to which the index belongs |
|
710 | 4 | * @param string|string[] $columns The columns of the index |
|
711 | 4 | * @return array |
|
712 | */ |
||
713 | 4 | protected function resolveIndex($tableName, $columns) |
|
714 | 4 | { |
|
715 | 4 | $columns = array_map('strtolower', (array)$columns); |
|
716 | $indexes = $this->getIndexes($tableName); |
||
717 | $matches = []; |
||
718 | |||
719 | foreach ($indexes as $name => $index) { |
||
720 | 1 | $indexCols = array_map('strtolower', $index); |
|
721 | if ($columns == $indexCols) { |
||
722 | $matches[] = $name; |
||
723 | 1 | } |
|
724 | } |
||
725 | |||
726 | return $matches; |
||
727 | 1 | } |
|
728 | |||
729 | 1 | /** |
|
730 | * {@inheritdoc} |
||
731 | 1 | */ |
|
732 | 1 | public function hasIndex($tableName, $columns) |
|
733 | 1 | { |
|
734 | 1 | return (bool)$this->resolveIndex($tableName, $columns); |
|
735 | 1 | } |
|
736 | 1 | ||
737 | /** |
||
738 | 1 | * {@inheritdoc} |
|
739 | 1 | */ |
|
740 | 1 | public function hasIndexByName($tableName, $indexName) |
|
741 | 1 | { |
|
742 | 1 | $indexName = strtolower($indexName); |
|
743 | 1 | $indexes = $this->getIndexes($tableName); |
|
744 | 1 | ||
745 | foreach (array_keys($indexes) as $index) { |
||
746 | 1 | if ($indexName === strtolower($index)) { |
|
747 | return true; |
||
748 | 1 | } |
|
749 | } |
||
750 | |||
751 | return false; |
||
752 | } |
||
753 | |||
754 | 1 | /** |
|
755 | * {@inheritdoc} |
||
756 | 1 | */ |
|
757 | 1 | protected function getAddIndexInstructions(Table $table, Index $index) |
|
758 | 1 | { |
|
759 | 1 | $indexColumnArray = []; |
|
760 | 1 | foreach ($index->getColumns() as $column) { |
|
761 | 1 | $indexColumnArray[] = sprintf('`%s` ASC', $column); |
|
762 | 1 | } |
|
763 | $indexColumns = implode(',', $indexColumnArray); |
||
764 | 1 | $sql = sprintf( |
|
765 | 'CREATE %s ON %s (%s)', |
||
766 | 1 | $this->getIndexSqlDefinition($table, $index), |
|
767 | 1 | $this->quoteTableName($table->getName()), |
|
768 | 1 | $indexColumns |
|
769 | 1 | ); |
|
770 | 1 | ||
771 | return new AlterInstructions([], [$sql]); |
||
772 | 1 | } |
|
773 | |||
774 | 1 | /** |
|
775 | 1 | * {@inheritdoc} |
|
776 | 1 | */ |
|
777 | protected function getDropIndexByColumnsInstructions($tableName, $columns) |
||
778 | { |
||
779 | $instructions = new AlterInstructions(); |
||
780 | $indexNames = $this->resolveIndex($tableName, $columns); |
||
781 | $schema = $this->getSchemaName($tableName, true)['schema']; |
||
782 | foreach ($indexNames as $indexName) { |
||
783 | if (strpos($indexName, 'sqlite_autoindex_') !== 0) { |
||
784 | $instructions->addPostStep(sprintf( |
||
785 | 'DROP INDEX %s%s', |
||
786 | $schema, |
||
787 | $this->quoteColumnName($indexName) |
||
788 | )); |
||
789 | } |
||
790 | } |
||
791 | |||
792 | return $instructions; |
||
793 | } |
||
794 | |||
795 | /** |
||
796 | * {@inheritdoc} |
||
797 | */ |
||
798 | protected function getDropIndexByNameInstructions($tableName, $indexName) |
||
823 | 38 | ||
824 | /** |
||
825 | 43 | * {@inheritdoc} |
|
826 | 42 | */ |
|
827 | public function hasPrimaryKey($tableName, $columns, $constraint = null) |
||
828 | 43 | { |
|
829 | 2 | if (!is_null($constraint)) { |
|
830 | throw new \InvalidArgumentException('SQLite does not support named constraints.'); |
||
831 | 43 | } |
|
832 | 1 | ||
833 | $columns = array_map('strtolower', (array)$columns); |
||
834 | 43 | $primaryKey = array_map('strtolower', $this->getPrimaryKey($tableName)); |
|
835 | 1 | ||
836 | if (array_diff($primaryKey, $columns) || array_diff($columns, $primaryKey)) { |
||
837 | 43 | return false; |
|
838 | 42 | } |
|
839 | |||
840 | 43 | return true; |
|
841 | 1 | } |
|
842 | |||
843 | 43 | /** |
|
844 | 1 | * Get the primary key from a particular table. |
|
845 | * |
||
846 | 43 | * @param string $tableName Table Name |
|
847 | 43 | * @return string[] |
|
848 | 1 | */ |
|
849 | protected function getPrimaryKey($tableName) |
||
863 | |||
864 | 1 | /** |
|
865 | * {@inheritdoc} |
||
866 | */ |
||
867 | 1 | public function hasForeignKey($tableName, $columns, $constraint = null) |
|
886 | 2 | ||
887 | 2 | /** |
|
888 | 2 | * Get an array of foreign keys from a particular table. |
|
889 | 1 | * |
|
890 | 1 | * @param string $tableName Table Name |
|
891 | 2 | * @return array |
|
892 | */ |
||
893 | protected function getForeignKeys($tableName) |
||
908 | |||
909 | /** |
||
910 | 2 | * @param Table $table The Table |
|
911 | * @param string $column Column Name |
||
912 | * @return AlterInstructions |
||
913 | */ |
||
914 | protected function getAddPrimaryKeyInstructions(Table $table, $column) |
||
951 | |||
952 | /** |
||
953 | * @param Table $table Table |
||
954 | 2 | * @param string $column Column Name |
|
955 | * @return AlterInstructions |
||
956 | 2 | */ |
|
957 | protected function getDropPrimaryKeyInstructions($table, $column) |
||
980 | 42 | ||
981 | 42 | /** |
|
982 | 42 | * {@inheritdoc} |
|
983 | */ |
||
984 | protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreignKey) |
||
1007 | 42 | ||
1008 | /** |
||
1009 | 42 | * {@inheritdoc} |
|
1010 | 42 | */ |
|
1011 | 42 | protected function getDropForeignKeyInstructions($tableName, $constraint) |
|
1015 | |||
1016 | /** |
||
1017 | 42 | * {@inheritdoc} |
|
1018 | */ |
||
1019 | 42 | protected function getDropForeignKeyByColumnsInstructions($tableName, $columns) |
|
1059 | 8 | ||
1060 | /** |
||
1061 | * {@inheritdoc} |
||
1062 | */ |
||
1063 | public function getSqlType($type, $limit = null) |
||
1109 | |||
1110 | /** |
||
1111 | * Returns Phinx type by SQL type |
||
1112 | * |
||
1113 | * @param string $sqlTypeDef SQL type |
||
1114 | * @throws UnsupportedColumnTypeException |
||
1115 | * @return array |
||
1116 | */ |
||
1117 | public function getPhinxType($sqlTypeDef) |
||
1190 | |||
1191 | /** |
||
1192 | * {@inheritdoc} |
||
1193 | */ |
||
1194 | public function createDatabase($name, $options = []) |
||
1198 | |||
1199 | /** |
||
1200 | * {@inheritdoc} |
||
1201 | */ |
||
1202 | public function hasDatabase($name) |
||
1206 | |||
1207 | /** |
||
1208 | * {@inheritdoc} |
||
1209 | */ |
||
1210 | public function dropDatabase($name) |
||
1220 | |||
1221 | /** |
||
1222 | * Gets the SQLite Column Definition for a Column object. |
||
1223 | * |
||
1224 | * @param \Phinx\Db\Table\Column $column Column |
||
1225 | * @return string |
||
1226 | */ |
||
1227 | protected function getColumnSqlDefinition(Column $column) |
||
1262 | |||
1263 | /** |
||
1264 | * Gets the comment Definition for a Column object. |
||
1265 | * |
||
1266 | * @param \Phinx\Db\Table\Column $column Column |
||
1267 | * @return string |
||
1268 | */ |
||
1269 | protected function getCommentDefinition(Column $column) |
||
1277 | |||
1278 | /** |
||
1279 | * Gets the SQLite Index Definition for an Index object. |
||
1280 | * |
||
1281 | * @param \Phinx\Db\Table\Table $table Table |
||
1282 | * @param \Phinx\Db\Table\Index $index Index |
||
1283 | * @return string |
||
1284 | */ |
||
1285 | protected function getIndexSqlDefinition(Table $table, Index $index) |
||
1305 | |||
1306 | /** |
||
1307 | * {@inheritdoc} |
||
1308 | */ |
||
1309 | public function getColumnTypes() |
||
1310 | { |
||
1311 | return array_merge(parent::getColumnTypes(), ['enum', 'json', 'jsonb']); |
||
1312 | } |
||
1313 | |||
1314 | /** |
||
1315 | * Gets the SQLite Foreign Key Definition for an ForeignKey object. |
||
1316 | * |
||
1317 | * @param \Phinx\Db\Table\ForeignKey $foreignKey |
||
1318 | * @return string |
||
1319 | */ |
||
1320 | View Code Duplication | protected function getForeignKeySqlDefinition(ForeignKey $foreignKey) |
|
1346 | |||
1347 | /** |
||
1348 | * {@inheritDoc} |
||
1349 | * |
||
1350 | */ |
||
1351 | public function getDecoratedConnection() |
||
1374 | } |
||
1375 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.