Complex classes like SQLite3SchemaManager 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 SQLite3SchemaManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | use Debug; |
||
9 | use Director; |
||
10 | |||
11 | /** |
||
12 | * SQLite schema manager class |
||
13 | * |
||
14 | * @package SQLite3 |
||
15 | */ |
||
16 | class SQLite3SchemaManager extends DBSchemaManager |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * Instance of the database controller this schema belongs to |
||
21 | * |
||
22 | * @var SQLite3Database |
||
23 | */ |
||
24 | protected $database = null; |
||
25 | |||
26 | /** |
||
27 | * Flag indicating whether or not the database has been checked and repaired |
||
28 | * |
||
29 | * @var boolean |
||
30 | */ |
||
31 | protected static $checked_and_repaired = false; |
||
32 | |||
33 | /** |
||
34 | * Should schema be vacuumed during checkeAndRepairTable? |
||
35 | * |
||
36 | * @var boolean |
||
37 | */ |
||
38 | public static $vacuum = true; |
||
39 | |||
40 | public function createDatabase($name) |
||
45 | |||
46 | public function dropDatabase($name) |
||
60 | |||
61 | public function databaseList() |
||
94 | |||
95 | public function databaseExists($name) |
||
100 | |||
101 | /** |
||
102 | * Empties any cached enum values |
||
103 | */ |
||
104 | public function flushCache() |
||
108 | |||
109 | public function schemaUpdate($callback) |
||
131 | |||
132 | /** |
||
133 | * Empty a specific table |
||
134 | * |
||
135 | * @param string $table |
||
136 | */ |
||
137 | public function clearTable($table) |
||
143 | |||
144 | public function createTable($table, $fields = null, $indexes = null, $options = null, $advancedOptions = null) |
||
172 | |||
173 | public function alterTable($tableName, $newFields = null, $newIndexes = null, $alteredFields = null, |
||
200 | |||
201 | public function renameTable($oldTableName, $newTableName) |
||
205 | |||
206 | public function checkAndRepairTable($tableName = null) |
||
238 | |||
239 | public function createField($table, $field, $spec) |
||
243 | |||
244 | /** |
||
245 | * Change the database type of the given field. |
||
246 | * @param string $tableName The name of the tbale the field is in. |
||
247 | * @param string $fieldName The name of the field to change. |
||
248 | * @param string $fieldSpec The new field specification |
||
249 | */ |
||
250 | public function alterField($tableName, $fieldName, $fieldSpec) |
||
292 | |||
293 | public function renameField($tableName, $oldName, $newName) |
||
335 | |||
336 | public function fieldList($table) |
||
360 | |||
361 | /** |
||
362 | * Create an index on a table. |
||
363 | * |
||
364 | * @param string $tableName The name of the table. |
||
365 | * @param string $indexName The name of the index. |
||
366 | * @param array $indexSpec The specification of the index, see Database::requireIndex() for more details. |
||
367 | */ |
||
368 | public function createIndex($tableName, $indexName, $indexSpec) |
||
376 | |||
377 | public function alterIndex($tableName, $indexName, $indexSpec) |
||
386 | |||
387 | /** |
||
388 | * Builds the internal SQLLite index name given the silverstripe table and index name. |
||
389 | * |
||
390 | * The name is built using the table and index name in order to prevent name collisions |
||
391 | * between indexes of the same name across multiple tables |
||
392 | * |
||
393 | * @param string $tableName |
||
394 | * @param string $indexName |
||
395 | * @return string The SQLite3 name of the index |
||
396 | */ |
||
397 | protected function buildSQLiteIndexName($tableName, $indexName) |
||
401 | |||
402 | protected function parseIndexSpec($name, $spec) |
||
413 | |||
414 | public function indexKey($table, $index, $spec) |
||
418 | |||
419 | public function indexList($table) |
||
446 | |||
447 | public function tableList() |
||
457 | |||
458 | /** |
||
459 | * Return a boolean type-formatted string |
||
460 | * |
||
461 | * @param array $values Contains a tokenised list of info about this data type |
||
462 | * @return string |
||
463 | */ |
||
464 | public function boolean($values) |
||
469 | |||
470 | /** |
||
471 | * Return a date type-formatted string |
||
472 | * |
||
473 | * @param array $values Contains a tokenised list of info about this data type |
||
474 | * @return string |
||
475 | */ |
||
476 | public function date($values) |
||
480 | |||
481 | /** |
||
482 | * Return a decimal type-formatted string |
||
483 | * |
||
484 | * @param array $values Contains a tokenised list of info about this data type |
||
485 | * @return string |
||
486 | */ |
||
487 | public function decimal($values) |
||
492 | |||
493 | /** |
||
494 | * Cached list of enum values indexed by table.column |
||
495 | * |
||
496 | * @var array |
||
497 | */ |
||
498 | protected $enum_map = array(); |
||
499 | |||
500 | /** |
||
501 | * Return a enum type-formatted string |
||
502 | * |
||
503 | * enums are not supported. as a workaround to store allowed values we creates an additional table |
||
504 | * |
||
505 | * @param array $values Contains a tokenised list of info about this data type |
||
506 | * @return string |
||
507 | */ |
||
508 | public function enum($values) |
||
535 | |||
536 | /** |
||
537 | * Return a set type-formatted string |
||
538 | * This type doesn't exist in SQLite either |
||
539 | * |
||
540 | * @see SQLite3SchemaManager::enum() |
||
541 | * |
||
542 | * @param array $values Contains a tokenised list of info about this data type |
||
543 | * @return string |
||
544 | */ |
||
545 | public function set($values) |
||
549 | |||
550 | /** |
||
551 | * Return a float type-formatted string |
||
552 | * |
||
553 | * @param array $values Contains a tokenised list of info about this data type |
||
554 | * @return string |
||
555 | */ |
||
556 | public function float($values) |
||
560 | |||
561 | /** |
||
562 | * Return a Double type-formatted string |
||
563 | * |
||
564 | * @param array $values Contains a tokenised list of info about this data type |
||
565 | * @return string |
||
566 | */ |
||
567 | public function double($values) |
||
571 | |||
572 | /** |
||
573 | * Return a int type-formatted string |
||
574 | * |
||
575 | * @param array $values Contains a tokenised list of info about this data type |
||
576 | * @return string |
||
577 | */ |
||
578 | public function int($values) |
||
582 | |||
583 | /** |
||
584 | * Return a bigint type-formatted string |
||
585 | * |
||
586 | * @param array $values Contains a tokenised list of info about this data type |
||
587 | * @return string |
||
588 | */ |
||
589 | public function bigint($values) |
||
593 | |||
594 | /** |
||
595 | * Return a datetime type-formatted string |
||
596 | * For SQLite3, we simply return the word 'TEXT', no other parameters are necessary |
||
597 | * |
||
598 | * @param array $values Contains a tokenised list of info about this data type |
||
599 | * @return string |
||
600 | */ |
||
601 | public function datetime($values) |
||
605 | |||
606 | /** |
||
607 | * Return a text type-formatted string |
||
608 | * |
||
609 | * @param array $values Contains a tokenised list of info about this data type |
||
610 | * @return string |
||
611 | */ |
||
612 | public function text($values) |
||
616 | |||
617 | /** |
||
618 | * Return a time type-formatted string |
||
619 | * |
||
620 | * @param array $values Contains a tokenised list of info about this data type |
||
621 | * @return string |
||
622 | */ |
||
623 | public function time($values) |
||
627 | |||
628 | /** |
||
629 | * Return a varchar type-formatted string |
||
630 | * |
||
631 | * @param array $values Contains a tokenised list of info about this data type |
||
632 | * @return string |
||
633 | */ |
||
634 | public function varchar($values) |
||
638 | |||
639 | /* |
||
640 | * Return a 4 digit numeric type. MySQL has a proprietary 'Year' type. |
||
641 | * For SQLite3 we use TEXT |
||
642 | */ |
||
643 | public function year($values, $asDbValue = false) |
||
647 | |||
648 | public function IdColumn($asDbValue = false, $hasAutoIncPK = true) |
||
652 | |||
653 | public function hasTable($tableName) |
||
660 | |||
661 | /** |
||
662 | * Return enum values for the given field |
||
663 | * |
||
664 | * @param string $tableName |
||
665 | * @param string $fieldName |
||
666 | * @return array |
||
667 | */ |
||
668 | public function enumValuesForField($tableName, $fieldName) |
||
691 | |||
692 | public function dbDataType($type) |
||
693 | { |
||
694 | $values = array( |
||
695 | 'unsigned integer' => 'INT' |
||
705 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.