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 => 'NUMBER(10) NOT NULL PRIMARY KEY', |
||
28 | Schema::TYPE_UPK => 'NUMBER(10) UNSIGNED NOT NULL PRIMARY KEY', |
||
29 | Schema::TYPE_BIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY', |
||
30 | Schema::TYPE_UBIGPK => 'NUMBER(20) UNSIGNED NOT NULL PRIMARY KEY', |
||
31 | Schema::TYPE_CHAR => 'CHAR(1)', |
||
32 | Schema::TYPE_STRING => 'VARCHAR2(255)', |
||
33 | Schema::TYPE_TEXT => 'CLOB', |
||
34 | Schema::TYPE_SMALLINT => 'NUMBER(5)', |
||
35 | Schema::TYPE_INTEGER => 'NUMBER(10)', |
||
36 | Schema::TYPE_BIGINT => 'NUMBER(20)', |
||
37 | Schema::TYPE_FLOAT => 'NUMBER', |
||
38 | Schema::TYPE_DOUBLE => 'NUMBER', |
||
39 | Schema::TYPE_DECIMAL => 'NUMBER', |
||
40 | Schema::TYPE_DATETIME => 'TIMESTAMP', |
||
41 | Schema::TYPE_TIMESTAMP => 'TIMESTAMP', |
||
42 | Schema::TYPE_TIME => 'TIMESTAMP', |
||
43 | Schema::TYPE_DATE => 'DATE', |
||
44 | Schema::TYPE_BINARY => 'BLOB', |
||
45 | Schema::TYPE_BOOLEAN => 'NUMBER(1)', |
||
46 | Schema::TYPE_MONEY => 'NUMBER(19,4)', |
||
47 | ]; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * @inheritdoc |
||
52 | */ |
||
53 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
80 | |||
81 | /** |
||
82 | * Builds a SQL statement for renaming a DB table. |
||
83 | * |
||
84 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
85 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
86 | * @return string the SQL statement for renaming a DB table. |
||
87 | */ |
||
88 | public function renameTable($table, $newName) |
||
92 | |||
93 | /** |
||
94 | * Builds a SQL statement for changing the definition of a column. |
||
95 | * |
||
96 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
97 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
98 | * @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any) |
||
99 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
100 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
101 | * @return string the SQL statement for changing the definition of a column. |
||
102 | */ |
||
103 | public function alterColumn($table, $column, $type) |
||
109 | |||
110 | /** |
||
111 | * Builds a SQL statement for dropping an index. |
||
112 | * |
||
113 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
114 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
115 | * @return string the SQL statement for dropping an index. |
||
116 | */ |
||
117 | public function dropIndex($name, $table) |
||
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | public function resetSequence($table, $value = null) |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
167 | |||
168 | /** |
||
169 | * @inheritdoc |
||
170 | */ |
||
171 | public function insert($table, $columns, &$params) |
||
214 | |||
215 | /** |
||
216 | * Generates a batch INSERT SQL statement. |
||
217 | * For example, |
||
218 | * |
||
219 | * ```php |
||
220 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
221 | * ['Tom', 30], |
||
222 | * ['Jane', 20], |
||
223 | * ['Linda', 25], |
||
224 | * ]); |
||
225 | * ``` |
||
226 | * |
||
227 | * Note that the values in each row must match the corresponding column names. |
||
228 | * |
||
229 | * @param string $table the table that new rows will be inserted into. |
||
230 | * @param array $columns the column names |
||
231 | * @param array $rows the rows to be batch inserted into the table |
||
232 | * @return string the batch INSERT SQL statement |
||
233 | */ |
||
234 | public function batchInsert($table, $columns, $rows) |
||
275 | |||
276 | /** |
||
277 | * @inheritdoc |
||
278 | * @since 2.0.8 |
||
279 | */ |
||
280 | public function selectExists($rawSql) |
||
284 | |||
285 | /** |
||
286 | * @inheritdoc |
||
287 | * @since 2.0.8 |
||
288 | */ |
||
289 | public function dropCommentFromColumn($table, $column) |
||
293 | |||
294 | /** |
||
295 | * @inheritdoc |
||
296 | * @since 2.0.8 |
||
297 | */ |
||
298 | public function dropCommentFromTable($table) |
||
302 | } |
||
303 |