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 |
||
| 26 | class QueryBuilder extends \yii\base\BaseObject |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * The prefix for automatically generated query binding parameters. |
||
| 30 | */ |
||
| 31 | const PARAM_PREFIX = ':qp'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Connection the database connection. |
||
| 35 | */ |
||
| 36 | public $db; |
||
| 37 | /** |
||
| 38 | * @var string the separator between different fragments of a SQL statement. |
||
| 39 | * Defaults to an empty space. This is mainly used by [[build()]] when generating a SQL statement. |
||
| 40 | */ |
||
| 41 | public $separator = ' '; |
||
| 42 | /** |
||
| 43 | * @var array the abstract column types mapped to physical column types. |
||
| 44 | * This is mainly used to support creating/modifying tables using DB-independent data type specifications. |
||
| 45 | * Child classes should override this property to declare supported type mappings. |
||
| 46 | */ |
||
| 47 | public $typeMap = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array map of query condition to builder methods. |
||
| 51 | * These methods are used by [[buildCondition]] to build SQL conditions from array syntax. |
||
| 52 | */ |
||
| 53 | protected $conditionBuilders = [ |
||
| 54 | 'NOT' => 'buildNotCondition', |
||
| 55 | 'AND' => 'buildAndCondition', |
||
| 56 | 'OR' => 'buildAndCondition', |
||
| 57 | 'BETWEEN' => 'buildBetweenCondition', |
||
| 58 | 'NOT BETWEEN' => 'buildBetweenCondition', |
||
| 59 | 'IN' => 'buildInCondition', |
||
| 60 | 'NOT IN' => 'buildInCondition', |
||
| 61 | 'LIKE' => 'buildLikeCondition', |
||
| 62 | 'NOT LIKE' => 'buildLikeCondition', |
||
| 63 | 'OR LIKE' => 'buildLikeCondition', |
||
| 64 | 'OR NOT LIKE' => 'buildLikeCondition', |
||
| 65 | 'EXISTS' => 'buildExistsCondition', |
||
| 66 | 'NOT EXISTS' => 'buildExistsCondition', |
||
| 67 | ]; |
||
| 68 | /** |
||
| 69 | * @var array map of chars to their replacements in LIKE conditions. |
||
| 70 | * By default it's configured to escape `%`, `_` and `\` with `\`. |
||
| 71 | * @since 2.0.12. |
||
| 72 | */ |
||
| 73 | protected $likeEscapingReplacements = [ |
||
| 74 | '%' => '\%', |
||
| 75 | '_' => '\_', |
||
| 76 | '\\' => '\\\\', |
||
| 77 | ]; |
||
| 78 | /** |
||
| 79 | * @var string|null character used to escape special characters in LIKE conditions. |
||
| 80 | * By default it's assumed to be `\`. |
||
| 81 | * @since 2.0.12 |
||
| 82 | */ |
||
| 83 | protected $likeEscapeCharacter; |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Constructor. |
||
| 88 | * @param Connection $connection the database connection. |
||
| 89 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
| 90 | */ |
||
| 91 | 1163 | public function __construct($connection, $config = []) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
| 99 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
| 100 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
| 101 | * be included in the result with the additional parameters generated during the query building process. |
||
| 102 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
| 103 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
| 104 | * include those provided in `$params`. |
||
| 105 | */ |
||
| 106 | 653 | public function build($query, $params = []) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Creates an INSERT SQL statement. |
||
| 149 | * |
||
| 150 | * For example, |
||
| 151 | * |
||
| 152 | * ```php |
||
| 153 | * $sql = $queryBuilder->insert('user', [ |
||
| 154 | * 'name' => 'Sam', |
||
| 155 | * 'age' => 30, |
||
| 156 | * ], $params); |
||
| 157 | * ``` |
||
| 158 | * |
||
| 159 | * The method will properly escape the table and column names. |
||
| 160 | * |
||
| 161 | * @param string $table the table that new rows will be inserted into. |
||
| 162 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
||
| 163 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
| 164 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
| 165 | * @param array $params the binding parameters that will be generated by this method. |
||
| 166 | * They should be bound to the DB command later. |
||
| 167 | * @return string the INSERT SQL |
||
| 168 | */ |
||
| 169 | 263 | public function insert($table, $columns, &$params) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
| 208 | * |
||
| 209 | * @param \yii\db\Query $columns Object, which represents select query. |
||
| 210 | * @param \yii\db\Schema $schema Schema object to quote column name. |
||
| 211 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
| 212 | * be included in the result with the additional parameters generated during the query building process. |
||
| 213 | * @return array |
||
| 214 | * @throws InvalidParamException if query's select does not contain named parameters only. |
||
| 215 | * @since 2.0.11 |
||
| 216 | */ |
||
| 217 | 15 | protected function prepareInsertSelectSubQuery($columns, $schema, $params = []) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Generates a batch INSERT SQL statement. |
||
| 241 | * |
||
| 242 | * For example, |
||
| 243 | * |
||
| 244 | * ```php |
||
| 245 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
| 246 | * ['Tom', 30], |
||
| 247 | * ['Jane', 20], |
||
| 248 | * ['Linda', 25], |
||
| 249 | * ]); |
||
| 250 | * ``` |
||
| 251 | * |
||
| 252 | * Note that the values in each row must match the corresponding column names. |
||
| 253 | * |
||
| 254 | * The method will properly escape the column names, and quote the values to be inserted. |
||
| 255 | * |
||
| 256 | * @param string $table the table that new rows will be inserted into. |
||
| 257 | * @param array $columns the column names |
||
| 258 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
| 259 | * @return string the batch INSERT SQL statement |
||
| 260 | */ |
||
| 261 | 22 | public function batchInsert($table, $columns, $rows) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Creates an UPDATE SQL statement. |
||
| 309 | * |
||
| 310 | * For example, |
||
| 311 | * |
||
| 312 | * ```php |
||
| 313 | * $params = []; |
||
| 314 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
| 315 | * ``` |
||
| 316 | * |
||
| 317 | * The method will properly escape the table and column names. |
||
| 318 | * |
||
| 319 | * @param string $table the table to be updated. |
||
| 320 | * @param array $columns the column data (name => value) to be updated. |
||
| 321 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
| 322 | * refer to [[Query::where()]] on how to specify condition. |
||
| 323 | * @param array $params the binding parameters that will be modified by this method |
||
| 324 | * so that they can be bound to the DB command later. |
||
| 325 | * @return string the UPDATE SQL |
||
| 326 | */ |
||
| 327 | 92 | public function update($table, $columns, $condition, &$params) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Creates a DELETE SQL statement. |
||
| 357 | * |
||
| 358 | * For example, |
||
| 359 | * |
||
| 360 | * ```php |
||
| 361 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
| 362 | * ``` |
||
| 363 | * |
||
| 364 | * The method will properly escape the table and column names. |
||
| 365 | * |
||
| 366 | * @param string $table the table where the data will be deleted from. |
||
| 367 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
| 368 | * refer to [[Query::where()]] on how to specify condition. |
||
| 369 | * @param array $params the binding parameters that will be modified by this method |
||
| 370 | * so that they can be bound to the DB command later. |
||
| 371 | * @return string the DELETE SQL |
||
| 372 | */ |
||
| 373 | 303 | public function delete($table, $condition, &$params) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Builds a SQL statement for creating a new DB table. |
||
| 383 | * |
||
| 384 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
| 385 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
| 386 | * stands for the column type which can contain an abstract DB type. |
||
| 387 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
| 388 | * |
||
| 389 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
| 390 | * inserted into the generated SQL. |
||
| 391 | * |
||
| 392 | * For example, |
||
| 393 | * |
||
| 394 | * ```php |
||
| 395 | * $sql = $queryBuilder->createTable('user', [ |
||
| 396 | * 'id' => 'pk', |
||
| 397 | * 'name' => 'string', |
||
| 398 | * 'age' => 'integer', |
||
| 399 | * ]); |
||
| 400 | * ``` |
||
| 401 | * |
||
| 402 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
| 403 | * @param array $columns the columns (name => definition) in the new table. |
||
| 404 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
| 405 | * @return string the SQL statement for creating a new DB table. |
||
| 406 | */ |
||
| 407 | 110 | public function createTable($table, $columns, $options = null) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Builds a SQL statement for renaming a DB table. |
||
| 424 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
| 425 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
| 426 | * @return string the SQL statement for renaming a DB table. |
||
| 427 | */ |
||
| 428 | 1 | public function renameTable($oldName, $newName) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Builds a SQL statement for dropping a DB table. |
||
| 435 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
| 436 | * @return string the SQL statement for dropping a DB table. |
||
| 437 | */ |
||
| 438 | 19 | public function dropTable($table) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
| 445 | * @param string $name the name of the primary key constraint. |
||
| 446 | * @param string $table the table that the primary key constraint will be added to. |
||
| 447 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
| 448 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
| 449 | */ |
||
| 450 | 6 | public function addPrimaryKey($name, $table, $columns) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
| 467 | * @param string $name the name of the primary key constraint to be removed. |
||
| 468 | * @param string $table the table that the primary key constraint will be removed from. |
||
| 469 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
| 470 | */ |
||
| 471 | 2 | public function dropPrimaryKey($name, $table) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Builds a SQL statement for truncating a DB table. |
||
| 479 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
| 480 | * @return string the SQL statement for truncating a DB table. |
||
| 481 | */ |
||
| 482 | 9 | public function truncateTable($table) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Builds a SQL statement for adding a new DB column. |
||
| 489 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
| 490 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
| 491 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
| 492 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
| 493 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
| 494 | * @return string the SQL statement for adding a new column. |
||
| 495 | */ |
||
| 496 | 4 | public function addColumn($table, $column, $type) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Builds a SQL statement for dropping a DB column. |
||
| 505 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
| 506 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
| 507 | * @return string the SQL statement for dropping a DB column. |
||
| 508 | */ |
||
| 509 | public function dropColumn($table, $column) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Builds a SQL statement for renaming a column. |
||
| 517 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
| 518 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
| 519 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
| 520 | * @return string the SQL statement for renaming a DB column. |
||
| 521 | */ |
||
| 522 | public function renameColumn($table, $oldName, $newName) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Builds a SQL statement for changing the definition of a column. |
||
| 531 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
| 532 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
| 533 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
| 534 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
| 535 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
| 536 | * will become 'varchar(255) not null'. |
||
| 537 | * @return string the SQL statement for changing the definition of a column. |
||
| 538 | */ |
||
| 539 | 1 | public function alterColumn($table, $column, $type) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
| 549 | * The method will properly quote the table and column names. |
||
| 550 | * @param string $name the name of the foreign key constraint. |
||
| 551 | * @param string $table the table that the foreign key constraint will be added to. |
||
| 552 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
| 553 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
| 554 | * @param string $refTable the table that the foreign key references to. |
||
| 555 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
| 556 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
| 557 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
| 558 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
| 559 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
| 560 | */ |
||
| 561 | 8 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Builds a SQL statement for dropping a foreign key constraint. |
||
| 580 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
| 581 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
| 582 | * @return string the SQL statement for dropping a foreign key constraint. |
||
| 583 | */ |
||
| 584 | 3 | public function dropForeignKey($name, $table) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Builds a SQL statement for creating a new index. |
||
| 592 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
| 593 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
| 594 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
| 595 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
| 596 | * by the method, unless a parenthesis is found in the name. |
||
| 597 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
| 598 | * @return string the SQL statement for creating a new index. |
||
| 599 | */ |
||
| 600 | 6 | public function createIndex($name, $table, $columns, $unique = false) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Builds a SQL statement for dropping an index. |
||
| 610 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
| 611 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
| 612 | * @return string the SQL statement for dropping an index. |
||
| 613 | */ |
||
| 614 | 4 | public function dropIndex($name, $table) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Creates a SQL command for adding an unique constraint to an existing table. |
||
| 621 | * @param string $name the name of the unique constraint. |
||
| 622 | * The name will be properly quoted by the method. |
||
| 623 | * @param string $table the table that the unique constraint will be added to. |
||
| 624 | * The name will be properly quoted by the method. |
||
| 625 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
| 626 | * If there are multiple columns, separate them with commas. |
||
| 627 | * The name will be properly quoted by the method. |
||
| 628 | * @return string the SQL statement for adding an unique constraint to an existing table. |
||
| 629 | * @since 2.0.13 |
||
| 630 | */ |
||
| 631 | 6 | public function addUnique($name, $table, $columns) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Creates a SQL command for dropping an unique constraint. |
||
| 647 | * @param string $name the name of the unique constraint to be dropped. |
||
| 648 | * The name will be properly quoted by the method. |
||
| 649 | * @param string $table the table whose unique constraint is to be dropped. |
||
| 650 | * The name will be properly quoted by the method. |
||
| 651 | * @return string the SQL statement for dropping an unique constraint. |
||
| 652 | * @since 2.0.13 |
||
| 653 | */ |
||
| 654 | 2 | public function dropUnique($name, $table) |
|
| 659 | |||
| 660 | /** |
||
| 661 | * Creates a SQL command for adding a check constraint to an existing table. |
||
| 662 | * @param string $name the name of the check constraint. |
||
| 663 | * The name will be properly quoted by the method. |
||
| 664 | * @param string $table the table that the check constraint will be added to. |
||
| 665 | * The name will be properly quoted by the method. |
||
| 666 | * @param string $expression the SQL of the `CHECK` constraint. |
||
| 667 | * @return string the SQL statement for adding a check constraint to an existing table. |
||
| 668 | * @since 2.0.13 |
||
| 669 | */ |
||
| 670 | 2 | public function addCheck($name, $table, $expression) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * Creates a SQL command for dropping a check constraint. |
||
| 678 | * @param string $name the name of the check constraint to be dropped. |
||
| 679 | * The name will be properly quoted by the method. |
||
| 680 | * @param string $table the table whose check constraint is to be dropped. |
||
| 681 | * The name will be properly quoted by the method. |
||
| 682 | * @return string the SQL statement for dropping a check constraint. |
||
| 683 | * @since 2.0.13 |
||
| 684 | */ |
||
| 685 | 2 | public function dropCheck($name, $table) |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
| 693 | * @param string $name the name of the default value constraint. |
||
| 694 | * The name will be properly quoted by the method. |
||
| 695 | * @param string $table the table that the default value constraint will be added to. |
||
| 696 | * The name will be properly quoted by the method. |
||
| 697 | * @param string $column the name of the column to that the constraint will be added on. |
||
| 698 | * The name will be properly quoted by the method. |
||
| 699 | * @param mixed $value default value. |
||
| 700 | * @return string the SQL statement for adding a default value constraint to an existing table. |
||
| 701 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
| 702 | * @since 2.0.13 |
||
| 703 | */ |
||
| 704 | public function addDefaultValue($name, $table, $column, $value) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Creates a SQL command for dropping a default value constraint. |
||
| 711 | * @param string $name the name of the default value constraint to be dropped. |
||
| 712 | * The name will be properly quoted by the method. |
||
| 713 | * @param string $table the table whose default value constraint is to be dropped. |
||
| 714 | * The name will be properly quoted by the method. |
||
| 715 | * @return string the SQL statement for dropping a default value constraint. |
||
| 716 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
| 717 | * @since 2.0.13 |
||
| 718 | */ |
||
| 719 | public function dropDefaultValue($name, $table) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
| 726 | * The sequence will be reset such that the primary key of the next new row inserted |
||
| 727 | * will have the specified value or 1. |
||
| 728 | * @param string $table the name of the table whose primary key sequence will be reset |
||
| 729 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
| 730 | * the next new row's primary key will have a value 1. |
||
| 731 | * @return string the SQL statement for resetting sequence |
||
| 732 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
| 733 | */ |
||
| 734 | public function resetSequence($table, $value = null) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Builds a SQL statement for enabling or disabling integrity check. |
||
| 741 | * @param bool $check whether to turn on or off the integrity check. |
||
| 742 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
| 743 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
| 744 | * @return string the SQL statement for checking integrity |
||
| 745 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
| 746 | */ |
||
| 747 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Builds a SQL command for adding comment to column. |
||
| 754 | * |
||
| 755 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 756 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
| 757 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
| 758 | * @return string the SQL statement for adding comment on column |
||
| 759 | * @since 2.0.8 |
||
| 760 | */ |
||
| 761 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
| 765 | |||
| 766 | /** |
||
| 767 | * Builds a SQL command for adding comment to table. |
||
| 768 | * |
||
| 769 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 770 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
| 771 | * @return string the SQL statement for adding comment on table |
||
| 772 | * @since 2.0.8 |
||
| 773 | */ |
||
| 774 | 1 | public function addCommentOnTable($table, $comment) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Builds a SQL command for adding comment to column. |
||
| 781 | * |
||
| 782 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 783 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
| 784 | * @return string the SQL statement for adding comment on column |
||
| 785 | * @since 2.0.8 |
||
| 786 | */ |
||
| 787 | 2 | public function dropCommentFromColumn($table, $column) |
|
| 791 | |||
| 792 | /** |
||
| 793 | * Builds a SQL command for adding comment to table. |
||
| 794 | * |
||
| 795 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 796 | * @return string the SQL statement for adding comment on column |
||
| 797 | * @since 2.0.8 |
||
| 798 | */ |
||
| 799 | 1 | public function dropCommentFromTable($table) |
|
| 803 | |||
| 804 | /** |
||
| 805 | * Converts an abstract column type into a physical column type. |
||
| 806 | * |
||
| 807 | * The conversion is done using the type map specified in [[typeMap]]. |
||
| 808 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
| 809 | * physical types): |
||
| 810 | * |
||
| 811 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
| 812 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
| 813 | * - `upk`: an unsigned auto-incremental primary key type, will be converted into "int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
| 814 | * - `char`: char type, will be converted into "char(1)" |
||
| 815 | * - `string`: string type, will be converted into "varchar(255)" |
||
| 816 | * - `text`: a long string type, will be converted into "text" |
||
| 817 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
| 818 | * - `integer`: integer type, will be converted into "int(11)" |
||
| 819 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
| 820 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
| 821 | * - `float``: float number type, will be converted into "float" |
||
| 822 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
| 823 | * - `datetime`: datetime type, will be converted into "datetime" |
||
| 824 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
| 825 | * - `time`: time type, will be converted into "time" |
||
| 826 | * - `date`: date type, will be converted into "date" |
||
| 827 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
| 828 | * - `binary`: binary data type, will be converted into "blob" |
||
| 829 | * |
||
| 830 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
| 831 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
| 832 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
| 833 | * |
||
| 834 | * For some of the abstract types you can also specify a length or precision constraint |
||
| 835 | * by appending it in round brackets directly to the type. |
||
| 836 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
| 837 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
| 838 | * be ignored. |
||
| 839 | * |
||
| 840 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
| 841 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
| 842 | * @return string physical column type. |
||
| 843 | */ |
||
| 844 | 114 | public function getColumnType($type) |
|
| 864 | |||
| 865 | /** |
||
| 866 | * @param array $columns |
||
| 867 | * @param array $params the binding parameters to be populated |
||
| 868 | * @param bool $distinct |
||
| 869 | * @param string $selectOption |
||
| 870 | * @return string the SELECT clause built from [[Query::$select]]. |
||
| 871 | */ |
||
| 872 | 949 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
| 910 | |||
| 911 | /** |
||
| 912 | * @param array $tables |
||
| 913 | * @param array $params the binding parameters to be populated |
||
| 914 | * @return string the FROM clause built from [[Query::$from]]. |
||
| 915 | */ |
||
| 916 | 949 | public function buildFrom($tables, &$params) |
|
| 926 | |||
| 927 | /** |
||
| 928 | * @param array $joins |
||
| 929 | * @param array $params the binding parameters to be populated |
||
| 930 | * @return string the JOIN clause built from [[Query::$join]]. |
||
| 931 | * @throws Exception if the $joins parameter is not in proper format |
||
| 932 | */ |
||
| 933 | 949 | public function buildJoin($joins, &$params) |
|
| 958 | |||
| 959 | /** |
||
| 960 | * Quotes table names passed. |
||
| 961 | * |
||
| 962 | * @param array $tables |
||
| 963 | * @param array $params |
||
| 964 | * @return array |
||
| 965 | */ |
||
| 966 | 694 | private function quoteTableNames($tables, &$params) |
|
| 988 | |||
| 989 | /** |
||
| 990 | * @param string|array $condition |
||
| 991 | * @param array $params the binding parameters to be populated |
||
| 992 | * @return string the WHERE clause built from [[Query::$where]]. |
||
| 993 | */ |
||
| 994 | 1012 | public function buildWhere($condition, &$params) |
|
| 1000 | |||
| 1001 | /** |
||
| 1002 | * @param array $columns |
||
| 1003 | * @return string the GROUP BY clause |
||
| 1004 | */ |
||
| 1005 | 949 | public function buildGroupBy($columns) |
|
| 1020 | |||
| 1021 | /** |
||
| 1022 | * @param string|array $condition |
||
| 1023 | * @param array $params the binding parameters to be populated |
||
| 1024 | * @return string the HAVING clause built from [[Query::$having]]. |
||
| 1025 | */ |
||
| 1026 | 949 | public function buildHaving($condition, &$params) |
|
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
| 1035 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
| 1036 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
| 1037 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
| 1038 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
| 1039 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
| 1040 | */ |
||
| 1041 | 949 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
| 1054 | |||
| 1055 | /** |
||
| 1056 | * @param array $columns |
||
| 1057 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
| 1058 | */ |
||
| 1059 | 949 | public function buildOrderBy($columns) |
|
| 1075 | |||
| 1076 | /** |
||
| 1077 | * @param int $limit |
||
| 1078 | * @param int $offset |
||
| 1079 | * @return string the LIMIT and OFFSET clauses |
||
| 1080 | */ |
||
| 1081 | 317 | public function buildLimit($limit, $offset) |
|
| 1093 | |||
| 1094 | /** |
||
| 1095 | * Checks to see if the given limit is effective. |
||
| 1096 | * @param mixed $limit the given limit |
||
| 1097 | * @return bool whether the limit is effective |
||
| 1098 | */ |
||
| 1099 | 613 | protected function hasLimit($limit) |
|
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Checks to see if the given offset is effective. |
||
| 1106 | * @param mixed $offset the given offset |
||
| 1107 | * @return bool whether the offset is effective |
||
| 1108 | */ |
||
| 1109 | 613 | protected function hasOffset($offset) |
|
| 1113 | |||
| 1114 | /** |
||
| 1115 | * @param array $unions |
||
| 1116 | * @param array $params the binding parameters to be populated |
||
| 1117 | * @return string the UNION clause built from [[Query::$union]]. |
||
| 1118 | */ |
||
| 1119 | 653 | public function buildUnion($unions, &$params) |
|
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Processes columns and properly quotes them if necessary. |
||
| 1141 | * It will join all columns into a string with comma as separators. |
||
| 1142 | * @param string|array $columns the columns to be processed |
||
| 1143 | * @return string the processing result |
||
| 1144 | */ |
||
| 1145 | 32 | public function buildColumns($columns) |
|
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Parses the condition specification and generates the corresponding SQL expression. |
||
| 1167 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
| 1168 | * on how to specify a condition. |
||
| 1169 | * @param array $params the binding parameters to be populated |
||
| 1170 | * @return string the generated SQL expression |
||
| 1171 | */ |
||
| 1172 | 1012 | public function buildCondition($condition, &$params) |
|
| 1200 | |||
| 1201 | /** |
||
| 1202 | * Creates a condition based on column-value pairs. |
||
| 1203 | * @param array $condition the condition specification. |
||
| 1204 | * @param array $params the binding parameters to be populated |
||
| 1205 | * @return string the generated SQL expression |
||
| 1206 | */ |
||
| 1207 | 563 | public function buildHashCondition($condition, &$params) |
|
| 1235 | |||
| 1236 | /** |
||
| 1237 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
| 1238 | * @param string $operator the operator to use for connecting the given operands |
||
| 1239 | * @param array $operands the SQL expressions to connect. |
||
| 1240 | * @param array $params the binding parameters to be populated |
||
| 1241 | * @return string the generated SQL expression |
||
| 1242 | */ |
||
| 1243 | 197 | public function buildAndCondition($operator, $operands, &$params) |
|
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Inverts an SQL expressions with `NOT` operator. |
||
| 1269 | * @param string $operator the operator to use for connecting the given operands |
||
| 1270 | * @param array $operands the SQL expressions to connect. |
||
| 1271 | * @param array $params the binding parameters to be populated |
||
| 1272 | * @return string the generated SQL expression |
||
| 1273 | * @throws InvalidParamException if wrong number of operands have been given. |
||
| 1274 | */ |
||
| 1275 | 6 | public function buildNotCondition($operator, $operands, &$params) |
|
| 1291 | |||
| 1292 | /** |
||
| 1293 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
| 1294 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
| 1295 | * @param array $operands the first operand is the column name. The second and third operands |
||
| 1296 | * describe the interval that column value should be in. |
||
| 1297 | * @param array $params the binding parameters to be populated |
||
| 1298 | * @return string the generated SQL expression |
||
| 1299 | * @throws InvalidParamException if wrong number of operands have been given. |
||
| 1300 | */ |
||
| 1301 | 21 | public function buildBetweenCondition($operator, $operands, &$params) |
|
| 1333 | |||
| 1334 | /** |
||
| 1335 | * Creates an SQL expressions with the `IN` operator. |
||
| 1336 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
| 1337 | * @param array $operands the first operand is the column name. If it is an array |
||
| 1338 | * a composite IN condition will be generated. |
||
| 1339 | * The second operand is an array of values that column value should be among. |
||
| 1340 | * If it is an empty array the generated expression will be a `false` value if |
||
| 1341 | * operator is `IN` and empty if operator is `NOT IN`. |
||
| 1342 | * @param array $params the binding parameters to be populated |
||
| 1343 | * @return string the generated SQL expression |
||
| 1344 | * @throws Exception if wrong number of operands have been given. |
||
| 1345 | */ |
||
| 1346 | 228 | public function buildInCondition($operator, $operands, &$params) |
|
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Builds SQL for IN condition. |
||
| 1410 | * |
||
| 1411 | * @param string $operator |
||
| 1412 | * @param array $columns |
||
| 1413 | * @param Query $values |
||
| 1414 | * @param array $params |
||
| 1415 | * @return string SQL |
||
| 1416 | */ |
||
| 1417 | 14 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Builds SQL for IN condition. |
||
| 1439 | * |
||
| 1440 | * @param string $operator |
||
| 1441 | * @param array|\Traversable $columns |
||
| 1442 | * @param array $values |
||
| 1443 | * @param array $params |
||
| 1444 | * @return string SQL |
||
| 1445 | */ |
||
| 1446 | 10 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
| 1474 | |||
| 1475 | /** |
||
| 1476 | * Creates an SQL expressions with the `LIKE` operator. |
||
| 1477 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
| 1478 | * @param array $operands an array of two or three operands |
||
| 1479 | * |
||
| 1480 | * - The first operand is the column name. |
||
| 1481 | * - The second operand is a single value or an array of values that column value |
||
| 1482 | * should be compared with. If it is an empty array the generated expression will |
||
| 1483 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
| 1484 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
| 1485 | * - An optional third operand can also be provided to specify how to escape special characters |
||
| 1486 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
| 1487 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
| 1488 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
| 1489 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
| 1490 | * the values will be automatically enclosed within a pair of percentage characters. |
||
| 1491 | * @param array $params the binding parameters to be populated |
||
| 1492 | * @return string the generated SQL expression |
||
| 1493 | * @throws InvalidParamException if wrong number of operands have been given. |
||
| 1494 | */ |
||
| 1495 | 75 | public function buildLikeCondition($operator, $operands, &$params) |
|
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Creates an SQL expressions with the `EXISTS` operator. |
||
| 1548 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
| 1549 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
| 1550 | * @param array $params the binding parameters to be populated |
||
| 1551 | * @return string the generated SQL expression |
||
| 1552 | * @throws InvalidParamException if the operand is not a [[Query]] object. |
||
| 1553 | */ |
||
| 1554 | 18 | public function buildExistsCondition($operator, $operands, &$params) |
|
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Creates an SQL expressions like `"column" operator value`. |
||
| 1566 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
| 1567 | * @param array $operands contains two column names. |
||
| 1568 | * @param array $params the binding parameters to be populated |
||
| 1569 | * @return string the generated SQL expression |
||
| 1570 | * @throws InvalidParamException if wrong number of operands have been given. |
||
| 1571 | */ |
||
| 1572 | 36 | public function buildSimpleCondition($operator, $operands, &$params) |
|
| 1601 | |||
| 1602 | /** |
||
| 1603 | * Creates a SELECT EXISTS() SQL statement. |
||
| 1604 | * @param string $rawSql the subquery in a raw form to select from. |
||
| 1605 | * @return string the SELECT EXISTS() SQL statement. |
||
| 1606 | * @since 2.0.8 |
||
| 1607 | */ |
||
| 1608 | 60 | public function selectExists($rawSql) |
|
| 1612 | } |
||
| 1613 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.