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 | 767 | 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 | 529 | public function build($query, $params = []) |
|
130 | |||
131 | /** |
||
132 | * Creates an INSERT SQL statement. |
||
133 | * For example, |
||
134 | * |
||
135 | * ```php |
||
136 | * $sql = $queryBuilder->insert('user', [ |
||
137 | * 'name' => 'Sam', |
||
138 | * 'age' => 30, |
||
139 | * ], $params); |
||
140 | * ``` |
||
141 | * |
||
142 | * The method will properly escape the table and column names. |
||
143 | * |
||
144 | * @param string $table the table that new rows will be inserted into. |
||
145 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
146 | * @param array $params the binding parameters that will be generated by this method. |
||
147 | * They should be bound to the DB command later. |
||
148 | * @return string the INSERT SQL |
||
149 | */ |
||
150 | 101 | public function insert($table, $columns, &$params) |
|
178 | |||
179 | /** |
||
180 | * Generates a batch INSERT SQL statement. |
||
181 | * For example, |
||
182 | * |
||
183 | * ```php |
||
184 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
185 | * ['Tom', 30], |
||
186 | * ['Jane', 20], |
||
187 | * ['Linda', 25], |
||
188 | * ]); |
||
189 | * ``` |
||
190 | * |
||
191 | * Note that the values in each row must match the corresponding column names. |
||
192 | * |
||
193 | * The method will properly escape the column names, and quote the values to be inserted. |
||
194 | * |
||
195 | * @param string $table the table that new rows will be inserted into. |
||
196 | * @param array $columns the column names |
||
197 | * @param array $rows the rows to be batch inserted into the table |
||
198 | * @return string the batch INSERT SQL statement |
||
199 | */ |
||
200 | 3 | public function batchInsert($table, $columns, $rows) |
|
239 | |||
240 | /** |
||
241 | * Creates an UPDATE SQL statement. |
||
242 | * For example, |
||
243 | * |
||
244 | * ```php |
||
245 | * $params = []; |
||
246 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
247 | * ``` |
||
248 | * |
||
249 | * The method will properly escape the table and column names. |
||
250 | * |
||
251 | * @param string $table the table to be updated. |
||
252 | * @param array $columns the column data (name => value) to be updated. |
||
253 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
254 | * refer to [[Query::where()]] on how to specify condition. |
||
255 | * @param array $params the binding parameters that will be modified by this method |
||
256 | * so that they can be bound to the DB command later. |
||
257 | * @return string the UPDATE SQL |
||
258 | */ |
||
259 | 67 | public function update($table, $columns, $condition, &$params) |
|
286 | |||
287 | /** |
||
288 | * Creates a DELETE SQL statement. |
||
289 | * For example, |
||
290 | * |
||
291 | * ```php |
||
292 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
293 | * ``` |
||
294 | * |
||
295 | * The method will properly escape the table and column names. |
||
296 | * |
||
297 | * @param string $table the table where the data will be deleted from. |
||
298 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
299 | * refer to [[Query::where()]] on how to specify condition. |
||
300 | * @param array $params the binding parameters that will be modified by this method |
||
301 | * so that they can be bound to the DB command later. |
||
302 | * @return string the DELETE SQL |
||
303 | */ |
||
304 | 142 | public function delete($table, $condition, &$params) |
|
311 | |||
312 | /** |
||
313 | * Builds a SQL statement for creating a new DB table. |
||
314 | * |
||
315 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
316 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
317 | * stands for the column type which can contain an abstract DB type. |
||
318 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
319 | * |
||
320 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
321 | * inserted into the generated SQL. |
||
322 | * |
||
323 | * For example, |
||
324 | * |
||
325 | * ```php |
||
326 | * $sql = $queryBuilder->createTable('user', [ |
||
327 | * 'id' => 'pk', |
||
328 | * 'name' => 'string', |
||
329 | * 'age' => 'integer', |
||
330 | * ]); |
||
331 | * ``` |
||
332 | * |
||
333 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
334 | * @param array $columns the columns (name => definition) in the new table. |
||
335 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
336 | * @return string the SQL statement for creating a new DB table. |
||
337 | */ |
||
338 | 57 | public function createTable($table, $columns, $options = null) |
|
352 | |||
353 | /** |
||
354 | * Builds a SQL statement for renaming a DB table. |
||
355 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
356 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
357 | * @return string the SQL statement for renaming a DB table. |
||
358 | */ |
||
359 | 1 | public function renameTable($oldName, $newName) |
|
363 | |||
364 | /** |
||
365 | * Builds a SQL statement for dropping a DB table. |
||
366 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
367 | * @return string the SQL statement for dropping a DB table. |
||
368 | */ |
||
369 | 11 | public function dropTable($table) |
|
373 | |||
374 | /** |
||
375 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
376 | * @param string $name the name of the primary key constraint. |
||
377 | * @param string $table the table that the primary key constraint will be added to. |
||
378 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
379 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
380 | */ |
||
381 | 3 | public function addPrimaryKey($name, $table, $columns) |
|
395 | |||
396 | /** |
||
397 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
398 | * @param string $name the name of the primary key constraint to be removed. |
||
399 | * @param string $table the table that the primary key constraint will be removed from. |
||
400 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
401 | */ |
||
402 | 1 | public function dropPrimaryKey($name, $table) |
|
407 | |||
408 | /** |
||
409 | * Builds a SQL statement for truncating a DB table. |
||
410 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
411 | * @return string the SQL statement for truncating a DB table. |
||
412 | */ |
||
413 | 5 | public function truncateTable($table) |
|
417 | |||
418 | /** |
||
419 | * Builds a SQL statement for adding a new DB column. |
||
420 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
421 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
422 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
423 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
424 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
425 | * @return string the SQL statement for adding a new column. |
||
426 | */ |
||
427 | 4 | public function addColumn($table, $column, $type) |
|
433 | |||
434 | /** |
||
435 | * Builds a SQL statement for dropping a DB column. |
||
436 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
437 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
438 | * @return string the SQL statement for dropping a DB column. |
||
439 | */ |
||
440 | public function dropColumn($table, $column) |
||
445 | |||
446 | /** |
||
447 | * Builds a SQL statement for renaming a column. |
||
448 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
449 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
450 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
451 | * @return string the SQL statement for renaming a DB column. |
||
452 | */ |
||
453 | public function renameColumn($table, $oldName, $newName) |
||
459 | |||
460 | /** |
||
461 | * Builds a SQL statement for changing the definition of a column. |
||
462 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
463 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
464 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
465 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
466 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
467 | * will become 'varchar(255) not null'. |
||
468 | * @return string the SQL statement for changing the definition of a column. |
||
469 | */ |
||
470 | 1 | public function alterColumn($table, $column, $type) |
|
477 | |||
478 | /** |
||
479 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
480 | * The method will properly quote the table and column names. |
||
481 | * @param string $name the name of the foreign key constraint. |
||
482 | * @param string $table the table that the foreign key constraint will be added to. |
||
483 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
484 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
485 | * @param string $refTable the table that the foreign key references to. |
||
486 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
487 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
488 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
489 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
490 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
491 | */ |
||
492 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
508 | |||
509 | /** |
||
510 | * Builds a SQL statement for dropping a foreign key constraint. |
||
511 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
512 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
513 | * @return string the SQL statement for dropping a foreign key constraint. |
||
514 | */ |
||
515 | public function dropForeignKey($name, $table) |
||
520 | |||
521 | /** |
||
522 | * Builds a SQL statement for creating a new index. |
||
523 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
524 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
525 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
526 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
527 | * by the method, unless a parenthesis is found in the name. |
||
528 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
529 | * @return string the SQL statement for creating a new index. |
||
530 | */ |
||
531 | 1 | public function createIndex($name, $table, $columns, $unique = false) |
|
538 | |||
539 | /** |
||
540 | * Builds a SQL statement for dropping an index. |
||
541 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
542 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
543 | * @return string the SQL statement for dropping an index. |
||
544 | */ |
||
545 | public function dropIndex($name, $table) |
||
549 | |||
550 | /** |
||
551 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
552 | * The sequence will be reset such that the primary key of the next new row inserted |
||
553 | * will have the specified value or 1. |
||
554 | * @param string $table the name of the table whose primary key sequence will be reset |
||
555 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
556 | * the next new row's primary key will have a value 1. |
||
557 | * @return string the SQL statement for resetting sequence |
||
558 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
559 | */ |
||
560 | public function resetSequence($table, $value = null) |
||
564 | |||
565 | /** |
||
566 | * Builds a SQL statement for enabling or disabling integrity check. |
||
567 | * @param bool $check whether to turn on or off the integrity check. |
||
568 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
569 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
570 | * @return string the SQL statement for checking integrity |
||
571 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
572 | */ |
||
573 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
577 | |||
578 | /** |
||
579 | * Builds a SQL command for adding comment to column |
||
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 $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
583 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
584 | * @return string the SQL statement for adding comment on column |
||
585 | * @since 2.0.8 |
||
586 | */ |
||
587 | 1 | public function addCommentOnColumn($table, $column, $comment) |
|
592 | |||
593 | /** |
||
594 | * Builds a SQL command for adding comment to table |
||
595 | * |
||
596 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
597 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
598 | * @return string the SQL statement for adding comment on table |
||
599 | * @since 2.0.8 |
||
600 | */ |
||
601 | 1 | public function addCommentOnTable($table, $comment) |
|
605 | |||
606 | /** |
||
607 | * Builds a SQL command for adding comment to column |
||
608 | * |
||
609 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
610 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
611 | * @return string the SQL statement for adding comment on column |
||
612 | * @since 2.0.8 |
||
613 | */ |
||
614 | 1 | public function dropCommentFromColumn($table, $column) |
|
618 | |||
619 | /** |
||
620 | * Builds a SQL command for adding comment to table |
||
621 | * |
||
622 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
623 | * @return string the SQL statement for adding comment on column |
||
624 | * @since 2.0.8 |
||
625 | */ |
||
626 | 1 | public function dropCommentFromTable($table) |
|
630 | |||
631 | /** |
||
632 | * Converts an abstract column type into a physical column type. |
||
633 | * The conversion is done using the type map specified in [[typeMap]]. |
||
634 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
635 | * physical types): |
||
636 | * |
||
637 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
638 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
639 | * - `unsignedpk`: an unsigned auto-incremental primary key type, will be converted into "int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
640 | * - `char`: char type, will be converted into "char(1)" |
||
641 | * - `string`: string type, will be converted into "varchar(255)" |
||
642 | * - `text`: a long string type, will be converted into "text" |
||
643 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
644 | * - `integer`: integer type, will be converted into "int(11)" |
||
645 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
646 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
647 | * - `float``: float number type, will be converted into "float" |
||
648 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
649 | * - `datetime`: datetime type, will be converted into "datetime" |
||
650 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
651 | * - `time`: time type, will be converted into "time" |
||
652 | * - `date`: date type, will be converted into "date" |
||
653 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
654 | * - `binary`: binary data type, will be converted into "blob" |
||
655 | * |
||
656 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
657 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
658 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
659 | * |
||
660 | * For some of the abstract types you can also specify a length or precision constraint |
||
661 | * by appending it in round brackets directly to the type. |
||
662 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
663 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
664 | * be ignored. |
||
665 | * |
||
666 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
667 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
668 | * @return string physical column type. |
||
669 | */ |
||
670 | 61 | public function getColumnType($type) |
|
690 | |||
691 | /** |
||
692 | * @param array $columns |
||
693 | * @param array $params the binding parameters to be populated |
||
694 | * @param bool $distinct |
||
695 | * @param string $selectOption |
||
696 | * @return string the SELECT clause built from [[Query::$select]]. |
||
697 | */ |
||
698 | 756 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
736 | |||
737 | /** |
||
738 | * @param array $tables |
||
739 | * @param array $params the binding parameters to be populated |
||
740 | * @return string the FROM clause built from [[Query::$from]]. |
||
741 | */ |
||
742 | 756 | public function buildFrom($tables, &$params) |
|
752 | |||
753 | /** |
||
754 | * @param array $joins |
||
755 | * @param array $params the binding parameters to be populated |
||
756 | * @return string the JOIN clause built from [[Query::$join]]. |
||
757 | * @throws Exception if the $joins parameter is not in proper format |
||
758 | */ |
||
759 | 756 | public function buildJoin($joins, &$params) |
|
784 | |||
785 | /** |
||
786 | * Quotes table names passed |
||
787 | * |
||
788 | * @param array $tables |
||
789 | * @param array $params |
||
790 | * @return array |
||
791 | */ |
||
792 | 507 | private function quoteTableNames($tables, &$params) |
|
813 | |||
814 | /** |
||
815 | * @param string|array $condition |
||
816 | * @param array $params the binding parameters to be populated |
||
817 | * @return string the WHERE clause built from [[Query::$where]]. |
||
818 | */ |
||
819 | 774 | public function buildWhere($condition, &$params) |
|
825 | |||
826 | /** |
||
827 | * @param array $columns |
||
828 | * @return string the GROUP BY clause |
||
829 | */ |
||
830 | 756 | public function buildGroupBy($columns) |
|
844 | |||
845 | /** |
||
846 | * @param string|array $condition |
||
847 | * @param array $params the binding parameters to be populated |
||
848 | * @return string the HAVING clause built from [[Query::$having]]. |
||
849 | */ |
||
850 | 756 | public function buildHaving($condition, &$params) |
|
856 | |||
857 | /** |
||
858 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
859 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
860 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
861 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
862 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
863 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
864 | */ |
||
865 | 756 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
877 | |||
878 | /** |
||
879 | * @param array $columns |
||
880 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
881 | */ |
||
882 | 756 | public function buildOrderBy($columns) |
|
898 | |||
899 | /** |
||
900 | * @param int $limit |
||
901 | * @param int $offset |
||
902 | * @return string the LIMIT and OFFSET clauses |
||
903 | */ |
||
904 | 223 | public function buildLimit($limit, $offset) |
|
916 | |||
917 | /** |
||
918 | * Checks to see if the given limit is effective. |
||
919 | * @param mixed $limit the given limit |
||
920 | * @return bool whether the limit is effective |
||
921 | */ |
||
922 | 756 | protected function hasLimit($limit) |
|
926 | |||
927 | /** |
||
928 | * Checks to see if the given offset is effective. |
||
929 | * @param mixed $offset the given offset |
||
930 | * @return bool whether the offset is effective |
||
931 | */ |
||
932 | 756 | protected function hasOffset($offset) |
|
937 | |||
938 | /** |
||
939 | * @param array $unions |
||
940 | * @param array $params the binding parameters to be populated |
||
941 | * @return string the UNION clause built from [[Query::$union]]. |
||
942 | */ |
||
943 | 529 | public function buildUnion($unions, &$params) |
|
962 | |||
963 | /** |
||
964 | * Processes columns and properly quotes them if necessary. |
||
965 | * It will join all columns into a string with comma as separators. |
||
966 | * @param string|array $columns the columns to be processed |
||
967 | * @return string the processing result |
||
968 | */ |
||
969 | 3 | public function buildColumns($columns) |
|
988 | |||
989 | /** |
||
990 | * Parses the condition specification and generates the corresponding SQL expression. |
||
991 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
992 | * on how to specify a condition. |
||
993 | * @param array $params the binding parameters to be populated |
||
994 | * @return string the generated SQL expression |
||
995 | */ |
||
996 | 774 | public function buildCondition($condition, &$params) |
|
1022 | |||
1023 | /** |
||
1024 | * Creates a condition based on column-value pairs. |
||
1025 | * @param array $condition the condition specification. |
||
1026 | * @param array $params the binding parameters to be populated |
||
1027 | * @return string the generated SQL expression |
||
1028 | */ |
||
1029 | 389 | public function buildHashCondition($condition, &$params) |
|
1056 | |||
1057 | /** |
||
1058 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1059 | * @param string $operator the operator to use for connecting the given operands |
||
1060 | * @param array $operands the SQL expressions to connect. |
||
1061 | * @param array $params the binding parameters to be populated |
||
1062 | * @return string the generated SQL expression |
||
1063 | */ |
||
1064 | 98 | public function buildAndCondition($operator, $operands, &$params) |
|
1087 | |||
1088 | /** |
||
1089 | * Inverts an SQL expressions with `NOT` operator. |
||
1090 | * @param string $operator the operator to use for connecting the given operands |
||
1091 | * @param array $operands the SQL expressions to connect. |
||
1092 | * @param array $params the binding parameters to be populated |
||
1093 | * @return string the generated SQL expression |
||
1094 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1095 | */ |
||
1096 | 3 | public function buildNotCondition($operator, $operands, &$params) |
|
1112 | |||
1113 | /** |
||
1114 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1115 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1116 | * @param array $operands the first operand is the column name. The second and third operands |
||
1117 | * describe the interval that column value should be in. |
||
1118 | * @param array $params the binding parameters to be populated |
||
1119 | * @return string the generated SQL expression |
||
1120 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1121 | */ |
||
1122 | 21 | public function buildBetweenCondition($operator, $operands, &$params) |
|
1154 | |||
1155 | /** |
||
1156 | * Creates an SQL expressions with the `IN` operator. |
||
1157 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1158 | * @param array $operands the first operand is the column name. If it is an array |
||
1159 | * a composite IN condition will be generated. |
||
1160 | * The second operand is an array of values that column value should be among. |
||
1161 | * If it is an empty array the generated expression will be a `false` value if |
||
1162 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1163 | * @param array $params the binding parameters to be populated |
||
1164 | * @return string the generated SQL expression |
||
1165 | * @throws Exception if wrong number of operands have been given. |
||
1166 | */ |
||
1167 | 201 | public function buildInCondition($operator, $operands, &$params) |
|
1228 | |||
1229 | /** |
||
1230 | * Builds SQL for IN condition |
||
1231 | * |
||
1232 | * @param string $operator |
||
1233 | * @param array $columns |
||
1234 | * @param Query $values |
||
1235 | * @param array $params |
||
1236 | * @return string SQL |
||
1237 | */ |
||
1238 | 14 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
1255 | |||
1256 | /** |
||
1257 | * Builds SQL for IN condition |
||
1258 | * |
||
1259 | * @param string $operator |
||
1260 | * @param array|\Traversable $columns |
||
1261 | * @param array $values |
||
1262 | * @param array $params |
||
1263 | * @return string SQL |
||
1264 | */ |
||
1265 | 10 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
1293 | |||
1294 | /** |
||
1295 | * Creates an SQL expressions with the `LIKE` operator. |
||
1296 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1297 | * @param array $operands an array of two or three operands |
||
1298 | * |
||
1299 | * - The first operand is the column name. |
||
1300 | * - The second operand is a single value or an array of values that column value |
||
1301 | * should be compared with. If it is an empty array the generated expression will |
||
1302 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1303 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1304 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1305 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1306 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1307 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1308 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1309 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1310 | * @param array $params the binding parameters to be populated |
||
1311 | * @return string the generated SQL expression |
||
1312 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1313 | */ |
||
1314 | 72 | public function buildLikeCondition($operator, $operands, &$params) |
|
1360 | |||
1361 | /** |
||
1362 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1363 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1364 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1365 | * @param array $params the binding parameters to be populated |
||
1366 | * @return string the generated SQL expression |
||
1367 | * @throws InvalidParamException if the operand is not a [[Query]] object. |
||
1368 | */ |
||
1369 | 18 | public function buildExistsCondition($operator, $operands, &$params) |
|
1378 | |||
1379 | /** |
||
1380 | * Creates an SQL expressions like `"column" operator value`. |
||
1381 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1382 | * @param array $operands contains two column names. |
||
1383 | * @param array $params the binding parameters to be populated |
||
1384 | * @return string the generated SQL expression |
||
1385 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1386 | */ |
||
1387 | 30 | public function buildSimpleCondition($operator, $operands, &$params) |
|
1415 | |||
1416 | /** |
||
1417 | * Creates a SELECT EXISTS() SQL statement. |
||
1418 | * @param string $rawSql the subquery in a raw form to select from. |
||
1419 | * @return string the SELECT EXISTS() SQL statement. |
||
1420 | * @since 2.0.8 |
||
1421 | */ |
||
1422 | 51 | public function selectExists($rawSql) |
|
1426 | } |
||
1427 |