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 |
||
20 | class QueryBuilder extends \yii\db\QueryBuilder |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
25 | */ |
||
26 | public $typeMap = [ |
||
27 | Schema::TYPE_PK => 'integer NOT NULL PRIMARY KEY', |
||
28 | Schema::TYPE_UPK => 'integer NOT NULL PRIMARY KEY', |
||
29 | Schema::TYPE_BIGPK => 'bigint NOT NULL PRIMARY KEY', |
||
30 | Schema::TYPE_UBIGPK => 'bigint NOT NULL PRIMARY KEY', |
||
31 | Schema::TYPE_CHAR => 'char(1)', |
||
32 | Schema::TYPE_STRING => 'varchar(255)', |
||
33 | Schema::TYPE_TEXT => 'blob sub_type text', |
||
34 | Schema::TYPE_SMALLINT => 'smallint', |
||
35 | Schema::TYPE_INTEGER => 'integer', |
||
36 | Schema::TYPE_BIGINT => 'bigint', |
||
37 | Schema::TYPE_FLOAT => 'float', |
||
38 | Schema::TYPE_DOUBLE => 'double precision', |
||
39 | Schema::TYPE_DECIMAL => 'numeric(10,0)', |
||
40 | Schema::TYPE_DATETIME => 'timestamp', |
||
41 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
42 | Schema::TYPE_TIME => 'time', |
||
43 | Schema::TYPE_DATE => 'date', |
||
44 | Schema::TYPE_BINARY => 'blob', |
||
45 | Schema::TYPE_BOOLEAN => 'smallint', |
||
46 | Schema::TYPE_MONEY => 'numeric(18,4)', |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
51 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
52 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
53 | * be included in the result with the additional parameters generated during the query building process. |
||
54 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
55 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
56 | * include those provided in `$params`. |
||
57 | */ |
||
58 | 175 | public function build($query, $params = []) |
|
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 175 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | 5 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | 175 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
189 | |||
190 | /** |
||
191 | * @param array $unions |
||
192 | * @param array $params the binding parameters to be populated |
||
193 | * @return string the UNION clause built from [[Query::$union]]. |
||
194 | */ |
||
195 | 175 | public function buildUnion($unions, &$params) |
|
214 | |||
215 | /** |
||
216 | * |
||
217 | * @param Expression $value |
||
218 | * @return Expression |
||
219 | */ |
||
220 | 3 | protected function convertExpression($value) |
|
235 | |||
236 | /** |
||
237 | * @inheritdoc |
||
238 | */ |
||
239 | 22 | public function insert($table, $columns, &$params) |
|
258 | |||
259 | /** |
||
260 | * @inheritdoc |
||
261 | */ |
||
262 | 13 | public function update($table, $columns, $condition, &$params) |
|
279 | |||
280 | /** |
||
281 | * @inheritdoc |
||
282 | */ |
||
283 | public function batchInsert($table, $columns, $rows) |
||
284 | { |
||
285 | $schema = $this->db->getSchema(); |
||
286 | if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
||
287 | $columnSchemas = $tableSchema->columns; |
||
288 | } else { |
||
289 | $columnSchemas = []; |
||
290 | } |
||
291 | |||
292 | $values = []; |
||
293 | foreach ($rows as $row) { |
||
294 | $vs = []; |
||
295 | foreach ($row as $i => $value) { |
||
296 | if (isset($columns[$i], $columnSchemas[$columns[$i]]) && !is_array($value)) { |
||
297 | $value = $columnSchemas[$columns[$i]]->dbTypecast($value); |
||
298 | } |
||
299 | if (is_string($value)) { |
||
300 | $value = $schema->quoteValue($value); |
||
301 | } elseif ($value === false) { |
||
302 | $value = 0; |
||
303 | } elseif ($value === null) { |
||
304 | $value = 'NULL'; |
||
305 | } |
||
306 | $vs[] = $value; |
||
307 | } |
||
308 | $values[] = 'INSERT INTO ' . $schema->quoteTableName($table) |
||
309 | . ' (' . implode(', ', $columns) . ') VALUES (' . implode(', ', $vs) . ');'; |
||
310 | } |
||
311 | |||
312 | foreach ($columns as $i => $name) { |
||
313 | $columns[$i] = $schema->quoteColumnName($name); |
||
314 | } |
||
315 | |||
316 | return 'EXECUTE block AS BEGIN ' . implode(' ', $values) . ' END;'; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @inheritdoc |
||
321 | */ |
||
322 | 1 | public function renameTable($oldName, $newName) |
|
326 | |||
327 | /** |
||
328 | * @inheritdoc |
||
329 | */ |
||
330 | 2 | public function truncateTable($table) |
|
334 | |||
335 | /** |
||
336 | * @inheritdoc |
||
337 | */ |
||
338 | 1 | public function dropColumn($table, $column) |
|
343 | |||
344 | /** |
||
345 | * @inheritdoc |
||
346 | */ |
||
347 | 1 | public function renameColumn($table, $oldName, $newName) |
|
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | */ |
||
357 | 3 | public function alterColumn($table, $column, $type) |
|
358 | { |
||
359 | 3 | $schema = $this->db->getSchema(); |
|
360 | 3 | $tableSchema = $schema->getTableSchema($table); |
|
361 | 3 | $columnSchema = $tableSchema->getColumn($column); |
|
362 | |||
363 | 3 | $allowNullNewType = !preg_match("/not +null/i", $type); |
|
364 | |||
365 | 3 | $type = preg_replace("/ +(not)? *null/i", "", $type); |
|
366 | |||
367 | 3 | $hasType = false; |
|
368 | |||
369 | 3 | $matches = []; |
|
370 | 3 | if (isset($this->typeMap[$type])) { |
|
371 | 2 | $hasType = true; |
|
372 | 3 | } elseif (preg_match('/^(\w+)[\( ]/', $type, $matches)) { |
|
373 | 2 | if (isset($this->typeMap[$matches[1]])) { |
|
374 | 2 | $hasType = true; |
|
375 | 2 | } |
|
376 | 2 | } |
|
377 | |||
378 | 3 | $baseSql = 'ALTER TABLE ' . $this->db->quoteTableName($table) |
|
379 | 3 | . ' ALTER '. $this->db->quoteColumnName($column) |
|
380 | 3 | . (($hasType) ? ' TYPE ': ' ') . $this->getColumnType($type); |
|
381 | |||
382 | 3 | if ($columnSchema->allowNull == $allowNullNewType) { |
|
383 | 2 | return $baseSql; |
|
384 | } else { |
||
385 | $sql = 'EXECUTE BLOCK AS BEGIN' |
||
386 | 2 | . ' EXECUTE STATEMENT ' . $this->db->quoteValue($baseSql) . ';' |
|
387 | 2 | . ' UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = ' . ($allowNullNewType ? 'NULL' : '1') |
|
388 | 2 | . ' WHERE UPPER(RDB$FIELD_NAME) = UPPER(\'' . $column . '\') AND UPPER(RDB$RELATION_NAME) = UPPER(\'' . $table . '\');'; |
|
389 | /** |
||
390 | * In any case (whichever option you choose), make sure that the column doesn't have any NULLs. |
||
391 | * Firebird will not check it for you. Later when you backup the database, everything is fine, |
||
392 | * but restore will fail as the NOT NULL column has NULLs in it. To be safe, each time you change from NULL to NOT NULL. |
||
393 | */ |
||
394 | 2 | if (!$allowNullNewType) { |
|
395 | 2 | $sql .= ' UPDATE ' . $this->db->quoteTableName($table) . ' SET ' . $this->db->quoteColumnName($column) . ' = 0' |
|
396 | 2 | . ' WHERE ' . $this->db->quoteColumnName($column) . ' IS NULL;'; |
|
397 | 2 | } |
|
398 | 2 | $sql .= ' END'; |
|
399 | 2 | return $sql; |
|
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @inheritdoc |
||
405 | */ |
||
406 | 1 | public function dropIndex($name, $table) |
|
410 | |||
411 | /** |
||
412 | * @inheritdoc |
||
413 | */ |
||
414 | 1 | public function resetSequence($table, $value = null) |
|
415 | { |
||
416 | 1 | $tableSchema = $this->db->getTableSchema($table); |
|
417 | 1 | if ($tableSchema === null) { |
|
418 | throw new InvalidParamException("Table not found: $table"); |
||
419 | } |
||
420 | 1 | if ($tableSchema->sequenceName === null) { |
|
421 | throw new InvalidParamException("There is not sequence associated with table '$table'."); |
||
422 | } |
||
423 | |||
424 | 1 | if ($value !== null) { |
|
425 | 1 | $value = (int) $value; |
|
426 | 1 | } else { |
|
427 | // use master connection to get the biggest PK value |
||
428 | 1 | $value = $this->db->useMaster(function(Connection $db) use ($tableSchema) { |
|
429 | 1 | $key = false; |
|
430 | 1 | foreach ($tableSchema->primaryKey as $name) { |
|
431 | 1 | if ($tableSchema->columns[$name]->autoIncrement) { |
|
432 | 1 | $key = $name; |
|
433 | 1 | break; |
|
434 | } |
||
435 | 1 | } |
|
436 | 1 | if ($key === false){ |
|
437 | return 0; |
||
438 | } |
||
439 | 1 | return $db->createCommand("SELECT MAX({$this->db->quoteColumnName($key)}) FROM {$this->db->quoteTableName($tableSchema->name)}")->queryScalar(); |
|
440 | 1 | }) + 1; |
|
441 | } |
||
442 | |||
443 | 1 | return "ALTER SEQUENCE {$this->db->quoteColumnName($tableSchema->sequenceName)} RESTART WITH $value"; |
|
444 | } |
||
445 | |||
446 | /** |
||
447 | * @inheritdoc |
||
448 | */ |
||
449 | 5 | public function createTable($table, $columns, $options = null) |
|
483 | |||
484 | /** |
||
485 | * @inheritdoc |
||
486 | */ |
||
487 | 3 | public function dropTable($table) |
|
506 | |||
507 | /** |
||
508 | * Creates a SELECT EXISTS() SQL statement. |
||
509 | * @param string $rawSql the subquery in a raw form to select from. |
||
510 | * @return string the SELECT EXISTS() SQL statement. |
||
511 | * |
||
512 | * @since 2.0.8 |
||
513 | */ |
||
514 | 3 | public function selectExists($rawSql) |
|
518 | } |
||
519 |