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 |
||
23 | class QueryBuilder extends \yii\base\Object |
||
24 | { |
||
25 | /** |
||
26 | * The prefix for automatically generated query binding parameters. |
||
27 | */ |
||
28 | const PARAM_PREFIX = ':qp'; |
||
29 | |||
30 | /** |
||
31 | * @var Connection the database connection. |
||
32 | */ |
||
33 | public $db; |
||
34 | /** |
||
35 | * @var string the separator between different fragments of a SQL statement. |
||
36 | * Defaults to an empty space. This is mainly used by [[build()]] when generating a SQL statement. |
||
37 | */ |
||
38 | public $separator = ' '; |
||
39 | /** |
||
40 | * @var array the abstract column types mapped to physical column types. |
||
41 | * This is mainly used to support creating/modifying tables using DB-independent data type specifications. |
||
42 | * Child classes should override this property to declare supported type mappings. |
||
43 | */ |
||
44 | public $typeMap = []; |
||
45 | |||
46 | /** |
||
47 | * @var array map of query condition to builder methods. |
||
48 | * These methods are used by [[buildCondition]] to build SQL conditions from array syntax. |
||
49 | */ |
||
50 | protected $conditionBuilders = [ |
||
51 | 'NOT' => 'buildNotCondition', |
||
52 | 'AND' => 'buildAndCondition', |
||
53 | 'OR' => 'buildAndCondition', |
||
54 | 'BETWEEN' => 'buildBetweenCondition', |
||
55 | 'NOT BETWEEN' => 'buildBetweenCondition', |
||
56 | 'IN' => 'buildInCondition', |
||
57 | 'NOT IN' => 'buildInCondition', |
||
58 | 'LIKE' => 'buildLikeCondition', |
||
59 | 'NOT LIKE' => 'buildLikeCondition', |
||
60 | 'OR LIKE' => 'buildLikeCondition', |
||
61 | 'OR NOT LIKE' => 'buildLikeCondition', |
||
62 | 'EXISTS' => 'buildExistsCondition', |
||
63 | 'NOT EXISTS' => 'buildExistsCondition', |
||
64 | ]; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Constructor. |
||
69 | * @param Connection $connection the database connection. |
||
70 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
71 | */ |
||
72 | 639 | public function __construct($connection, $config = []) |
|
77 | |||
78 | /** |
||
79 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
80 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
81 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
82 | * be included in the result with the additional parameters generated during the query building process. |
||
83 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
84 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
85 | * include those provided in `$params`. |
||
86 | */ |
||
87 | 434 | public function build($query, $params = []) |
|
136 | |||
137 | /** |
||
138 | * Creates an INSERT SQL statement. |
||
139 | * For example, |
||
140 | * |
||
141 | * ```php |
||
142 | * $sql = $queryBuilder->insert('user', [ |
||
143 | * 'name' => 'Sam', |
||
144 | * 'age' => 30, |
||
145 | * ], $params); |
||
146 | * ``` |
||
147 | * |
||
148 | * The method will properly escape the table and column names. |
||
149 | * |
||
150 | * @param string $table the table that new rows will be inserted into. |
||
151 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
152 | * @param array $params the binding parameters that will be generated by this method. |
||
153 | * They should be bound to the DB command later. |
||
154 | * @return string the INSERT SQL |
||
155 | */ |
||
156 | 68 | public function insert($table, $columns, &$params) |
|
184 | |||
185 | /** |
||
186 | * Generates a batch INSERT SQL statement. |
||
187 | * For example, |
||
188 | * |
||
189 | * ```php |
||
190 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
191 | * ['Tom', 30], |
||
192 | * ['Jane', 20], |
||
193 | * ['Linda', 25], |
||
194 | * ]); |
||
195 | * ``` |
||
196 | * |
||
197 | * Note that the values in each row must match the corresponding column names. |
||
198 | * |
||
199 | * The method will properly escape the column names, and quote the values to be inserted. |
||
200 | * |
||
201 | * @param string $table the table that new rows will be inserted into. |
||
202 | * @param array $columns the column names |
||
203 | * @param array $rows the rows to be batch inserted into the table |
||
204 | * @return string the batch INSERT SQL statement |
||
205 | */ |
||
206 | 2 | public function batchInsert($table, $columns, $rows) |
|
241 | |||
242 | /** |
||
243 | * Creates an UPDATE SQL statement. |
||
244 | * For example, |
||
245 | * |
||
246 | * ```php |
||
247 | * $params = []; |
||
248 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
249 | * ``` |
||
250 | * |
||
251 | * The method will properly escape the table and column names. |
||
252 | * |
||
253 | * @param string $table the table to be updated. |
||
254 | * @param array $columns the column data (name => value) to be updated. |
||
255 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
256 | * refer to [[Query::where()]] on how to specify condition. |
||
257 | * @param array $params the binding parameters that will be modified by this method |
||
258 | * so that they can be bound to the DB command later. |
||
259 | * @return string the UPDATE SQL |
||
260 | */ |
||
261 | 61 | public function update($table, $columns, $condition, &$params) |
|
288 | |||
289 | /** |
||
290 | * Creates a DELETE SQL statement. |
||
291 | * For example, |
||
292 | * |
||
293 | * ```php |
||
294 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
295 | * ``` |
||
296 | * |
||
297 | * The method will properly escape the table and column names. |
||
298 | * |
||
299 | * @param string $table the table where the data will be deleted from. |
||
300 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
301 | * refer to [[Query::where()]] on how to specify condition. |
||
302 | * @param array $params the binding parameters that will be modified by this method |
||
303 | * so that they can be bound to the DB command later. |
||
304 | * @return string the DELETE SQL |
||
305 | */ |
||
306 | 95 | public function delete($table, $condition, &$params) |
|
313 | |||
314 | /** |
||
315 | * Builds a SQL statement for creating a new DB table. |
||
316 | * |
||
317 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
318 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
319 | * stands for the column type which can contain an abstract DB type. |
||
320 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
321 | * |
||
322 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
323 | * inserted into the generated SQL. |
||
324 | * |
||
325 | * For example, |
||
326 | * |
||
327 | * ```php |
||
328 | * $sql = $queryBuilder->createTable('user', [ |
||
329 | * 'id' => 'pk', |
||
330 | * 'name' => 'string', |
||
331 | * 'age' => 'integer', |
||
332 | * ]); |
||
333 | * ``` |
||
334 | * |
||
335 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
336 | * @param array $columns the columns (name => definition) in the new table. |
||
337 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
338 | * @return string the SQL statement for creating a new DB table. |
||
339 | */ |
||
340 | 36 | public function createTable($table, $columns, $options = null) |
|
354 | |||
355 | /** |
||
356 | * Builds a SQL statement for renaming a DB table. |
||
357 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
358 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
359 | * @return string the SQL statement for renaming a DB table. |
||
360 | */ |
||
361 | 1 | public function renameTable($oldName, $newName) |
|
365 | |||
366 | /** |
||
367 | * Builds a SQL statement for dropping a DB table. |
||
368 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
369 | * @return string the SQL statement for dropping a DB table. |
||
370 | */ |
||
371 | 7 | public function dropTable($table) |
|
375 | |||
376 | /** |
||
377 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
378 | * @param string $name the name of the primary key constraint. |
||
379 | * @param string $table the table that the primary key constraint will be added to. |
||
380 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
381 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
382 | */ |
||
383 | 2 | public function addPrimaryKey($name, $table, $columns) |
|
397 | |||
398 | /** |
||
399 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
400 | * @param string $name the name of the primary key constraint to be removed. |
||
401 | * @param string $table the table that the primary key constraint will be removed from. |
||
402 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
403 | */ |
||
404 | 1 | public function dropPrimaryKey($name, $table) |
|
409 | |||
410 | /** |
||
411 | * Builds a SQL statement for truncating a DB table. |
||
412 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
413 | * @return string the SQL statement for truncating a DB table. |
||
414 | */ |
||
415 | 5 | public function truncateTable($table) |
|
419 | |||
420 | /** |
||
421 | * Builds a SQL statement for adding a new DB column. |
||
422 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
423 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
424 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
425 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
426 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
427 | * @return string the SQL statement for adding a new column. |
||
428 | */ |
||
429 | 4 | public function addColumn($table, $column, $type) |
|
435 | |||
436 | /** |
||
437 | * Builds a SQL statement for dropping a DB column. |
||
438 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
439 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
440 | * @return string the SQL statement for dropping a DB column. |
||
441 | */ |
||
442 | public function dropColumn($table, $column) |
||
447 | |||
448 | /** |
||
449 | * Builds a SQL statement for renaming a column. |
||
450 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
451 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
452 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
453 | * @return string the SQL statement for renaming a DB column. |
||
454 | */ |
||
455 | public function renameColumn($table, $oldName, $newName) |
||
461 | |||
462 | /** |
||
463 | * Builds a SQL statement for changing the definition of a column. |
||
464 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
465 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
466 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
467 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
468 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
469 | * will become 'varchar(255) not null'. |
||
470 | * @return string the SQL statement for changing the definition of a column. |
||
471 | */ |
||
472 | 1 | public function alterColumn($table, $column, $type) |
|
479 | |||
480 | /** |
||
481 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
482 | * The method will properly quote the table and column names. |
||
483 | * @param string $name the name of the foreign key constraint. |
||
484 | * @param string $table the table that the foreign key constraint will be added to. |
||
485 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
486 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
487 | * @param string $refTable the table that the foreign key references to. |
||
488 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
489 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
490 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
491 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
492 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
493 | */ |
||
494 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
510 | |||
511 | /** |
||
512 | * Builds a SQL statement for dropping a foreign key constraint. |
||
513 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
514 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
515 | * @return string the SQL statement for dropping a foreign key constraint. |
||
516 | */ |
||
517 | public function dropForeignKey($name, $table) |
||
522 | |||
523 | /** |
||
524 | * Builds a SQL statement for creating a new index. |
||
525 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
526 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
527 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
528 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
529 | * by the method, unless a parenthesis is found in the name. |
||
530 | * @param boolean $unique whether to add UNIQUE constraint on the created index. |
||
531 | * @return string the SQL statement for creating a new index. |
||
532 | */ |
||
533 | public function createIndex($name, $table, $columns, $unique = false) |
||
540 | |||
541 | /** |
||
542 | * Builds a SQL statement for dropping an index. |
||
543 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
544 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
545 | * @return string the SQL statement for dropping an index. |
||
546 | */ |
||
547 | public function dropIndex($name, $table) |
||
551 | |||
552 | /** |
||
553 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
554 | * The sequence will be reset such that the primary key of the next new row inserted |
||
555 | * will have the specified value or 1. |
||
556 | * @param string $table the name of the table whose primary key sequence will be reset |
||
557 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
558 | * the next new row's primary key will have a value 1. |
||
559 | * @return string the SQL statement for resetting sequence |
||
560 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
561 | */ |
||
562 | public function resetSequence($table, $value = null) |
||
566 | |||
567 | /** |
||
568 | * Builds a SQL statement for enabling or disabling integrity check. |
||
569 | * @param boolean $check whether to turn on or off the integrity check. |
||
570 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
571 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
572 | * @return string the SQL statement for checking integrity |
||
573 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
574 | */ |
||
575 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
579 | |||
580 | /** |
||
581 | * Converts an abstract column type into a physical column type. |
||
582 | * The conversion is done using the type map specified in [[typeMap]]. |
||
583 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
584 | * physical types): |
||
585 | * |
||
586 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
587 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
588 | * - `string`: string type, will be converted into "varchar(255)" |
||
589 | * - `text`: a long string type, will be converted into "text" |
||
590 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
591 | * - `integer`: integer type, will be converted into "int(11)" |
||
592 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
593 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
594 | * - `float``: float number type, will be converted into "float" |
||
595 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
596 | * - `datetime`: datetime type, will be converted into "datetime" |
||
597 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
598 | * - `time`: time type, will be converted into "time" |
||
599 | * - `date`: date type, will be converted into "date" |
||
600 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
601 | * - `binary`: binary data type, will be converted into "blob" |
||
602 | * |
||
603 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
604 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
605 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
606 | * |
||
607 | * For some of the abstract types you can also specify a length or precision constraint |
||
608 | * by appending it in round brackets directly to the type. |
||
609 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
610 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
611 | * be ignored. |
||
612 | * |
||
613 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
614 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
615 | * @return string physical column type. |
||
616 | */ |
||
617 | 40 | public function getColumnType($type) |
|
637 | |||
638 | /** |
||
639 | * @param array $columns |
||
640 | * @param array $params the binding parameters to be populated |
||
641 | * @param boolean $distinct |
||
642 | * @param string $selectOption |
||
643 | * @return string the SELECT clause built from [[Query::$select]]. |
||
644 | */ |
||
645 | 619 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
683 | |||
684 | /** |
||
685 | * @param array $tables |
||
686 | * @param array $params the binding parameters to be populated |
||
687 | * @return string the FROM clause built from [[Query::$from]]. |
||
688 | */ |
||
689 | 619 | public function buildFrom($tables, &$params) |
|
699 | |||
700 | /** |
||
701 | * @param array $joins |
||
702 | * @param array $params the binding parameters to be populated |
||
703 | * @return string the JOIN clause built from [[Query::$join]]. |
||
704 | * @throws Exception if the $joins parameter is not in proper format |
||
705 | */ |
||
706 | 619 | public function buildJoin($joins, &$params) |
|
731 | |||
732 | /** |
||
733 | * Quotes table names passed |
||
734 | * |
||
735 | * @param array $tables |
||
736 | * @param array $params |
||
737 | * @return array |
||
738 | */ |
||
739 | 397 | private function quoteTableNames($tables, &$params) |
|
760 | |||
761 | /** |
||
762 | * @param string|array $condition |
||
763 | * @param array $params the binding parameters to be populated |
||
764 | * @return string the WHERE clause built from [[Query::$where]]. |
||
765 | */ |
||
766 | 634 | public function buildWhere($condition, &$params) |
|
772 | |||
773 | /** |
||
774 | * @param array $columns |
||
775 | * @return string the GROUP BY clause |
||
776 | */ |
||
777 | 619 | public function buildGroupBy($columns) |
|
791 | |||
792 | /** |
||
793 | * @param string|array $condition |
||
794 | * @param array $params the binding parameters to be populated |
||
795 | * @return string the HAVING clause built from [[Query::$having]]. |
||
796 | */ |
||
797 | 619 | public function buildHaving($condition, &$params) |
|
803 | |||
804 | /** |
||
805 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
806 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
807 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
808 | * @param integer $limit the limit number. See [[Query::limit]] for more details. |
||
809 | * @param integer $offset the offset number. See [[Query::offset]] for more details. |
||
810 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
811 | */ |
||
812 | 619 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
824 | |||
825 | /** |
||
826 | * @param array $columns |
||
827 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
828 | */ |
||
829 | 619 | public function buildOrderBy($columns) |
|
845 | |||
846 | /** |
||
847 | * @param integer $limit |
||
848 | * @param integer $offset |
||
849 | * @return string the LIMIT and OFFSET clauses |
||
850 | */ |
||
851 | 185 | public function buildLimit($limit, $offset) |
|
863 | |||
864 | /** |
||
865 | * Checks to see if the given limit is effective. |
||
866 | * @param mixed $limit the given limit |
||
867 | * @return boolean whether the limit is effective |
||
868 | */ |
||
869 | 619 | protected function hasLimit($limit) |
|
873 | |||
874 | /** |
||
875 | * Checks to see if the given offset is effective. |
||
876 | * @param mixed $offset the given offset |
||
877 | * @return boolean whether the offset is effective |
||
878 | */ |
||
879 | 619 | protected function hasOffset($offset) |
|
884 | |||
885 | /** |
||
886 | * @param array $unions |
||
887 | * @param array $params the binding parameters to be populated |
||
888 | * @return string the UNION clause built from [[Query::$union]]. |
||
889 | */ |
||
890 | 434 | public function buildUnion($unions, &$params) |
|
909 | |||
910 | /** |
||
911 | * Processes columns and properly quotes them if necessary. |
||
912 | * It will join all columns into a string with comma as separators. |
||
913 | * @param string|array $columns the columns to be processed |
||
914 | * @return string the processing result |
||
915 | */ |
||
916 | public function buildColumns($columns) |
||
935 | |||
936 | /** |
||
937 | * Parses the condition specification and generates the corresponding SQL expression. |
||
938 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
939 | * on how to specify a condition. |
||
940 | * @param array $params the binding parameters to be populated |
||
941 | * @return string the generated SQL expression |
||
942 | */ |
||
943 | 634 | public function buildCondition($condition, &$params) |
|
969 | |||
970 | /** |
||
971 | * Creates a condition based on column-value pairs. |
||
972 | * @param array $condition the condition specification. |
||
973 | * @param array $params the binding parameters to be populated |
||
974 | * @return string the generated SQL expression |
||
975 | */ |
||
976 | 304 | public function buildHashCondition($condition, &$params) |
|
1003 | |||
1004 | /** |
||
1005 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1006 | * @param string $operator the operator to use for connecting the given operands |
||
1007 | * @param array $operands the SQL expressions to connect. |
||
1008 | * @param array $params the binding parameters to be populated |
||
1009 | * @return string the generated SQL expression |
||
1010 | */ |
||
1011 | 95 | public function buildAndCondition($operator, $operands, &$params) |
|
1028 | |||
1029 | /** |
||
1030 | * Inverts an SQL expressions with `NOT` operator. |
||
1031 | * @param string $operator the operator to use for connecting the given operands |
||
1032 | * @param array $operands the SQL expressions to connect. |
||
1033 | * @param array $params the binding parameters to be populated |
||
1034 | * @return string the generated SQL expression |
||
1035 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1036 | */ |
||
1037 | 3 | public function buildNotCondition($operator, $operands, &$params) |
|
1053 | |||
1054 | /** |
||
1055 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1056 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1057 | * @param array $operands the first operand is the column name. The second and third operands |
||
1058 | * describe the interval that column value should be in. |
||
1059 | * @param array $params the binding parameters to be populated |
||
1060 | * @return string the generated SQL expression |
||
1061 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1062 | */ |
||
1063 | 21 | public function buildBetweenCondition($operator, $operands, &$params) |
|
1095 | |||
1096 | /** |
||
1097 | * Creates an SQL expressions with the `IN` operator. |
||
1098 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1099 | * @param array $operands the first operand is the column name. If it is an array |
||
1100 | * a composite IN condition will be generated. |
||
1101 | * The second operand is an array of values that column value should be among. |
||
1102 | * If it is an empty array the generated expression will be a `false` value if |
||
1103 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1104 | * @param array $params the binding parameters to be populated |
||
1105 | * @return string the generated SQL expression |
||
1106 | * @throws Exception if wrong number of operands have been given. |
||
1107 | */ |
||
1108 | 153 | public function buildInCondition($operator, $operands, &$params) |
|
1161 | |||
1162 | /** |
||
1163 | * Builds SQL for IN condition |
||
1164 | * |
||
1165 | * @param string $operator |
||
1166 | * @param array $columns |
||
1167 | * @param Query $values |
||
1168 | * @param array $params |
||
1169 | * @return string SQL |
||
1170 | */ |
||
1171 | 14 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
1188 | |||
1189 | /** |
||
1190 | * Builds SQL for IN condition |
||
1191 | * |
||
1192 | * @param string $operator |
||
1193 | * @param array $columns |
||
1194 | * @param array $values |
||
1195 | * @param array $params |
||
1196 | * @return string SQL |
||
1197 | */ |
||
1198 | 6 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
1222 | |||
1223 | /** |
||
1224 | * Creates an SQL expressions with the `LIKE` operator. |
||
1225 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1226 | * @param array $operands an array of two or three operands |
||
1227 | * |
||
1228 | * - The first operand is the column name. |
||
1229 | * - The second operand is a single value or an array of values that column value |
||
1230 | * should be compared with. If it is an empty array the generated expression will |
||
1231 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1232 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1233 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1234 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1235 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1236 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1237 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1238 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1239 | * @param array $params the binding parameters to be populated |
||
1240 | * @return string the generated SQL expression |
||
1241 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1242 | */ |
||
1243 | 72 | public function buildLikeCondition($operator, $operands, &$params) |
|
1289 | |||
1290 | /** |
||
1291 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1292 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1293 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1294 | * @param array $params the binding parameters to be populated |
||
1295 | * @return string the generated SQL expression |
||
1296 | * @throws InvalidParamException if the operand is not a [[Query]] object. |
||
1297 | */ |
||
1298 | 18 | public function buildExistsCondition($operator, $operands, &$params) |
|
1307 | |||
1308 | /** |
||
1309 | * Creates an SQL expressions like `"column" operator value`. |
||
1310 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1311 | * @param array $operands contains two column names. |
||
1312 | * @param array $params the binding parameters to be populated |
||
1313 | * @return string the generated SQL expression |
||
1314 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1315 | */ |
||
1316 | 30 | public function buildSimpleCondition($operator, $operands, &$params) |
|
1344 | } |
||
1345 |