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 | * @inheritdoc |
||
66 | */ |
||
67 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
68 | { |
||
69 | $orderBy = $this->buildOrderBy($orderBy); |
||
70 | if ($orderBy !== '') { |
||
71 | $sql .= $this->separator . $orderBy; |
||
72 | } |
||
73 | |||
74 | $filters = []; |
||
75 | if ($this->hasOffset($offset)) { |
||
76 | $filters[] = 'rowNumId > ' . $offset; |
||
77 | } |
||
78 | if ($this->hasLimit($limit)) { |
||
79 | $filters[] = 'rownum <= ' . $limit; |
||
80 | } |
||
81 | if (empty($filters)) { |
||
82 | return $sql; |
||
83 | } |
||
84 | |||
85 | $filter = implode(' AND ', $filters); |
||
86 | return <<<EOD |
||
87 | WITH USER_SQL AS ($sql), |
||
88 | PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL) |
||
89 | SELECT * |
||
90 | FROM PAGINATION |
||
91 | WHERE $filter |
||
92 | EOD; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Builds a SQL statement for renaming a DB table. |
||
97 | * |
||
98 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
99 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
100 | * @return string the SQL statement for renaming a DB table. |
||
101 | */ |
||
102 | public function renameTable($table, $newName) |
||
103 | { |
||
104 | return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' RENAME TO ' . $this->db->quoteTableName($newName); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Builds a SQL statement for changing the definition of a column. |
||
109 | * |
||
110 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
111 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
112 | * @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any) |
||
113 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
114 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
115 | * @return string the SQL statement for changing the definition of a column. |
||
116 | */ |
||
117 | public function alterColumn($table, $column, $type) |
||
118 | { |
||
119 | $type = $this->getColumnType($type); |
||
120 | |||
121 | return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' MODIFY ' . $this->db->quoteColumnName($column) . ' ' . $this->getColumnType($type); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Builds a SQL statement for dropping an index. |
||
126 | * |
||
127 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
128 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
129 | * @return string the SQL statement for dropping an index. |
||
130 | */ |
||
131 | public function dropIndex($name, $table) |
||
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | public function resetSequence($table, $value = null) |
||
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | */ |
||
185 | public function insert($table, $columns, &$params) |
||
228 | |||
229 | /** |
||
230 | * Generates a batch INSERT SQL statement. |
||
231 | * For example, |
||
232 | * |
||
233 | * ```php |
||
234 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
235 | * ['Tom', 30], |
||
236 | * ['Jane', 20], |
||
237 | * ['Linda', 25], |
||
238 | * ]); |
||
239 | * ``` |
||
240 | * |
||
241 | * Note that the values in each row must match the corresponding column names. |
||
242 | * |
||
243 | * @param string $table the table that new rows will be inserted into. |
||
244 | * @param array $columns the column names |
||
245 | * @param array $rows the rows to be batch inserted into the table |
||
246 | * @return string the batch INSERT SQL statement |
||
247 | */ |
||
248 | public function batchInsert($table, $columns, $rows) |
||
292 | |||
293 | /** |
||
294 | * @inheritdoc |
||
295 | * @since 2.0.8 |
||
296 | */ |
||
297 | public function selectExists($rawSql) |
||
301 | |||
302 | /** |
||
303 | * @inheritdoc |
||
304 | * @since 2.0.8 |
||
305 | */ |
||
306 | public function dropCommentFromColumn($table, $column) |
||
310 | |||
311 | /** |
||
312 | * @inheritdoc |
||
313 | * @since 2.0.8 |
||
314 | */ |
||
315 | public function dropCommentFromTable($table) |
||
319 | |||
320 | /** |
||
321 | * @inheritDoc |
||
322 | */ |
||
323 | public function buildLikeCondition($operator, $operands, &$params) |
||
334 | } |
||
335 |