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 | /** |
||
| 24 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
| 25 | */ |
||
| 26 | public $typeMap = [ |
||
| 27 | Schema::TYPE_PK => 'integer NOT NULL PRIMARY KEY', |
||
| 28 | Schema::TYPE_UPK => 'integer NOT NULL PRIMARY KEY', |
||
| 29 | Schema::TYPE_BIGPK => 'bigint NOT NULL PRIMARY KEY', |
||
| 30 | Schema::TYPE_UBIGPK => 'bigint NOT NULL PRIMARY KEY', |
||
| 31 | Schema::TYPE_CHAR => 'char(1)', |
||
| 32 | Schema::TYPE_STRING => 'varchar(255)', |
||
| 33 | Schema::TYPE_TEXT => 'blob sub_type text', |
||
| 34 | Schema::TYPE_SMALLINT => 'smallint', |
||
| 35 | Schema::TYPE_INTEGER => 'integer', |
||
| 36 | Schema::TYPE_BIGINT => 'bigint', |
||
| 37 | Schema::TYPE_FLOAT => 'float', |
||
| 38 | Schema::TYPE_DOUBLE => 'double precision', |
||
| 39 | Schema::TYPE_DECIMAL => 'numeric(10,0)', |
||
| 40 | Schema::TYPE_DATETIME => 'timestamp', |
||
| 41 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
| 42 | Schema::TYPE_TIME => 'time', |
||
| 43 | Schema::TYPE_DATE => 'date', |
||
| 44 | Schema::TYPE_BINARY => 'blob', |
||
| 45 | Schema::TYPE_BOOLEAN => 'smallint', |
||
| 46 | Schema::TYPE_MONEY => 'numeric(18,4)', |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
| 51 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
| 52 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
| 53 | * be included in the result with the additional parameters generated during the query building process. |
||
| 54 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
| 55 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
| 56 | * include those provided in `$params`. |
||
| 57 | */ |
||
| 58 | 99 | public function build($query, $params = []) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @inheritdoc |
||
| 101 | */ |
||
| 102 | 99 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @inheritdoc |
||
| 124 | */ |
||
| 125 | 4 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritdoc |
||
| 150 | */ |
||
| 151 | 99 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
| 152 | { |
||
| 153 | |||
| 154 | 99 | $orderBy = $this->buildOrderBy($orderBy); |
|
| 155 | 99 | if ($orderBy !== '') { |
|
| 156 | 1 | $sql .= $this->separator . $orderBy; |
|
| 157 | 1 | } |
|
| 158 | |||
| 159 | 99 | $limit = $limit !== null ? intval($limit) : -1; |
|
| 160 | 99 | $offset = $offset !== null ? intval($offset) : -1; |
|
| 161 | // If ignoring both params then do nothing |
||
| 162 | 99 | if ($offset < 0 && $limit < 0) { |
|
| 163 | 99 | return $sql; |
|
| 164 | } |
||
| 165 | // If we are ignoring limit then return full result set starting |
||
| 166 | // from $offset. In Firebird this can only be done with SKIP |
||
| 167 | if ($offset >= 0 && $limit < 0) { |
||
| 168 | $count = 1; //Only do it once |
||
| 169 | $sql = preg_replace('/^SELECT /i', 'SELECT SKIP ' . (int) $offset . ' ', $sql, $count); |
||
| 170 | return $sql; |
||
| 171 | } |
||
| 172 | // If we are ignoring $offset then return $limit rows. |
||
| 173 | // ie, return the first $limit rows in the set. |
||
| 174 | if ($offset < 0 && $limit >= 0) { |
||
| 175 | $count = 1; //Only do it once |
||
| 176 | $sql = preg_replace('/^SELECT /i', 'SELECT FIRST ' . (int) $limit . ' ', $sql, $count); |
||
| 177 | return $sql; |
||
| 178 | } |
||
| 179 | // Otherwise apply the params and return the amended sql. |
||
| 180 | if ($offset >= 0 && $limit >= 0) { |
||
| 181 | $count = 1; //Only do it once |
||
| 182 | $sql = preg_replace('/^SELECT /i', 'SELECT FIRST ' . (int) $limit . ' SKIP ' . (int) $offset, $sql, $count); |
||
| 183 | return $sql; |
||
| 184 | } |
||
| 185 | // If we have fallen through the cracks then just pass |
||
| 186 | // the sql back. |
||
| 187 | return $sql; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param array $unions |
||
| 192 | * @param array $params the binding parameters to be populated |
||
| 193 | * @return string the UNION clause built from [[Query::$union]]. |
||
| 194 | */ |
||
| 195 | 99 | public function buildUnion($unions, &$params) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * |
||
| 217 | * @param Expression $value |
||
| 218 | * @return Expression |
||
| 219 | */ |
||
| 220 | protected function convertExpression($value) |
||
| 221 | { |
||
| 222 | if (!($value instanceof Expression)) { |
||
| 223 | return $value; |
||
| 224 | } |
||
| 225 | |||
| 226 | $expressionMap = [ |
||
| 227 | "strftime('%Y')" => "EXTRACT(YEAR FROM TIMESTAMP 'now')" |
||
| 228 | ]; |
||
| 229 | |||
| 230 | if (isset($expressionMap[$value->expression])) { |
||
| 231 | return new Expression($expressionMap[$value->expression]); |
||
| 232 | } |
||
| 233 | return $value; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @inheritdoc |
||
| 238 | */ |
||
| 239 | 1 | public function insert($table, $columns, &$params) |
|
| 240 | { |
||
| 241 | 1 | $schema = $this->db->getSchema(); |
|
| 242 | 1 | if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
|
| 243 | 1 | $columnSchemas = $tableSchema->columns; |
|
| 244 | 1 | } else { |
|
| 245 | $columnSchemas = []; |
||
| 246 | } |
||
| 247 | |||
| 248 | //Empty insert |
||
| 249 | 1 | if(empty($columns) && !empty($columnSchemas)){ |
|
| 250 | $columns = []; |
||
| 251 | foreach ($columnSchemas as $columnSchema) { |
||
| 252 | if(!$columnSchema->autoIncrement){ |
||
| 253 | $columns[$columnSchema->name] = $columnSchema->defaultValue; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | 1 | foreach ($columns as $name => $value) { |
|
| 259 | 1 | if ($value instanceof Expression) { |
|
| 260 | $columns[$name] = $this->convertExpression($value); |
||
| 261 | 1 | } elseif (isset($columnSchemas[$name]) && in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) { |
|
| 262 | $columns[$name] = [$value, 'blob']; |
||
| 263 | } |
||
| 264 | 1 | } |
|
| 265 | |||
| 266 | 1 | return parent::insert($table, $columns, $params); |
|
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @inheritdoc |
||
| 271 | */ |
||
| 272 | public function update($table, $columns, $condition, &$params) |
||
| 273 | { |
||
| 274 | $schema = $this->db->getSchema(); |
||
| 275 | if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
||
| 276 | $columnSchemas = $tableSchema->columns; |
||
| 277 | } else { |
||
| 278 | $columnSchemas = []; |
||
| 279 | } |
||
| 280 | foreach ($columns as $name => $value) { |
||
| 281 | if ($value instanceof Expression) { |
||
| 282 | $columns[$name] = $this->convertExpression($value); |
||
| 283 | } elseif (isset($columnSchemas[$name]) && in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) { |
||
| 284 | $columns[$name] = [$value, 'blob']; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | return parent::update($table, $columns, $condition, $params); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @inheritdoc |
||
| 292 | */ |
||
| 293 | public function batchInsert($table, $columns, $rows) |
||
| 294 | { |
||
| 295 | if (empty($rows)) { |
||
| 296 | return ''; |
||
| 297 | } |
||
| 298 | |||
| 299 | $schema = $this->db->getSchema(); |
||
| 300 | if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
||
| 301 | $columnSchemas = $tableSchema->columns; |
||
| 302 | } else { |
||
| 303 | $columnSchemas = []; |
||
| 304 | } |
||
| 305 | |||
| 306 | $values = []; |
||
| 307 | foreach ($rows as $row) { |
||
| 308 | $vs = []; |
||
| 309 | foreach ($row as $i => $value) { |
||
| 310 | if (isset($columns[$i], $columnSchemas[$columns[$i]]) && !is_array($value)) { |
||
| 311 | $value = $columnSchemas[$columns[$i]]->dbTypecast($value); |
||
| 312 | } |
||
| 313 | if (is_string($value)) { |
||
| 314 | $value = $schema->quoteValue($value); |
||
| 315 | } elseif ($value === false) { |
||
| 316 | $value = 0; |
||
| 317 | } elseif ($value === null) { |
||
| 318 | $value = 'NULL'; |
||
| 319 | } |
||
| 320 | $vs[] = $value; |
||
| 321 | } |
||
| 322 | $values[] = 'INSERT INTO ' . $schema->quoteTableName($table) |
||
| 323 | . ' (' . implode(', ', $columns) . ') VALUES (' . implode(', ', $vs) . ');'; |
||
| 324 | } |
||
| 325 | |||
| 326 | foreach ($columns as $i => $name) { |
||
| 327 | $columns[$i] = $schema->quoteColumnName($name); |
||
| 328 | } |
||
| 329 | |||
| 330 | return 'EXECUTE block AS BEGIN ' . implode(' ', $values) . ' END;'; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @inheritdoc |
||
| 335 | */ |
||
| 336 | 1 | public function renameTable($oldName, $newName) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @inheritdoc |
||
| 343 | */ |
||
| 344 | public function truncateTable($table) |
||
| 345 | { |
||
| 346 | return "DELETE FROM " . $this->db->quoteTableName($table); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @inheritdoc |
||
| 351 | */ |
||
| 352 | public function dropColumn($table, $column) |
||
| 353 | { |
||
| 354 | return "ALTER TABLE " . $this->db->quoteTableName($table) |
||
| 355 | . " DROP " . $this->db->quoteColumnName($column); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @inheritdoc |
||
| 360 | */ |
||
| 361 | public function renameColumn($table, $oldName, $newName) |
||
| 362 | { |
||
| 363 | return "ALTER TABLE " . $this->db->quoteTableName($table) |
||
| 364 | . " ALTER " . $this->db->quoteColumnName($oldName) |
||
| 365 | . " TO " . $this->db->quoteColumnName($newName); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @inheritdoc |
||
| 370 | */ |
||
| 371 | 1 | public function alterColumn($table, $column, $type) |
|
| 372 | { |
||
| 373 | 1 | $schema = $this->db->getSchema(); |
|
| 374 | 1 | $tableSchema = $schema->getTableSchema($table); |
|
| 375 | 1 | $columnSchema = $tableSchema->getColumn($column); |
|
| 376 | |||
| 377 | 1 | $allowNullNewType = !preg_match("/not +null/i", $type); |
|
| 378 | |||
| 379 | 1 | $type = preg_replace("/ +(not)? *null/i", "", $type); |
|
| 380 | |||
| 381 | 1 | $hasType = false; |
|
| 382 | |||
| 383 | 1 | $matches = []; |
|
| 384 | 1 | if (isset($this->typeMap[$type])) { |
|
| 385 | $hasType = true; |
||
| 386 | 1 | } elseif (preg_match('/^(\w+)[\( ]/', $type, $matches)) { |
|
| 387 | 1 | if (isset($this->typeMap[$matches[1]])) { |
|
| 388 | 1 | $hasType = true; |
|
| 389 | 1 | } |
|
| 390 | 1 | } |
|
| 391 | |||
| 392 | 1 | $baseSql = 'ALTER TABLE ' . $this->db->quoteTableName($table) |
|
| 393 | 1 | . ' ALTER ' . $this->db->quoteColumnName($column) |
|
| 394 | 1 | . (($hasType) ? ' TYPE ' : ' ') . $this->getColumnType($type); |
|
| 395 | |||
| 396 | 1 | if ($columnSchema->allowNull == $allowNullNewType) { |
|
| 397 | return $baseSql; |
||
| 398 | } else { |
||
| 399 | $sql = 'EXECUTE BLOCK AS BEGIN' |
||
| 400 | 1 | . ' EXECUTE STATEMENT ' . $this->db->quoteValue($baseSql) . ';' |
|
| 401 | 1 | . ' UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = ' . ($allowNullNewType ? 'NULL' : '1') |
|
| 402 | 1 | . ' WHERE UPPER(RDB$FIELD_NAME) = UPPER(\'' . $column . '\') AND UPPER(RDB$RELATION_NAME) = UPPER(\'' . $table . '\');'; |
|
| 403 | /** |
||
| 404 | * In any case (whichever option you choose), make sure that the column doesn't have any NULLs. |
||
| 405 | * Firebird will not check it for you. Later when you backup the database, everything is fine, |
||
| 406 | * but restore will fail as the NOT NULL column has NULLs in it. To be safe, each time you change from NULL to NOT NULL. |
||
| 407 | */ |
||
| 408 | 1 | if (!$allowNullNewType) { |
|
| 409 | 1 | $sql .= ' UPDATE ' . $this->db->quoteTableName($table) . ' SET ' . $this->db->quoteColumnName($column) . ' = 0' |
|
| 410 | 1 | . ' WHERE ' . $this->db->quoteColumnName($column) . ' IS NULL;'; |
|
| 411 | 1 | } |
|
| 412 | 1 | $sql .= ' END'; |
|
| 413 | 1 | return $sql; |
|
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @inheritdoc |
||
| 419 | */ |
||
| 420 | public function dropIndex($name, $table) |
||
| 421 | { |
||
| 422 | return 'DROP INDEX ' . $this->db->quoteTableName($name); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @inheritdoc |
||
| 427 | */ |
||
| 428 | public function resetSequence($table, $value = null) |
||
| 429 | { |
||
| 430 | $tableSchema = $this->db->getTableSchema($table); |
||
| 431 | if ($tableSchema === null) { |
||
| 432 | throw new InvalidParamException("Table not found: $table"); |
||
| 433 | } |
||
| 434 | if ($tableSchema->sequenceName === null) { |
||
| 435 | throw new InvalidParamException("There is not sequence associated with table '$table'."); |
||
| 436 | } |
||
| 437 | |||
| 438 | if ($value !== null) { |
||
| 439 | $value = (int) $value; |
||
| 440 | } else { |
||
| 441 | // use master connection to get the biggest PK value |
||
| 442 | $value = $this->db->useMaster(function(Connection $db) use ($tableSchema) { |
||
| 443 | $key = false; |
||
| 444 | foreach ($tableSchema->primaryKey as $name) { |
||
| 445 | if ($tableSchema->columns[$name]->autoIncrement) { |
||
| 446 | $key = $name; |
||
| 447 | break; |
||
| 448 | } |
||
| 449 | } |
||
| 450 | if ($key === false) { |
||
| 451 | return 0; |
||
| 452 | } |
||
| 453 | return $db->createCommand("SELECT MAX({$this->db->quoteColumnName($key)}) FROM {$this->db->quoteTableName($tableSchema->name)}")->queryScalar(); |
||
| 454 | }) + 1; |
||
| 455 | } |
||
| 456 | |||
| 457 | return "ALTER SEQUENCE {$this->db->quoteColumnName($tableSchema->sequenceName)} RESTART WITH $value"; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @inheritdoc |
||
| 462 | */ |
||
| 463 | 2 | public function createTable($table, $columns, $options = null) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @inheritdoc |
||
| 500 | */ |
||
| 501 | 1 | public function dropTable($table) |
|
| 502 | { |
||
| 503 | 1 | $sql = parent::dropTable($table); |
|
| 504 | |||
| 505 | 1 | $tableSchema = $this->db->getTableSchema($table); |
|
| 506 | 1 | if ($tableSchema === null || $tableSchema->sequenceName === null) { |
|
| 507 | return $sql; |
||
| 508 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Creates a SELECT EXISTS() SQL statement. |
||
| 523 | * @param string $rawSql the subquery in a raw form to select from. |
||
| 524 | * @return string the SELECT EXISTS() SQL statement. |
||
| 525 | * |
||
| 526 | * @since 2.0.8 |
||
| 527 | */ |
||
| 528 | public function selectExists($rawSql) |
||
| 532 | } |
||
| 533 |