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 | |||
70 | /** |
||
71 | * Constructor. |
||
72 | * @param Connection $connection the database connection. |
||
73 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
74 | */ |
||
75 | 755 | public function __construct($connection, $config = []) |
|
80 | |||
81 | /** |
||
82 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
83 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
84 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
85 | * be included in the result with the additional parameters generated during the query building process. |
||
86 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
87 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
88 | * include those provided in `$params`. |
||
89 | */ |
||
90 | 518 | public function build($query, $params = []) |
|
115 | |||
116 | /** |
||
117 | * Creates an INSERT SQL statement. |
||
118 | * For example, |
||
119 | * |
||
120 | * ```php |
||
121 | * $sql = $queryBuilder->insert('user', [ |
||
122 | * 'name' => 'Sam', |
||
123 | * 'age' => 30, |
||
124 | * ], $params); |
||
125 | * ``` |
||
126 | * |
||
127 | * The method will properly escape the table and column names. |
||
128 | * |
||
129 | * @param string $table the table that new rows will be inserted into. |
||
130 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
131 | * @param array $params the binding parameters that will be generated by this method. |
||
132 | * They should be bound to the DB command later. |
||
133 | * @return string the INSERT SQL |
||
134 | */ |
||
135 | 95 | public function insert($table, $columns, &$params) |
|
163 | |||
164 | /** |
||
165 | * Generates a batch INSERT SQL statement. |
||
166 | * For example, |
||
167 | * |
||
168 | * ```php |
||
169 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
170 | * ['Tom', 30], |
||
171 | * ['Jane', 20], |
||
172 | * ['Linda', 25], |
||
173 | * ]); |
||
174 | * ``` |
||
175 | * |
||
176 | * Note that the values in each row must match the corresponding column names. |
||
177 | * |
||
178 | * The method will properly escape the column names, and quote the values to be inserted. |
||
179 | * |
||
180 | * @param string $table the table that new rows will be inserted into. |
||
181 | * @param array $columns the column names |
||
182 | * @param array $rows the rows to be batch inserted into the table |
||
183 | * @return string the batch INSERT SQL statement |
||
184 | */ |
||
185 | 2 | public function batchInsert($table, $columns, $rows) |
|
224 | |||
225 | /** |
||
226 | * Creates an UPDATE SQL statement. |
||
227 | * For example, |
||
228 | * |
||
229 | * ```php |
||
230 | * $params = []; |
||
231 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
232 | * ``` |
||
233 | * |
||
234 | * The method will properly escape the table and column names. |
||
235 | * |
||
236 | * @param string $table the table to be updated. |
||
237 | * @param array $columns the column data (name => value) to be updated. |
||
238 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
239 | * refer to [[Query::where()]] on how to specify condition. |
||
240 | * @param array $params the binding parameters that will be modified by this method |
||
241 | * so that they can be bound to the DB command later. |
||
242 | * @return string the UPDATE SQL |
||
243 | */ |
||
244 | 67 | public function update($table, $columns, $condition, &$params) |
|
271 | |||
272 | /** |
||
273 | * Creates a DELETE SQL statement. |
||
274 | * For example, |
||
275 | * |
||
276 | * ```php |
||
277 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
278 | * ``` |
||
279 | * |
||
280 | * The method will properly escape the table and column names. |
||
281 | * |
||
282 | * @param string $table the table where the data will be deleted from. |
||
283 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
284 | * refer to [[Query::where()]] on how to specify condition. |
||
285 | * @param array $params the binding parameters that will be modified by this method |
||
286 | * so that they can be bound to the DB command later. |
||
287 | * @return string the DELETE SQL |
||
288 | */ |
||
289 | 132 | public function delete($table, $condition, &$params) |
|
296 | |||
297 | /** |
||
298 | * Builds a SQL statement for creating a new DB table. |
||
299 | * |
||
300 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
301 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
302 | * stands for the column type which can contain an abstract DB type. |
||
303 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
304 | * |
||
305 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
306 | * inserted into the generated SQL. |
||
307 | * |
||
308 | * For example, |
||
309 | * |
||
310 | * ```php |
||
311 | * $sql = $queryBuilder->createTable('user', [ |
||
312 | * 'id' => 'pk', |
||
313 | * 'name' => 'string', |
||
314 | * 'age' => 'integer', |
||
315 | * ]); |
||
316 | * ``` |
||
317 | * |
||
318 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
319 | * @param array $columns the columns (name => definition) in the new table. |
||
320 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
321 | * @return string the SQL statement for creating a new DB table. |
||
322 | */ |
||
323 | 55 | public function createTable($table, $columns, $options = null) |
|
337 | |||
338 | /** |
||
339 | * Builds a SQL statement for renaming a DB table. |
||
340 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
341 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
342 | * @return string the SQL statement for renaming a DB table. |
||
343 | */ |
||
344 | 1 | public function renameTable($oldName, $newName) |
|
348 | |||
349 | /** |
||
350 | * Builds a SQL statement for dropping a DB table. |
||
351 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
352 | * @return string the SQL statement for dropping a DB table. |
||
353 | */ |
||
354 | 11 | public function dropTable($table) |
|
358 | |||
359 | /** |
||
360 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
361 | * @param string $name the name of the primary key constraint. |
||
362 | * @param string $table the table that the primary key constraint will be added to. |
||
363 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
364 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
365 | */ |
||
366 | 2 | public function addPrimaryKey($name, $table, $columns) |
|
380 | |||
381 | /** |
||
382 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
383 | * @param string $name the name of the primary key constraint to be removed. |
||
384 | * @param string $table the table that the primary key constraint will be removed from. |
||
385 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
386 | */ |
||
387 | 1 | public function dropPrimaryKey($name, $table) |
|
392 | |||
393 | /** |
||
394 | * Builds a SQL statement for truncating a DB table. |
||
395 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
396 | * @return string the SQL statement for truncating a DB table. |
||
397 | */ |
||
398 | 5 | public function truncateTable($table) |
|
402 | |||
403 | /** |
||
404 | * Builds a SQL statement for adding a new DB column. |
||
405 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
406 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
407 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
408 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
409 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
410 | * @return string the SQL statement for adding a new column. |
||
411 | */ |
||
412 | 4 | public function addColumn($table, $column, $type) |
|
418 | |||
419 | /** |
||
420 | * Builds a SQL statement for dropping a DB column. |
||
421 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
422 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
423 | * @return string the SQL statement for dropping a DB column. |
||
424 | */ |
||
425 | public function dropColumn($table, $column) |
||
430 | |||
431 | /** |
||
432 | * Builds a SQL statement for renaming a column. |
||
433 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
434 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
435 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
436 | * @return string the SQL statement for renaming a DB column. |
||
437 | */ |
||
438 | public function renameColumn($table, $oldName, $newName) |
||
444 | |||
445 | /** |
||
446 | * Builds a SQL statement for changing the definition of a column. |
||
447 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
448 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
449 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
450 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
451 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
452 | * will become 'varchar(255) not null'. |
||
453 | * @return string the SQL statement for changing the definition of a column. |
||
454 | */ |
||
455 | 1 | public function alterColumn($table, $column, $type) |
|
462 | |||
463 | /** |
||
464 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
465 | * The method will properly quote the table and column names. |
||
466 | * @param string $name the name of the foreign key constraint. |
||
467 | * @param string $table the table that the foreign key constraint will be added to. |
||
468 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
469 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
470 | * @param string $refTable the table that the foreign key references to. |
||
471 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
472 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
473 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
474 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
475 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
476 | */ |
||
477 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
493 | |||
494 | /** |
||
495 | * Builds a SQL statement for dropping a foreign key constraint. |
||
496 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
497 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
498 | * @return string the SQL statement for dropping a foreign key constraint. |
||
499 | */ |
||
500 | public function dropForeignKey($name, $table) |
||
505 | |||
506 | /** |
||
507 | * Builds a SQL statement for creating a new index. |
||
508 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
509 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
510 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
511 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
512 | * by the method, unless a parenthesis is found in the name. |
||
513 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
514 | * @return string the SQL statement for creating a new index. |
||
515 | */ |
||
516 | 1 | public function createIndex($name, $table, $columns, $unique = false) |
|
523 | |||
524 | /** |
||
525 | * Builds a SQL statement for dropping an index. |
||
526 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
527 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
528 | * @return string the SQL statement for dropping an index. |
||
529 | */ |
||
530 | public function dropIndex($name, $table) |
||
534 | |||
535 | /** |
||
536 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
537 | * The sequence will be reset such that the primary key of the next new row inserted |
||
538 | * will have the specified value or 1. |
||
539 | * @param string $table the name of the table whose primary key sequence will be reset |
||
540 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
541 | * the next new row's primary key will have a value 1. |
||
542 | * @return string the SQL statement for resetting sequence |
||
543 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
544 | */ |
||
545 | public function resetSequence($table, $value = null) |
||
549 | |||
550 | /** |
||
551 | * Builds a SQL statement for enabling or disabling integrity check. |
||
552 | * @param bool $check whether to turn on or off the integrity check. |
||
553 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
554 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
555 | * @return string the SQL statement for checking integrity |
||
556 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
557 | */ |
||
558 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
562 | |||
563 | /** |
||
564 | * Builds a SQL command for adding comment to column |
||
565 | * |
||
566 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
567 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
568 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
569 | * @return string the SQL statement for adding comment on column |
||
570 | * @since 2.0.8 |
||
571 | */ |
||
572 | 1 | public function addCommentOnColumn($table, $column, $comment) |
|
577 | |||
578 | /** |
||
579 | * Builds a SQL command for adding comment to table |
||
580 | * |
||
581 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
582 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
583 | * @return string the SQL statement for adding comment on table |
||
584 | * @since 2.0.8 |
||
585 | */ |
||
586 | 1 | public function addCommentOnTable($table, $comment) |
|
590 | |||
591 | /** |
||
592 | * Builds a SQL command for adding comment to column |
||
593 | * |
||
594 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
595 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
596 | * @return string the SQL statement for adding comment on column |
||
597 | * @since 2.0.8 |
||
598 | */ |
||
599 | 1 | public function dropCommentFromColumn($table, $column) |
|
603 | |||
604 | /** |
||
605 | * Builds a SQL command for adding comment to table |
||
606 | * |
||
607 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
608 | * @return string the SQL statement for adding comment on column |
||
609 | * @since 2.0.8 |
||
610 | */ |
||
611 | 1 | public function dropCommentFromTable($table) |
|
615 | |||
616 | /** |
||
617 | * Converts an abstract column type into a physical column type. |
||
618 | * The conversion is done using the type map specified in [[typeMap]]. |
||
619 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
620 | * physical types): |
||
621 | * |
||
622 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
623 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
624 | * - `unsignedpk`: an unsigned auto-incremental primary key type, will be converted into "int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
625 | * - `char`: char type, will be converted into "char(1)" |
||
626 | * - `string`: string type, will be converted into "varchar(255)" |
||
627 | * - `text`: a long string type, will be converted into "text" |
||
628 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
629 | * - `integer`: integer type, will be converted into "int(11)" |
||
630 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
631 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
632 | * - `float``: float number type, will be converted into "float" |
||
633 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
634 | * - `datetime`: datetime type, will be converted into "datetime" |
||
635 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
636 | * - `time`: time type, will be converted into "time" |
||
637 | * - `date`: date type, will be converted into "date" |
||
638 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
639 | * - `binary`: binary data type, will be converted into "blob" |
||
640 | * |
||
641 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
642 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
643 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
644 | * |
||
645 | * For some of the abstract types you can also specify a length or precision constraint |
||
646 | * by appending it in round brackets directly to the type. |
||
647 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
648 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
649 | * be ignored. |
||
650 | * |
||
651 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
652 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
653 | * @return string physical column type. |
||
654 | */ |
||
655 | 59 | public function getColumnType($type) |
|
675 | |||
676 | /** |
||
677 | * @param array $columns |
||
678 | * @param array $params the binding parameters to be populated |
||
679 | * @param bool $distinct |
||
680 | * @param string $selectOption |
||
681 | * @return string the SELECT clause built from [[Query::$select]]. |
||
682 | */ |
||
683 | 740 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
721 | |||
722 | /** |
||
723 | * @param array $tables |
||
724 | * @param array $params the binding parameters to be populated |
||
725 | * @return string the FROM clause built from [[Query::$from]]. |
||
726 | */ |
||
727 | 740 | public function buildFrom($tables, &$params) |
|
737 | |||
738 | /** |
||
739 | * @param array $joins |
||
740 | * @param array $params the binding parameters to be populated |
||
741 | * @return string the JOIN clause built from [[Query::$join]]. |
||
742 | * @throws Exception if the $joins parameter is not in proper format |
||
743 | */ |
||
744 | 740 | public function buildJoin($joins, &$params) |
|
769 | |||
770 | /** |
||
771 | * Quotes table names passed |
||
772 | * |
||
773 | * @param array $tables |
||
774 | * @param array $params |
||
775 | * @return array |
||
776 | */ |
||
777 | 491 | private function quoteTableNames($tables, &$params) |
|
798 | |||
799 | /** |
||
800 | * @param string|array $condition |
||
801 | * @param array $params the binding parameters to be populated |
||
802 | * @return string the WHERE clause built from [[Query::$where]]. |
||
803 | */ |
||
804 | 758 | public function buildWhere($condition, &$params) |
|
810 | |||
811 | /** |
||
812 | * @param array $columns |
||
813 | * @param array $params the binding parameters to be populated |
||
814 | * @return string the GROUP BY clause |
||
815 | */ |
||
816 | 740 | public function buildGroupBy($columns, &$params) |
|
831 | |||
832 | /** |
||
833 | * @param string|array $condition |
||
834 | * @param array $params the binding parameters to be populated |
||
835 | * @return string the HAVING clause built from [[Query::$having]]. |
||
836 | */ |
||
837 | 740 | public function buildHaving($condition, &$params) |
|
843 | |||
844 | /** |
||
845 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
846 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
847 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
848 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
849 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
850 | * @param array $params the binding parameters to be populated |
||
851 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
852 | */ |
||
853 | 740 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset, &$params) |
|
865 | |||
866 | /** |
||
867 | * @param array $columns |
||
868 | * @param array $params the binding parameters to be populated |
||
869 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
870 | */ |
||
871 | 740 | public function buildOrderBy($columns, &$params) |
|
888 | |||
889 | /** |
||
890 | * @param int $limit |
||
891 | * @param int $offset |
||
892 | * @return string the LIMIT and OFFSET clauses |
||
893 | */ |
||
894 | 220 | public function buildLimit($limit, $offset) |
|
906 | |||
907 | /** |
||
908 | * Checks to see if the given limit is effective. |
||
909 | * @param mixed $limit the given limit |
||
910 | * @return bool whether the limit is effective |
||
911 | */ |
||
912 | 740 | protected function hasLimit($limit) |
|
916 | |||
917 | /** |
||
918 | * Checks to see if the given offset is effective. |
||
919 | * @param mixed $offset the given offset |
||
920 | * @return bool whether the offset is effective |
||
921 | */ |
||
922 | 740 | protected function hasOffset($offset) |
|
927 | |||
928 | /** |
||
929 | * @param array $unions |
||
930 | * @param array $params the binding parameters to be populated |
||
931 | * @return string the UNION clause built from [[Query::$union]]. |
||
932 | */ |
||
933 | 518 | public function buildUnion($unions, &$params) |
|
952 | |||
953 | /** |
||
954 | * Processes columns and properly quotes them if necessary. |
||
955 | * It will join all columns into a string with comma as separators. |
||
956 | * @param string|array $columns the columns to be processed |
||
957 | * @return string the processing result |
||
958 | */ |
||
959 | 3 | public function buildColumns($columns) |
|
978 | |||
979 | /** |
||
980 | * Parses the condition specification and generates the corresponding SQL expression. |
||
981 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
982 | * on how to specify a condition. |
||
983 | * @param array $params the binding parameters to be populated |
||
984 | * @return string the generated SQL expression |
||
985 | */ |
||
986 | 758 | public function buildCondition($condition, &$params) |
|
1012 | |||
1013 | /** |
||
1014 | * Creates a condition based on column-value pairs. |
||
1015 | * @param array $condition the condition specification. |
||
1016 | * @param array $params the binding parameters to be populated |
||
1017 | * @return string the generated SQL expression |
||
1018 | */ |
||
1019 | 379 | public function buildHashCondition($condition, &$params) |
|
1046 | |||
1047 | /** |
||
1048 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1049 | * @param string $operator the operator to use for connecting the given operands |
||
1050 | * @param array $operands the SQL expressions to connect. |
||
1051 | * @param array $params the binding parameters to be populated |
||
1052 | * @return string the generated SQL expression |
||
1053 | */ |
||
1054 | 98 | public function buildAndCondition($operator, $operands, &$params) |
|
1077 | |||
1078 | /** |
||
1079 | * Inverts an SQL expressions with `NOT` operator. |
||
1080 | * @param string $operator the operator to use for connecting the given operands |
||
1081 | * @param array $operands the SQL expressions to connect. |
||
1082 | * @param array $params the binding parameters to be populated |
||
1083 | * @return string the generated SQL expression |
||
1084 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1085 | */ |
||
1086 | 3 | public function buildNotCondition($operator, $operands, &$params) |
|
1102 | |||
1103 | /** |
||
1104 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1105 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1106 | * @param array $operands the first operand is the column name. The second and third operands |
||
1107 | * describe the interval that column value should be in. |
||
1108 | * @param array $params the binding parameters to be populated |
||
1109 | * @return string the generated SQL expression |
||
1110 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1111 | */ |
||
1112 | 21 | public function buildBetweenCondition($operator, $operands, &$params) |
|
1144 | |||
1145 | /** |
||
1146 | * Creates an SQL expressions with the `IN` operator. |
||
1147 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1148 | * @param array $operands the first operand is the column name. If it is an array |
||
1149 | * a composite IN condition will be generated. |
||
1150 | * The second operand is an array of values that column value should be among. |
||
1151 | * If it is an empty array the generated expression will be a `false` value if |
||
1152 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1153 | * @param array $params the binding parameters to be populated |
||
1154 | * @return string the generated SQL expression |
||
1155 | * @throws Exception if wrong number of operands have been given. |
||
1156 | */ |
||
1157 | 200 | public function buildInCondition($operator, $operands, &$params) |
|
1218 | |||
1219 | /** |
||
1220 | * Builds SQL for IN condition |
||
1221 | * |
||
1222 | * @param string $operator |
||
1223 | * @param array $columns |
||
1224 | * @param Query $values |
||
1225 | * @param array $params |
||
1226 | * @return string SQL |
||
1227 | */ |
||
1228 | 14 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
1245 | |||
1246 | /** |
||
1247 | * Builds SQL for IN condition |
||
1248 | * |
||
1249 | * @param string $operator |
||
1250 | * @param array|\Traversable $columns |
||
1251 | * @param array $values |
||
1252 | * @param array $params |
||
1253 | * @return string SQL |
||
1254 | */ |
||
1255 | 10 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
1283 | |||
1284 | /** |
||
1285 | * Creates an SQL expressions with the `LIKE` operator. |
||
1286 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1287 | * @param array $operands an array of two or three operands |
||
1288 | * |
||
1289 | * - The first operand is the column name. |
||
1290 | * - The second operand is a single value or an array of values that column value |
||
1291 | * should be compared with. If it is an empty array the generated expression will |
||
1292 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1293 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1294 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1295 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1296 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1297 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1298 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1299 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1300 | * @param array $params the binding parameters to be populated |
||
1301 | * @return string the generated SQL expression |
||
1302 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1303 | */ |
||
1304 | 72 | public function buildLikeCondition($operator, $operands, &$params) |
|
1350 | |||
1351 | /** |
||
1352 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1353 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1354 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1355 | * @param array $params the binding parameters to be populated |
||
1356 | * @return string the generated SQL expression |
||
1357 | * @throws InvalidParamException if the operand is not a [[Query]] object. |
||
1358 | */ |
||
1359 | 18 | public function buildExistsCondition($operator, $operands, &$params) |
|
1368 | |||
1369 | /** |
||
1370 | * Creates an SQL expressions like `"column" operator value`. |
||
1371 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1372 | * @param array $operands contains two column names. |
||
1373 | * @param array $params the binding parameters to be populated |
||
1374 | * @return string the generated SQL expression |
||
1375 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1376 | */ |
||
1377 | 30 | public function buildSimpleCondition($operator, $operands, &$params) |
|
1405 | |||
1406 | /** |
||
1407 | * Creates a SELECT EXISTS() SQL statement. |
||
1408 | * @param string $rawSql the subquery in a raw form to select from. |
||
1409 | * @return string the SELECT EXISTS() SQL statement. |
||
1410 | * @since 2.0.8 |
||
1411 | */ |
||
1412 | 45 | public function selectExists($rawSql) |
|
1416 | } |
||
1417 |