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 | * Defines a UNIQUE index for [[createIndex()]]. |
||
27 | * @since 2.0.6 |
||
28 | */ |
||
29 | const INDEX_UNIQUE = 'unique'; |
||
30 | /** |
||
31 | * Defines a B-tree index for [[createIndex()]]. |
||
32 | * @since 2.0.6 |
||
33 | */ |
||
34 | const INDEX_B_TREE = 'btree'; |
||
35 | /** |
||
36 | * Defines a hash index for [[createIndex()]]. |
||
37 | * @since 2.0.6 |
||
38 | */ |
||
39 | const INDEX_HASH = 'hash'; |
||
40 | /** |
||
41 | * Defines a GiST index for [[createIndex()]]. |
||
42 | * @since 2.0.6 |
||
43 | */ |
||
44 | const INDEX_GIST = 'gist'; |
||
45 | /** |
||
46 | * Defines a GIN index for [[createIndex()]]. |
||
47 | * @since 2.0.6 |
||
48 | */ |
||
49 | const INDEX_GIN = 'gin'; |
||
50 | |||
51 | /** |
||
52 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
53 | */ |
||
54 | public $typeMap = [ |
||
55 | Schema::TYPE_PK => 'serial NOT NULL PRIMARY KEY', |
||
56 | Schema::TYPE_UPK => 'serial NOT NULL PRIMARY KEY', |
||
57 | Schema::TYPE_BIGPK => 'bigserial NOT NULL PRIMARY KEY', |
||
58 | Schema::TYPE_UBIGPK => 'bigserial NOT NULL PRIMARY KEY', |
||
59 | Schema::TYPE_CHAR => 'char(1)', |
||
60 | Schema::TYPE_STRING => 'varchar(255)', |
||
61 | Schema::TYPE_TEXT => 'text', |
||
62 | Schema::TYPE_TINYINT => 'smallint', |
||
63 | Schema::TYPE_SMALLINT => 'smallint', |
||
64 | Schema::TYPE_INTEGER => 'integer', |
||
65 | Schema::TYPE_BIGINT => 'bigint', |
||
66 | Schema::TYPE_FLOAT => 'double precision', |
||
67 | Schema::TYPE_DOUBLE => 'double precision', |
||
68 | Schema::TYPE_DECIMAL => 'numeric(10,0)', |
||
69 | Schema::TYPE_DATETIME => 'timestamp(0)', |
||
70 | Schema::TYPE_TIMESTAMP => 'timestamp(0)', |
||
71 | Schema::TYPE_TIME => 'time(0)', |
||
72 | Schema::TYPE_DATE => 'date', |
||
73 | Schema::TYPE_BINARY => 'bytea', |
||
74 | Schema::TYPE_BOOLEAN => 'boolean', |
||
75 | Schema::TYPE_MONEY => 'numeric(19,4)', |
||
76 | Schema::TYPE_JSON => 'jsonb', |
||
77 | ]; |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 523 | protected function defaultConditionClasses() |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 523 | protected function defaultExpressionBuilders() |
|
102 | |||
103 | /** |
||
104 | * Builds a SQL statement for creating a new index. |
||
105 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
106 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
107 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
108 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
109 | * by the method, unless a parenthesis is found in the name. |
||
110 | * @param bool|string $unique whether to make this a UNIQUE index constraint. You can pass `true` or [[INDEX_UNIQUE]] to create |
||
111 | * a unique index, `false` to make a non-unique index using the default index type, or one of the following constants to specify |
||
112 | * the index method to use: [[INDEX_B_TREE]], [[INDEX_HASH]], [[INDEX_GIST]], [[INDEX_GIN]]. |
||
113 | * @return string the SQL statement for creating a new index. |
||
114 | * @see http://www.postgresql.org/docs/8.2/static/sql-createindex.html |
||
115 | */ |
||
116 | 6 | public function createIndex($name, $table, $columns, $unique = false) |
|
132 | |||
133 | /** |
||
134 | * Builds a SQL statement for dropping an index. |
||
135 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
136 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
137 | * @return string the SQL statement for dropping an index. |
||
138 | */ |
||
139 | 2 | public function dropIndex($name, $table) |
|
143 | |||
144 | /** |
||
145 | * Builds a SQL statement for renaming a DB table. |
||
146 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
147 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
148 | * @return string the SQL statement for renaming a DB table. |
||
149 | */ |
||
150 | 1 | public function renameTable($oldName, $newName) |
|
154 | |||
155 | /** |
||
156 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
157 | * The sequence will be reset such that the primary key of the next new row inserted |
||
158 | * will have the specified value or 1. |
||
159 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
160 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
161 | * the next new row's primary key will have a value 1. |
||
162 | * @return string the SQL statement for resetting sequence |
||
163 | * @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table. |
||
164 | */ |
||
165 | 5 | public function resetSequence($tableName, $value = null) |
|
186 | |||
187 | /** |
||
188 | * Builds a SQL statement for enabling or disabling integrity check. |
||
189 | * @param bool $check whether to turn on or off the integrity check. |
||
190 | * @param string $schema the schema of the tables. |
||
191 | * @param string $table the table name. |
||
192 | * @return string the SQL statement for checking integrity |
||
193 | */ |
||
194 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
213 | |||
214 | /** |
||
215 | * Builds a SQL statement for truncating a DB table. |
||
216 | * Explicitly restarts identity for PGSQL to be consistent with other databases which all do this by default. |
||
217 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
218 | * @return string the SQL statement for truncating a DB table. |
||
219 | */ |
||
220 | 1 | public function truncateTable($table) |
|
224 | |||
225 | /** |
||
226 | * Builds a SQL statement for changing the definition of a column. |
||
227 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
228 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
229 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
230 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
231 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
232 | * will become 'varchar(255) not null'. You can also use PostgreSQL-specific syntax such as `SET NOT NULL`. |
||
233 | * @return string the SQL statement for changing the definition of a column. |
||
234 | */ |
||
235 | 2 | public function alterColumn($table, $column, $type) |
|
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | 190 | public function insert($table, $columns, &$params) |
|
254 | |||
255 | /** |
||
256 | * @inheritdoc |
||
257 | * @see https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT |
||
258 | * @see https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-in-postgresql/8702291#8702291 |
||
259 | */ |
||
260 | 22 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
|
272 | |||
273 | /** |
||
274 | * [[upsert()]] implementation for PostgreSQL 9.5 or higher. |
||
275 | * @param string $table |
||
276 | * @param array|Query $insertColumns |
||
277 | * @param array|bool $updateColumns |
||
278 | * @param array $params |
||
279 | * @return string |
||
280 | */ |
||
281 | 22 | private function newUpsert($table, $insertColumns, $updateColumns, &$params) |
|
302 | |||
303 | /** |
||
304 | * [[upsert()]] implementation for PostgreSQL older than 9.5. |
||
305 | * @param string $table |
||
306 | * @param array|Query $insertColumns |
||
307 | * @param array|bool $updateColumns |
||
308 | * @param array $params |
||
309 | * @return string |
||
310 | */ |
||
311 | private function oldUpsert($table, $insertColumns, $updateColumns, &$params) |
||
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | 43 | public function update($table, $columns, $condition, &$params) |
|
397 | |||
398 | /** |
||
399 | * Normalizes data to be saved into the table, performing extra preparations and type converting, if necessary. |
||
400 | * |
||
401 | * @param string $table the table that data will be saved into. |
||
402 | * @param array|Query $columns the column data (name => value) to be saved into the table or instance |
||
403 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
404 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
405 | * @return array normalized columns |
||
406 | * @since 2.0.9 |
||
407 | */ |
||
408 | 201 | private function normalizeTableRowData($table, $columns) |
|
425 | |||
426 | /** |
||
427 | * {@inheritdoc} |
||
428 | */ |
||
429 | 16 | public function batchInsert($table, $columns, $rows) |
|
476 | } |
||
477 |