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_BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
31 | Schema::TYPE_STRING => 'varchar(255)', |
||
32 | Schema::TYPE_TEXT => 'text', |
||
33 | Schema::TYPE_SMALLINT => 'smallint', |
||
34 | Schema::TYPE_INTEGER => 'integer', |
||
35 | Schema::TYPE_BIGINT => 'bigint', |
||
36 | Schema::TYPE_FLOAT => 'float', |
||
37 | Schema::TYPE_DOUBLE => 'double', |
||
38 | Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||
39 | Schema::TYPE_DATETIME => 'datetime', |
||
40 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
41 | Schema::TYPE_TIME => 'time', |
||
42 | Schema::TYPE_DATE => 'date', |
||
43 | Schema::TYPE_BINARY => 'blob', |
||
44 | Schema::TYPE_BOOLEAN => 'boolean', |
||
45 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
46 | ]; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Generates a batch INSERT SQL statement. |
||
51 | * For example, |
||
52 | * |
||
53 | * ```php |
||
54 | * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
||
55 | * ['Tom', 30], |
||
56 | * ['Jane', 20], |
||
57 | * ['Linda', 25], |
||
58 | * ])->execute(); |
||
59 | * ``` |
||
60 | * |
||
61 | * Note that the values in each row must match the corresponding column names. |
||
62 | * |
||
63 | * @param string $table the table that new rows will be inserted into. |
||
64 | * @param array $columns the column names |
||
65 | * @param array $rows the rows to be batch inserted into the table |
||
66 | * @return string the batch INSERT SQL statement |
||
67 | */ |
||
68 | 1 | public function batchInsert($table, $columns, $rows) |
|
110 | |||
111 | /** |
||
112 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
113 | * The sequence will be reset such that the primary key of the next new row inserted |
||
114 | * will have the specified value or 1. |
||
115 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
116 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
117 | * the next new row's primary key will have a value 1. |
||
118 | * @return string the SQL statement for resetting sequence |
||
119 | * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||
120 | */ |
||
121 | public function resetSequence($tableName, $value = null) |
||
122 | { |
||
123 | $db = $this->db; |
||
124 | $table = $db->getTableSchema($tableName); |
||
125 | if ($table !== null && $table->sequenceName !== null) { |
||
126 | if ($value === null) { |
||
127 | $key = reset($table->primaryKey); |
||
128 | $tableName = $db->quoteTableName($tableName); |
||
129 | $value = $this->db->useMaster(function (Connection $db) use ($key, $tableName) { |
||
130 | return $db->createCommand("SELECT MAX('$key') FROM $tableName")->queryScalar(); |
||
131 | }); |
||
132 | } else { |
||
133 | $value = (int) $value - 1; |
||
134 | } |
||
135 | try { |
||
136 | $db->createCommand("UPDATE sqlite_sequence SET seq='$value' WHERE name='{$table->name}'")->execute(); |
||
137 | } catch (Exception $e) { |
||
138 | // it's possible that sqlite_sequence does not exist |
||
139 | } |
||
140 | } elseif ($table === null) { |
||
141 | throw new InvalidParamException("Table not found: $tableName"); |
||
142 | } else { |
||
143 | throw new InvalidParamException("There is not sequence associated with table '$tableName'.'"); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Enables or disables integrity check. |
||
149 | * @param boolean $check whether to turn on or off the integrity check. |
||
150 | * @param string $schema the schema of the tables. Meaningless for SQLite. |
||
151 | * @param string $table the table name. Meaningless for SQLite. |
||
152 | * @return string the SQL statement for checking integrity |
||
153 | * @throws NotSupportedException this is not supported by SQLite |
||
154 | */ |
||
155 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
159 | |||
160 | /** |
||
161 | * Builds a SQL statement for truncating a DB table. |
||
162 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
163 | * @return string the SQL statement for truncating a DB table. |
||
164 | */ |
||
165 | 1 | public function truncateTable($table) |
|
169 | |||
170 | /** |
||
171 | * Builds a SQL statement for dropping an index. |
||
172 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
173 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
174 | * @return string the SQL statement for dropping an index. |
||
175 | */ |
||
176 | public function dropIndex($name, $table) |
||
180 | |||
181 | /** |
||
182 | * Builds a SQL statement for dropping a DB column. |
||
183 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
184 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
185 | * @return string the SQL statement for dropping a DB column. |
||
186 | * @throws NotSupportedException this is not supported by SQLite |
||
187 | */ |
||
188 | public function dropColumn($table, $column) |
||
192 | |||
193 | /** |
||
194 | * Builds a SQL statement for renaming a column. |
||
195 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
196 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
197 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
198 | * @return string the SQL statement for renaming a DB column. |
||
199 | * @throws NotSupportedException this is not supported by SQLite |
||
200 | */ |
||
201 | public function renameColumn($table, $oldName, $newName) |
||
205 | |||
206 | /** |
||
207 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
208 | * The method will properly quote the table and column names. |
||
209 | * @param string $name the name of the foreign key constraint. |
||
210 | * @param string $table the table that the foreign key constraint will be added to. |
||
211 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
212 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
213 | * @param string $refTable the table that the foreign key references to. |
||
214 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
215 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
216 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
217 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
218 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
219 | * @throws NotSupportedException this is not supported by SQLite |
||
220 | */ |
||
221 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
225 | |||
226 | /** |
||
227 | * Builds a SQL statement for dropping a foreign key constraint. |
||
228 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
229 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
230 | * @return string the SQL statement for dropping a foreign key constraint. |
||
231 | * @throws NotSupportedException this is not supported by SQLite |
||
232 | */ |
||
233 | public function dropForeignKey($name, $table) |
||
237 | |||
238 | /** |
||
239 | * Builds a SQL statement for renaming a DB table. |
||
240 | * |
||
241 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
242 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
243 | * @return string the SQL statement for renaming a DB table. |
||
244 | */ |
||
245 | 2 | public function renameTable($table, $newName) |
|
249 | |||
250 | /** |
||
251 | * Builds a SQL statement for changing the definition of a column. |
||
252 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
253 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
254 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
255 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
256 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
257 | * will become 'varchar(255) not null'. |
||
258 | * @return string the SQL statement for changing the definition of a column. |
||
259 | * @throws NotSupportedException this is not supported by SQLite |
||
260 | */ |
||
261 | public function alterColumn($table, $column, $type) |
||
265 | |||
266 | /** |
||
267 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
268 | * @param string $name the name of the primary key constraint. |
||
269 | * @param string $table the table that the primary key constraint will be added to. |
||
270 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
271 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
272 | * @throws NotSupportedException this is not supported by SQLite |
||
273 | */ |
||
274 | 1 | public function addPrimaryKey($name, $table, $columns) |
|
278 | |||
279 | /** |
||
280 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
281 | * @param string $name the name of the primary key constraint to be removed. |
||
282 | * @param string $table the table that the primary key constraint will be removed from. |
||
283 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
284 | * @throws NotSupportedException this is not supported by SQLite |
||
285 | */ |
||
286 | public function dropPrimaryKey($name, $table) |
||
290 | |||
291 | /** |
||
292 | * @inheritdoc |
||
293 | */ |
||
294 | 185 | public function buildLimit($limit, $offset) |
|
310 | |||
311 | /** |
||
312 | * @inheritdoc |
||
313 | * @throws NotSupportedException if `$columns` is an array |
||
314 | */ |
||
315 | 2 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
322 | |||
323 | /** |
||
324 | * Builds SQL for IN condition |
||
325 | * |
||
326 | * @param string $operator |
||
327 | * @param array $columns |
||
328 | * @param array $values |
||
329 | * @param array $params |
||
330 | * @return string SQL |
||
331 | */ |
||
332 | 3 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
355 | |||
356 | /** |
||
357 | * @inheritdoc |
||
358 | */ |
||
359 | 185 | public function build($query, $params = []) |
|
408 | |||
409 | /** |
||
410 | * @inheritdoc |
||
411 | */ |
||
412 | 185 | public function buildUnion($unions, &$params) |
|
431 | } |
||
432 |