Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class QueryBuilder extends \yii\db\QueryBuilder |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
| 25 | */ |
||
| 26 | public $typeMap = [ |
||
| 27 | Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
| 28 | Schema::TYPE_UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
| 29 | Schema::TYPE_BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
| 30 | Schema::TYPE_UBIGPK => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
| 31 | Schema::TYPE_CHAR => 'char(1)', |
||
| 32 | Schema::TYPE_STRING => 'varchar(255)', |
||
| 33 | Schema::TYPE_TEXT => 'text', |
||
| 34 | Schema::TYPE_SMALLINT => 'smallint(6)', |
||
| 35 | Schema::TYPE_INTEGER => 'int(11)', |
||
| 36 | Schema::TYPE_BIGINT => 'bigint(20)', |
||
| 37 | Schema::TYPE_FLOAT => 'float', |
||
| 38 | Schema::TYPE_DOUBLE => 'double', |
||
| 39 | Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||
| 40 | Schema::TYPE_DATETIME => 'datetime', |
||
| 41 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
| 42 | Schema::TYPE_TIME => 'time', |
||
| 43 | Schema::TYPE_DATE => 'date', |
||
| 44 | Schema::TYPE_BINARY => 'blob', |
||
| 45 | Schema::TYPE_BOOLEAN => 'tinyint(1)', |
||
| 46 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
| 47 | ]; |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Builds a SQL statement for renaming a column. |
||
| 52 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
| 53 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
| 54 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
| 55 | * @return string the SQL statement for renaming a DB column. |
||
| 56 | * @throws Exception |
||
| 57 | */ |
||
| 58 | public function renameColumn($table, $oldName, $newName) |
||
| 59 | { |
||
| 60 | $quotedTable = $this->db->quoteTableName($table); |
||
| 61 | $row = $this->db->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryOne(); |
||
| 62 | if ($row === false) { |
||
| 63 | throw new Exception("Unable to find column '$oldName' in table '$table'."); |
||
| 64 | } |
||
| 65 | if (isset($row['Create Table'])) { |
||
| 66 | $sql = $row['Create Table']; |
||
| 67 | } else { |
||
| 68 | $row = array_values($row); |
||
| 69 | $sql = $row[1]; |
||
| 70 | } |
||
| 71 | if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) { |
||
| 72 | foreach ($matches[1] as $i => $c) { |
||
| 73 | if ($c === $oldName) { |
||
| 74 | return "ALTER TABLE $quotedTable CHANGE " |
||
| 75 | . $this->db->quoteColumnName($oldName) . ' ' |
||
| 76 | . $this->db->quoteColumnName($newName) . ' ' |
||
| 77 | . $matches[2][$i]; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | // try to give back a SQL anyway |
||
| 82 | return "ALTER TABLE $quotedTable CHANGE " |
||
| 83 | . $this->db->quoteColumnName($oldName) . ' ' |
||
| 84 | . $this->db->quoteColumnName($newName); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritdoc |
||
| 89 | * @see https://bugs.mysql.com/bug.php?id=48875 |
||
| 90 | */ |
||
| 91 | 12 | public function createIndex($name, $table, $columns, $unique = false) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Builds a SQL statement for dropping a foreign key constraint. |
||
| 102 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
| 103 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
| 104 | * @return string the SQL statement for dropping a foreign key constraint. |
||
| 105 | */ |
||
| 106 | 3 | public function dropForeignKey($name, $table) |
|
| 107 | { |
||
| 108 | 3 | return 'ALTER TABLE ' . $this->db->quoteTableName($table) |
|
| 109 | 3 | . ' DROP FOREIGN KEY ' . $this->db->quoteColumnName($name); |
|
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
| 114 | * @param string $name the name of the primary key constraint to be removed. |
||
| 115 | * @param string $table the table that the primary key constraint will be removed from. |
||
| 116 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
| 117 | */ |
||
| 118 | 2 | public function dropPrimaryKey($name, $table) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @inheritDoc |
||
| 125 | */ |
||
| 126 | 2 | public function dropUnique($name, $table) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @inheritDoc |
||
| 133 | * @throws NotSupportedException this is not supported by MySQL. |
||
| 134 | */ |
||
| 135 | public function addCheck($name, $table, $expression) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @inheritDoc |
||
| 142 | * @throws NotSupportedException this is not supported by MySQL. |
||
| 143 | */ |
||
| 144 | public function dropCheck($name, $table) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
| 151 | * The sequence will be reset such that the primary key of the next new row inserted |
||
| 152 | * will have the specified value or 1. |
||
| 153 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
| 154 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
| 155 | * the next new row's primary key will have a value 1. |
||
| 156 | * @return string the SQL statement for resetting sequence |
||
| 157 | * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||
| 158 | */ |
||
| 159 | 5 | public function resetSequence($tableName, $value = null) |
|
| 160 | { |
||
| 161 | 5 | $table = $this->db->getTableSchema($tableName); |
|
| 162 | 5 | if ($table !== null && $table->sequenceName !== null) { |
|
| 163 | 5 | $tableName = $this->db->quoteTableName($tableName); |
|
| 164 | 5 | if ($value === null) { |
|
| 165 | 1 | $key = reset($table->primaryKey); |
|
| 166 | 1 | $value = $this->db->createCommand("SELECT MAX(`$key`) FROM $tableName")->queryScalar() + 1; |
|
| 167 | } else { |
||
| 168 | 5 | $value = (int) $value; |
|
| 169 | } |
||
| 170 | |||
| 171 | 5 | return "ALTER TABLE $tableName AUTO_INCREMENT=$value"; |
|
| 172 | } elseif ($table === null) { |
||
| 173 | throw new InvalidParamException("Table not found: $tableName"); |
||
| 174 | } |
||
| 175 | |||
| 176 | throw new InvalidParamException("There is no sequence associated with table '$tableName'."); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Builds a SQL statement for enabling or disabling integrity check. |
||
| 181 | * @param bool $check whether to turn on or off the integrity check. |
||
| 182 | * @param string $schema the schema of the tables. Meaningless for MySQL. |
||
| 183 | * @param string $table the table name. Meaningless for MySQL. |
||
| 184 | * @return string the SQL statement for checking integrity |
||
| 185 | */ |
||
| 186 | 2 | public function checkIntegrity($check = true, $schema = '', $table = '') |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @inheritdoc |
||
| 193 | */ |
||
| 194 | 306 | public function buildLimit($limit, $offset) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @inheritdoc |
||
| 214 | */ |
||
| 215 | 306 | protected function hasLimit($limit) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @inheritdoc |
||
| 223 | */ |
||
| 224 | 306 | protected function hasOffset($offset) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @inheritdoc |
||
| 233 | */ |
||
| 234 | 121 | public function insert($table, $columns, &$params) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @inheritdoc |
||
| 280 | * @since 2.0.8 |
||
| 281 | */ |
||
| 282 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @inheritdoc |
||
| 296 | * @since 2.0.8 |
||
| 297 | */ |
||
| 298 | 1 | public function addCommentOnTable($table, $comment) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @inheritdoc |
||
| 305 | * @since 2.0.8 |
||
| 306 | */ |
||
| 307 | 2 | public function dropCommentFromColumn($table, $column) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @inheritdoc |
||
| 314 | * @since 2.0.8 |
||
| 315 | */ |
||
| 316 | 1 | public function dropCommentFromTable($table) |
|
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Gets column definition. |
||
| 324 | * |
||
| 325 | * @param string $table table name |
||
| 326 | * @param string $column column name |
||
| 327 | * @return null|string the column definition |
||
| 328 | * @throws Exception in case when table does not contain column |
||
| 329 | */ |
||
| 330 | 2 | private function getColumnDefinition($table, $column) |
|
| 352 | } |
||
| 353 |