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 | * @inheritdoc |
||
51 | */ |
||
52 | protected $likeEscapeCharacter = '!'; |
||
53 | /** |
||
54 | * `\` is initialized in [[buildLikeCondition()]] method since |
||
55 | * we need to choose replacement value based on [[\yii\db\Schema::quoteValue()]]. |
||
56 | * @inheritdoc |
||
57 | */ |
||
58 | protected $likeEscapingReplacements = [ |
||
59 | '%' => '!%', |
||
60 | '_' => '!_', |
||
61 | '!' => '!!', |
||
62 | ]; |
||
63 | |||
64 | |||
65 | /** |
||
66 | * @inheritdoc |
||
67 | */ |
||
68 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset, &$params) |
||
69 | { |
||
70 | $orderBy = $this->buildOrderBy($orderBy, $params); |
||
71 | if ($orderBy !== '') { |
||
72 | $sql .= $this->separator . $orderBy; |
||
73 | } |
||
74 | |||
75 | $filters = []; |
||
76 | if ($this->hasOffset($offset)) { |
||
77 | $filters[] = 'rowNumId > ' . $offset; |
||
78 | } |
||
79 | if ($this->hasLimit($limit)) { |
||
80 | $filters[] = 'rownum <= ' . $limit; |
||
81 | } |
||
82 | if (empty($filters)) { |
||
83 | return $sql; |
||
84 | } |
||
85 | |||
86 | $filter = implode(' AND ', $filters); |
||
87 | return <<<EOD |
||
88 | WITH USER_SQL AS ($sql), |
||
89 | PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL) |
||
90 | SELECT * |
||
91 | FROM PAGINATION |
||
92 | WHERE $filter |
||
93 | EOD; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Builds a SQL statement for renaming a DB table. |
||
98 | * |
||
99 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
100 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
101 | * @return string the SQL statement for renaming a DB table. |
||
102 | */ |
||
103 | public function renameTable($table, $newName) |
||
107 | |||
108 | /** |
||
109 | * Builds a SQL statement for changing the definition of a column. |
||
110 | * |
||
111 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
112 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
113 | * @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any) |
||
114 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
115 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
116 | * @return string the SQL statement for changing the definition of a column. |
||
117 | */ |
||
118 | public function alterColumn($table, $column, $type) |
||
124 | |||
125 | /** |
||
126 | * Builds a SQL statement for dropping an index. |
||
127 | * |
||
128 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
129 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
130 | * @return string the SQL statement for dropping an index. |
||
131 | */ |
||
132 | public function dropIndex($name, $table) |
||
136 | |||
137 | /** |
||
138 | * @inheritdoc |
||
139 | */ |
||
140 | public function resetSequence($table, $value = null) |
||
162 | |||
163 | /** |
||
164 | * @inheritdoc |
||
165 | */ |
||
166 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | */ |
||
186 | public function insert($table, $columns, &$params) |
||
229 | |||
230 | /** |
||
231 | * Generates a batch INSERT SQL statement. |
||
232 | * For example, |
||
233 | * |
||
234 | * ```php |
||
235 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
236 | * ['Tom', 30], |
||
237 | * ['Jane', 20], |
||
238 | * ['Linda', 25], |
||
239 | * ]); |
||
240 | * ``` |
||
241 | * |
||
242 | * Note that the values in each row must match the corresponding column names. |
||
243 | * |
||
244 | * @param string $table the table that new rows will be inserted into. |
||
245 | * @param array $columns the column names |
||
246 | * @param array $rows the rows to be batch inserted into the table |
||
247 | * @return string the batch INSERT SQL statement |
||
248 | */ |
||
249 | public function batchInsert($table, $columns, $rows) |
||
293 | |||
294 | /** |
||
295 | * @inheritdoc |
||
296 | * @since 2.0.8 |
||
297 | */ |
||
298 | public function selectExists($rawSql) |
||
302 | |||
303 | /** |
||
304 | * @inheritdoc |
||
305 | * @since 2.0.8 |
||
306 | */ |
||
307 | public function dropCommentFromColumn($table, $column) |
||
311 | |||
312 | /** |
||
313 | * @inheritdoc |
||
314 | * @since 2.0.8 |
||
315 | */ |
||
316 | public function dropCommentFromTable($table) |
||
320 | |||
321 | /** |
||
322 | * @inheritDoc |
||
323 | */ |
||
324 | public function buildLikeCondition($operator, $operands, &$params) |
||
335 | |||
336 | /** |
||
337 | * @inheritdoc |
||
338 | */ |
||
339 | public function buildInCondition($operator, $operands, &$params) |
||
348 | |||
349 | /** |
||
350 | * Oracle DBMS does not support more than 1000 parameters in `IN` condition. |
||
351 | * This method splits long `IN` condition into series of smaller ones. |
||
352 | * |
||
353 | * @param string $operator |
||
354 | * @param array $operands |
||
355 | * @param array $params |
||
356 | * @return null|string null when split is not required. Otherwise - built SQL condition. |
||
357 | * @throws Exception |
||
358 | * @since 2.0.12 |
||
359 | */ |
||
360 | protected function splitInCondition($operator, $operands, &$params) |
||
389 | |||
390 | } |
||
391 |