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 | 1367 | public function __construct($connection, $config = []) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 1367 | 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 | 1367 | 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 | 1367 | protected function defaultExpressionBuilders() |
|
178 | |||
179 | /** |
||
180 | * Setter for [[expressionBuilders]] property. |
||
181 | * |
||
182 | * @param string[] $builders array of builder that should be merged with [[expressionBuilders]] |
||
183 | * @since 2.0.14 |
||
184 | * @see expressionBuilders |
||
185 | */ |
||
186 | public function setExpressionBuilders($builders) |
||
190 | |||
191 | /** |
||
192 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
193 | * |
||
194 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
195 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
196 | * be included in the result with the additional parameters generated during the query building process. |
||
197 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
198 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
199 | * include those provided in `$params`. |
||
200 | */ |
||
201 | 780 | public function build($query, $params = []) |
|
241 | |||
242 | /** |
||
243 | * Builds given $expression |
||
244 | * |
||
245 | * @param ExpressionInterface $expression the expression to be built |
||
246 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
247 | * be included in the result with the additional parameters generated during the expression building process. |
||
248 | * @return string the SQL statement that will not be neither quoted nor encoded before passing to DBMS |
||
249 | * @see ExpressionInterface |
||
250 | * @see ExpressionBuilderInterface |
||
251 | * @see expressionBuilders |
||
252 | * @since 2.0.14 |
||
253 | * @throws InvalidArgumentException when $expression building is not supported by this QueryBuilder. |
||
254 | */ |
||
255 | 990 | public function buildExpression(ExpressionInterface $expression, &$params = []) |
|
261 | |||
262 | /** |
||
263 | * Gets object of [[ExpressionBuilderInterface]] that is suitable for $expression. |
||
264 | * Uses [[expressionBuilders]] array to find a suitable builder class. |
||
265 | * |
||
266 | * @param ExpressionInterface $expression |
||
267 | * @return ExpressionBuilderInterface |
||
268 | * @see expressionBuilders |
||
269 | * @since 2.0.14 |
||
270 | * @throws InvalidArgumentException when $expression building is not supported by this QueryBuilder. |
||
271 | */ |
||
272 | 990 | public function getExpressionBuilder(ExpressionInterface $expression) |
|
295 | |||
296 | /** |
||
297 | * Creates an INSERT SQL statement. |
||
298 | * For example, |
||
299 | * ```php |
||
300 | * $sql = $queryBuilder->insert('user', [ |
||
301 | * 'name' => 'Sam', |
||
302 | * 'age' => 30, |
||
303 | * ], $params); |
||
304 | * ``` |
||
305 | * The method will properly escape the table and column names. |
||
306 | * |
||
307 | * @param string $table the table that new rows will be inserted into. |
||
308 | * @param array|Query $columns the column data (name => value) to be inserted into the table or instance |
||
309 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
310 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
311 | * @param array $params the binding parameters that will be generated by this method. |
||
312 | * They should be bound to the DB command later. |
||
313 | * @return string the INSERT SQL |
||
314 | */ |
||
315 | 530 | public function insert($table, $columns, &$params) |
|
322 | |||
323 | /** |
||
324 | * Prepares a `VALUES` part for an `INSERT` SQL statement. |
||
325 | * |
||
326 | * @param string $table the table that new rows will be inserted into. |
||
327 | * @param array|Query $columns the column data (name => value) to be inserted into the table or instance |
||
328 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
329 | * @param array $params the binding parameters that will be generated by this method. |
||
330 | * They should be bound to the DB command later. |
||
331 | * @return array array of column names, placeholders, values and params. |
||
332 | * @since 2.0.14 |
||
333 | */ |
||
334 | 544 | protected function prepareInsertValues($table, $columns, $params = []) |
|
361 | |||
362 | /** |
||
363 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
364 | * |
||
365 | * @param Query $columns Object, which represents select query. |
||
366 | * @param \yii\db\Schema $schema Schema object to quote column name. |
||
367 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
368 | * be included in the result with the additional parameters generated during the query building process. |
||
369 | * @return array array of column names, values and params. |
||
370 | * @throws InvalidArgumentException if query's select does not contain named parameters only. |
||
371 | * @since 2.0.11 |
||
372 | */ |
||
373 | 42 | protected function prepareInsertSelectSubQuery($columns, $schema, $params = []) |
|
394 | |||
395 | /** |
||
396 | * Generates a batch INSERT SQL statement. |
||
397 | * |
||
398 | * For example, |
||
399 | * |
||
400 | * ```php |
||
401 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
402 | * ['Tom', 30], |
||
403 | * ['Jane', 20], |
||
404 | * ['Linda', 25], |
||
405 | * ]); |
||
406 | * ``` |
||
407 | * |
||
408 | * Note that the values in each row must match the corresponding column names. |
||
409 | * |
||
410 | * The method will properly escape the column names, and quote the values to be inserted. |
||
411 | * |
||
412 | * @param string $table the table that new rows will be inserted into. |
||
413 | * @param array $columns the column names |
||
414 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
415 | * @return string the batch INSERT SQL statement |
||
416 | */ |
||
417 | 26 | public function batchInsert($table, $columns, $rows) |
|
462 | |||
463 | /** |
||
464 | * Creates an SQL statement to insert rows into a database table if |
||
465 | * they do not already exist (matching unique constraints), |
||
466 | * or update them if they do. |
||
467 | * |
||
468 | * For example, |
||
469 | * |
||
470 | * ```php |
||
471 | * $sql = $queryBuilder->upsert('pages', [ |
||
472 | * 'name' => 'Front page', |
||
473 | * 'url' => 'http://example.com/', // url is unique |
||
474 | * 'visits' => 0, |
||
475 | * ], [ |
||
476 | * 'visits' => new \yii\db\Expression('visits + 1'), |
||
477 | * ], $params); |
||
478 | * ``` |
||
479 | * |
||
480 | * The method will properly escape the table and column names. |
||
481 | * |
||
482 | * @param string $table the table that new rows will be inserted into/updated in. |
||
483 | * @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance |
||
484 | * of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement. |
||
485 | * @param array|bool $updateColumns the column data (name => value) to be updated if they already exist. |
||
486 | * If `true` is passed, the column data will be updated to match the insert column data. |
||
487 | * If `false` is passed, no update will be performed if the column data already exists. |
||
488 | * @param array $params the binding parameters that will be generated by this method. |
||
489 | * They should be bound to the DB command later. |
||
490 | * @return string the resulting SQL. |
||
491 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
492 | * @since 2.0.14 |
||
493 | */ |
||
494 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
||
498 | |||
499 | /** |
||
500 | * @param string $table |
||
501 | * @param array|Query $insertColumns |
||
502 | * @param array|bool $updateColumns |
||
503 | * @param Constraint[] $constraints this parameter recieves a matched constraint list. |
||
504 | * The constraints will be unique by their column names. |
||
505 | * @return array |
||
506 | * @since 2.0.14 |
||
507 | */ |
||
508 | 66 | protected function prepareUpsertColumns($table, $insertColumns, $updateColumns, &$constraints = []) |
|
523 | |||
524 | /** |
||
525 | * Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) |
||
526 | * for the named table removing constraints which did not cover the specified column list. |
||
527 | * The column list will be unique by column names. |
||
528 | * |
||
529 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
530 | * @param string[] $columns source column list. |
||
531 | * @param Constraint[] $constraints this parameter optionally recieves a matched constraint list. |
||
532 | * The constraints will be unique by their column names. |
||
533 | * @return string[] column list. |
||
534 | */ |
||
535 | 66 | private function getTableUniqueColumnNames($name, $columns, &$constraints = []) |
|
571 | |||
572 | /** |
||
573 | * Creates an UPDATE SQL statement. |
||
574 | * |
||
575 | * For example, |
||
576 | * |
||
577 | * ```php |
||
578 | * $params = []; |
||
579 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
580 | * ``` |
||
581 | * |
||
582 | * The method will properly escape the table and column names. |
||
583 | * |
||
584 | * @param string $table the table to be updated. |
||
585 | * @param array $columns the column data (name => value) to be updated. |
||
586 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
587 | * refer to [[Query::where()]] on how to specify condition. |
||
588 | * @param array $params the binding parameters that will be modified by this method |
||
589 | * so that they can be bound to the DB command later. |
||
590 | * @return string the UPDATE SQL |
||
591 | */ |
||
592 | 126 | public function update($table, $columns, $condition, &$params) |
|
599 | |||
600 | /** |
||
601 | * Prepares a `SET` parts for an `UPDATE` SQL statement. |
||
602 | * @param string $table the table to be updated. |
||
603 | * @param array $columns the column data (name => value) to be updated. |
||
604 | * @param array $params the binding parameters that will be modified by this method |
||
605 | * so that they can be bound to the DB command later. |
||
606 | * @return array an array `SET` parts for an `UPDATE` SQL statement (the first array element) and params (the second array element). |
||
607 | * @since 2.0.14 |
||
608 | */ |
||
609 | 158 | protected function prepareUpdateSets($table, $columns, $params = []) |
|
627 | |||
628 | /** |
||
629 | * Creates a DELETE SQL statement. |
||
630 | * |
||
631 | * For example, |
||
632 | * |
||
633 | * ```php |
||
634 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
635 | * ``` |
||
636 | * |
||
637 | * The method will properly escape the table and column names. |
||
638 | * |
||
639 | * @param string $table the table where the data will be deleted from. |
||
640 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
641 | * refer to [[Query::where()]] on how to specify condition. |
||
642 | * @param array $params the binding parameters that will be modified by this method |
||
643 | * so that they can be bound to the DB command later. |
||
644 | * @return string the DELETE SQL |
||
645 | */ |
||
646 | 353 | public function delete($table, $condition, &$params) |
|
653 | |||
654 | /** |
||
655 | * Builds a SQL statement for creating a new DB table. |
||
656 | * |
||
657 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
658 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
659 | * stands for the column type which can contain an abstract DB type. |
||
660 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
661 | * |
||
662 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
663 | * inserted into the generated SQL. |
||
664 | * |
||
665 | * For example, |
||
666 | * |
||
667 | * ```php |
||
668 | * $sql = $queryBuilder->createTable('user', [ |
||
669 | * 'id' => 'pk', |
||
670 | * 'name' => 'string', |
||
671 | * 'age' => 'integer', |
||
672 | * ]); |
||
673 | * ``` |
||
674 | * |
||
675 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
676 | * @param array $columns the columns (name => definition) in the new table. |
||
677 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
678 | * @return string the SQL statement for creating a new DB table. |
||
679 | */ |
||
680 | 134 | public function createTable($table, $columns, $options = null) |
|
694 | |||
695 | /** |
||
696 | * Builds a SQL statement for renaming a DB table. |
||
697 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
698 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
699 | * @return string the SQL statement for renaming a DB table. |
||
700 | */ |
||
701 | 1 | public function renameTable($oldName, $newName) |
|
705 | |||
706 | /** |
||
707 | * Builds a SQL statement for dropping a DB table. |
||
708 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
709 | * @return string the SQL statement for dropping a DB table. |
||
710 | */ |
||
711 | 39 | public function dropTable($table) |
|
715 | |||
716 | /** |
||
717 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
718 | * @param string $name the name of the primary key constraint. |
||
719 | * @param string $table the table that the primary key constraint will be added to. |
||
720 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
721 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
722 | */ |
||
723 | 6 | public function addPrimaryKey($name, $table, $columns) |
|
737 | |||
738 | /** |
||
739 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
740 | * @param string $name the name of the primary key constraint to be removed. |
||
741 | * @param string $table the table that the primary key constraint will be removed from. |
||
742 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
743 | */ |
||
744 | 2 | public function dropPrimaryKey($name, $table) |
|
749 | |||
750 | /** |
||
751 | * Builds a SQL statement for truncating a DB table. |
||
752 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
753 | * @return string the SQL statement for truncating a DB table. |
||
754 | */ |
||
755 | 11 | public function truncateTable($table) |
|
759 | |||
760 | /** |
||
761 | * Builds a SQL statement for adding a new DB column. |
||
762 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
763 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
764 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
765 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
766 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
767 | * @return string the SQL statement for adding a new column. |
||
768 | */ |
||
769 | 4 | public function addColumn($table, $column, $type) |
|
775 | |||
776 | /** |
||
777 | * Builds a SQL statement for dropping a DB column. |
||
778 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
779 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
780 | * @return string the SQL statement for dropping a DB column. |
||
781 | */ |
||
782 | public function dropColumn($table, $column) |
||
787 | |||
788 | /** |
||
789 | * Builds a SQL statement for renaming a column. |
||
790 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
791 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
792 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
793 | * @return string the SQL statement for renaming a DB column. |
||
794 | */ |
||
795 | public function renameColumn($table, $oldName, $newName) |
||
801 | |||
802 | /** |
||
803 | * Builds a SQL statement for changing the definition of a column. |
||
804 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
805 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
806 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
807 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
808 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
809 | * will become 'varchar(255) not null'. |
||
810 | * @return string the SQL statement for changing the definition of a column. |
||
811 | */ |
||
812 | 1 | public function alterColumn($table, $column, $type) |
|
819 | |||
820 | /** |
||
821 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
822 | * The method will properly quote the table and column names. |
||
823 | * @param string $name the name of the foreign key constraint. |
||
824 | * @param string $table the table that the foreign key constraint will be added to. |
||
825 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
826 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
827 | * @param string $refTable the table that the foreign key references to. |
||
828 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
829 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
830 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
831 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
832 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
833 | */ |
||
834 | 8 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
850 | |||
851 | /** |
||
852 | * Builds a SQL statement for dropping a foreign key constraint. |
||
853 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
854 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
855 | * @return string the SQL statement for dropping a foreign key constraint. |
||
856 | */ |
||
857 | 3 | public function dropForeignKey($name, $table) |
|
862 | |||
863 | /** |
||
864 | * Builds a SQL statement for creating a new index. |
||
865 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
866 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
867 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
868 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
869 | * by the method, unless a parenthesis is found in the name. |
||
870 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
871 | * @return string the SQL statement for creating a new index. |
||
872 | */ |
||
873 | 6 | public function createIndex($name, $table, $columns, $unique = false) |
|
880 | |||
881 | /** |
||
882 | * Builds a SQL statement for dropping an index. |
||
883 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
884 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
885 | * @return string the SQL statement for dropping an index. |
||
886 | */ |
||
887 | 4 | public function dropIndex($name, $table) |
|
891 | |||
892 | /** |
||
893 | * Creates a SQL command for adding an unique constraint to an existing table. |
||
894 | * @param string $name the name of the unique constraint. |
||
895 | * The name will be properly quoted by the method. |
||
896 | * @param string $table the table that the unique constraint will be added to. |
||
897 | * The name will be properly quoted by the method. |
||
898 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
899 | * If there are multiple columns, separate them with commas. |
||
900 | * The name will be properly quoted by the method. |
||
901 | * @return string the SQL statement for adding an unique constraint to an existing table. |
||
902 | * @since 2.0.13 |
||
903 | */ |
||
904 | 6 | public function addUnique($name, $table, $columns) |
|
917 | |||
918 | /** |
||
919 | * Creates a SQL command for dropping an unique constraint. |
||
920 | * @param string $name the name of the unique constraint to be dropped. |
||
921 | * The name will be properly quoted by the method. |
||
922 | * @param string $table the table whose unique constraint is to be dropped. |
||
923 | * The name will be properly quoted by the method. |
||
924 | * @return string the SQL statement for dropping an unique constraint. |
||
925 | * @since 2.0.13 |
||
926 | */ |
||
927 | 2 | public function dropUnique($name, $table) |
|
932 | |||
933 | /** |
||
934 | * Creates a SQL command for adding a check constraint to an existing table. |
||
935 | * @param string $name the name of the check constraint. |
||
936 | * The name will be properly quoted by the method. |
||
937 | * @param string $table the table that the check constraint will be added to. |
||
938 | * The name will be properly quoted by the method. |
||
939 | * @param string $expression the SQL of the `CHECK` constraint. |
||
940 | * @return string the SQL statement for adding a check constraint to an existing table. |
||
941 | * @since 2.0.13 |
||
942 | */ |
||
943 | 2 | public function addCheck($name, $table, $expression) |
|
948 | |||
949 | /** |
||
950 | * Creates a SQL command for dropping a check constraint. |
||
951 | * @param string $name the name of the check constraint to be dropped. |
||
952 | * The name will be properly quoted by the method. |
||
953 | * @param string $table the table whose check constraint is to be dropped. |
||
954 | * The name will be properly quoted by the method. |
||
955 | * @return string the SQL statement for dropping a check constraint. |
||
956 | * @since 2.0.13 |
||
957 | */ |
||
958 | 2 | public function dropCheck($name, $table) |
|
963 | |||
964 | /** |
||
965 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
966 | * @param string $name the name of the default value constraint. |
||
967 | * The name will be properly quoted by the method. |
||
968 | * @param string $table the table that the default value constraint will be added to. |
||
969 | * The name will be properly quoted by the method. |
||
970 | * @param string $column the name of the column to that the constraint will be added on. |
||
971 | * The name will be properly quoted by the method. |
||
972 | * @param mixed $value default value. |
||
973 | * @return string the SQL statement for adding a default value constraint to an existing table. |
||
974 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
975 | * @since 2.0.13 |
||
976 | */ |
||
977 | public function addDefaultValue($name, $table, $column, $value) |
||
981 | |||
982 | /** |
||
983 | * Creates a SQL command for dropping a default value constraint. |
||
984 | * @param string $name the name of the default value constraint to be dropped. |
||
985 | * The name will be properly quoted by the method. |
||
986 | * @param string $table the table whose default value constraint is to be dropped. |
||
987 | * The name will be properly quoted by the method. |
||
988 | * @return string the SQL statement for dropping a default value constraint. |
||
989 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
990 | * @since 2.0.13 |
||
991 | */ |
||
992 | public function dropDefaultValue($name, $table) |
||
996 | |||
997 | /** |
||
998 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
999 | * The sequence will be reset such that the primary key of the next new row inserted |
||
1000 | * will have the specified value or 1. |
||
1001 | * @param string $table the name of the table whose primary key sequence will be reset |
||
1002 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
1003 | * the next new row's primary key will have a value 1. |
||
1004 | * @return string the SQL statement for resetting sequence |
||
1005 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
1006 | */ |
||
1007 | public function resetSequence($table, $value = null) |
||
1011 | |||
1012 | /** |
||
1013 | * Builds a SQL statement for enabling or disabling integrity check. |
||
1014 | * @param bool $check whether to turn on or off the integrity check. |
||
1015 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
1016 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
1017 | * @return string the SQL statement for checking integrity |
||
1018 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
1019 | */ |
||
1020 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
1024 | |||
1025 | /** |
||
1026 | * Builds a SQL command for adding comment to column. |
||
1027 | * |
||
1028 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1029 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
1030 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
1031 | * @return string the SQL statement for adding comment on column |
||
1032 | * @since 2.0.8 |
||
1033 | */ |
||
1034 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
1038 | |||
1039 | /** |
||
1040 | * Builds a SQL command for adding comment to table. |
||
1041 | * |
||
1042 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1043 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
1044 | * @return string the SQL statement for adding comment on table |
||
1045 | * @since 2.0.8 |
||
1046 | */ |
||
1047 | 1 | public function addCommentOnTable($table, $comment) |
|
1051 | |||
1052 | /** |
||
1053 | * Builds a SQL command for adding comment to column. |
||
1054 | * |
||
1055 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1056 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
1057 | * @return string the SQL statement for adding comment on column |
||
1058 | * @since 2.0.8 |
||
1059 | */ |
||
1060 | 2 | public function dropCommentFromColumn($table, $column) |
|
1064 | |||
1065 | /** |
||
1066 | * Builds a SQL command for adding comment to table. |
||
1067 | * |
||
1068 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1069 | * @return string the SQL statement for adding comment on column |
||
1070 | * @since 2.0.8 |
||
1071 | */ |
||
1072 | 1 | public function dropCommentFromTable($table) |
|
1076 | |||
1077 | /** |
||
1078 | * Creates a SQL View. |
||
1079 | * |
||
1080 | * @param string $viewName the name of the view to be created. |
||
1081 | * @param string|Query $subQuery the select statement which defines the view. |
||
1082 | * This can be either a string or a [[Query]] object. |
||
1083 | * @return string the `CREATE VIEW` SQL statement. |
||
1084 | * @since 2.0.14 |
||
1085 | */ |
||
1086 | 3 | public function createView($viewName, $subQuery) |
|
1101 | |||
1102 | /** |
||
1103 | * Drops a SQL View. |
||
1104 | * |
||
1105 | * @param string $viewName the name of the view to be dropped. |
||
1106 | * @return string the `DROP VIEW` SQL statement. |
||
1107 | * @since 2.0.14 |
||
1108 | */ |
||
1109 | 3 | public function dropView($viewName) |
|
1113 | |||
1114 | /** |
||
1115 | * Converts an abstract column type into a physical column type. |
||
1116 | * |
||
1117 | * The conversion is done using the type map specified in [[typeMap]]. |
||
1118 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
1119 | * physical types): |
||
1120 | * |
||
1121 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
1122 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
1123 | * - `upk`: an unsigned auto-incremental primary key type, will be converted into "int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
1124 | * - `char`: char type, will be converted into "char(1)" |
||
1125 | * - `string`: string type, will be converted into "varchar(255)" |
||
1126 | * - `text`: a long string type, will be converted into "text" |
||
1127 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
1128 | * - `integer`: integer type, will be converted into "int(11)" |
||
1129 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
1130 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
1131 | * - `float``: float number type, will be converted into "float" |
||
1132 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
1133 | * - `datetime`: datetime type, will be converted into "datetime" |
||
1134 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
1135 | * - `time`: time type, will be converted into "time" |
||
1136 | * - `date`: date type, will be converted into "date" |
||
1137 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
1138 | * - `binary`: binary data type, will be converted into "blob" |
||
1139 | * |
||
1140 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
1141 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
1142 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
1143 | * |
||
1144 | * For some of the abstract types you can also specify a length or precision constraint |
||
1145 | * by appending it in round brackets directly to the type. |
||
1146 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
1147 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
1148 | * be ignored. |
||
1149 | * |
||
1150 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
1151 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
1152 | * @return string physical column type. |
||
1153 | */ |
||
1154 | 138 | public function getColumnType($type) |
|
1174 | |||
1175 | /** |
||
1176 | * @param array $columns |
||
1177 | * @param array $params the binding parameters to be populated |
||
1178 | * @param bool $distinct |
||
1179 | * @param string $selectOption |
||
1180 | * @return string the SELECT clause built from [[Query::$select]]. |
||
1181 | */ |
||
1182 | 1102 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
1219 | |||
1220 | /** |
||
1221 | * @param array $tables |
||
1222 | * @param array $params the binding parameters to be populated |
||
1223 | * @return string the FROM clause built from [[Query::$from]]. |
||
1224 | */ |
||
1225 | 1102 | public function buildFrom($tables, &$params) |
|
1235 | |||
1236 | /** |
||
1237 | * @param array $joins |
||
1238 | * @param array $params the binding parameters to be populated |
||
1239 | * @return string the JOIN clause built from [[Query::$join]]. |
||
1240 | * @throws Exception if the $joins parameter is not in proper format |
||
1241 | */ |
||
1242 | 1102 | public function buildJoin($joins, &$params) |
|
1267 | |||
1268 | /** |
||
1269 | * Quotes table names passed. |
||
1270 | * |
||
1271 | * @param array $tables |
||
1272 | * @param array $params |
||
1273 | * @return array |
||
1274 | */ |
||
1275 | 802 | private function quoteTableNames($tables, &$params) |
|
1297 | |||
1298 | /** |
||
1299 | * @param string|array $condition |
||
1300 | * @param array $params the binding parameters to be populated |
||
1301 | * @return string the WHERE clause built from [[Query::$where]]. |
||
1302 | */ |
||
1303 | 1179 | public function buildWhere($condition, &$params) |
|
1309 | |||
1310 | /** |
||
1311 | * @param array $columns |
||
1312 | * @return string the GROUP BY clause |
||
1313 | */ |
||
1314 | 1102 | public function buildGroupBy($columns) |
|
1329 | |||
1330 | /** |
||
1331 | * @param string|array $condition |
||
1332 | * @param array $params the binding parameters to be populated |
||
1333 | * @return string the HAVING clause built from [[Query::$having]]. |
||
1334 | */ |
||
1335 | 1102 | public function buildHaving($condition, &$params) |
|
1341 | |||
1342 | /** |
||
1343 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
1344 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
1345 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
1346 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
1347 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
1348 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
1349 | */ |
||
1350 | 1102 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
1363 | |||
1364 | /** |
||
1365 | * @param array $columns |
||
1366 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
1367 | */ |
||
1368 | 1102 | public function buildOrderBy($columns) |
|
1384 | |||
1385 | /** |
||
1386 | * @param int $limit |
||
1387 | * @param int $offset |
||
1388 | * @return string the LIMIT and OFFSET clauses |
||
1389 | */ |
||
1390 | 401 | public function buildLimit($limit, $offset) |
|
1402 | |||
1403 | /** |
||
1404 | * Checks to see if the given limit is effective. |
||
1405 | * @param mixed $limit the given limit |
||
1406 | * @return bool whether the limit is effective |
||
1407 | */ |
||
1408 | 723 | protected function hasLimit($limit) |
|
1412 | |||
1413 | /** |
||
1414 | * Checks to see if the given offset is effective. |
||
1415 | * @param mixed $offset the given offset |
||
1416 | * @return bool whether the offset is effective |
||
1417 | */ |
||
1418 | 723 | protected function hasOffset($offset) |
|
1422 | |||
1423 | /** |
||
1424 | * @param array $unions |
||
1425 | * @param array $params the binding parameters to be populated |
||
1426 | * @return string the UNION clause built from [[Query::$union]]. |
||
1427 | */ |
||
1428 | 780 | public function buildUnion($unions, &$params) |
|
1447 | |||
1448 | /** |
||
1449 | * Processes columns and properly quotes them if necessary. |
||
1450 | * It will join all columns into a string with comma as separators. |
||
1451 | * @param string|array $columns the columns to be processed |
||
1452 | * @return string the processing result |
||
1453 | */ |
||
1454 | 32 | public function buildColumns($columns) |
|
1477 | |||
1478 | /** |
||
1479 | * Parses the condition specification and generates the corresponding SQL expression. |
||
1480 | * @param string|array|ExpressionInterface $condition the condition specification. Please refer to [[Query::where()]] |
||
1481 | * on how to specify a condition. |
||
1482 | * @param array $params the binding parameters to be populated |
||
1483 | * @return string the generated SQL expression |
||
1484 | */ |
||
1485 | 1179 | public function buildCondition($condition, &$params) |
|
1499 | |||
1500 | /** |
||
1501 | * Transforms $condition defined in array format (as described in [[Query::where()]] |
||
1502 | * to instance of [[yii\db\condition\ConditionInterface|ConditionInterface]] according to |
||
1503 | * [[conditionClasses]] map. |
||
1504 | * |
||
1505 | * @param string|array $condition |
||
1506 | * @see conditionClasses |
||
1507 | * @return ConditionInterface |
||
1508 | * @since 2.0.14 |
||
1509 | */ |
||
1510 | 930 | public function createConditionFromArray($condition) |
|
1526 | |||
1527 | /** |
||
1528 | * Creates a condition based on column-value pairs. |
||
1529 | * @param array $condition the condition specification. |
||
1530 | * @param array $params the binding parameters to be populated |
||
1531 | * @return string the generated SQL expression |
||
1532 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1533 | */ |
||
1534 | public function buildHashCondition($condition, &$params) |
||
1538 | |||
1539 | /** |
||
1540 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1541 | * @param string $operator the operator to use for connecting the given operands |
||
1542 | * @param array $operands the SQL expressions to connect. |
||
1543 | * @param array $params the binding parameters to be populated |
||
1544 | * @return string the generated SQL expression |
||
1545 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1546 | */ |
||
1547 | public function buildAndCondition($operator, $operands, &$params) |
||
1552 | |||
1553 | /** |
||
1554 | * Inverts an SQL expressions with `NOT` operator. |
||
1555 | * @param string $operator the operator to use for connecting the given operands |
||
1556 | * @param array $operands the SQL expressions to connect. |
||
1557 | * @param array $params the binding parameters to be populated |
||
1558 | * @return string the generated SQL expression |
||
1559 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1560 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1561 | */ |
||
1562 | public function buildNotCondition($operator, $operands, &$params) |
||
1567 | |||
1568 | /** |
||
1569 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1570 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1571 | * @param array $operands the first operand is the column name. The second and third operands |
||
1572 | * describe the interval that column value should be in. |
||
1573 | * @param array $params the binding parameters to be populated |
||
1574 | * @return string the generated SQL expression |
||
1575 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1576 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1577 | */ |
||
1578 | public function buildBetweenCondition($operator, $operands, &$params) |
||
1583 | |||
1584 | /** |
||
1585 | * Creates an SQL expressions with the `IN` operator. |
||
1586 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1587 | * @param array $operands the first operand is the column name. If it is an array |
||
1588 | * a composite IN condition will be generated. |
||
1589 | * The second operand is an array of values that column value should be among. |
||
1590 | * If it is an empty array the generated expression will be a `false` value if |
||
1591 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1592 | * @param array $params the binding parameters to be populated |
||
1593 | * @return string the generated SQL expression |
||
1594 | * @throws Exception if wrong number of operands have been given. |
||
1595 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1596 | */ |
||
1597 | public function buildInCondition($operator, $operands, &$params) |
||
1602 | |||
1603 | /** |
||
1604 | * Creates an SQL expressions with the `LIKE` operator. |
||
1605 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1606 | * @param array $operands an array of two or three operands |
||
1607 | * |
||
1608 | * - The first operand is the column name. |
||
1609 | * - The second operand is a single value or an array of values that column value |
||
1610 | * should be compared with. If it is an empty array the generated expression will |
||
1611 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1612 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1613 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1614 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1615 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1616 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1617 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1618 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1619 | * @param array $params the binding parameters to be populated |
||
1620 | * @return string the generated SQL expression |
||
1621 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1622 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1623 | */ |
||
1624 | public function buildLikeCondition($operator, $operands, &$params) |
||
1629 | |||
1630 | /** |
||
1631 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1632 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1633 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1634 | * @param array $params the binding parameters to be populated |
||
1635 | * @return string the generated SQL expression |
||
1636 | * @throws InvalidArgumentException if the operand is not a [[Query]] object. |
||
1637 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1638 | */ |
||
1639 | public function buildExistsCondition($operator, $operands, &$params) |
||
1644 | |||
1645 | /** |
||
1646 | * Creates an SQL expressions like `"column" operator value`. |
||
1647 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1648 | * @param array $operands contains two column names. |
||
1649 | * @param array $params the binding parameters to be populated |
||
1650 | * @return string the generated SQL expression |
||
1651 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1652 | * @deprecated since 2.0.14. Use `buildCondition()` instead. |
||
1653 | */ |
||
1654 | public function buildSimpleCondition($operator, $operands, &$params) |
||
1659 | |||
1660 | /** |
||
1661 | * Creates a SELECT EXISTS() SQL statement. |
||
1662 | * @param string $rawSql the subquery in a raw form to select from. |
||
1663 | * @return string the SELECT EXISTS() SQL statement. |
||
1664 | * @since 2.0.8 |
||
1665 | */ |
||
1666 | 61 | public function selectExists($rawSql) |
|
1670 | |||
1671 | /** |
||
1672 | * Helper method to add $value to $params array using [[PARAM_PREFIX]]. |
||
1673 | * |
||
1674 | * @param string|null $value |
||
1675 | * @param array $params passed by reference |
||
1676 | * @return string the placeholder name in $params array |
||
1677 | * |
||
1678 | * @since 2.0.14 |
||
1679 | */ |
||
1680 | 979 | public function bindParam($value, &$params) |
|
1687 | } |
||
1688 |
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..