Complex classes like QueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class QueryBuilder extends \yii\base\Object |
||
27 | { |
||
28 | /** |
||
29 | * The prefix for automatically generated query binding parameters. |
||
30 | */ |
||
31 | const PARAM_PREFIX = ':qp'; |
||
32 | |||
33 | /** |
||
34 | * @var Connection the database connection. |
||
35 | */ |
||
36 | public $db; |
||
37 | /** |
||
38 | * @var string the separator between different fragments of a SQL statement. |
||
39 | * Defaults to an empty space. This is mainly used by [[build()]] when generating a SQL statement. |
||
40 | */ |
||
41 | public $separator = ' '; |
||
42 | /** |
||
43 | * @var array the abstract column types mapped to physical column types. |
||
44 | * This is mainly used to support creating/modifying tables using DB-independent data type specifications. |
||
45 | * Child classes should override this property to declare supported type mappings. |
||
46 | */ |
||
47 | public $typeMap = []; |
||
48 | |||
49 | /** |
||
50 | * @var array map of query condition to builder methods. |
||
51 | * These methods are used by [[buildCondition]] to build SQL conditions from array syntax. |
||
52 | */ |
||
53 | protected $conditionBuilders = [ |
||
54 | 'NOT' => 'buildNotCondition', |
||
55 | 'AND' => 'buildAndCondition', |
||
56 | 'OR' => 'buildAndCondition', |
||
57 | 'BETWEEN' => 'buildBetweenCondition', |
||
58 | 'NOT BETWEEN' => 'buildBetweenCondition', |
||
59 | 'IN' => 'buildInCondition', |
||
60 | 'NOT IN' => 'buildInCondition', |
||
61 | 'LIKE' => 'buildLikeCondition', |
||
62 | 'NOT LIKE' => 'buildLikeCondition', |
||
63 | 'OR LIKE' => 'buildLikeCondition', |
||
64 | 'OR NOT LIKE' => 'buildLikeCondition', |
||
65 | 'EXISTS' => 'buildExistsCondition', |
||
66 | 'NOT EXISTS' => 'buildExistsCondition', |
||
67 | ]; |
||
68 | /** |
||
69 | * @var array map of chars to their replacements in LIKE conditions. |
||
70 | * By default it's configured to escape `%`, `_` and `\` with `\`. |
||
71 | * @since 2.0.12. |
||
72 | */ |
||
73 | protected $likeEscapingReplacements = [ |
||
74 | '%' => '\%', |
||
75 | '_' => '\_', |
||
76 | '\\' => '\\\\', |
||
77 | ]; |
||
78 | /** |
||
79 | * @var string|null character used to escape special characters in LIKE conditions. |
||
80 | * By default it's assumed to be `\`. |
||
81 | * @since 2.0.12 |
||
82 | */ |
||
83 | protected $likeEscapeCharacter; |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Constructor. |
||
88 | * @param Connection $connection the database connection. |
||
89 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
90 | */ |
||
91 | 991 | public function __construct($connection, $config = []) |
|
96 | |||
97 | /** |
||
98 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
99 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
100 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
101 | * be included in the result with the additional parameters generated during the query building process. |
||
102 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
103 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
104 | * include those provided in `$params`. |
||
105 | */ |
||
106 | 580 | public function build($query, $params = []) |
|
146 | |||
147 | /** |
||
148 | * Creates an INSERT SQL statement. |
||
149 | * For example, |
||
150 | * |
||
151 | * ```php |
||
152 | * $sql = $queryBuilder->insert('user', [ |
||
153 | * 'name' => 'Sam', |
||
154 | * 'age' => 30, |
||
155 | * ], $params); |
||
156 | * ``` |
||
157 | * |
||
158 | * The method will properly escape the table and column names. |
||
159 | * |
||
160 | * @param string $table the table that new rows will be inserted into. |
||
161 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
||
162 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
163 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
164 | * @param array $params the binding parameters that will be generated by this method. |
||
165 | * They should be bound to the DB command later. |
||
166 | * @return string the INSERT SQL |
||
167 | */ |
||
168 | 191 | public function insert($table, $columns, &$params) |
|
204 | |||
205 | /** |
||
206 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
207 | * |
||
208 | * @param \yii\db\Query $columns Object, which represents select query. |
||
209 | * @param \yii\db\Schema $schema Schema object to quote column name. |
||
210 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
211 | * be included in the result with the additional parameters generated during the query building process. |
||
212 | * @return array |
||
213 | * @throws InvalidParamException if query's select does not contain named parameters only. |
||
214 | * @since 2.0.11 |
||
215 | */ |
||
216 | 15 | protected function prepareInsertSelectSubQuery($columns, $schema, $params = []) |
|
237 | |||
238 | /** |
||
239 | * Generates a batch INSERT SQL statement. |
||
240 | * For example, |
||
241 | * |
||
242 | * ```php |
||
243 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
244 | * ['Tom', 30], |
||
245 | * ['Jane', 20], |
||
246 | * ['Linda', 25], |
||
247 | * ]); |
||
248 | * ``` |
||
249 | * |
||
250 | * Note that the values in each row must match the corresponding column names. |
||
251 | * |
||
252 | * The method will properly escape the column names, and quote the values to be inserted. |
||
253 | * |
||
254 | * @param string $table the table that new rows will be inserted into. |
||
255 | * @param array $columns the column names |
||
256 | * @param array $rows the rows to be batch inserted into the table |
||
257 | * @return string the batch INSERT SQL statement |
||
258 | */ |
||
259 | 20 | public function batchInsert($table, $columns, $rows) |
|
301 | |||
302 | /** |
||
303 | * Creates an UPDATE SQL statement. |
||
304 | * For example, |
||
305 | * |
||
306 | * ```php |
||
307 | * $params = []; |
||
308 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
309 | * ``` |
||
310 | * |
||
311 | * The method will properly escape the table and column names. |
||
312 | * |
||
313 | * @param string $table the table to be updated. |
||
314 | * @param array $columns the column data (name => value) to be updated. |
||
315 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
316 | * refer to [[Query::where()]] on how to specify condition. |
||
317 | * @param array $params the binding parameters that will be modified by this method |
||
318 | * so that they can be bound to the DB command later. |
||
319 | * @return string the UPDATE SQL |
||
320 | */ |
||
321 | 79 | public function update($table, $columns, $condition, &$params) |
|
348 | |||
349 | /** |
||
350 | * Creates a DELETE SQL statement. |
||
351 | * For example, |
||
352 | * |
||
353 | * ```php |
||
354 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
355 | * ``` |
||
356 | * |
||
357 | * The method will properly escape the table and column names. |
||
358 | * |
||
359 | * @param string $table the table where the data will be deleted from. |
||
360 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
361 | * refer to [[Query::where()]] on how to specify condition. |
||
362 | * @param array $params the binding parameters that will be modified by this method |
||
363 | * so that they can be bound to the DB command later. |
||
364 | * @return string the DELETE SQL |
||
365 | */ |
||
366 | 194 | public function delete($table, $condition, &$params) |
|
373 | |||
374 | /** |
||
375 | * Builds a SQL statement for creating a new DB table. |
||
376 | * |
||
377 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
378 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
379 | * stands for the column type which can contain an abstract DB type. |
||
380 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
381 | * |
||
382 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
383 | * inserted into the generated SQL. |
||
384 | * |
||
385 | * For example, |
||
386 | * |
||
387 | * ```php |
||
388 | * $sql = $queryBuilder->createTable('user', [ |
||
389 | * 'id' => 'pk', |
||
390 | * 'name' => 'string', |
||
391 | * 'age' => 'integer', |
||
392 | * ]); |
||
393 | * ``` |
||
394 | * |
||
395 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
396 | * @param array $columns the columns (name => definition) in the new table. |
||
397 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
398 | * @return string the SQL statement for creating a new DB table. |
||
399 | */ |
||
400 | 86 | public function createTable($table, $columns, $options = null) |
|
414 | |||
415 | /** |
||
416 | * Builds a SQL statement for renaming a DB table. |
||
417 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
418 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
419 | * @return string the SQL statement for renaming a DB table. |
||
420 | */ |
||
421 | 1 | public function renameTable($oldName, $newName) |
|
425 | |||
426 | /** |
||
427 | * Builds a SQL statement for dropping a DB table. |
||
428 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
429 | * @return string the SQL statement for dropping a DB table. |
||
430 | */ |
||
431 | 18 | public function dropTable($table) |
|
435 | |||
436 | /** |
||
437 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
438 | * @param string $name the name of the primary key constraint. |
||
439 | * @param string $table the table that the primary key constraint will be added to. |
||
440 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
441 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
442 | */ |
||
443 | 2 | public function addPrimaryKey($name, $table, $columns) |
|
457 | |||
458 | /** |
||
459 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
460 | * @param string $name the name of the primary key constraint to be removed. |
||
461 | * @param string $table the table that the primary key constraint will be removed from. |
||
462 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
463 | */ |
||
464 | 1 | public function dropPrimaryKey($name, $table) |
|
469 | |||
470 | /** |
||
471 | * Builds a SQL statement for truncating a DB table. |
||
472 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
473 | * @return string the SQL statement for truncating a DB table. |
||
474 | */ |
||
475 | 9 | public function truncateTable($table) |
|
479 | |||
480 | /** |
||
481 | * Builds a SQL statement for adding a new DB column. |
||
482 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
483 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
484 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
485 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
486 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
487 | * @return string the SQL statement for adding a new column. |
||
488 | */ |
||
489 | 4 | public function addColumn($table, $column, $type) |
|
495 | |||
496 | /** |
||
497 | * Builds a SQL statement for dropping a DB column. |
||
498 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
499 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
500 | * @return string the SQL statement for dropping a DB column. |
||
501 | */ |
||
502 | public function dropColumn($table, $column) |
||
507 | |||
508 | /** |
||
509 | * Builds a SQL statement for renaming a column. |
||
510 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
511 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
512 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
513 | * @return string the SQL statement for renaming a DB column. |
||
514 | */ |
||
515 | public function renameColumn($table, $oldName, $newName) |
||
521 | |||
522 | /** |
||
523 | * Builds a SQL statement for changing the definition of a column. |
||
524 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
525 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
526 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
527 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
528 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
529 | * will become 'varchar(255) not null'. |
||
530 | * @return string the SQL statement for changing the definition of a column. |
||
531 | */ |
||
532 | 1 | public function alterColumn($table, $column, $type) |
|
539 | |||
540 | /** |
||
541 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
542 | * The method will properly quote the table and column names. |
||
543 | * @param string $name the name of the foreign key constraint. |
||
544 | * @param string $table the table that the foreign key constraint will be added to. |
||
545 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
546 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
547 | * @param string $refTable the table that the foreign key references to. |
||
548 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
549 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
550 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
551 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
552 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
553 | */ |
||
554 | 2 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
555 | { |
||
556 | 2 | $sql = 'ALTER TABLE ' . $this->db->quoteTableName($table) |
|
557 | 2 | . ' ADD CONSTRAINT ' . $this->db->quoteColumnName($name) |
|
558 | 2 | . ' FOREIGN KEY (' . $this->buildColumns($columns) . ')' |
|
559 | 2 | . ' REFERENCES ' . $this->db->quoteTableName($refTable) |
|
560 | 2 | . ' (' . $this->buildColumns($refColumns) . ')'; |
|
561 | 2 | if ($delete !== null) { |
|
562 | $sql .= ' ON DELETE ' . $delete; |
||
563 | } |
||
564 | 2 | if ($update !== null) { |
|
565 | $sql .= ' ON UPDATE ' . $update; |
||
566 | } |
||
567 | |||
568 | 2 | return $sql; |
|
569 | } |
||
570 | |||
571 | /** |
||
572 | * Builds a SQL statement for dropping a foreign key constraint. |
||
573 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
574 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
575 | * @return string the SQL statement for dropping a foreign key constraint. |
||
576 | */ |
||
577 | 1 | public function dropForeignKey($name, $table) |
|
582 | |||
583 | /** |
||
584 | * Builds a SQL statement for creating a new index. |
||
585 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
586 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
587 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
588 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
589 | * by the method, unless a parenthesis is found in the name. |
||
590 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
591 | * @return string the SQL statement for creating a new index. |
||
592 | */ |
||
593 | 1 | public function createIndex($name, $table, $columns, $unique = false) |
|
600 | |||
601 | /** |
||
602 | * Builds a SQL statement for dropping an index. |
||
603 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
604 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
605 | * @return string the SQL statement for dropping an index. |
||
606 | */ |
||
607 | public function dropIndex($name, $table) |
||
611 | |||
612 | /** |
||
613 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
614 | * The sequence will be reset such that the primary key of the next new row inserted |
||
615 | * will have the specified value or 1. |
||
616 | * @param string $table the name of the table whose primary key sequence will be reset |
||
617 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
618 | * the next new row's primary key will have a value 1. |
||
619 | * @return string the SQL statement for resetting sequence |
||
620 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
621 | */ |
||
622 | public function resetSequence($table, $value = null) |
||
626 | |||
627 | /** |
||
628 | * Builds a SQL statement for enabling or disabling integrity check. |
||
629 | * @param bool $check whether to turn on or off the integrity check. |
||
630 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
631 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
632 | * @return string the SQL statement for checking integrity |
||
633 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
634 | */ |
||
635 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
639 | |||
640 | /** |
||
641 | * Builds a SQL command for adding comment to column |
||
642 | * |
||
643 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
644 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
645 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
646 | * @return string the SQL statement for adding comment on column |
||
647 | * @since 2.0.8 |
||
648 | */ |
||
649 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
654 | |||
655 | /** |
||
656 | * Builds a SQL command for adding comment to table |
||
657 | * |
||
658 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
659 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
660 | * @return string the SQL statement for adding comment on table |
||
661 | * @since 2.0.8 |
||
662 | */ |
||
663 | 1 | public function addCommentOnTable($table, $comment) |
|
667 | |||
668 | /** |
||
669 | * Builds a SQL command for adding comment to column |
||
670 | * |
||
671 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
672 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
673 | * @return string the SQL statement for adding comment on column |
||
674 | * @since 2.0.8 |
||
675 | */ |
||
676 | 2 | public function dropCommentFromColumn($table, $column) |
|
680 | |||
681 | /** |
||
682 | * Builds a SQL command for adding comment to table |
||
683 | * |
||
684 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
685 | * @return string the SQL statement for adding comment on column |
||
686 | * @since 2.0.8 |
||
687 | */ |
||
688 | 1 | public function dropCommentFromTable($table) |
|
692 | |||
693 | /** |
||
694 | * Converts an abstract column type into a physical column type. |
||
695 | * The conversion is done using the type map specified in [[typeMap]]. |
||
696 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
697 | * physical types): |
||
698 | * |
||
699 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
700 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
701 | * - `unsignedpk`: an unsigned auto-incremental primary key type, will be converted into "int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
702 | * - `char`: char type, will be converted into "char(1)" |
||
703 | * - `string`: string type, will be converted into "varchar(255)" |
||
704 | * - `text`: a long string type, will be converted into "text" |
||
705 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
706 | * - `integer`: integer type, will be converted into "int(11)" |
||
707 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
708 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
709 | * - `float``: float number type, will be converted into "float" |
||
710 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
711 | * - `datetime`: datetime type, will be converted into "datetime" |
||
712 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
713 | * - `time`: time type, will be converted into "time" |
||
714 | * - `date`: date type, will be converted into "date" |
||
715 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
716 | * - `binary`: binary data type, will be converted into "blob" |
||
717 | * |
||
718 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
719 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
720 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
721 | * |
||
722 | * For some of the abstract types you can also specify a length or precision constraint |
||
723 | * by appending it in round brackets directly to the type. |
||
724 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
725 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
726 | * be ignored. |
||
727 | * |
||
728 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
729 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
730 | * @return string physical column type. |
||
731 | */ |
||
732 | 90 | public function getColumnType($type) |
|
752 | |||
753 | /** |
||
754 | * @param array $columns |
||
755 | * @param array $params the binding parameters to be populated |
||
756 | * @param bool $distinct |
||
757 | * @param string $selectOption |
||
758 | * @return string the SELECT clause built from [[Query::$select]]. |
||
759 | */ |
||
760 | 851 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
798 | |||
799 | /** |
||
800 | * @param array $tables |
||
801 | * @param array $params the binding parameters to be populated |
||
802 | * @return string the FROM clause built from [[Query::$from]]. |
||
803 | */ |
||
804 | 851 | public function buildFrom($tables, &$params) |
|
814 | |||
815 | /** |
||
816 | * @param array $joins |
||
817 | * @param array $params the binding parameters to be populated |
||
818 | * @return string the JOIN clause built from [[Query::$join]]. |
||
819 | * @throws Exception if the $joins parameter is not in proper format |
||
820 | */ |
||
821 | 851 | public function buildJoin($joins, &$params) |
|
846 | |||
847 | /** |
||
848 | * Quotes table names passed |
||
849 | * |
||
850 | * @param array $tables |
||
851 | * @param array $params |
||
852 | * @return array |
||
853 | */ |
||
854 | 602 | private function quoteTableNames($tables, &$params) |
|
875 | |||
876 | /** |
||
877 | * @param string|array $condition |
||
878 | * @param array $params the binding parameters to be populated |
||
879 | * @return string the WHERE clause built from [[Query::$where]]. |
||
880 | */ |
||
881 | 873 | public function buildWhere($condition, &$params) |
|
887 | |||
888 | /** |
||
889 | * @param array $columns |
||
890 | * @return string the GROUP BY clause |
||
891 | */ |
||
892 | 851 | public function buildGroupBy($columns) |
|
906 | |||
907 | /** |
||
908 | * @param string|array $condition |
||
909 | * @param array $params the binding parameters to be populated |
||
910 | * @return string the HAVING clause built from [[Query::$having]]. |
||
911 | */ |
||
912 | 851 | public function buildHaving($condition, &$params) |
|
918 | |||
919 | /** |
||
920 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
921 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
922 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
923 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
924 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
925 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
926 | */ |
||
927 | 851 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
939 | |||
940 | /** |
||
941 | * @param array $columns |
||
942 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
943 | */ |
||
944 | 851 | public function buildOrderBy($columns) |
|
960 | |||
961 | /** |
||
962 | * @param int $limit |
||
963 | * @param int $offset |
||
964 | * @return string the LIMIT and OFFSET clauses |
||
965 | */ |
||
966 | 281 | public function buildLimit($limit, $offset) |
|
978 | |||
979 | /** |
||
980 | * Checks to see if the given limit is effective. |
||
981 | * @param mixed $limit the given limit |
||
982 | * @return bool whether the limit is effective |
||
983 | */ |
||
984 | 552 | protected function hasLimit($limit) |
|
988 | |||
989 | /** |
||
990 | * Checks to see if the given offset is effective. |
||
991 | * @param mixed $offset the given offset |
||
992 | * @return bool whether the offset is effective |
||
993 | */ |
||
994 | 552 | protected function hasOffset($offset) |
|
998 | |||
999 | /** |
||
1000 | * @param array $unions |
||
1001 | * @param array $params the binding parameters to be populated |
||
1002 | * @return string the UNION clause built from [[Query::$union]]. |
||
1003 | */ |
||
1004 | 580 | public function buildUnion($unions, &$params) |
|
1023 | |||
1024 | /** |
||
1025 | * Processes columns and properly quotes them if necessary. |
||
1026 | * It will join all columns into a string with comma as separators. |
||
1027 | * @param string|array $columns the columns to be processed |
||
1028 | * @return string the processing result |
||
1029 | */ |
||
1030 | 11 | public function buildColumns($columns) |
|
1049 | |||
1050 | /** |
||
1051 | * Parses the condition specification and generates the corresponding SQL expression. |
||
1052 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
1053 | * on how to specify a condition. |
||
1054 | * @param array $params the binding parameters to be populated |
||
1055 | * @return string the generated SQL expression |
||
1056 | */ |
||
1057 | 873 | public function buildCondition($condition, &$params) |
|
1083 | |||
1084 | /** |
||
1085 | * Creates a condition based on column-value pairs. |
||
1086 | * @param array $condition the condition specification. |
||
1087 | * @param array $params the binding parameters to be populated |
||
1088 | * @return string the generated SQL expression |
||
1089 | */ |
||
1090 | 465 | public function buildHashCondition($condition, &$params) |
|
1117 | |||
1118 | /** |
||
1119 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1120 | * @param string $operator the operator to use for connecting the given operands |
||
1121 | * @param array $operands the SQL expressions to connect. |
||
1122 | * @param array $params the binding parameters to be populated |
||
1123 | * @return string the generated SQL expression |
||
1124 | */ |
||
1125 | 170 | public function buildAndCondition($operator, $operands, &$params) |
|
1148 | |||
1149 | /** |
||
1150 | * Inverts an SQL expressions with `NOT` operator. |
||
1151 | * @param string $operator the operator to use for connecting the given operands |
||
1152 | * @param array $operands the SQL expressions to connect. |
||
1153 | * @param array $params the binding parameters to be populated |
||
1154 | * @return string the generated SQL expression |
||
1155 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1156 | */ |
||
1157 | 3 | public function buildNotCondition($operator, $operands, &$params) |
|
1173 | |||
1174 | /** |
||
1175 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1176 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1177 | * @param array $operands the first operand is the column name. The second and third operands |
||
1178 | * describe the interval that column value should be in. |
||
1179 | * @param array $params the binding parameters to be populated |
||
1180 | * @return string the generated SQL expression |
||
1181 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1182 | */ |
||
1183 | 21 | public function buildBetweenCondition($operator, $operands, &$params) |
|
1215 | |||
1216 | /** |
||
1217 | * Creates an SQL expressions with the `IN` operator. |
||
1218 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1219 | * @param array $operands the first operand is the column name. If it is an array |
||
1220 | * a composite IN condition will be generated. |
||
1221 | * The second operand is an array of values that column value should be among. |
||
1222 | * If it is an empty array the generated expression will be a `false` value if |
||
1223 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1224 | * @param array $params the binding parameters to be populated |
||
1225 | * @return string the generated SQL expression |
||
1226 | * @throws Exception if wrong number of operands have been given. |
||
1227 | */ |
||
1228 | 224 | public function buildInCondition($operator, $operands, &$params) |
|
1289 | |||
1290 | /** |
||
1291 | * Builds SQL for IN condition |
||
1292 | * |
||
1293 | * @param string $operator |
||
1294 | * @param array $columns |
||
1295 | * @param Query $values |
||
1296 | * @param array $params |
||
1297 | * @return string SQL |
||
1298 | */ |
||
1299 | 14 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
1316 | |||
1317 | /** |
||
1318 | * Builds SQL for IN condition |
||
1319 | * |
||
1320 | * @param string $operator |
||
1321 | * @param array|\Traversable $columns |
||
1322 | * @param array $values |
||
1323 | * @param array $params |
||
1324 | * @return string SQL |
||
1325 | */ |
||
1326 | 10 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
1354 | |||
1355 | /** |
||
1356 | * Creates an SQL expressions with the `LIKE` operator. |
||
1357 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1358 | * @param array $operands an array of two or three operands |
||
1359 | * |
||
1360 | * - The first operand is the column name. |
||
1361 | * - The second operand is a single value or an array of values that column value |
||
1362 | * should be compared with. If it is an empty array the generated expression will |
||
1363 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1364 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1365 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1366 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1367 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1368 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1369 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1370 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1371 | * @param array $params the binding parameters to be populated |
||
1372 | * @return string the generated SQL expression |
||
1373 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1374 | */ |
||
1375 | 75 | public function buildLikeCondition($operator, $operands, &$params) |
|
1425 | |||
1426 | /** |
||
1427 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1428 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1429 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1430 | * @param array $params the binding parameters to be populated |
||
1431 | * @return string the generated SQL expression |
||
1432 | * @throws InvalidParamException if the operand is not a [[Query]] object. |
||
1433 | */ |
||
1434 | 18 | public function buildExistsCondition($operator, $operands, &$params) |
|
1443 | |||
1444 | /** |
||
1445 | * Creates an SQL expressions like `"column" operator value`. |
||
1446 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1447 | * @param array $operands contains two column names. |
||
1448 | * @param array $params the binding parameters to be populated |
||
1449 | * @return string the generated SQL expression |
||
1450 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1451 | */ |
||
1452 | 36 | public function buildSimpleCondition($operator, $operands, &$params) |
|
1480 | |||
1481 | /** |
||
1482 | * Creates a SELECT EXISTS() SQL statement. |
||
1483 | * @param string $rawSql the subquery in a raw form to select from. |
||
1484 | * @return string the SELECT EXISTS() SQL statement. |
||
1485 | * @since 2.0.8 |
||
1486 | */ |
||
1487 | 56 | public function selectExists($rawSql) |
|
1491 | } |
||
1492 |