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 |
||
28 | class QueryBuilder extends \yii\base\BaseObject |
||
29 | { |
||
30 | /** |
||
31 | * The prefix for automatically generated query binding parameters. |
||
32 | */ |
||
33 | const PARAM_PREFIX = ':qp'; |
||
34 | |||
35 | /** |
||
36 | * @var Connection the database connection. |
||
37 | */ |
||
38 | public $db; |
||
39 | /** |
||
40 | * @var string the separator between different fragments of a SQL statement. |
||
41 | * Defaults to an empty space. This is mainly used by [[build()]] when generating a SQL statement. |
||
42 | */ |
||
43 | public $separator = ' '; |
||
44 | /** |
||
45 | * @var array the abstract column types mapped to physical column types. |
||
46 | * This is mainly used to support creating/modifying tables using DB-independent data type specifications. |
||
47 | * Child classes should override this property to declare supported type mappings. |
||
48 | */ |
||
49 | public $typeMap = []; |
||
50 | |||
51 | /** |
||
52 | * @var array map of query condition to builder methods. |
||
53 | * These methods are used by [[buildCondition]] to build SQL conditions from array syntax. |
||
54 | * @deprecated since 2.0.14. Is not used, will be dropped in 2.1.0 |
||
55 | */ |
||
56 | protected $conditionBuilders = []; |
||
57 | |||
58 | /** |
||
59 | * @var array map of condition aliases to condition classes. For example: |
||
60 | * |
||
61 | * ```php |
||
62 | * return [ |
||
63 | * 'LIKE' => yii\db\condition\LikeCondition::class, |
||
64 | * ]; |
||
65 | * ``` |
||
66 | * |
||
67 | * This property is used by [[createConditionFromArray]] method. |
||
68 | * See default condition classes list in [[defaultConditionClasses()]] method. |
||
69 | * |
||
70 | * In case you want to add custom conditions support, use the [[setConditionClasses()]] method. |
||
71 | * |
||
72 | * @see setConditonClasses() |
||
73 | * @see defaultConditionClasses() |
||
74 | * @since 2.0.14 |
||
75 | */ |
||
76 | protected $conditionClasses = []; |
||
77 | |||
78 | /** |
||
79 | * @var string[]|ExpressionBuilderInterface[] maps expression class to expression builder class. |
||
80 | * For example: |
||
81 | * |
||
82 | * ```php |
||
83 | * [ |
||
84 | * yii\db\Expression::class => yii\db\ExpressionBuilder::class |
||
85 | * ] |
||
86 | * ``` |
||
87 | * This property is mainly used by [[buildExpression()]] to build SQL expressions form expression objects. |
||
88 | * See default values in [[defaultExpressionBuilders()]] method. |
||
89 | * |
||
90 | * |
||
91 | * To override existing builders or add custom, use [[setExpressionBuilder()]] method. New items will be added |
||
92 | * to the end of this array. |
||
93 | * |
||
94 | * To find a builder, [[buildExpression()]] will check the expression class for its exact presence in this map. |
||
95 | * In case it is NOT present, the array will be iterated in reverse direction, checking whether the expression |
||
96 | * extends the class, defined in this map. |
||
97 | * |
||
98 | * @see setExpressionBuilders() |
||
99 | * @see defaultExpressionBuilders() |
||
100 | * @since 2.0.14 |
||
101 | */ |
||
102 | protected $expressionBuilders = []; |
||
103 | |||
104 | /** |
||
105 | * Constructor. |
||
106 | * @param Connection $connection the database connection. |
||
107 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
108 | */ |
||
109 | 1403 | public function __construct($connection, $config = []) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 1403 | public function init() |
|
125 | |||
126 | /** |
||
127 | * Contains array of default condition classes. Extend this method, if you want to change |
||
128 | * default condition classes for the query builder. See [[conditionClasses]] docs for details. |
||
129 | * |
||
130 | * @return array |
||
131 | * @see conditionClasses |
||
132 | * @since 2.0.14 |
||
133 | */ |
||
134 | 1403 | protected function defaultConditionClasses() |
|
152 | |||
153 | /** |
||
154 | * Contains array of default expression builders. Extend this method and override it, if you want to change |
||
155 | * default expression builders for this query builder. See [[expressionBuilders]] docs for details. |
||
156 | * |
||
157 | * @return array |
||
158 | * @see $expressionBuilders |
||
159 | * @since 2.0.14 |
||
160 | */ |
||
161 | 1403 | protected function defaultExpressionBuilders() |
|
180 | |||
181 | /** |
||
182 | * Setter for [[expressionBuilders]] property. |
||
183 | * |
||
184 | * @param string[] $builders array of builder that should be merged with [[expressionBuilders]] |
||
185 | * @since 2.0.14 |
||
186 | * @see expressionBuilders |
||
187 | */ |
||
188 | public function setExpressionBuilders($builders) |
||
192 | |||
193 | /** |
||
194 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
195 | * |
||
196 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
197 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
198 | * be included in the result with the additional parameters generated during the query building process. |
||
199 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
200 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
201 | * include those provided in `$params`. |
||
202 | */ |
||
203 | 804 | public function build($query, $params = []) |
|
243 | |||
244 | /** |
||
245 | * Builds given $expression |
||
246 | * |
||
247 | * @param ExpressionInterface $expression the expression to be built |
||
248 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
249 | * be included in the result with the additional parameters generated during the expression building process. |
||
250 | * @return string the SQL statement that will not be neither quoted nor encoded before passing to DBMS |
||
251 | * @see ExpressionInterface |
||
252 | * @see ExpressionBuilderInterface |
||
253 | * @see expressionBuilders |
||
254 | * @since 2.0.14 |
||
255 | * @throws InvalidArgumentException when $expression building is not supported by this QueryBuilder. |
||
256 | */ |
||
257 | 1029 | public function buildExpression(ExpressionInterface $expression, &$params = []) |
|
263 | |||
264 | /** |
||
265 | * Gets object of [[ExpressionBuilderInterface]] that is suitable for $expression. |
||
266 | * Uses [[expressionBuilders]] array to find a suitable builder class. |
||
267 | * |
||
268 | * @param ExpressionInterface $expression |
||
269 | * @return ExpressionBuilderInterface |
||
270 | * @see expressionBuilders |
||
271 | * @since 2.0.14 |
||
272 | * @throws InvalidArgumentException when $expression building is not supported by this QueryBuilder. |
||
273 | */ |
||
274 | 1029 | public function getExpressionBuilder(ExpressionInterface $expression) |
|
301 | |||
302 | /** |
||
303 | * Creates an INSERT SQL statement. |
||
304 | * For example, |
||
305 | * ```php |
||
306 | * $sql = $queryBuilder->insert('user', [ |
||
307 | * 'name' => 'Sam', |
||
308 | * 'age' => 30, |
||
309 | * ], $params); |
||
310 | * ``` |
||
311 | * The method will properly escape the table and column names. |
||
312 | * |
||
313 | * @param string $table the table that new rows will be inserted into. |
||
314 | * @param array|Query $columns the column data (name => value) to be inserted into the table or instance |
||
315 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
316 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
317 | * @param array $params the binding parameters that will be generated by this method. |
||
318 | * They should be bound to the DB command later. |
||
319 | * @return string the INSERT SQL |
||
320 | */ |
||
321 | 530 | public function insert($table, $columns, &$params) |
|
328 | |||
329 | /** |
||
330 | * Prepares a `VALUES` part for an `INSERT` SQL statement. |
||
331 | * |
||
332 | * @param string $table the table that new rows will be inserted into. |
||
333 | * @param array|Query $columns the column data (name => value) to be inserted into the table or instance |
||
334 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
335 | * @param array $params the binding parameters that will be generated by this method. |
||
336 | * They should be bound to the DB command later. |
||
337 | * @return array array of column names, placeholders, values and params. |
||
338 | * @since 2.0.14 |
||
339 | */ |
||
340 | 544 | protected function prepareInsertValues($table, $columns, $params = []) |
|
367 | |||
368 | /** |
||
369 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
370 | * |
||
371 | * @param Query $columns Object, which represents select query. |
||
372 | * @param \yii\db\Schema $schema Schema object to quote column name. |
||
373 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
374 | * be included in the result with the additional parameters generated during the query building process. |
||
375 | * @return array array of column names, values and params. |
||
376 | * @throws InvalidArgumentException if query's select does not contain named parameters only. |
||
377 | * @since 2.0.11 |
||
378 | */ |
||
379 | 42 | protected function prepareInsertSelectSubQuery($columns, $schema, $params = []) |
|
400 | |||
401 | /** |
||
402 | * Generates a batch INSERT SQL statement. |
||
403 | * |
||
404 | * For example, |
||
405 | * |
||
406 | * ```php |
||
407 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
408 | * ['Tom', 30], |
||
409 | * ['Jane', 20], |
||
410 | * ['Linda', 25], |
||
411 | * ]); |
||
412 | * ``` |
||
413 | * |
||
414 | * Note that the values in each row must match the corresponding column names. |
||
415 | * |
||
416 | * The method will properly escape the column names, and quote the values to be inserted. |
||
417 | * |
||
418 | * @param string $table the table that new rows will be inserted into. |
||
419 | * @param array $columns the column names |
||
420 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
421 | * @return string the batch INSERT SQL statement |
||
422 | */ |
||
423 | 26 | public function batchInsert($table, $columns, $rows) |
|
468 | |||
469 | /** |
||
470 | * Creates an SQL statement to insert rows into a database table if |
||
471 | * they do not already exist (matching unique constraints), |
||
472 | * or update them if they do. |
||
473 | * |
||
474 | * For example, |
||
475 | * |
||
476 | * ```php |
||
477 | * $sql = $queryBuilder->upsert('pages', [ |
||
478 | * 'name' => 'Front page', |
||
479 | * 'url' => 'http://example.com/', // url is unique |
||
480 | * 'visits' => 0, |
||
481 | * ], [ |
||
482 | * 'visits' => new \yii\db\Expression('visits + 1'), |
||
483 | * ], $params); |
||
484 | * ``` |
||
485 | * |
||
486 | * The method will properly escape the table and column names. |
||
487 | * |
||
488 | * @param string $table the table that new rows will be inserted into/updated in. |
||
489 | * @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance |
||
490 | * of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement. |
||
491 | * @param array|bool $updateColumns the column data (name => value) to be updated if they already exist. |
||
492 | * If `true` is passed, the column data will be updated to match the insert column data. |
||
493 | * If `false` is passed, no update will be performed if the column data already exists. |
||
494 | * @param array $params the binding parameters that will be generated by this method. |
||
495 | * They should be bound to the DB command later. |
||
496 | * @return string the resulting SQL. |
||
497 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
498 | * @since 2.0.14 |
||
499 | */ |
||
500 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
||
504 | |||
505 | /** |
||
506 | * @param string $table |
||
507 | * @param array|Query $insertColumns |
||
508 | * @param array|bool $updateColumns |
||
509 | * @param Constraint[] $constraints this parameter recieves a matched constraint list. |
||
510 | * The constraints will be unique by their column names. |
||
511 | * @return array |
||
512 | * @since 2.0.14 |
||
513 | */ |
||
514 | 66 | protected function prepareUpsertColumns($table, $insertColumns, $updateColumns, &$constraints = []) |
|
529 | |||
530 | /** |
||
531 | * Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) |
||
532 | * for the named table removing constraints which did not cover the specified column list. |
||
533 | * The column list will be unique by column names. |
||
534 | * |
||
535 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
536 | * @param string[] $columns source column list. |
||
537 | * @param Constraint[] $constraints this parameter optionally recieves a matched constraint list. |
||
538 | * The constraints will be unique by their column names. |
||
539 | * @return string[] column list. |
||
540 | */ |
||
541 | 66 | private function getTableUniqueColumnNames($name, $columns, &$constraints = []) |
|
577 | |||
578 | /** |
||
579 | * Creates an UPDATE SQL statement. |
||
580 | * |
||
581 | * For example, |
||
582 | * |
||
583 | * ```php |
||
584 | * $params = []; |
||
585 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
586 | * ``` |
||
587 | * |
||
588 | * The method will properly escape the table and column names. |
||
589 | * |
||
590 | * @param string $table the table to be updated. |
||
591 | * @param array $columns the column data (name => value) to be updated. |
||
592 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
593 | * refer to [[Query::where()]] on how to specify condition. |
||
594 | * @param array $params the binding parameters that will be modified by this method |
||
595 | * so that they can be bound to the DB command later. |
||
596 | * @return string the UPDATE SQL |
||
597 | */ |
||
598 | 126 | public function update($table, $columns, $condition, &$params) |
|
605 | |||
606 | /** |
||
607 | * Prepares a `SET` parts for an `UPDATE` SQL statement. |
||
608 | * @param string $table the table to be updated. |
||
609 | * @param array $columns the column data (name => value) to be updated. |
||
610 | * @param array $params the binding parameters that will be modified by this method |
||
611 | * so that they can be bound to the DB command later. |
||
612 | * @return array an array `SET` parts for an `UPDATE` SQL statement (the first array element) and params (the second array element). |
||
613 | * @since 2.0.14 |
||
614 | */ |
||
615 | 158 | protected function prepareUpdateSets($table, $columns, $params = []) |
|
633 | |||
634 | /** |
||
635 | * Creates a DELETE SQL statement. |
||
636 | * |
||
637 | * For example, |
||
638 | * |
||
639 | * ```php |
||
640 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
641 | * ``` |
||
642 | * |
||
643 | * The method will properly escape the table and column names. |
||
644 | * |
||
645 | * @param string $table the table where the data will be deleted from. |
||
646 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
647 | * refer to [[Query::where()]] on how to specify condition. |
||
648 | * @param array $params the binding parameters that will be modified by this method |
||
649 | * so that they can be bound to the DB command later. |
||
650 | * @return string the DELETE SQL |
||
651 | */ |
||
652 | 353 | public function delete($table, $condition, &$params) |
|
659 | |||
660 | /** |
||
661 | * Builds a SQL statement for creating a new DB table. |
||
662 | * |
||
663 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
664 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
665 | * stands for the column type which can contain an abstract DB type. |
||
666 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
667 | * |
||
668 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
669 | * inserted into the generated SQL. |
||
670 | * |
||
671 | * For example, |
||
672 | * |
||
673 | * ```php |
||
674 | * $sql = $queryBuilder->createTable('user', [ |
||
675 | * 'id' => 'pk', |
||
676 | * 'name' => 'string', |
||
677 | * 'age' => 'integer', |
||
678 | * ]); |
||
679 | * ``` |
||
680 | * |
||
681 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
682 | * @param array $columns the columns (name => definition) in the new table. |
||
683 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
684 | * @return string the SQL statement for creating a new DB table. |
||
685 | */ |
||
686 | 134 | public function createTable($table, $columns, $options = null) |
|
700 | |||
701 | /** |
||
702 | * Builds a SQL statement for renaming a DB table. |
||
703 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
704 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
705 | * @return string the SQL statement for renaming a DB table. |
||
706 | */ |
||
707 | 1 | public function renameTable($oldName, $newName) |
|
711 | |||
712 | /** |
||
713 | * Builds a SQL statement for dropping a DB table. |
||
714 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
715 | * @return string the SQL statement for dropping a DB table. |
||
716 | */ |
||
717 | 39 | public function dropTable($table) |
|
721 | |||
722 | /** |
||
723 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
724 | * @param string $name the name of the primary key constraint. |
||
725 | * @param string $table the table that the primary key constraint will be added to. |
||
726 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
727 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
728 | */ |
||
729 | 6 | public function addPrimaryKey($name, $table, $columns) |
|
743 | |||
744 | /** |
||
745 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
746 | * @param string $name the name of the primary key constraint to be removed. |
||
747 | * @param string $table the table that the primary key constraint will be removed from. |
||
748 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
749 | */ |
||
750 | 2 | public function dropPrimaryKey($name, $table) |
|
755 | |||
756 | /** |
||
757 | * Builds a SQL statement for truncating a DB table. |
||
758 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
759 | * @return string the SQL statement for truncating a DB table. |
||
760 | */ |
||
761 | 11 | public function truncateTable($table) |
|
765 | |||
766 | /** |
||
767 | * Builds a SQL statement for adding a new DB column. |
||
768 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
769 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
770 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
771 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
772 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
773 | * @return string the SQL statement for adding a new column. |
||
774 | */ |
||
775 | 4 | public function addColumn($table, $column, $type) |
|
781 | |||
782 | /** |
||
783 | * Builds a SQL statement for dropping a DB column. |
||
784 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
785 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
786 | * @return string the SQL statement for dropping a DB column. |
||
787 | */ |
||
788 | public function dropColumn($table, $column) |
||
793 | |||
794 | /** |
||
795 | * Builds a SQL statement for renaming a column. |
||
796 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
797 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
798 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
799 | * @return string the SQL statement for renaming a DB column. |
||
800 | */ |
||
801 | public function renameColumn($table, $oldName, $newName) |
||
807 | |||
808 | /** |
||
809 | * Builds a SQL statement for changing the definition of a column. |
||
810 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
811 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
812 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
813 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
814 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
815 | * will become 'varchar(255) not null'. |
||
816 | * @return string the SQL statement for changing the definition of a column. |
||
817 | */ |
||
818 | 1 | public function alterColumn($table, $column, $type) |
|
825 | |||
826 | /** |
||
827 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
828 | * The method will properly quote the table and column names. |
||
829 | * @param string $name the name of the foreign key constraint. |
||
830 | * @param string $table the table that the foreign key constraint will be added to. |
||
831 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
832 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
833 | * @param string $refTable the table that the foreign key references to. |
||
834 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
835 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
836 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
837 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
838 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
839 | */ |
||
840 | 8 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
856 | |||
857 | /** |
||
858 | * Builds a SQL statement for dropping a foreign key constraint. |
||
859 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
860 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
861 | * @return string the SQL statement for dropping a foreign key constraint. |
||
862 | */ |
||
863 | 3 | public function dropForeignKey($name, $table) |
|
868 | |||
869 | /** |
||
870 | * Builds a SQL statement for creating a new index. |
||
871 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
872 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
873 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
874 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
875 | * by the method, unless a parenthesis is found in the name. |
||
876 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
877 | * @return string the SQL statement for creating a new index. |
||
878 | */ |
||
879 | 6 | public function createIndex($name, $table, $columns, $unique = false) |
|
886 | |||
887 | /** |
||
888 | * Builds a SQL statement for dropping an index. |
||
889 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
890 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
891 | * @return string the SQL statement for dropping an index. |
||
892 | */ |
||
893 | 4 | public function dropIndex($name, $table) |
|
897 | |||
898 | /** |
||
899 | * Creates a SQL command for adding an unique constraint to an existing table. |
||
900 | * @param string $name the name of the unique constraint. |
||
901 | * The name will be properly quoted by the method. |
||
902 | * @param string $table the table that the unique constraint will be added to. |
||
903 | * The name will be properly quoted by the method. |
||
904 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
905 | * If there are multiple columns, separate them with commas. |
||
906 | * The name will be properly quoted by the method. |
||
907 | * @return string the SQL statement for adding an unique constraint to an existing table. |
||
908 | * @since 2.0.13 |
||
909 | */ |
||
910 | 6 | public function addUnique($name, $table, $columns) |
|
923 | |||
924 | /** |
||
925 | * Creates a SQL command for dropping an unique constraint. |
||
926 | * @param string $name the name of the unique constraint to be dropped. |
||
927 | * The name will be properly quoted by the method. |
||
928 | * @param string $table the table whose unique constraint is to be dropped. |
||
929 | * The name will be properly quoted by the method. |
||
930 | * @return string the SQL statement for dropping an unique constraint. |
||
931 | * @since 2.0.13 |
||
932 | */ |
||
933 | 2 | public function dropUnique($name, $table) |
|
938 | |||
939 | /** |
||
940 | * Creates a SQL command for adding a check constraint to an existing table. |
||
941 | * @param string $name the name of the check constraint. |
||
942 | * The name will be properly quoted by the method. |
||
943 | * @param string $table the table that the check constraint will be added to. |
||
944 | * The name will be properly quoted by the method. |
||
945 | * @param string $expression the SQL of the `CHECK` constraint. |
||
946 | * @return string the SQL statement for adding a check constraint to an existing table. |
||
947 | * @since 2.0.13 |
||
948 | */ |
||
949 | 2 | public function addCheck($name, $table, $expression) |
|
954 | |||
955 | /** |
||
956 | * Creates a SQL command for dropping a check constraint. |
||
957 | * @param string $name the name of the check constraint to be dropped. |
||
958 | * The name will be properly quoted by the method. |
||
959 | * @param string $table the table whose check constraint is to be dropped. |
||
960 | * The name will be properly quoted by the method. |
||
961 | * @return string the SQL statement for dropping a check constraint. |
||
962 | * @since 2.0.13 |
||
963 | */ |
||
964 | 2 | public function dropCheck($name, $table) |
|
969 | |||
970 | /** |
||
971 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
972 | * @param string $name the name of the default value constraint. |
||
973 | * The name will be properly quoted by the method. |
||
974 | * @param string $table the table that the default value constraint will be added to. |
||
975 | * The name will be properly quoted by the method. |
||
976 | * @param string $column the name of the column to that the constraint will be added on. |
||
977 | * The name will be properly quoted by the method. |
||
978 | * @param mixed $value default value. |
||
979 | * @return string the SQL statement for adding a default value constraint to an existing table. |
||
980 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
981 | * @since 2.0.13 |
||
982 | */ |
||
983 | public function addDefaultValue($name, $table, $column, $value) |
||
987 | |||
988 | /** |
||
989 | * Creates a SQL command for dropping a default value constraint. |
||
990 | * @param string $name the name of the default value constraint to be dropped. |
||
991 | * The name will be properly quoted by the method. |
||
992 | * @param string $table the table whose default value constraint is to be dropped. |
||
993 | * The name will be properly quoted by the method. |
||
994 | * @return string the SQL statement for dropping a default value constraint. |
||
995 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
996 | * @since 2.0.13 |
||
997 | */ |
||
998 | public function dropDefaultValue($name, $table) |
||
1002 | |||
1003 | /** |
||
1004 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
1005 | * The sequence will be reset such that the primary key of the next new row inserted |
||
1006 | * will have the specified value or 1. |
||
1007 | * @param string $table the name of the table whose primary key sequence will be reset |
||
1008 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
1009 | * the next new row's primary key will have a value 1. |
||
1010 | * @return string the SQL statement for resetting sequence |
||
1011 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
1012 | */ |
||
1013 | public function resetSequence($table, $value = null) |
||
1017 | |||
1018 | /** |
||
1019 | * Builds a SQL statement for enabling or disabling integrity check. |
||
1020 | * @param bool $check whether to turn on or off the integrity check. |
||
1021 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
1022 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
1023 | * @return string the SQL statement for checking integrity |
||
1024 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
1025 | */ |
||
1026 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
1030 | |||
1031 | /** |
||
1032 | * Builds a SQL command for adding comment to column. |
||
1033 | * |
||
1034 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1035 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
1036 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
1037 | * @return string the SQL statement for adding comment on column |
||
1038 | * @since 2.0.8 |
||
1039 | */ |
||
1040 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
1044 | |||
1045 | /** |
||
1046 | * Builds a SQL command for adding comment to table. |
||
1047 | * |
||
1048 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1049 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
1050 | * @return string the SQL statement for adding comment on table |
||
1051 | * @since 2.0.8 |
||
1052 | */ |
||
1053 | 1 | public function addCommentOnTable($table, $comment) |
|
1057 | |||
1058 | /** |
||
1059 | * Builds a SQL command for adding comment to column. |
||
1060 | * |
||
1061 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1062 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
1063 | * @return string the SQL statement for adding comment on column |
||
1064 | * @since 2.0.8 |
||
1065 | */ |
||
1066 | 2 | public function dropCommentFromColumn($table, $column) |
|
1070 | |||
1071 | /** |
||
1072 | * Builds a SQL command for adding comment to table. |
||
1073 | * |
||
1074 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1075 | * @return string the SQL statement for adding comment on column |
||
1076 | * @since 2.0.8 |
||
1077 | */ |
||
1078 | 1 | public function dropCommentFromTable($table) |
|
1082 | |||
1083 | /** |
||
1084 | * Creates a SQL View. |
||
1085 | * |
||
1086 | * @param string $viewName the name of the view to be created. |
||
1087 | * @param string|Query $subQuery the select statement which defines the view. |
||
1088 | * This can be either a string or a [[Query]] object. |
||
1089 | * @return string the `CREATE VIEW` SQL statement. |
||
1090 | * @since 2.0.14 |
||
1091 | */ |
||
1092 | 3 | public function createView($viewName, $subQuery) |
|
1107 | |||
1108 | /** |
||
1109 | * Drops a SQL View. |
||
1110 | * |
||
1111 | * @param string $viewName the name of the view to be dropped. |
||
1112 | * @return string the `DROP VIEW` SQL statement. |
||
1113 | * @since 2.0.14 |
||
1114 | */ |
||
1115 | 3 | public function dropView($viewName) |
|
1119 | |||
1120 | /** |
||
1121 | * Converts an abstract column type into a physical column type. |
||
1122 | * |
||
1123 | * The conversion is done using the type map specified in [[typeMap]]. |
||
1124 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
1125 | * physical types): |
||
1126 | * |
||
1127 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
1128 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
1129 | * - `upk`: an unsigned auto-incremental primary key type, will be converted into "int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
1130 | * - `char`: char type, will be converted into "char(1)" |
||
1131 | * - `string`: string type, will be converted into "varchar(255)" |
||
1132 | * - `text`: a long string type, will be converted into "text" |
||
1133 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
1134 | * - `integer`: integer type, will be converted into "int(11)" |
||
1135 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
1136 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
1137 | * - `float``: float number type, will be converted into "float" |
||
1138 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
1139 | * - `datetime`: datetime type, will be converted into "datetime" |
||
1140 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
1141 | * - `time`: time type, will be converted into "time" |
||
1142 | * - `date`: date type, will be converted into "date" |
||
1143 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
1144 | * - `binary`: binary data type, will be converted into "blob" |
||
1145 | * |
||
1146 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
1147 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
1148 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
1149 | * |
||
1150 | * For some of the abstract types you can also specify a length or precision constraint |
||
1151 | * by appending it in round brackets directly to the type. |
||
1152 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
1153 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
1154 | * be ignored. |
||
1155 | * |
||
1156 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
1157 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
1158 | * @return string physical column type. |
||
1159 | */ |
||
1160 | 138 | public function getColumnType($type) |
|
1180 | |||
1181 | /** |
||
1182 | * @param array $columns |
||
1183 | * @param array $params the binding parameters to be populated |
||
1184 | * @param bool $distinct |
||
1185 | * @param string $selectOption |
||
1186 | * @return string the SELECT clause built from [[Query::$select]]. |
||
1187 | */ |
||
1188 | 1138 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
1225 | |||
1226 | /** |
||
1227 | * @param array $tables |
||
1228 | * @param array $params the binding parameters to be populated |
||
1229 | * @return string the FROM clause built from [[Query::$from]]. |
||
1230 | */ |
||
1231 | 1138 | public function buildFrom($tables, &$params) |
|
1241 | |||
1242 | /** |
||
1243 | * @param array $joins |
||
1244 | * @param array $params the binding parameters to be populated |
||
1245 | * @return string the JOIN clause built from [[Query::$join]]. |
||
1246 | * @throws Exception if the $joins parameter is not in proper format |
||
1247 | */ |
||
1248 | 1138 | public function buildJoin($joins, &$params) |
|
1273 | |||
1274 | /** |
||
1275 | * Quotes table names passed. |
||
1276 | * |
||
1277 | * @param array $tables |
||
1278 | * @param array $params |
||
1279 | * @return array |
||
1280 | */ |
||
1281 | 826 | private function quoteTableNames($tables, &$params) |
|
1303 | |||
1304 | /** |
||
1305 | * @param string|array $condition |
||
1306 | * @param array $params the binding parameters to be populated |
||
1307 | * @return string the WHERE clause built from [[Query::$where]]. |
||
1308 | */ |
||
1309 | 1215 | public function buildWhere($condition, &$params) |
|
1315 | |||
1316 | /** |
||
1317 | * @param array $columns |
||
1318 | * @return string the GROUP BY clause |
||
1319 | */ |
||
1320 | 1138 | public function buildGroupBy($columns) |
|
1335 | |||
1336 | /** |
||
1337 | * @param string|array $condition |
||
1338 | * @param array $params the binding parameters to be populated |
||
1339 | * @return string the HAVING clause built from [[Query::$having]]. |
||
1340 | */ |
||
1341 | 1138 | public function buildHaving($condition, &$params) |
|
1347 | |||
1348 | /** |
||
1349 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
1350 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
1351 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
1352 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
1353 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
1354 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
1355 | */ |
||
1356 | 1138 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
1369 | |||
1370 | /** |
||
1371 | * @param array $columns |
||
1372 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
1373 | */ |
||
1374 | 1138 | public function buildOrderBy($columns) |
|
1390 | |||
1391 | /** |
||
1392 | * @param int $limit |
||
1393 | * @param int $offset |
||
1394 | * @return string the LIMIT and OFFSET clauses |
||
1395 | */ |
||
1396 | 413 | public function buildLimit($limit, $offset) |
|
1408 | |||
1409 | /** |
||
1410 | * Checks to see if the given limit is effective. |
||
1411 | * @param mixed $limit the given limit |
||
1412 | * @return bool whether the limit is effective |
||
1413 | */ |
||
1414 | 747 | protected function hasLimit($limit) |
|
1418 | |||
1419 | /** |
||
1420 | * Checks to see if the given offset is effective. |
||
1421 | * @param mixed $offset the given offset |
||
1422 | * @return bool whether the offset is effective |
||
1423 | */ |
||
1424 | 747 | protected function hasOffset($offset) |
|
1428 | |||
1429 | /** |
||
1430 | * @param array $unions |
||
1431 | * @param array $params the binding parameters to be populated |
||
1432 | * @return string the UNION clause built from [[Query::$union]]. |
||
1433 | */ |
||
1434 | 804 | public function buildUnion($unions, &$params) |
|
1453 | |||
1454 | /** |
||
1455 | * Processes columns and properly quotes them if necessary. |
||
1456 | * It will join all columns into a string with comma as separators. |
||
1457 | * @param string|array $columns the columns to be processed |
||
1458 | * @return string the processing result |
||
1459 | */ |
||
1460 | 32 | public function buildColumns($columns) |
|
1483 | |||
1484 | /** |
||
1485 | * Parses the condition specification and generates the corresponding SQL expression. |
||
1486 | * @param string|array|ExpressionInterface $condition the condition specification. Please refer to [[Query::where()]] |
||
1487 | * on how to specify a condition. |
||
1488 | * @param array $params the binding parameters to be populated |
||
1489 | * @return string the generated SQL expression |
||
1490 | */ |
||
1491 | 1215 | public function buildCondition($condition, &$params) |
|
1505 | |||
1506 | /** |
||
1507 | * Transforms $condition defined in array format (as described in [[Query::where()]] |
||
1508 | * to instance of [[yii\db\condition\ConditionInterface|ConditionInterface]] according to |
||
1509 | * [[conditionClasses]] map. |
||
1510 | * |
||
1511 | * @param string|array $condition |
||
1512 | * @see conditionClasses |
||
1513 | * @return ConditionInterface |
||
1514 | * @since 2.0.14 |
||
1515 | */ |
||
1516 | 951 | public function createConditionFromArray($condition) |
|
1532 | |||
1533 | /** |
||
1534 | * Creates a condition based on column-value pairs. |
||
1535 | * @param array $condition the condition specification. |
||
1536 | * @param array $params the binding parameters to be populated |
||
1537 | * @return string the generated SQL expression |
||
1538 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1539 | */ |
||
1540 | public function buildHashCondition($condition, &$params) |
||
1544 | |||
1545 | /** |
||
1546 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1547 | * @param string $operator the operator to use for connecting the given operands |
||
1548 | * @param array $operands the SQL expressions to connect. |
||
1549 | * @param array $params the binding parameters to be populated |
||
1550 | * @return string the generated SQL expression |
||
1551 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1552 | */ |
||
1553 | public function buildAndCondition($operator, $operands, &$params) |
||
1558 | |||
1559 | /** |
||
1560 | * Inverts an SQL expressions with `NOT` operator. |
||
1561 | * @param string $operator the operator to use for connecting the given operands |
||
1562 | * @param array $operands the SQL expressions to connect. |
||
1563 | * @param array $params the binding parameters to be populated |
||
1564 | * @return string the generated SQL expression |
||
1565 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1566 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1567 | */ |
||
1568 | public function buildNotCondition($operator, $operands, &$params) |
||
1573 | |||
1574 | /** |
||
1575 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1576 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1577 | * @param array $operands the first operand is the column name. The second and third operands |
||
1578 | * describe the interval that column value should be in. |
||
1579 | * @param array $params the binding parameters to be populated |
||
1580 | * @return string the generated SQL expression |
||
1581 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1582 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1583 | */ |
||
1584 | public function buildBetweenCondition($operator, $operands, &$params) |
||
1589 | |||
1590 | /** |
||
1591 | * Creates an SQL expressions with the `IN` operator. |
||
1592 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1593 | * @param array $operands the first operand is the column name. If it is an array |
||
1594 | * a composite IN condition will be generated. |
||
1595 | * The second operand is an array of values that column value should be among. |
||
1596 | * If it is an empty array the generated expression will be a `false` value if |
||
1597 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1598 | * @param array $params the binding parameters to be populated |
||
1599 | * @return string the generated SQL expression |
||
1600 | * @throws Exception if wrong number of operands have been given. |
||
1601 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1602 | */ |
||
1603 | public function buildInCondition($operator, $operands, &$params) |
||
1608 | |||
1609 | /** |
||
1610 | * Creates an SQL expressions with the `LIKE` operator. |
||
1611 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1612 | * @param array $operands an array of two or three operands |
||
1613 | * |
||
1614 | * - The first operand is the column name. |
||
1615 | * - The second operand is a single value or an array of values that column value |
||
1616 | * should be compared with. If it is an empty array the generated expression will |
||
1617 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1618 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1619 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1620 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1621 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1622 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1623 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1624 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1625 | * @param array $params the binding parameters to be populated |
||
1626 | * @return string the generated SQL expression |
||
1627 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1628 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1629 | */ |
||
1630 | public function buildLikeCondition($operator, $operands, &$params) |
||
1635 | |||
1636 | /** |
||
1637 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1638 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1639 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1640 | * @param array $params the binding parameters to be populated |
||
1641 | * @return string the generated SQL expression |
||
1642 | * @throws InvalidArgumentException if the operand is not a [[Query]] object. |
||
1643 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1644 | */ |
||
1645 | public function buildExistsCondition($operator, $operands, &$params) |
||
1650 | |||
1651 | /** |
||
1652 | * Creates an SQL expressions like `"column" operator value`. |
||
1653 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1654 | * @param array $operands contains two column names. |
||
1655 | * @param array $params the binding parameters to be populated |
||
1656 | * @return string the generated SQL expression |
||
1657 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1658 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1659 | */ |
||
1660 | public function buildSimpleCondition($operator, $operands, &$params) |
||
1665 | |||
1666 | /** |
||
1667 | * Creates a SELECT EXISTS() SQL statement. |
||
1668 | * @param string $rawSql the subquery in a raw form to select from. |
||
1669 | * @return string the SELECT EXISTS() SQL statement. |
||
1670 | * @since 2.0.8 |
||
1671 | */ |
||
1672 | 61 | public function selectExists($rawSql) |
|
1676 | |||
1677 | /** |
||
1678 | * Helper method to add $value to $params array using [[PARAM_PREFIX]]. |
||
1679 | * |
||
1680 | * @param string|null $value |
||
1681 | * @param array $params passed by reference |
||
1682 | * @return string the placeholder name in $params array |
||
1683 | * |
||
1684 | * @since 2.0.14 |
||
1685 | */ |
||
1686 | 1003 | public function bindParam($value, &$params) |
|
1693 | } |
||
1694 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..