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\Object |
||
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 | 991 | 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 | 580 | public function build($query, $params = []) |
|
131 | |||
132 | /** |
||
133 | * Creates an INSERT SQL statement. |
||
134 | * For example, |
||
135 | * |
||
136 | * ```php |
||
137 | * $sql = $queryBuilder->insert('user', [ |
||
138 | * 'name' => 'Sam', |
||
139 | * 'age' => 30, |
||
140 | * ], $params); |
||
141 | * ``` |
||
142 | * |
||
143 | * The method will properly escape the table and column names. |
||
144 | * |
||
145 | * @param string $table the table that new rows will be inserted into. |
||
146 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
||
147 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
148 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
149 | * @param array $params the binding parameters that will be generated by this method. |
||
150 | * They should be bound to the DB command later. |
||
151 | * @return string the INSERT SQL |
||
152 | */ |
||
153 | 191 | public function insert($table, $columns, &$params) |
|
189 | |||
190 | /** |
||
191 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
192 | * |
||
193 | * @param \yii\db\Query $columns Object, which represents select query. |
||
194 | * @param \yii\db\Schema $schema Schema object to quote column name. |
||
195 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
196 | * be included in the result with the additional parameters generated during the query building process. |
||
197 | * @return array |
||
198 | * @throws InvalidArgumentException if query's select does not contain named parameters only. |
||
199 | * @since 2.0.11 |
||
200 | */ |
||
201 | 15 | protected function prepareInsertSelectSubQuery($columns, $schema, $params = []) |
|
222 | |||
223 | /** |
||
224 | * Generates a batch INSERT SQL statement. |
||
225 | * For example, |
||
226 | * |
||
227 | * ```php |
||
228 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
229 | * ['Tom', 30], |
||
230 | * ['Jane', 20], |
||
231 | * ['Linda', 25], |
||
232 | * ]); |
||
233 | * ``` |
||
234 | * |
||
235 | * Note that the values in each row must match the corresponding column names. |
||
236 | * |
||
237 | * The method will properly escape the column names, and quote the values to be inserted. |
||
238 | * |
||
239 | * @param string $table the table that new rows will be inserted into. |
||
240 | * @param array $columns the column names |
||
241 | * @param array $rows the rows to be batch inserted into the table |
||
242 | * @return string the batch INSERT SQL statement |
||
243 | */ |
||
244 | 20 | public function batchInsert($table, $columns, $rows) |
|
286 | |||
287 | /** |
||
288 | * Creates an UPDATE SQL statement. |
||
289 | * For example, |
||
290 | * |
||
291 | * ```php |
||
292 | * $params = []; |
||
293 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
294 | * ``` |
||
295 | * |
||
296 | * The method will properly escape the table and column names. |
||
297 | * |
||
298 | * @param string $table the table to be updated. |
||
299 | * @param array $columns the column data (name => value) to be updated. |
||
300 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
301 | * refer to [[Query::where()]] on how to specify condition. |
||
302 | * @param array $params the binding parameters that will be modified by this method |
||
303 | * so that they can be bound to the DB command later. |
||
304 | * @return string the UPDATE SQL |
||
305 | */ |
||
306 | 79 | public function update($table, $columns, $condition, &$params) |
|
333 | |||
334 | /** |
||
335 | * Creates a DELETE SQL statement. |
||
336 | * For example, |
||
337 | * |
||
338 | * ```php |
||
339 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
340 | * ``` |
||
341 | * |
||
342 | * The method will properly escape the table and column names. |
||
343 | * |
||
344 | * @param string $table the table where the data will be deleted from. |
||
345 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
346 | * refer to [[Query::where()]] on how to specify condition. |
||
347 | * @param array $params the binding parameters that will be modified by this method |
||
348 | * so that they can be bound to the DB command later. |
||
349 | * @return string the DELETE SQL |
||
350 | */ |
||
351 | 194 | public function delete($table, $condition, &$params) |
|
358 | |||
359 | /** |
||
360 | * Builds a SQL statement for creating a new DB table. |
||
361 | * |
||
362 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
363 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
364 | * stands for the column type which can contain an abstract DB type. |
||
365 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
366 | * |
||
367 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
368 | * inserted into the generated SQL. |
||
369 | * |
||
370 | * For example, |
||
371 | * |
||
372 | * ```php |
||
373 | * $sql = $queryBuilder->createTable('user', [ |
||
374 | * 'id' => 'pk', |
||
375 | * 'name' => 'string', |
||
376 | * 'age' => 'integer', |
||
377 | * ]); |
||
378 | * ``` |
||
379 | * |
||
380 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
381 | * @param array $columns the columns (name => definition) in the new table. |
||
382 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
383 | * @return string the SQL statement for creating a new DB table. |
||
384 | */ |
||
385 | 86 | public function createTable($table, $columns, $options = null) |
|
399 | |||
400 | /** |
||
401 | * Builds a SQL statement for renaming a DB table. |
||
402 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
403 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
404 | * @return string the SQL statement for renaming a DB table. |
||
405 | */ |
||
406 | 1 | public function renameTable($oldName, $newName) |
|
410 | |||
411 | /** |
||
412 | * Builds a SQL statement for dropping a DB table. |
||
413 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
414 | * @return string the SQL statement for dropping a DB table. |
||
415 | */ |
||
416 | 18 | public function dropTable($table) |
|
420 | |||
421 | /** |
||
422 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
423 | * @param string $name the name of the primary key constraint. |
||
424 | * @param string $table the table that the primary key constraint will be added to. |
||
425 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
426 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
427 | */ |
||
428 | 2 | public function addPrimaryKey($name, $table, $columns) |
|
442 | |||
443 | /** |
||
444 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
445 | * @param string $name the name of the primary key constraint to be removed. |
||
446 | * @param string $table the table that the primary key constraint will be removed from. |
||
447 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
448 | */ |
||
449 | 1 | public function dropPrimaryKey($name, $table) |
|
454 | |||
455 | /** |
||
456 | * Builds a SQL statement for truncating a DB table. |
||
457 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
458 | * @return string the SQL statement for truncating a DB table. |
||
459 | */ |
||
460 | 9 | public function truncateTable($table) |
|
464 | |||
465 | /** |
||
466 | * Builds a SQL statement for adding a new DB column. |
||
467 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
468 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
469 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
470 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
471 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
472 | * @return string the SQL statement for adding a new column. |
||
473 | */ |
||
474 | 4 | public function addColumn($table, $column, $type) |
|
480 | |||
481 | /** |
||
482 | * Builds a SQL statement for dropping a DB column. |
||
483 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
484 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
485 | * @return string the SQL statement for dropping a DB column. |
||
486 | */ |
||
487 | public function dropColumn($table, $column) |
||
492 | |||
493 | /** |
||
494 | * Builds a SQL statement for renaming a column. |
||
495 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
496 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
497 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
498 | * @return string the SQL statement for renaming a DB column. |
||
499 | */ |
||
500 | public function renameColumn($table, $oldName, $newName) |
||
506 | |||
507 | /** |
||
508 | * Builds a SQL statement for changing the definition of a column. |
||
509 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
510 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
511 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
512 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
513 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
514 | * will become 'varchar(255) not null'. |
||
515 | * @return string the SQL statement for changing the definition of a column. |
||
516 | */ |
||
517 | 1 | public function alterColumn($table, $column, $type) |
|
524 | |||
525 | /** |
||
526 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
527 | * The method will properly quote the table and column names. |
||
528 | * @param string $name the name of the foreign key constraint. |
||
529 | * @param string $table the table that the foreign key constraint will be added to. |
||
530 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
531 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
532 | * @param string $refTable the table that the foreign key references to. |
||
533 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
534 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
535 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
536 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
537 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
538 | */ |
||
539 | 2 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
555 | |||
556 | /** |
||
557 | * Builds a SQL statement for dropping a foreign key constraint. |
||
558 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
559 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
560 | * @return string the SQL statement for dropping a foreign key constraint. |
||
561 | */ |
||
562 | 1 | public function dropForeignKey($name, $table) |
|
567 | |||
568 | /** |
||
569 | * Builds a SQL statement for creating a new index. |
||
570 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
571 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
572 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
573 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
574 | * by the method, unless a parenthesis is found in the name. |
||
575 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
576 | * @return string the SQL statement for creating a new index. |
||
577 | */ |
||
578 | 1 | public function createIndex($name, $table, $columns, $unique = false) |
|
585 | |||
586 | /** |
||
587 | * Builds a SQL statement for dropping an index. |
||
588 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
589 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
590 | * @return string the SQL statement for dropping an index. |
||
591 | */ |
||
592 | public function dropIndex($name, $table) |
||
596 | |||
597 | /** |
||
598 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
599 | * The sequence will be reset such that the primary key of the next new row inserted |
||
600 | * will have the specified value or 1. |
||
601 | * @param string $table the name of the table whose primary key sequence will be reset |
||
602 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
603 | * the next new row's primary key will have a value 1. |
||
604 | * @return string the SQL statement for resetting sequence |
||
605 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
606 | */ |
||
607 | public function resetSequence($table, $value = null) |
||
611 | |||
612 | /** |
||
613 | * Builds a SQL statement for enabling or disabling integrity check. |
||
614 | * @param bool $check whether to turn on or off the integrity check. |
||
615 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
616 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
617 | * @return string the SQL statement for checking integrity |
||
618 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
619 | */ |
||
620 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
624 | |||
625 | /** |
||
626 | * Builds a SQL command for adding comment to column |
||
627 | * |
||
628 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
629 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
630 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
631 | * @return string the SQL statement for adding comment on column |
||
632 | * @since 2.0.8 |
||
633 | */ |
||
634 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
639 | |||
640 | /** |
||
641 | * Builds a SQL command for adding comment to table |
||
642 | * |
||
643 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
644 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
645 | * @return string the SQL statement for adding comment on table |
||
646 | * @since 2.0.8 |
||
647 | */ |
||
648 | 1 | public function addCommentOnTable($table, $comment) |
|
652 | |||
653 | /** |
||
654 | * Builds a SQL command for adding comment to column |
||
655 | * |
||
656 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
657 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
658 | * @return string the SQL statement for adding comment on column |
||
659 | * @since 2.0.8 |
||
660 | */ |
||
661 | 2 | public function dropCommentFromColumn($table, $column) |
|
665 | |||
666 | /** |
||
667 | * Builds a SQL command for adding comment to table |
||
668 | * |
||
669 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
670 | * @return string the SQL statement for adding comment on column |
||
671 | * @since 2.0.8 |
||
672 | */ |
||
673 | 1 | public function dropCommentFromTable($table) |
|
677 | |||
678 | /** |
||
679 | * Converts an abstract column type into a physical column type. |
||
680 | * The conversion is done using the type map specified in [[typeMap]]. |
||
681 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
682 | * physical types): |
||
683 | * |
||
684 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
685 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
686 | * - `unsignedpk`: an unsigned auto-incremental primary key type, will be converted into "int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
687 | * - `char`: char type, will be converted into "char(1)" |
||
688 | * - `string`: string type, will be converted into "varchar(255)" |
||
689 | * - `text`: a long string type, will be converted into "text" |
||
690 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
691 | * - `integer`: integer type, will be converted into "int(11)" |
||
692 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
693 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
694 | * - `float``: float number type, will be converted into "float" |
||
695 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
696 | * - `datetime`: datetime type, will be converted into "datetime" |
||
697 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
698 | * - `time`: time type, will be converted into "time" |
||
699 | * - `date`: date type, will be converted into "date" |
||
700 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
701 | * - `binary`: binary data type, will be converted into "blob" |
||
702 | * |
||
703 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
704 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
705 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
706 | * |
||
707 | * For some of the abstract types you can also specify a length or precision constraint |
||
708 | * by appending it in round brackets directly to the type. |
||
709 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
710 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
711 | * be ignored. |
||
712 | * |
||
713 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
714 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
715 | * @return string physical column type. |
||
716 | */ |
||
717 | 90 | public function getColumnType($type) |
|
737 | |||
738 | /** |
||
739 | * @param array $columns |
||
740 | * @param array $params the binding parameters to be populated |
||
741 | * @param bool $distinct |
||
742 | * @param string $selectOption |
||
743 | * @return string the SELECT clause built from [[Query::$select]]. |
||
744 | */ |
||
745 | 851 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
783 | |||
784 | /** |
||
785 | * @param array $tables |
||
786 | * @param array $params the binding parameters to be populated |
||
787 | * @return string the FROM clause built from [[Query::$from]]. |
||
788 | */ |
||
789 | 851 | public function buildFrom($tables, &$params) |
|
799 | |||
800 | /** |
||
801 | * @param array $joins |
||
802 | * @param array $params the binding parameters to be populated |
||
803 | * @return string the JOIN clause built from [[Query::$join]]. |
||
804 | * @throws Exception if the $joins parameter is not in proper format |
||
805 | */ |
||
806 | 851 | public function buildJoin($joins, &$params) |
|
831 | |||
832 | /** |
||
833 | * Quotes table names passed |
||
834 | * |
||
835 | * @param array $tables |
||
836 | * @param array $params |
||
837 | * @return array |
||
838 | */ |
||
839 | 602 | private function quoteTableNames($tables, &$params) |
|
860 | |||
861 | /** |
||
862 | * @param string|array $condition |
||
863 | * @param array $params the binding parameters to be populated |
||
864 | * @return string the WHERE clause built from [[Query::$where]]. |
||
865 | */ |
||
866 | 873 | public function buildWhere($condition, &$params) |
|
872 | |||
873 | /** |
||
874 | * @param array $columns |
||
875 | * @param array $params the binding parameters to be populated |
||
876 | * @return string the GROUP BY clause |
||
877 | */ |
||
878 | 851 | public function buildGroupBy($columns, &$params) |
|
893 | |||
894 | /** |
||
895 | * @param string|array $condition |
||
896 | * @param array $params the binding parameters to be populated |
||
897 | * @return string the HAVING clause built from [[Query::$having]]. |
||
898 | */ |
||
899 | 851 | public function buildHaving($condition, &$params) |
|
905 | |||
906 | /** |
||
907 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
908 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
909 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
910 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
911 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
912 | * @param array $params the binding parameters to be populated |
||
913 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
914 | */ |
||
915 | 851 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset, &$params) |
|
927 | |||
928 | /** |
||
929 | * @param array $columns |
||
930 | * @param array $params the binding parameters to be populated |
||
931 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
932 | */ |
||
933 | 851 | public function buildOrderBy($columns, &$params) |
|
950 | |||
951 | /** |
||
952 | * @param int $limit |
||
953 | * @param int $offset |
||
954 | * @return string the LIMIT and OFFSET clauses |
||
955 | */ |
||
956 | 281 | public function buildLimit($limit, $offset) |
|
968 | |||
969 | /** |
||
970 | * Checks to see if the given limit is effective. |
||
971 | * @param mixed $limit the given limit |
||
972 | * @return bool whether the limit is effective |
||
973 | */ |
||
974 | 552 | protected function hasLimit($limit) |
|
978 | |||
979 | /** |
||
980 | * Checks to see if the given offset is effective. |
||
981 | * @param mixed $offset the given offset |
||
982 | * @return bool whether the offset is effective |
||
983 | */ |
||
984 | 552 | protected function hasOffset($offset) |
|
988 | |||
989 | /** |
||
990 | * @param array $unions |
||
991 | * @param array $params the binding parameters to be populated |
||
992 | * @return string the UNION clause built from [[Query::$union]]. |
||
993 | */ |
||
994 | 580 | public function buildUnion($unions, &$params) |
|
1013 | |||
1014 | /** |
||
1015 | * Processes columns and properly quotes them if necessary. |
||
1016 | * It will join all columns into a string with comma as separators. |
||
1017 | * @param string|array $columns the columns to be processed |
||
1018 | * @return string the processing result |
||
1019 | */ |
||
1020 | 11 | public function buildColumns($columns) |
|
1039 | |||
1040 | /** |
||
1041 | * Parses the condition specification and generates the corresponding SQL expression. |
||
1042 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
1043 | * on how to specify a condition. |
||
1044 | * @param array $params the binding parameters to be populated |
||
1045 | * @return string the generated SQL expression |
||
1046 | */ |
||
1047 | 873 | public function buildCondition($condition, &$params) |
|
1073 | |||
1074 | /** |
||
1075 | * Creates a condition based on column-value pairs. |
||
1076 | * @param array $condition the condition specification. |
||
1077 | * @param array $params the binding parameters to be populated |
||
1078 | * @return string the generated SQL expression |
||
1079 | */ |
||
1080 | 465 | public function buildHashCondition($condition, &$params) |
|
1107 | |||
1108 | /** |
||
1109 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1110 | * @param string $operator the operator to use for connecting the given operands |
||
1111 | * @param array $operands the SQL expressions to connect. |
||
1112 | * @param array $params the binding parameters to be populated |
||
1113 | * @return string the generated SQL expression |
||
1114 | */ |
||
1115 | 170 | public function buildAndCondition($operator, $operands, &$params) |
|
1138 | |||
1139 | /** |
||
1140 | * Inverts an SQL expressions with `NOT` operator. |
||
1141 | * @param string $operator the operator to use for connecting the given operands |
||
1142 | * @param array $operands the SQL expressions to connect. |
||
1143 | * @param array $params the binding parameters to be populated |
||
1144 | * @return string the generated SQL expression |
||
1145 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1146 | */ |
||
1147 | 3 | public function buildNotCondition($operator, $operands, &$params) |
|
1163 | |||
1164 | /** |
||
1165 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1166 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1167 | * @param array $operands the first operand is the column name. The second and third operands |
||
1168 | * describe the interval that column value should be in. |
||
1169 | * @param array $params the binding parameters to be populated |
||
1170 | * @return string the generated SQL expression |
||
1171 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1172 | */ |
||
1173 | 21 | public function buildBetweenCondition($operator, $operands, &$params) |
|
1205 | |||
1206 | /** |
||
1207 | * Creates an SQL expressions with the `IN` operator. |
||
1208 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1209 | * @param array $operands the first operand is the column name. If it is an array |
||
1210 | * a composite IN condition will be generated. |
||
1211 | * The second operand is an array of values that column value should be among. |
||
1212 | * If it is an empty array the generated expression will be a `false` value if |
||
1213 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1214 | * @param array $params the binding parameters to be populated |
||
1215 | * @return string the generated SQL expression |
||
1216 | * @throws Exception if wrong number of operands have been given. |
||
1217 | */ |
||
1218 | 224 | public function buildInCondition($operator, $operands, &$params) |
|
1279 | |||
1280 | /** |
||
1281 | * Builds SQL for IN condition |
||
1282 | * |
||
1283 | * @param string $operator |
||
1284 | * @param array $columns |
||
1285 | * @param Query $values |
||
1286 | * @param array $params |
||
1287 | * @return string SQL |
||
1288 | */ |
||
1289 | 14 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
1306 | |||
1307 | /** |
||
1308 | * Builds SQL for IN condition |
||
1309 | * |
||
1310 | * @param string $operator |
||
1311 | * @param array|\Traversable $columns |
||
1312 | * @param array $values |
||
1313 | * @param array $params |
||
1314 | * @return string SQL |
||
1315 | */ |
||
1316 | 10 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
1344 | |||
1345 | /** |
||
1346 | * Creates an SQL expressions with the `LIKE` operator. |
||
1347 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1348 | * @param array $operands an array of two or three operands |
||
1349 | * |
||
1350 | * - The first operand is the column name. |
||
1351 | * - The second operand is a single value or an array of values that column value |
||
1352 | * should be compared with. If it is an empty array the generated expression will |
||
1353 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1354 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1355 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1356 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1357 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1358 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1359 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1360 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1361 | * @param array $params the binding parameters to be populated |
||
1362 | * @return string the generated SQL expression |
||
1363 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1364 | */ |
||
1365 | 75 | public function buildLikeCondition($operator, $operands, &$params) |
|
1415 | |||
1416 | /** |
||
1417 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1418 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1419 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1420 | * @param array $params the binding parameters to be populated |
||
1421 | * @return string the generated SQL expression |
||
1422 | * @throws InvalidArgumentException if the operand is not a [[Query]] object. |
||
1423 | */ |
||
1424 | 18 | public function buildExistsCondition($operator, $operands, &$params) |
|
1433 | |||
1434 | /** |
||
1435 | * Creates an SQL expressions like `"column" operator value`. |
||
1436 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1437 | * @param array $operands contains two column names. |
||
1438 | * @param array $params the binding parameters to be populated |
||
1439 | * @return string the generated SQL expression |
||
1440 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1441 | */ |
||
1442 | 36 | public function buildSimpleCondition($operator, $operands, &$params) |
|
1470 | |||
1471 | /** |
||
1472 | * Creates a SELECT EXISTS() SQL statement. |
||
1473 | * @param string $rawSql the subquery in a raw form to select from. |
||
1474 | * @return string the SELECT EXISTS() SQL statement. |
||
1475 | * @since 2.0.8 |
||
1476 | */ |
||
1477 | 59 | public function selectExists($rawSql) |
|
1481 | } |
||
1482 |