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 | * Constructor. |
||
87 | * @param Connection $connection the database connection. |
||
88 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
89 | */ |
||
90 | public function __construct($connection, $config = []) |
||
95 | |||
96 | /** |
||
97 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
98 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
99 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
100 | * be included in the result with the additional parameters generated during the query building process. |
||
101 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
102 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
103 | * include those provided in `$params`. |
||
104 | */ |
||
105 | public function build($query, $params = []) |
||
145 | |||
146 | /** |
||
147 | * Creates an INSERT SQL statement. |
||
148 | * For example, |
||
149 | * |
||
150 | * ```php |
||
151 | * $sql = $queryBuilder->insert('user', [ |
||
152 | * 'name' => 'Sam', |
||
153 | * 'age' => 30, |
||
154 | * ], $params); |
||
155 | * ``` |
||
156 | * |
||
157 | * The method will properly escape the table and column names. |
||
158 | * |
||
159 | * @param string $table the table that new rows will be inserted into. |
||
160 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
||
161 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
162 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
163 | * @param array $params the binding parameters that will be generated by this method. |
||
164 | * They should be bound to the DB command later. |
||
165 | * @return string the INSERT SQL |
||
166 | */ |
||
167 | public function insert($table, $columns, &$params) |
||
203 | |||
204 | /** |
||
205 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
206 | * |
||
207 | * @param \yii\db\Query $columns Object, which represents select query |
||
208 | * @param \yii\db\Schema $schema Schema object to qoute column name |
||
209 | * @return array |
||
210 | * @since 2.0.11 |
||
211 | */ |
||
212 | protected function prepareInsertSelectSubQuery($columns, $schema) |
||
233 | |||
234 | /** |
||
235 | * Generates a batch INSERT SQL statement. |
||
236 | * For example, |
||
237 | * |
||
238 | * ```php |
||
239 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
240 | * ['Tom', 30], |
||
241 | * ['Jane', 20], |
||
242 | * ['Linda', 25], |
||
243 | * ]); |
||
244 | * ``` |
||
245 | * |
||
246 | * Note that the values in each row must match the corresponding column names. |
||
247 | * |
||
248 | * The method will properly escape the column names, and quote the values to be inserted. |
||
249 | * |
||
250 | * @param string $table the table that new rows will be inserted into. |
||
251 | * @param array $columns the column names |
||
252 | * @param array $rows the rows to be batch inserted into the table |
||
253 | * @return string the batch INSERT SQL statement |
||
254 | */ |
||
255 | public function batchInsert($table, $columns, $rows) |
||
297 | |||
298 | /** |
||
299 | * Creates an UPDATE SQL statement. |
||
300 | * For example, |
||
301 | * |
||
302 | * ```php |
||
303 | * $params = []; |
||
304 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
305 | * ``` |
||
306 | * |
||
307 | * The method will properly escape the table and column names. |
||
308 | * |
||
309 | * @param string $table the table to be updated. |
||
310 | * @param array $columns the column data (name => value) to be updated. |
||
311 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
312 | * refer to [[Query::where()]] on how to specify condition. |
||
313 | * @param array $params the binding parameters that will be modified by this method |
||
314 | * so that they can be bound to the DB command later. |
||
315 | * @return string the UPDATE SQL |
||
316 | */ |
||
317 | public function update($table, $columns, $condition, &$params) |
||
344 | |||
345 | /** |
||
346 | * Creates a DELETE SQL statement. |
||
347 | * For example, |
||
348 | * |
||
349 | * ```php |
||
350 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
351 | * ``` |
||
352 | * |
||
353 | * The method will properly escape the table and column names. |
||
354 | * |
||
355 | * @param string $table the table where the data will be deleted from. |
||
356 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
357 | * refer to [[Query::where()]] on how to specify condition. |
||
358 | * @param array $params the binding parameters that will be modified by this method |
||
359 | * so that they can be bound to the DB command later. |
||
360 | * @return string the DELETE SQL |
||
361 | */ |
||
362 | public function delete($table, $condition, &$params) |
||
369 | |||
370 | /** |
||
371 | * Builds a SQL statement for creating a new DB table. |
||
372 | * |
||
373 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
374 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
375 | * stands for the column type which can contain an abstract DB type. |
||
376 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
377 | * |
||
378 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
379 | * inserted into the generated SQL. |
||
380 | * |
||
381 | * For example, |
||
382 | * |
||
383 | * ```php |
||
384 | * $sql = $queryBuilder->createTable('user', [ |
||
385 | * 'id' => 'pk', |
||
386 | * 'name' => 'string', |
||
387 | * 'age' => 'integer', |
||
388 | * ]); |
||
389 | * ``` |
||
390 | * |
||
391 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
392 | * @param array $columns the columns (name => definition) in the new table. |
||
393 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
394 | * @return string the SQL statement for creating a new DB table. |
||
395 | */ |
||
396 | public function createTable($table, $columns, $options = null) |
||
410 | |||
411 | /** |
||
412 | * Builds a SQL statement for renaming a DB table. |
||
413 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
414 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
415 | * @return string the SQL statement for renaming a DB table. |
||
416 | */ |
||
417 | public function renameTable($oldName, $newName) |
||
421 | |||
422 | /** |
||
423 | * Builds a SQL statement for dropping a DB table. |
||
424 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
425 | * @return string the SQL statement for dropping a DB table. |
||
426 | */ |
||
427 | public function dropTable($table) |
||
431 | |||
432 | /** |
||
433 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
434 | * @param string $name the name of the primary key constraint. |
||
435 | * @param string $table the table that the primary key constraint will be added to. |
||
436 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
437 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
438 | */ |
||
439 | public function addPrimaryKey($name, $table, $columns) |
||
453 | |||
454 | /** |
||
455 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
456 | * @param string $name the name of the primary key constraint to be removed. |
||
457 | * @param string $table the table that the primary key constraint will be removed from. |
||
458 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
459 | */ |
||
460 | public function dropPrimaryKey($name, $table) |
||
465 | |||
466 | /** |
||
467 | * Builds a SQL statement for truncating a DB table. |
||
468 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
469 | * @return string the SQL statement for truncating a DB table. |
||
470 | */ |
||
471 | public function truncateTable($table) |
||
475 | |||
476 | /** |
||
477 | * Builds a SQL statement for adding a new DB column. |
||
478 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
479 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
480 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
481 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
482 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
483 | * @return string the SQL statement for adding a new column. |
||
484 | */ |
||
485 | public function addColumn($table, $column, $type) |
||
491 | |||
492 | /** |
||
493 | * Builds a SQL statement for dropping a DB column. |
||
494 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
495 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
496 | * @return string the SQL statement for dropping a DB column. |
||
497 | */ |
||
498 | public function dropColumn($table, $column) |
||
503 | |||
504 | /** |
||
505 | * Builds a SQL statement for renaming a column. |
||
506 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
507 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
508 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
509 | * @return string the SQL statement for renaming a DB column. |
||
510 | */ |
||
511 | public function renameColumn($table, $oldName, $newName) |
||
517 | |||
518 | /** |
||
519 | * Builds a SQL statement for changing the definition of a column. |
||
520 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
521 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
522 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
523 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
524 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
525 | * will become 'varchar(255) not null'. |
||
526 | * @return string the SQL statement for changing the definition of a column. |
||
527 | */ |
||
528 | public function alterColumn($table, $column, $type) |
||
535 | |||
536 | /** |
||
537 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
538 | * The method will properly quote the table and column names. |
||
539 | * @param string $name the name of the foreign key constraint. |
||
540 | * @param string $table the table that the foreign key constraint will be added to. |
||
541 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
542 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
543 | * @param string $refTable the table that the foreign key references to. |
||
544 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
545 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
546 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
547 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
548 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
549 | */ |
||
550 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
566 | |||
567 | /** |
||
568 | * Builds a SQL statement for dropping a foreign key constraint. |
||
569 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
570 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
571 | * @return string the SQL statement for dropping a foreign key constraint. |
||
572 | */ |
||
573 | public function dropForeignKey($name, $table) |
||
578 | |||
579 | /** |
||
580 | * Builds a SQL statement for creating a new index. |
||
581 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
582 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
583 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
584 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
585 | * by the method, unless a parenthesis is found in the name. |
||
586 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
587 | * @return string the SQL statement for creating a new index. |
||
588 | */ |
||
589 | public function createIndex($name, $table, $columns, $unique = false) |
||
596 | |||
597 | /** |
||
598 | * Builds a SQL statement for dropping an index. |
||
599 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
600 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
601 | * @return string the SQL statement for dropping an index. |
||
602 | */ |
||
603 | public function dropIndex($name, $table) |
||
607 | |||
608 | /** |
||
609 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
610 | * The sequence will be reset such that the primary key of the next new row inserted |
||
611 | * will have the specified value or 1. |
||
612 | * @param string $table the name of the table whose primary key sequence will be reset |
||
613 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
614 | * the next new row's primary key will have a value 1. |
||
615 | * @return string the SQL statement for resetting sequence |
||
616 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
617 | */ |
||
618 | public function resetSequence($table, $value = null) |
||
622 | |||
623 | /** |
||
624 | * Builds a SQL statement for enabling or disabling integrity check. |
||
625 | * @param bool $check whether to turn on or off the integrity check. |
||
626 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
627 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
628 | * @return string the SQL statement for checking integrity |
||
629 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
630 | */ |
||
631 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
635 | |||
636 | /** |
||
637 | * Builds a SQL command for adding comment to column |
||
638 | * |
||
639 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
640 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
641 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
642 | * @return string the SQL statement for adding comment on column |
||
643 | * @since 2.0.8 |
||
644 | */ |
||
645 | public function addCommentOnColumn($table, $column, $comment) |
||
650 | |||
651 | /** |
||
652 | * Builds a SQL command for adding comment to table |
||
653 | * |
||
654 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
655 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
656 | * @return string the SQL statement for adding comment on table |
||
657 | * @since 2.0.8 |
||
658 | */ |
||
659 | public function addCommentOnTable($table, $comment) |
||
663 | |||
664 | /** |
||
665 | * Builds a SQL command for adding comment to column |
||
666 | * |
||
667 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
668 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
669 | * @return string the SQL statement for adding comment on column |
||
670 | * @since 2.0.8 |
||
671 | */ |
||
672 | public function dropCommentFromColumn($table, $column) |
||
676 | |||
677 | /** |
||
678 | * Builds a SQL command for adding comment to table |
||
679 | * |
||
680 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
681 | * @return string the SQL statement for adding comment on column |
||
682 | * @since 2.0.8 |
||
683 | */ |
||
684 | public function dropCommentFromTable($table) |
||
688 | |||
689 | /** |
||
690 | * Converts an abstract column type into a physical column type. |
||
691 | * The conversion is done using the type map specified in [[typeMap]]. |
||
692 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
693 | * physical types): |
||
694 | * |
||
695 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
696 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
697 | * - `unsignedpk`: an unsigned auto-incremental primary key type, will be converted into "int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
698 | * - `char`: char type, will be converted into "char(1)" |
||
699 | * - `string`: string type, will be converted into "varchar(255)" |
||
700 | * - `text`: a long string type, will be converted into "text" |
||
701 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
702 | * - `integer`: integer type, will be converted into "int(11)" |
||
703 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
704 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
705 | * - `float``: float number type, will be converted into "float" |
||
706 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
707 | * - `datetime`: datetime type, will be converted into "datetime" |
||
708 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
709 | * - `time`: time type, will be converted into "time" |
||
710 | * - `date`: date type, will be converted into "date" |
||
711 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
712 | * - `binary`: binary data type, will be converted into "blob" |
||
713 | * |
||
714 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
715 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
716 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
717 | * |
||
718 | * For some of the abstract types you can also specify a length or precision constraint |
||
719 | * by appending it in round brackets directly to the type. |
||
720 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
721 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
722 | * be ignored. |
||
723 | * |
||
724 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
725 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
726 | * @return string physical column type. |
||
727 | */ |
||
728 | public function getColumnType($type) |
||
748 | |||
749 | /** |
||
750 | * @param array $columns |
||
751 | * @param array $params the binding parameters to be populated |
||
752 | * @param bool $distinct |
||
753 | * @param string $selectOption |
||
754 | * @return string the SELECT clause built from [[Query::$select]]. |
||
755 | */ |
||
756 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
||
794 | |||
795 | /** |
||
796 | * @param array $tables |
||
797 | * @param array $params the binding parameters to be populated |
||
798 | * @return string the FROM clause built from [[Query::$from]]. |
||
799 | */ |
||
800 | public function buildFrom($tables, &$params) |
||
810 | |||
811 | /** |
||
812 | * @param array $joins |
||
813 | * @param array $params the binding parameters to be populated |
||
814 | * @return string the JOIN clause built from [[Query::$join]]. |
||
815 | * @throws Exception if the $joins parameter is not in proper format |
||
816 | */ |
||
817 | public function buildJoin($joins, &$params) |
||
842 | |||
843 | /** |
||
844 | * Quotes table names passed |
||
845 | * |
||
846 | * @param array $tables |
||
847 | * @param array $params |
||
848 | * @return array |
||
849 | */ |
||
850 | private function quoteTableNames($tables, &$params) |
||
871 | |||
872 | /** |
||
873 | * @param string|array $condition |
||
874 | * @param array $params the binding parameters to be populated |
||
875 | * @return string the WHERE clause built from [[Query::$where]]. |
||
876 | */ |
||
877 | public function buildWhere($condition, &$params) |
||
883 | |||
884 | /** |
||
885 | * @param array $columns |
||
886 | * @return string the GROUP BY clause |
||
887 | */ |
||
888 | public function buildGroupBy($columns) |
||
902 | |||
903 | /** |
||
904 | * @param string|array $condition |
||
905 | * @param array $params the binding parameters to be populated |
||
906 | * @return string the HAVING clause built from [[Query::$having]]. |
||
907 | */ |
||
908 | public function buildHaving($condition, &$params) |
||
914 | |||
915 | /** |
||
916 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
917 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
918 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
919 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
920 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
921 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
922 | */ |
||
923 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
935 | |||
936 | /** |
||
937 | * @param array $columns |
||
938 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
939 | */ |
||
940 | public function buildOrderBy($columns) |
||
956 | |||
957 | /** |
||
958 | * @param int $limit |
||
959 | * @param int $offset |
||
960 | * @return string the LIMIT and OFFSET clauses |
||
961 | */ |
||
962 | public function buildLimit($limit, $offset) |
||
974 | |||
975 | /** |
||
976 | * Checks to see if the given limit is effective. |
||
977 | * @param mixed $limit the given limit |
||
978 | * @return bool whether the limit is effective |
||
979 | */ |
||
980 | protected function hasLimit($limit) |
||
984 | |||
985 | /** |
||
986 | * Checks to see if the given offset is effective. |
||
987 | * @param mixed $offset the given offset |
||
988 | * @return bool whether the offset is effective |
||
989 | */ |
||
990 | protected function hasOffset($offset) |
||
994 | |||
995 | /** |
||
996 | * @param array $unions |
||
997 | * @param array $params the binding parameters to be populated |
||
998 | * @return string the UNION clause built from [[Query::$union]]. |
||
999 | */ |
||
1000 | public function buildUnion($unions, &$params) |
||
1019 | |||
1020 | /** |
||
1021 | * Processes columns and properly quotes them if necessary. |
||
1022 | * It will join all columns into a string with comma as separators. |
||
1023 | * @param string|array $columns the columns to be processed |
||
1024 | * @return string the processing result |
||
1025 | */ |
||
1026 | public function buildColumns($columns) |
||
1045 | |||
1046 | /** |
||
1047 | * Parses the condition specification and generates the corresponding SQL expression. |
||
1048 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
1049 | * on how to specify a condition. |
||
1050 | * @param array $params the binding parameters to be populated |
||
1051 | * @return string the generated SQL expression |
||
1052 | */ |
||
1053 | public function buildCondition($condition, &$params) |
||
1079 | |||
1080 | /** |
||
1081 | * Creates a condition based on column-value pairs. |
||
1082 | * @param array $condition the condition specification. |
||
1083 | * @param array $params the binding parameters to be populated |
||
1084 | * @return string the generated SQL expression |
||
1085 | */ |
||
1086 | public function buildHashCondition($condition, &$params) |
||
1113 | |||
1114 | /** |
||
1115 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1116 | * @param string $operator the operator to use for connecting the given operands |
||
1117 | * @param array $operands the SQL expressions to connect. |
||
1118 | * @param array $params the binding parameters to be populated |
||
1119 | * @return string the generated SQL expression |
||
1120 | */ |
||
1121 | public function buildAndCondition($operator, $operands, &$params) |
||
1144 | |||
1145 | /** |
||
1146 | * Inverts an SQL expressions with `NOT` operator. |
||
1147 | * @param string $operator the operator to use for connecting the given operands |
||
1148 | * @param array $operands the SQL expressions to connect. |
||
1149 | * @param array $params the binding parameters to be populated |
||
1150 | * @return string the generated SQL expression |
||
1151 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1152 | */ |
||
1153 | public function buildNotCondition($operator, $operands, &$params) |
||
1169 | |||
1170 | /** |
||
1171 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1172 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1173 | * @param array $operands the first operand is the column name. The second and third operands |
||
1174 | * describe the interval that column value should be in. |
||
1175 | * @param array $params the binding parameters to be populated |
||
1176 | * @return string the generated SQL expression |
||
1177 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1178 | */ |
||
1179 | public function buildBetweenCondition($operator, $operands, &$params) |
||
1211 | |||
1212 | /** |
||
1213 | * Creates an SQL expressions with the `IN` operator. |
||
1214 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1215 | * @param array $operands the first operand is the column name. If it is an array |
||
1216 | * a composite IN condition will be generated. |
||
1217 | * The second operand is an array of values that column value should be among. |
||
1218 | * If it is an empty array the generated expression will be a `false` value if |
||
1219 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1220 | * @param array $params the binding parameters to be populated |
||
1221 | * @return string the generated SQL expression |
||
1222 | * @throws Exception if wrong number of operands have been given. |
||
1223 | */ |
||
1224 | public function buildInCondition($operator, $operands, &$params) |
||
1285 | |||
1286 | /** |
||
1287 | * Builds SQL for IN condition |
||
1288 | * |
||
1289 | * @param string $operator |
||
1290 | * @param array $columns |
||
1291 | * @param Query $values |
||
1292 | * @param array $params |
||
1293 | * @return string SQL |
||
1294 | */ |
||
1295 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
||
1312 | |||
1313 | /** |
||
1314 | * Builds SQL for IN condition |
||
1315 | * |
||
1316 | * @param string $operator |
||
1317 | * @param array|\Traversable $columns |
||
1318 | * @param array $values |
||
1319 | * @param array $params |
||
1320 | * @return string SQL |
||
1321 | */ |
||
1322 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
||
1350 | |||
1351 | /** |
||
1352 | * Creates an SQL expressions with the `LIKE` operator. |
||
1353 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1354 | * @param array $operands an array of two or three operands |
||
1355 | * |
||
1356 | * - The first operand is the column name. |
||
1357 | * - The second operand is a single value or an array of values that column value |
||
1358 | * should be compared with. If it is an empty array the generated expression will |
||
1359 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1360 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1361 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1362 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1363 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1364 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1365 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1366 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1367 | * @param array $params the binding parameters to be populated |
||
1368 | * @return string the generated SQL expression |
||
1369 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1370 | */ |
||
1371 | public function buildLikeCondition($operator, $operands, &$params) |
||
1421 | |||
1422 | /** |
||
1423 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1424 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1425 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1426 | * @param array $params the binding parameters to be populated |
||
1427 | * @return string the generated SQL expression |
||
1428 | * @throws InvalidParamException if the operand is not a [[Query]] object. |
||
1429 | */ |
||
1430 | public function buildExistsCondition($operator, $operands, &$params) |
||
1439 | |||
1440 | /** |
||
1441 | * Creates an SQL expressions like `"column" operator value`. |
||
1442 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1443 | * @param array $operands contains two column names. |
||
1444 | * @param array $params the binding parameters to be populated |
||
1445 | * @return string the generated SQL expression |
||
1446 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1447 | */ |
||
1448 | public function buildSimpleCondition($operator, $operands, &$params) |
||
1476 | |||
1477 | /** |
||
1478 | * Creates a SELECT EXISTS() SQL statement. |
||
1479 | * @param string $rawSql the subquery in a raw form to select from. |
||
1480 | * @return string the SELECT EXISTS() SQL statement. |
||
1481 | * @since 2.0.8 |
||
1482 | */ |
||
1483 | public function selectExists($rawSql) |
||
1487 | } |
||
1488 |