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 MSSQLSchemaManager 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 MSSQLSchemaManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class MSSQLSchemaManager extends DBSchemaManager |
||
|
|
|||
| 9 | { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Stores per-request cached constraint checks that come from the database. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected static $cached_checks = array(); |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Builds the internal MS SQL Server index name given the silverstripe table and index name |
||
| 20 | * |
||
| 21 | * @param string $tableName |
||
| 22 | * @param string $indexName |
||
| 23 | * @param string $prefix The optional prefix for the index. Defaults to "ix" for indexes. |
||
| 24 | * @return string The name of the index |
||
| 25 | */ |
||
| 26 | public function buildMSSQLIndexName($tableName, $indexName, $prefix = 'ix') |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * This will set up the full text search capabilities. |
||
| 39 | * |
||
| 40 | * @param string $name Name of full text catalog to use |
||
| 41 | */ |
||
| 42 | public function createFullTextCatalog($name = 'ftCatalog') |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Check that a fulltext catalog has been created yet. |
||
| 52 | * |
||
| 53 | * @param string $name Name of full text catalog to use |
||
| 54 | * @return boolean |
||
| 55 | */ |
||
| 56 | public function fullTextCatalogExists($name = 'ftCatalog') |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Sleep until the catalog has been fully rebuilt. This is a busy wait designed for situations |
||
| 66 | * when you need to be sure the index is up to date - for example in unit tests. |
||
| 67 | * |
||
| 68 | * TODO: move this to Database class? Can we assume this will be useful for all databases? |
||
| 69 | * Also see the wrapper functions "waitUntilIndexingFinished" in SearchFormTest and TranslatableSearchFormTest |
||
| 70 | * |
||
| 71 | * @param int $maxWaitingTime Time in seconds to wait for the database. |
||
| 72 | */ |
||
| 73 | public function waitUntilIndexingFinished($maxWaitingTime = 15) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Check if a fulltext index exists on a particular table name. |
||
| 96 | * |
||
| 97 | * @param string $tableName |
||
| 98 | * @return boolean TRUE index exists | FALSE index does not exist | NULL no support |
||
| 99 | */ |
||
| 100 | public function fulltextIndexExists($tableName) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * MSSQL stores the primary key column with an internal identifier, |
||
| 117 | * so a lookup needs to be done to determine it. |
||
| 118 | * |
||
| 119 | * @param string $tableName Name of table with primary key column "ID" |
||
| 120 | * @return string Internal identifier for primary key |
||
| 121 | */ |
||
| 122 | public function getPrimaryKey($tableName) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Gets the identity column of a table |
||
| 138 | * |
||
| 139 | * @param string $tableName |
||
| 140 | * @return string|null |
||
| 141 | */ |
||
| 142 | public function getIdentityColumn($tableName) |
||
| 156 | |||
| 157 | public function createDatabase($name) |
||
| 161 | |||
| 162 | public function dropDatabase($name) |
||
| 166 | |||
| 167 | public function databaseExists($name) |
||
| 177 | |||
| 178 | public function databaseList() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Create a new table. |
||
| 185 | * @param $tableName The name of the table |
||
| 186 | * @param $fields A map of field names to field types |
||
| 187 | * @param $indexes A map of indexes |
||
| 188 | * @param $options An map of additional options. The available keys are as follows: |
||
| 189 | * - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL. |
||
| 190 | * - 'temporary' - If true, then a temporary table will be created |
||
| 191 | * @return The table name generated. This may be different from the table name, for example with temporary tables. |
||
| 192 | */ |
||
| 193 | public function createTable($tableName, $fields = null, $indexes = null, $options = null, $advancedOptions = null) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Alter a table's schema. |
||
| 230 | * @param $table The name of the table to alter |
||
| 231 | * @param $newFields New fields, a map of field name => field schema |
||
| 232 | * @param $newIndexes New indexes, a map of index name => index type |
||
| 233 | * @param $alteredFields Updated fields, a map of field name => field schema |
||
| 234 | * @param $alteredIndexes Updated indexes, a map of index name => index type |
||
| 235 | */ |
||
| 236 | public function alterTable($tableName, $newFields = null, $newIndexes = null, $alteredFields = null, $alteredIndexes = null, $alteredOptions=null, $advancedOptions=null) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Given the table and column name, retrieve the constraint name for that column |
||
| 278 | * in the table. |
||
| 279 | * |
||
| 280 | * @param string $tableName Table name column resides in |
||
| 281 | * @param string $columnName Column name the constraint is for |
||
| 282 | * @return string|null |
||
| 283 | */ |
||
| 284 | public function getConstraintName($tableName, $columnName) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Given a table and column name, return a check constraint clause for that column in |
||
| 296 | * the table. |
||
| 297 | * |
||
| 298 | * This is an expensive query, so it is cached per-request and stored by table. The initial |
||
| 299 | * call for a table that has not been cached will query all columns and store that |
||
| 300 | * so subsequent calls are fast. |
||
| 301 | * |
||
| 302 | * @param string $tableName Table name column resides in |
||
| 303 | * @param string $columnName Column name the constraint is for |
||
| 304 | * @return string The check string |
||
| 305 | */ |
||
| 306 | public function getConstraintCheckClause($tableName, $columnName) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Return the name of the default constraint applied to $tableName.$colName. |
||
| 335 | * Will return null if no such constraint exists |
||
| 336 | * |
||
| 337 | * @param string $tableName Name of the table |
||
| 338 | * @param string $colName Name of the column |
||
| 339 | * @return string|null |
||
| 340 | */ |
||
| 341 | protected function defaultConstraintName($tableName, $colName) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get enum values from a constraint check clause. |
||
| 357 | * |
||
| 358 | * @param string $clause Check clause to parse values from |
||
| 359 | * @return array Enum values |
||
| 360 | */ |
||
| 361 | protected function enumValuesFromCheckClause($clause) |
||
| 373 | |||
| 374 | /* |
||
| 375 | * Creates an ALTER expression for a column in MS SQL |
||
| 376 | * |
||
| 377 | * @param string $tableName Name of the table to be altered |
||
| 378 | * @param string $colName Name of the column to be altered |
||
| 379 | * @param string $colSpec String which contains conditions for a column |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | protected function alterTableAlterColumn($tableName, $colName, $colSpec) |
||
| 437 | |||
| 438 | public function renameTable($oldTableName, $newTableName) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Checks a table's integrity and repairs it if necessary. |
||
| 445 | * NOTE: MSSQL does not appear to support any vacuum or optimise commands |
||
| 446 | * |
||
| 447 | * @var string $tableName The name of the table. |
||
| 448 | * @return boolean Return true if the table has integrity after the method is complete. |
||
| 449 | */ |
||
| 450 | public function checkAndRepairTable($tableName) |
||
| 454 | |||
| 455 | public function createField($tableName, $fieldName, $fieldSpec) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Change the database type of the given field. |
||
| 462 | * @param string $tableName The name of the tbale the field is in. |
||
| 463 | * @param string $fieldName The name of the field to change. |
||
| 464 | * @param string $fieldSpec The new field specification |
||
| 465 | */ |
||
| 466 | public function alterField($tableName, $fieldName, $fieldSpec) |
||
| 470 | |||
| 471 | public function renameField($tableName, $oldName, $newName) |
||
| 475 | |||
| 476 | public function fieldList($table) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Create an index on a table. |
||
| 578 | * @param string $tableName The name of the table. |
||
| 579 | * @param string $indexName The name of the index. |
||
| 580 | * @param string $indexSpec The specification of the index, see SS_Database::requireIndex() for more details. |
||
| 581 | */ |
||
| 582 | public function createIndex($tableName, $indexName, $indexSpec) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Return SQL for dropping and recreating an index |
||
| 589 | * |
||
| 590 | * @param string $tableName Name of table to create this index against |
||
| 591 | * @param string $indexName Name of this index |
||
| 592 | * @param array|string $indexSpec Index specification, either as a raw string |
||
| 593 | * or parsed array form |
||
| 594 | * @return string The SQL required to generate this index |
||
| 595 | */ |
||
| 596 | protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec) |
||
| 625 | |||
| 626 | public function alterIndex($tableName, $indexName, $indexSpec) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Return the list of indexes in a table. |
||
| 633 | * @param string $table The table name. |
||
| 634 | * @return array |
||
| 635 | */ |
||
| 636 | public function indexList($table) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * For a given table name, get all the internal index names, |
||
| 688 | * except for those that are primary keys and fulltext indexes. |
||
| 689 | * |
||
| 690 | * @return array |
||
| 691 | */ |
||
| 692 | public function indexNames($tableName) |
||
| 701 | |||
| 702 | public function tableList() |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Return a boolean type-formatted string |
||
| 713 | * We use 'bit' so that we can do numeric-based comparisons |
||
| 714 | * |
||
| 715 | * @params array $values Contains a tokenised list of info about this data type |
||
| 716 | * @return string |
||
| 717 | */ |
||
| 718 | public function boolean($values) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Return a date type-formatted string. |
||
| 726 | * |
||
| 727 | * @params array $values Contains a tokenised list of info about this data type |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | public function date($values) |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Return a decimal type-formatted string |
||
| 737 | * |
||
| 738 | * @params array $values Contains a tokenised list of info about this data type |
||
| 739 | * @return string |
||
| 740 | */ |
||
| 741 | public function decimal($values) |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Return a enum type-formatted string |
||
| 760 | * |
||
| 761 | * @params array $values Contains a tokenised list of info about this data type |
||
| 762 | * @return string |
||
| 763 | */ |
||
| 764 | public function enum($values) |
||
| 776 | |||
| 777 | /** |
||
| 778 | * @todo Make this work like {@link MySQLDatabase::set()} |
||
| 779 | */ |
||
| 780 | public function set($values) |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Return a float type-formatted string. |
||
| 787 | * |
||
| 788 | * @params array $values Contains a tokenised list of info about this data type |
||
| 789 | * @return string |
||
| 790 | */ |
||
| 791 | public function float($values) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Return a int type-formatted string |
||
| 798 | * |
||
| 799 | * @params array $values Contains a tokenised list of info about this data type |
||
| 800 | * @return string |
||
| 801 | */ |
||
| 802 | public function int($values) |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Return a bigint type-formatted string |
||
| 809 | * |
||
| 810 | * @params array $values Contains a tokenised list of info about this data type |
||
| 811 | * @return string |
||
| 812 | */ |
||
| 813 | public function bigint($values) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Return a datetime type-formatted string |
||
| 820 | * For MS SQL, we simply return the word 'timestamp', no other parameters are necessary |
||
| 821 | * |
||
| 822 | * @params array $values Contains a tokenised list of info about this data type |
||
| 823 | * @return string |
||
| 824 | */ |
||
| 825 | public function ss_datetime($values) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Return a text type-formatted string |
||
| 832 | * |
||
| 833 | * @params array $values Contains a tokenised list of info about this data type |
||
| 834 | * @return string |
||
| 835 | */ |
||
| 836 | public function text($values) |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Return a time type-formatted string. |
||
| 845 | * |
||
| 846 | * @params array $values Contains a tokenised list of info about this data type |
||
| 847 | * @return string |
||
| 848 | */ |
||
| 849 | public function time($values) |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Return a varchar type-formatted string |
||
| 856 | * |
||
| 857 | * @params array $values Contains a tokenised list of info about this data type |
||
| 858 | * @return string |
||
| 859 | */ |
||
| 860 | public function varchar($values) |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Return a 4 digit numeric type. |
||
| 869 | * @return string |
||
| 870 | */ |
||
| 871 | public function year($values) |
||
| 875 | |||
| 876 | /** |
||
| 877 | * This returns the column which is the primary key for each table |
||
| 878 | * @return string |
||
| 879 | */ |
||
| 880 | public function IdColumn($asDbValue = false, $hasAutoIncPK = true) |
||
| 890 | |||
| 891 | public function hasTable($tableName) |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Returns the values of the given enum field |
||
| 901 | * NOTE: Experimental; introduced for db-abstraction and may changed before 2.4 is released. |
||
| 902 | */ |
||
| 903 | public function enumValuesForField($tableName, $fieldName) |
||
| 915 | |||
| 916 | /** |
||
| 917 | * This is a lookup table for data types. |
||
| 918 | * |
||
| 919 | * For instance, MSSQL uses 'BIGINT', while MySQL uses 'UNSIGNED' |
||
| 920 | * and PostgreSQL uses 'INT'. |
||
| 921 | */ |
||
| 922 | public function dbDataType($type) |
||
| 933 | |||
| 934 | protected function indexKey($table, $index, $spec) |
||
| 938 | } |
||
| 939 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.