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 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
24 | */ |
||
25 | public $typeMap = [ |
||
26 | Schema::TYPE_PK => 'int IDENTITY PRIMARY KEY', |
||
27 | Schema::TYPE_UPK => 'int IDENTITY PRIMARY KEY', |
||
28 | Schema::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY', |
||
29 | Schema::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY', |
||
30 | Schema::TYPE_CHAR => 'nchar(1)', |
||
31 | Schema::TYPE_STRING => 'nvarchar(255)', |
||
32 | Schema::TYPE_TEXT => 'nvarchar(max)', |
||
33 | Schema::TYPE_SMALLINT => 'smallint', |
||
34 | Schema::TYPE_INTEGER => 'int', |
||
35 | Schema::TYPE_BIGINT => 'bigint', |
||
36 | Schema::TYPE_FLOAT => 'float', |
||
37 | Schema::TYPE_DOUBLE => 'float', |
||
38 | Schema::TYPE_DECIMAL => 'decimal', |
||
39 | Schema::TYPE_DATETIME => 'datetime', |
||
40 | Schema::TYPE_TIMESTAMP => 'datetime', |
||
41 | Schema::TYPE_TIME => 'time', |
||
42 | Schema::TYPE_DATE => 'date', |
||
43 | Schema::TYPE_BINARY => 'varbinary(max)', |
||
44 | Schema::TYPE_BOOLEAN => 'bit', |
||
45 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | protected $likeEscapingReplacements = [ |
||
52 | '%' => '\%', |
||
53 | '_' => '\_', |
||
54 | '[' => '\[', |
||
55 | ']' => '\]', |
||
56 | '\\' => '\\\\', |
||
57 | ]; |
||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | protected $likeEscapeCharacter = '\\'; |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
79 | |||
80 | /** |
||
81 | * Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2012 or newer. |
||
82 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
83 | * @param array $orderBy the order by columns. See [[\yii\db\Query::orderBy]] for more details on how to specify this parameter. |
||
84 | * @param int $limit the limit number. See [[\yii\db\Query::limit]] for more details. |
||
85 | * @param int $offset the offset number. See [[\yii\db\Query::offset]] for more details. |
||
86 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
87 | */ |
||
88 | protected function newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
106 | |||
107 | /** |
||
108 | * Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2005 to 2008. |
||
109 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
110 | * @param array $orderBy the order by columns. See [[\yii\db\Query::orderBy]] for more details on how to specify this parameter. |
||
111 | * @param int $limit the limit number. See [[\yii\db\Query::limit]] for more details. |
||
112 | * @param int $offset the offset number. See [[\yii\db\Query::offset]] for more details. |
||
113 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
114 | */ |
||
115 | protected function oldBuildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
136 | |||
137 | /** |
||
138 | * Builds a SQL statement for renaming a DB table. |
||
139 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
140 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
141 | * @return string the SQL statement for renaming a DB table. |
||
142 | */ |
||
143 | public function renameTable($oldName, $newName) |
||
147 | |||
148 | /** |
||
149 | * Builds a SQL statement for renaming a column. |
||
150 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
151 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
152 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
153 | * @return string the SQL statement for renaming a DB column. |
||
154 | */ |
||
155 | public function renameColumn($table, $oldName, $newName) |
||
162 | |||
163 | /** |
||
164 | * Builds a SQL statement for changing the definition of a column. |
||
165 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
166 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
167 | * @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any) |
||
168 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
169 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
170 | * @return string the SQL statement for changing the definition of a column. |
||
171 | */ |
||
172 | public function alterColumn($table, $column, $type) |
||
181 | |||
182 | /** |
||
183 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
184 | * The sequence will be reset such that the primary key of the next new row inserted |
||
185 | * will have the specified value or 1. |
||
186 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
187 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
188 | * the next new row's primary key will have a value 1. |
||
189 | * @return string the SQL statement for resetting sequence |
||
190 | * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||
191 | */ |
||
192 | public function resetSequence($tableName, $value = null) |
||
211 | |||
212 | /** |
||
213 | * Builds a SQL statement for enabling or disabling integrity check. |
||
214 | * @param bool $check whether to turn on or off the integrity check. |
||
215 | * @param string $schema the schema of the tables. |
||
216 | * @param string $table the table name. |
||
217 | * @return string the SQL statement for checking integrity |
||
218 | */ |
||
219 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
235 | |||
236 | /** |
||
237 | * @inheritdoc |
||
238 | * @since 2.0.8 |
||
239 | */ |
||
240 | public function addCommentOnColumn($table, $column, $comment) |
||
244 | |||
245 | /** |
||
246 | * @inheritdoc |
||
247 | * @since 2.0.8 |
||
248 | */ |
||
249 | public function addCommentOnTable($table, $comment) |
||
253 | |||
254 | /** |
||
255 | * @inheritdoc |
||
256 | * @since 2.0.8 |
||
257 | */ |
||
258 | public function dropCommentFromColumn($table, $column) |
||
262 | |||
263 | /** |
||
264 | * @inheritdoc |
||
265 | * @since 2.0.8 |
||
266 | */ |
||
267 | public function dropCommentFromTable($table) |
||
271 | |||
272 | /** |
||
273 | * Returns an array of column names given model name |
||
274 | * |
||
275 | * @param string $modelClass name of the model class |
||
276 | * @return array|null array of column names |
||
277 | */ |
||
278 | protected function getAllColumnNames($modelClass = null) |
||
288 | |||
289 | /** |
||
290 | * @var bool whether MSSQL used is old. |
||
291 | */ |
||
292 | private $_oldMssql; |
||
293 | |||
294 | /** |
||
295 | * @return bool whether the version of the MSSQL being used is older than 2012. |
||
296 | * @throws \yii\base\InvalidConfigException |
||
297 | * @throws \yii\db\Exception |
||
298 | */ |
||
299 | protected function isOldMssql() |
||
308 | |||
309 | /** |
||
310 | * @inheritdoc |
||
311 | * @throws NotSupportedException if `$columns` is an array |
||
312 | */ |
||
313 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
||
320 | |||
321 | /** |
||
322 | * Builds SQL for IN condition |
||
323 | * |
||
324 | * @param string $operator |
||
325 | * @param array $columns |
||
326 | * @param array $values |
||
327 | * @param array $params |
||
328 | * @return string SQL |
||
329 | */ |
||
330 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
||
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | * @since 2.0.8 |
||
357 | */ |
||
358 | public function selectExists($rawSql) |
||
362 | |||
363 | /** |
||
364 | * Normalizes data to be saved into the table, performing extra preparations and type converting, if necessary. |
||
365 | * @param string $table the table that data will be saved into. |
||
366 | * @param array $columns the column data (name => value) to be saved into the table. |
||
367 | * @return array normalized columns |
||
368 | */ |
||
369 | private function normalizeTableRowData($table, $columns, &$params) |
||
383 | |||
384 | /** |
||
385 | * @inheritdoc |
||
386 | */ |
||
387 | public function insert($table, $columns, &$params) |
||
391 | |||
392 | /** |
||
393 | * @inheritdoc |
||
394 | */ |
||
395 | public function update($table, $columns, $condition, &$params) |
||
399 | } |
||
400 |