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