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(18,0)', |
||
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 function defaultExpressionBuilders() |
||
52 | { |
||
53 | return array_merge(parent::defaultExpressionBuilders(), [ |
||
54 | 'yii\db\conditions\InCondition' => 'yii\db\mssql\conditions\InConditionBuilder', |
||
55 | 'yii\db\conditions\LikeCondition' => 'yii\db\mssql\conditions\LikeConditionBuilder', |
||
56 | ]); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
63 | { |
||
64 | if (!$this->hasOffset($offset) && !$this->hasLimit($limit)) { |
||
65 | $orderBy = $this->buildOrderBy($orderBy); |
||
66 | return $orderBy === '' ? $sql : $sql . $this->separator . $orderBy; |
||
67 | } |
||
68 | |||
69 | if (version_compare($this->db->getSchema()->getServerVersion(), '11', '<')) { |
||
70 | return $this->oldBuildOrderByAndLimit($sql, $orderBy, $limit, $offset); |
||
71 | } |
||
72 | |||
73 | return $this->newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2012 or newer. |
||
78 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
79 | * @param array $orderBy the order by columns. See [[\yii\db\Query::orderBy]] for more details on how to specify this parameter. |
||
80 | * @param int $limit the limit number. See [[\yii\db\Query::limit]] for more details. |
||
81 | * @param int $offset the offset number. See [[\yii\db\Query::offset]] for more details. |
||
82 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
83 | */ |
||
84 | protected function newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
102 | |||
103 | /** |
||
104 | * Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2005 to 2008. |
||
105 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
106 | * @param array $orderBy the order by columns. See [[\yii\db\Query::orderBy]] for more details on how to specify this parameter. |
||
107 | * @param int $limit the limit number. See [[\yii\db\Query::limit]] for more details. |
||
108 | * @param int $offset the offset number. See [[\yii\db\Query::offset]] for more details. |
||
109 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
110 | */ |
||
111 | protected function oldBuildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
132 | |||
133 | /** |
||
134 | * Builds a SQL statement for renaming a DB table. |
||
135 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
136 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
137 | * @return string the SQL statement for renaming a DB table. |
||
138 | */ |
||
139 | public function renameTable($oldName, $newName) |
||
143 | |||
144 | /** |
||
145 | * Builds a SQL statement for renaming a column. |
||
146 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
147 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
148 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
149 | * @return string the SQL statement for renaming a DB column. |
||
150 | */ |
||
151 | public function renameColumn($table, $oldName, $newName) |
||
158 | |||
159 | /** |
||
160 | * Builds a SQL statement for changing the definition of a column. |
||
161 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
162 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
163 | * @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any) |
||
164 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
165 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
166 | * @return string the SQL statement for changing the definition of a column. |
||
167 | */ |
||
168 | public function alterColumn($table, $column, $type) |
||
177 | |||
178 | /** |
||
179 | * @inheritDoc |
||
180 | */ |
||
181 | public function addDefaultValue($name, $table, $column, $value) |
||
187 | |||
188 | /** |
||
189 | * @inheritDoc |
||
190 | */ |
||
191 | public function dropDefaultValue($name, $table) |
||
196 | |||
197 | /** |
||
198 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
199 | * The sequence will be reset such that the primary key of the next new row inserted |
||
200 | * will have the specified value or 1. |
||
201 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
202 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
203 | * the next new row's primary key will have a value 1. |
||
204 | * @return string the SQL statement for resetting sequence |
||
205 | * @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table. |
||
206 | */ |
||
207 | public function resetSequence($tableName, $value = null) |
||
226 | |||
227 | /** |
||
228 | * Builds a SQL statement for enabling or disabling integrity check. |
||
229 | * @param bool $check whether to turn on or off the integrity check. |
||
230 | * @param string $schema the schema of the tables. |
||
231 | * @param string $table the table name. |
||
232 | * @return string the SQL statement for checking integrity |
||
233 | */ |
||
234 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | * @since 2.0.8 |
||
254 | */ |
||
255 | public function addCommentOnColumn($table, $column, $comment) |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | * @since 2.0.8 |
||
263 | */ |
||
264 | public function addCommentOnTable($table, $comment) |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | * @since 2.0.8 |
||
272 | */ |
||
273 | public function dropCommentFromColumn($table, $column) |
||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | * @since 2.0.8 |
||
281 | */ |
||
282 | public function dropCommentFromTable($table) |
||
286 | |||
287 | /** |
||
288 | * Returns an array of column names given model name. |
||
289 | * |
||
290 | * @param string $modelClass name of the model class |
||
291 | * @return array|null array of column names |
||
292 | */ |
||
293 | protected function getAllColumnNames($modelClass = null) |
||
302 | |||
303 | /** |
||
304 | * @return bool whether the version of the MSSQL being used is older than 2012. |
||
305 | * @throws \yii\base\InvalidConfigException |
||
306 | * @throws \yii\db\Exception |
||
307 | * @deprecated 2.0.14 Use [[Schema::getServerVersion]] with [[\version_compare()]]. |
||
308 | */ |
||
309 | protected function isOldMssql() |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | * @since 2.0.8 |
||
317 | */ |
||
318 | public function selectExists($rawSql) |
||
322 | |||
323 | /** |
||
324 | * Normalizes data to be saved into the table, performing extra preparations and type converting, if necessary. |
||
325 | * @param string $table the table that data will be saved into. |
||
326 | * @param array $columns the column data (name => value) to be saved into the table. |
||
327 | * @return array normalized columns |
||
328 | */ |
||
329 | private function normalizeTableRowData($table, $columns, &$params) |
||
345 | |||
346 | /** |
||
347 | * {@inheritdoc} |
||
348 | */ |
||
349 | public function insert($table, $columns, &$params) |
||
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | * @see https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql |
||
357 | * @see http://weblogs.sqlteam.com/dang/archive/2009/01/31/UPSERT-Race-Condition-With-MERGE.aspx |
||
358 | */ |
||
359 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
||
410 | |||
411 | /** |
||
412 | * {@inheritdoc} |
||
413 | */ |
||
414 | public function update($table, $columns, $condition, &$params) |
||
418 | } |
||
419 |