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 |
||
23 | class QueryBuilder extends \yii\db\QueryBuilder |
||
24 | { |
||
25 | /** |
||
26 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
27 | */ |
||
28 | public $typeMap = [ |
||
29 | Schema::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
30 | Schema::TYPE_UPK => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
31 | Schema::TYPE_BIGPK => 'bigint PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
32 | Schema::TYPE_UBIGPK => 'bigint UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
33 | Schema::TYPE_CHAR => 'char(1)', |
||
34 | Schema::TYPE_STRING => 'varchar(255)', |
||
35 | Schema::TYPE_TEXT => 'text', |
||
36 | Schema::TYPE_SMALLINT => 'smallint', |
||
37 | Schema::TYPE_INTEGER => 'integer', |
||
38 | Schema::TYPE_BIGINT => 'bigint', |
||
39 | Schema::TYPE_FLOAT => 'float', |
||
40 | Schema::TYPE_DOUBLE => 'double', |
||
41 | Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||
42 | Schema::TYPE_DATETIME => 'datetime', |
||
43 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
44 | Schema::TYPE_TIME => 'time', |
||
45 | Schema::TYPE_DATE => 'date', |
||
46 | Schema::TYPE_BINARY => 'blob', |
||
47 | Schema::TYPE_BOOLEAN => 'boolean', |
||
48 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
49 | ]; |
||
50 | |||
51 | |||
52 | /** |
||
53 | * Generates a batch INSERT SQL statement. |
||
54 | * For example, |
||
55 | * |
||
56 | * ```php |
||
57 | * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
||
58 | * ['Tom', 30], |
||
59 | * ['Jane', 20], |
||
60 | * ['Linda', 25], |
||
61 | * ])->execute(); |
||
62 | * ``` |
||
63 | * |
||
64 | * Note that the values in each row must match the corresponding column names. |
||
65 | * |
||
66 | * @param string $table the table that new rows will be inserted into. |
||
67 | * @param array $columns the column names |
||
68 | * @param array $rows the rows to be batch inserted into the table |
||
69 | * @return string the batch INSERT SQL statement |
||
70 | */ |
||
71 | 1 | public function batchInsert($table, $columns, $rows) |
|
113 | |||
114 | /** |
||
115 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
116 | * The sequence will be reset such that the primary key of the next new row inserted |
||
117 | * will have the specified value or 1. |
||
118 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
119 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
120 | * the next new row's primary key will have a value 1. |
||
121 | * @return string the SQL statement for resetting sequence |
||
122 | * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||
123 | */ |
||
124 | public function resetSequence($tableName, $value = null) |
||
149 | |||
150 | /** |
||
151 | * Enables or disables integrity check. |
||
152 | * @param boolean $check whether to turn on or off the integrity check. |
||
153 | * @param string $schema the schema of the tables. Meaningless for SQLite. |
||
154 | * @param string $table the table name. Meaningless for SQLite. |
||
155 | * @return string the SQL statement for checking integrity |
||
156 | * @throws NotSupportedException this is not supported by SQLite |
||
157 | */ |
||
158 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
162 | |||
163 | /** |
||
164 | * Builds a SQL statement for truncating a DB table. |
||
165 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
166 | * @return string the SQL statement for truncating a DB table. |
||
167 | */ |
||
168 | 1 | public function truncateTable($table) |
|
172 | |||
173 | /** |
||
174 | * Builds a SQL statement for dropping an index. |
||
175 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
176 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
177 | * @return string the SQL statement for dropping an index. |
||
178 | */ |
||
179 | public function dropIndex($name, $table) |
||
183 | |||
184 | /** |
||
185 | * Builds a SQL statement for dropping a DB column. |
||
186 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
187 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
188 | * @return string the SQL statement for dropping a DB column. |
||
189 | * @throws NotSupportedException this is not supported by SQLite |
||
190 | */ |
||
191 | public function dropColumn($table, $column) |
||
195 | |||
196 | /** |
||
197 | * Builds a SQL statement for renaming a column. |
||
198 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
199 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
200 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
201 | * @return string the SQL statement for renaming a DB column. |
||
202 | * @throws NotSupportedException this is not supported by SQLite |
||
203 | */ |
||
204 | public function renameColumn($table, $oldName, $newName) |
||
208 | |||
209 | /** |
||
210 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
211 | * The method will properly quote the table and column names. |
||
212 | * @param string $name the name of the foreign key constraint. |
||
213 | * @param string $table the table that the foreign key constraint will be added to. |
||
214 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
215 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
216 | * @param string $refTable the table that the foreign key references to. |
||
217 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
218 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
219 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
220 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
221 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
222 | * @throws NotSupportedException this is not supported by SQLite |
||
223 | */ |
||
224 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
228 | |||
229 | /** |
||
230 | * Builds a SQL statement for dropping a foreign key constraint. |
||
231 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
232 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
233 | * @return string the SQL statement for dropping a foreign key constraint. |
||
234 | * @throws NotSupportedException this is not supported by SQLite |
||
235 | */ |
||
236 | public function dropForeignKey($name, $table) |
||
240 | |||
241 | /** |
||
242 | * Builds a SQL statement for renaming a DB table. |
||
243 | * |
||
244 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
245 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
246 | * @return string the SQL statement for renaming a DB table. |
||
247 | */ |
||
248 | 2 | public function renameTable($table, $newName) |
|
252 | |||
253 | /** |
||
254 | * Builds a SQL statement for changing the definition of a column. |
||
255 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
256 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
257 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
258 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
259 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
260 | * will become 'varchar(255) not null'. |
||
261 | * @return string the SQL statement for changing the definition of a column. |
||
262 | * @throws NotSupportedException this is not supported by SQLite |
||
263 | */ |
||
264 | public function alterColumn($table, $column, $type) |
||
268 | |||
269 | /** |
||
270 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
271 | * @param string $name the name of the primary key constraint. |
||
272 | * @param string $table the table that the primary key constraint will be added to. |
||
273 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
274 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
275 | * @throws NotSupportedException this is not supported by SQLite |
||
276 | */ |
||
277 | public function addPrimaryKey($name, $table, $columns) |
||
281 | |||
282 | /** |
||
283 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
284 | * @param string $name the name of the primary key constraint to be removed. |
||
285 | * @param string $table the table that the primary key constraint will be removed from. |
||
286 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
287 | * @throws NotSupportedException this is not supported by SQLite |
||
288 | */ |
||
289 | public function dropPrimaryKey($name, $table) |
||
293 | |||
294 | /** |
||
295 | * @inheritdoc |
||
296 | * @throws NotSupportedException |
||
297 | * @since 2.0.8 |
||
298 | */ |
||
299 | public function addCommentOnColumn($table, $column, $comment) |
||
303 | |||
304 | /** |
||
305 | * @inheritdoc |
||
306 | * @throws NotSupportedException |
||
307 | * @since 2.0.8 |
||
308 | */ |
||
309 | public function addCommentOnTable($table, $comment) |
||
313 | |||
314 | /** |
||
315 | * @inheritdoc |
||
316 | * @throws NotSupportedException |
||
317 | * @since 2.0.8 |
||
318 | */ |
||
319 | public function dropCommentFromColumn($table, $column) |
||
323 | |||
324 | /** |
||
325 | * @inheritdoc |
||
326 | * @throws NotSupportedException |
||
327 | * @since 2.0.8 |
||
328 | */ |
||
329 | public function dropCommentFromTable($table) |
||
333 | |||
334 | /** |
||
335 | * @inheritdoc |
||
336 | */ |
||
337 | 203 | public function buildLimit($limit, $offset) |
|
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | * @throws NotSupportedException if `$columns` is an array |
||
357 | */ |
||
358 | 2 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
365 | |||
366 | /** |
||
367 | * Builds SQL for IN condition |
||
368 | * |
||
369 | * @param string $operator |
||
370 | * @param array $columns |
||
371 | * @param array $values |
||
372 | * @param array $params |
||
373 | * @return string SQL |
||
374 | */ |
||
375 | 5 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
398 | |||
399 | /** |
||
400 | * @inheritdoc |
||
401 | */ |
||
402 | 203 | public function build($query, $params = []) |
|
427 | |||
428 | /** |
||
429 | * @inheritdoc |
||
430 | */ |
||
431 | 203 | public function buildUnion($unions, &$params) |
|
450 | } |
||
451 |