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 |
||
33 | class QueryBuilder extends \yii\base\BaseObject |
||
34 | { |
||
35 | /** |
||
36 | * The prefix for automatically generated query binding parameters. |
||
37 | */ |
||
38 | const PARAM_PREFIX = ':qp'; |
||
39 | |||
40 | /** |
||
41 | * @var Connection the database connection. |
||
42 | */ |
||
43 | public $db; |
||
44 | /** |
||
45 | * @var string the separator between different fragments of a SQL statement. |
||
46 | * Defaults to an empty space. This is mainly used by [[build()]] when generating a SQL statement. |
||
47 | */ |
||
48 | public $separator = ' '; |
||
49 | /** |
||
50 | * @var array the abstract column types mapped to physical column types. |
||
51 | * This is mainly used to support creating/modifying tables using DB-independent data type specifications. |
||
52 | * Child classes should override this property to declare supported type mappings. |
||
53 | */ |
||
54 | public $typeMap = []; |
||
55 | |||
56 | /** |
||
57 | * @var array map of condition aliases to condition classes. For example: |
||
58 | * |
||
59 | * ```php |
||
60 | * return [ |
||
61 | * 'LIKE' => yii\db\condition\LikeCondition::class, |
||
62 | * ]; |
||
63 | * ``` |
||
64 | * |
||
65 | * This property is used by [[createConditionFromArray]] method. |
||
66 | * See default condition classes list in [[defaultConditionClasses()]] method. |
||
67 | * |
||
68 | * In case you want to add custom conditions support, use the [[setConditionClasses()]] method. |
||
69 | * |
||
70 | * @see setConditonClasses() |
||
71 | * @see defaultConditionClasses() |
||
72 | * @since 2.0.14 |
||
73 | */ |
||
74 | protected $conditionClasses = []; |
||
75 | /** |
||
76 | * @var string[]|ExpressionBuilderInterface[] maps expression class to expression builder class. |
||
77 | * For example: |
||
78 | * |
||
79 | * ```php |
||
80 | * [ |
||
81 | * yii\db\Expression::class => yii\db\ExpressionBuilder::class |
||
82 | * ] |
||
83 | * ``` |
||
84 | * This property is mainly used by [[buildExpression()]] to build SQL expressions form expression objects. |
||
85 | * See default values in [[defaultExpressionBuilders()]] method. |
||
86 | * |
||
87 | * |
||
88 | * To override existing builders or add custom, use [[setExpressionBuilder()]] method. New items will be added |
||
89 | * to the end of this array. |
||
90 | * |
||
91 | * To find a builder, [[buildExpression()]] will check the expression class for its exact presence in this map. |
||
92 | * In case it is NOT present, the array will be iterated in reverse direction, checking whether the expression |
||
93 | * extends the class, defined in this map. |
||
94 | * |
||
95 | * @see setExpressionBuilders() |
||
96 | * @see defaultExpressionBuilders() |
||
97 | * @since 2.0.14 |
||
98 | */ |
||
99 | protected $expressionBuilders = []; |
||
100 | |||
101 | |||
102 | /** |
||
103 | * Constructor. |
||
104 | * @param Connection $connection the database connection. |
||
105 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
|
|||
106 | */ |
||
107 | 3 | public function __construct($connection) |
|
113 | |||
114 | /** |
||
115 | * Contains array of default condition classes. Extend this method, if you want to change |
||
116 | * default condition classes for the query builder. See [[conditionClasses]] docs for details. |
||
117 | * |
||
118 | * @return array |
||
119 | * @see conditionClasses |
||
120 | * @since 2.0.14 |
||
121 | */ |
||
122 | 3 | protected function defaultConditionClasses() |
|
140 | |||
141 | /** |
||
142 | * Contains array of default expression builders. Extend this method and override it, if you want to change |
||
143 | * default expression builders for this query builder. See [[expressionBuilders]] docs for details. |
||
144 | * |
||
145 | * @return array |
||
146 | * @see $expressionBuilders |
||
147 | * @since 2.0.14 |
||
148 | */ |
||
149 | 3 | protected function defaultExpressionBuilders() |
|
168 | |||
169 | /** |
||
170 | * Setter for [[expressionBuilders]] property. |
||
171 | * |
||
172 | * @param string[] $builders array of builders that should be merged with the pre-defined ones |
||
173 | * in [[expressionBuilders]] property. |
||
174 | * @since 2.0.14 |
||
175 | * @see expressionBuilders |
||
176 | */ |
||
177 | public function setExpressionBuilders($builders) |
||
181 | |||
182 | /** |
||
183 | * Setter for [[conditionClasses]] property. |
||
184 | * |
||
185 | * @param string[] $classes map of condition aliases to condition classes. For example: |
||
186 | * |
||
187 | * ```php |
||
188 | * ['LIKE' => yii\db\condition\LikeCondition::class] |
||
189 | * ``` |
||
190 | * |
||
191 | * @since 2.0.14.2 |
||
192 | * @see conditionClasses |
||
193 | */ |
||
194 | public function setConditionClasses($classes) |
||
198 | |||
199 | /** |
||
200 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
201 | * |
||
202 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
203 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
204 | * be included in the result with the additional parameters generated during the query building process. |
||
205 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
206 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
207 | * include those provided in `$params`. |
||
208 | */ |
||
209 | public function build($query, $params = []) |
||
234 | |||
235 | /** |
||
236 | * Builds given $expression |
||
237 | * |
||
238 | * @param ExpressionInterface $expression the expression to be built |
||
239 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
240 | * be included in the result with the additional parameters generated during the expression building process. |
||
241 | * @return string the SQL statement that will not be neither quoted nor encoded before passing to DBMS |
||
242 | * @see ExpressionInterface |
||
243 | * @see ExpressionBuilderInterface |
||
244 | * @see expressionBuilders |
||
245 | * @since 2.0.14 |
||
246 | * @throws InvalidArgumentException when $expression building is not supported by this QueryBuilder. |
||
247 | */ |
||
248 | public function buildExpression(ExpressionInterface $expression, &$params = []) |
||
254 | |||
255 | /** |
||
256 | * Gets object of [[ExpressionBuilderInterface]] that is suitable for $expression. |
||
257 | * Uses [[expressionBuilders]] array to find a suitable builder class. |
||
258 | * |
||
259 | * @param ExpressionInterface $expression |
||
260 | * @return ExpressionBuilderInterface |
||
261 | * @see expressionBuilders |
||
262 | * @since 2.0.14 |
||
263 | * @throws InvalidArgumentException when $expression building is not supported by this QueryBuilder. |
||
264 | */ |
||
265 | public function getExpressionBuilder(ExpressionInterface $expression) |
||
292 | |||
293 | /** |
||
294 | * Creates an INSERT SQL statement. |
||
295 | * For example, |
||
296 | * |
||
297 | * ```php |
||
298 | * $sql = $queryBuilder->insert('user', [ |
||
299 | * 'name' => 'Sam', |
||
300 | * 'age' => 30, |
||
301 | * ], $params); |
||
302 | * ``` |
||
303 | * |
||
304 | * The method will properly escape the table and column names. |
||
305 | * |
||
306 | * @param string $table the table that new rows will be inserted into. |
||
307 | * @param array|Query $columns the column data (name => value) to be inserted into the table or instance |
||
308 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
309 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
310 | * @param array $params the binding parameters that will be generated by this method. |
||
311 | * They should be bound to the DB command later. |
||
312 | * @return string the INSERT SQL |
||
313 | */ |
||
314 | public function insert($table, $columns, &$params) |
||
321 | |||
322 | /** |
||
323 | * Prepares a `VALUES` part for an `INSERT` SQL statement. |
||
324 | * |
||
325 | * @param string $table the table that new rows will be inserted into. |
||
326 | * @param array|Query $columns the column data (name => value) to be inserted into the table or instance |
||
327 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
328 | * @param array $params the binding parameters that will be generated by this method. |
||
329 | * They should be bound to the DB command later. |
||
330 | * @return array array of column names, placeholders, values and params. |
||
331 | * @since 2.0.14 |
||
332 | */ |
||
333 | protected function prepareInsertValues($table, $columns, $params = []) |
||
360 | |||
361 | /** |
||
362 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
363 | * |
||
364 | * @param Query $columns Object, which represents select query. |
||
365 | * @param \yii\db\Schema $schema Schema object to quote column name. |
||
366 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
367 | * be included in the result with the additional parameters generated during the query building process. |
||
368 | * @return array array of column names, values and params. |
||
369 | * @throws InvalidArgumentException if query's select does not contain named parameters only. |
||
370 | * @since 2.0.11 |
||
371 | */ |
||
372 | protected function prepareInsertSelectSubQuery($columns, $schema, $params = []) |
||
393 | |||
394 | /** |
||
395 | * Generates a batch INSERT SQL statement. |
||
396 | * |
||
397 | * For example, |
||
398 | * |
||
399 | * ```php |
||
400 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
401 | * ['Tom', 30], |
||
402 | * ['Jane', 20], |
||
403 | * ['Linda', 25], |
||
404 | * ]); |
||
405 | * ``` |
||
406 | * |
||
407 | * Note that the values in each row must match the corresponding column names. |
||
408 | * |
||
409 | * The method will properly escape the column names, and quote the values to be inserted. |
||
410 | * |
||
411 | * @param string $table the table that new rows will be inserted into. |
||
412 | * @param array $columns the column names |
||
413 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
414 | * @param array $params the binding parameters. This parameter exists since 2.0.14 |
||
415 | * @return string the batch INSERT SQL statement |
||
416 | */ |
||
417 | public function batchInsert($table, $columns, $rows, &$params = []) |
||
464 | |||
465 | /** |
||
466 | * Creates an SQL statement to insert rows into a database table if |
||
467 | * they do not already exist (matching unique constraints), |
||
468 | * or update them if they do. |
||
469 | * |
||
470 | * For example, |
||
471 | * |
||
472 | * ```php |
||
473 | * $sql = $queryBuilder->upsert('pages', [ |
||
474 | * 'name' => 'Front page', |
||
475 | * 'url' => 'http://example.com/', // url is unique |
||
476 | * 'visits' => 0, |
||
477 | * ], [ |
||
478 | * 'visits' => new \yii\db\Expression('visits + 1'), |
||
479 | * ], $params); |
||
480 | * ``` |
||
481 | * |
||
482 | * The method will properly escape the table and column names. |
||
483 | * |
||
484 | * @param string $table the table that new rows will be inserted into/updated in. |
||
485 | * @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance |
||
486 | * of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement. |
||
487 | * @param array|bool $updateColumns the column data (name => value) to be updated if they already exist. |
||
488 | * If `true` is passed, the column data will be updated to match the insert column data. |
||
489 | * If `false` is passed, no update will be performed if the column data already exists. |
||
490 | * @param array $params the binding parameters that will be generated by this method. |
||
491 | * They should be bound to the DB command later. |
||
492 | * @return string the resulting SQL. |
||
493 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
494 | * @since 2.0.14 |
||
495 | */ |
||
496 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
||
500 | |||
501 | /** |
||
502 | * @param string $table |
||
503 | * @param array|Query $insertColumns |
||
504 | * @param array|bool $updateColumns |
||
505 | * @param Constraint[] $constraints this parameter recieves a matched constraint list. |
||
506 | * The constraints will be unique by their column names. |
||
507 | * @return array |
||
508 | * @since 2.0.14 |
||
509 | */ |
||
510 | protected function prepareUpsertColumns($table, $insertColumns, $updateColumns, &$constraints = []) |
||
525 | |||
526 | /** |
||
527 | * Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.) |
||
528 | * for the named table removing constraints which did not cover the specified column list. |
||
529 | * The column list will be unique by column names. |
||
530 | * |
||
531 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||
532 | * @param string[] $columns source column list. |
||
533 | * @param Constraint[] $constraints this parameter optionally recieves a matched constraint list. |
||
534 | * The constraints will be unique by their column names. |
||
535 | * @return string[] column list. |
||
536 | */ |
||
537 | private function getTableUniqueColumnNames($name, $columns, &$constraints = []) |
||
573 | |||
574 | /** |
||
575 | * Creates an UPDATE SQL statement. |
||
576 | * |
||
577 | * For example, |
||
578 | * |
||
579 | * ```php |
||
580 | * $params = []; |
||
581 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
582 | * ``` |
||
583 | * |
||
584 | * The method will properly escape the table and column names. |
||
585 | * |
||
586 | * @param string $table the table to be updated. |
||
587 | * @param array $columns the column data (name => value) to be updated. |
||
588 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
589 | * refer to [[Query::where()]] on how to specify condition. |
||
590 | * @param array $params the binding parameters that will be modified by this method |
||
591 | * so that they can be bound to the DB command later. |
||
592 | * @return string the UPDATE SQL |
||
593 | */ |
||
594 | public function update($table, $columns, $condition, &$params) |
||
601 | |||
602 | /** |
||
603 | * Prepares a `SET` parts for an `UPDATE` SQL statement. |
||
604 | * @param string $table the table to be updated. |
||
605 | * @param array $columns the column data (name => value) to be updated. |
||
606 | * @param array $params the binding parameters that will be modified by this method |
||
607 | * so that they can be bound to the DB command later. |
||
608 | * @return array an array `SET` parts for an `UPDATE` SQL statement (the first array element) and params (the second array element). |
||
609 | * @since 2.0.14 |
||
610 | */ |
||
611 | protected function prepareUpdateSets($table, $columns, $params = []) |
||
629 | |||
630 | /** |
||
631 | * Creates a DELETE SQL statement. |
||
632 | * |
||
633 | * For example, |
||
634 | * |
||
635 | * ```php |
||
636 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
637 | * ``` |
||
638 | * |
||
639 | * The method will properly escape the table and column names. |
||
640 | * |
||
641 | * @param string $table the table where the data will be deleted from. |
||
642 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
643 | * refer to [[Query::where()]] on how to specify condition. |
||
644 | * @param array $params the binding parameters that will be modified by this method |
||
645 | * so that they can be bound to the DB command later. |
||
646 | * @return string the DELETE SQL |
||
647 | */ |
||
648 | public function delete($table, $condition, &$params) |
||
655 | |||
656 | /** |
||
657 | * Builds a SQL statement for creating a new DB table. |
||
658 | * |
||
659 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
660 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
661 | * stands for the column type which can contain an abstract DB type. |
||
662 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
663 | * |
||
664 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
665 | * inserted into the generated SQL. |
||
666 | * |
||
667 | * For example, |
||
668 | * |
||
669 | * ```php |
||
670 | * $sql = $queryBuilder->createTable('user', [ |
||
671 | * 'id' => 'pk', |
||
672 | * 'name' => 'string', |
||
673 | * 'age' => 'integer', |
||
674 | * ]); |
||
675 | * ``` |
||
676 | * |
||
677 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
678 | * @param array $columns the columns (name => definition) in the new table. |
||
679 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
680 | * @return string the SQL statement for creating a new DB table. |
||
681 | */ |
||
682 | public function createTable($table, $columns, $options = null) |
||
696 | |||
697 | /** |
||
698 | * Builds a SQL statement for renaming a DB table. |
||
699 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
700 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
701 | * @return string the SQL statement for renaming a DB table. |
||
702 | */ |
||
703 | public function renameTable($oldName, $newName) |
||
707 | |||
708 | /** |
||
709 | * Builds a SQL statement for dropping a DB table. |
||
710 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
711 | * @return string the SQL statement for dropping a DB table. |
||
712 | */ |
||
713 | public function dropTable($table) |
||
717 | |||
718 | /** |
||
719 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
720 | * @param string $name the name of the primary key constraint. |
||
721 | * @param string $table the table that the primary key constraint will be added to. |
||
722 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
723 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
724 | */ |
||
725 | public function addPrimaryKey($name, $table, $columns) |
||
739 | |||
740 | /** |
||
741 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
742 | * @param string $name the name of the primary key constraint to be removed. |
||
743 | * @param string $table the table that the primary key constraint will be removed from. |
||
744 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
745 | */ |
||
746 | public function dropPrimaryKey($name, $table) |
||
751 | |||
752 | /** |
||
753 | * Builds a SQL statement for truncating a DB table. |
||
754 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
755 | * @return string the SQL statement for truncating a DB table. |
||
756 | */ |
||
757 | public function truncateTable($table) |
||
761 | |||
762 | /** |
||
763 | * Builds a SQL statement for adding a new DB column. |
||
764 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
765 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
766 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
767 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
768 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
769 | * @return string the SQL statement for adding a new column. |
||
770 | */ |
||
771 | public function addColumn($table, $column, $type) |
||
777 | |||
778 | /** |
||
779 | * Builds a SQL statement for dropping a DB column. |
||
780 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
781 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
782 | * @return string the SQL statement for dropping a DB column. |
||
783 | */ |
||
784 | public function dropColumn($table, $column) |
||
789 | |||
790 | /** |
||
791 | * Builds a SQL statement for renaming a column. |
||
792 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
793 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
794 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
795 | * @return string the SQL statement for renaming a DB column. |
||
796 | */ |
||
797 | public function renameColumn($table, $oldName, $newName) |
||
803 | |||
804 | /** |
||
805 | * Builds a SQL statement for changing the definition of a column. |
||
806 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
807 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
808 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
809 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
810 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
811 | * will become 'varchar(255) not null'. |
||
812 | * @return string the SQL statement for changing the definition of a column. |
||
813 | */ |
||
814 | public function alterColumn($table, $column, $type) |
||
821 | |||
822 | /** |
||
823 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
824 | * The method will properly quote the table and column names. |
||
825 | * @param string $name the name of the foreign key constraint. |
||
826 | * @param string $table the table that the foreign key constraint will be added to. |
||
827 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
828 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
829 | * @param string $refTable the table that the foreign key references to. |
||
830 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
831 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
832 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
833 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
834 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
835 | */ |
||
836 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
852 | |||
853 | /** |
||
854 | * Builds a SQL statement for dropping a foreign key constraint. |
||
855 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
856 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
857 | * @return string the SQL statement for dropping a foreign key constraint. |
||
858 | */ |
||
859 | public function dropForeignKey($name, $table) |
||
864 | |||
865 | /** |
||
866 | * Builds a SQL statement for creating a new index. |
||
867 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
868 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
869 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
870 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
871 | * by the method, unless a parenthesis is found in the name. |
||
872 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
873 | * @return string the SQL statement for creating a new index. |
||
874 | */ |
||
875 | public function createIndex($name, $table, $columns, $unique = false) |
||
882 | |||
883 | /** |
||
884 | * Builds a SQL statement for dropping an index. |
||
885 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
886 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
887 | * @return string the SQL statement for dropping an index. |
||
888 | */ |
||
889 | public function dropIndex($name, $table) |
||
893 | |||
894 | /** |
||
895 | * Creates a SQL command for adding an unique constraint to an existing table. |
||
896 | * @param string $name the name of the unique constraint. |
||
897 | * The name will be properly quoted by the method. |
||
898 | * @param string $table the table that the unique constraint will be added to. |
||
899 | * The name will be properly quoted by the method. |
||
900 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
901 | * If there are multiple columns, separate them with commas. |
||
902 | * The name will be properly quoted by the method. |
||
903 | * @return string the SQL statement for adding an unique constraint to an existing table. |
||
904 | * @since 2.0.13 |
||
905 | */ |
||
906 | public function addUnique($name, $table, $columns) |
||
919 | |||
920 | /** |
||
921 | * Creates a SQL command for dropping an unique constraint. |
||
922 | * @param string $name the name of the unique constraint to be dropped. |
||
923 | * The name will be properly quoted by the method. |
||
924 | * @param string $table the table whose unique constraint is to be dropped. |
||
925 | * The name will be properly quoted by the method. |
||
926 | * @return string the SQL statement for dropping an unique constraint. |
||
927 | * @since 2.0.13 |
||
928 | */ |
||
929 | public function dropUnique($name, $table) |
||
934 | |||
935 | /** |
||
936 | * Creates a SQL command for adding a check constraint to an existing table. |
||
937 | * @param string $name the name of the check constraint. |
||
938 | * The name will be properly quoted by the method. |
||
939 | * @param string $table the table that the check constraint will be added to. |
||
940 | * The name will be properly quoted by the method. |
||
941 | * @param string $expression the SQL of the `CHECK` constraint. |
||
942 | * @return string the SQL statement for adding a check constraint to an existing table. |
||
943 | * @since 2.0.13 |
||
944 | */ |
||
945 | public function addCheck($name, $table, $expression) |
||
950 | |||
951 | /** |
||
952 | * Creates a SQL command for dropping a check constraint. |
||
953 | * @param string $name the name of the check constraint to be dropped. |
||
954 | * The name will be properly quoted by the method. |
||
955 | * @param string $table the table whose check constraint is to be dropped. |
||
956 | * The name will be properly quoted by the method. |
||
957 | * @return string the SQL statement for dropping a check constraint. |
||
958 | * @since 2.0.13 |
||
959 | */ |
||
960 | public function dropCheck($name, $table) |
||
965 | |||
966 | /** |
||
967 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
968 | * @param string $name the name of the default value constraint. |
||
969 | * The name will be properly quoted by the method. |
||
970 | * @param string $table the table that the default value constraint will be added to. |
||
971 | * The name will be properly quoted by the method. |
||
972 | * @param string $column the name of the column to that the constraint will be added on. |
||
973 | * The name will be properly quoted by the method. |
||
974 | * @param mixed $value default value. |
||
975 | * @return string the SQL statement for adding a default value constraint to an existing table. |
||
976 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
977 | * @since 2.0.13 |
||
978 | */ |
||
979 | public function addDefaultValue($name, $table, $column, $value) |
||
983 | |||
984 | /** |
||
985 | * Creates a SQL command for dropping a default value constraint. |
||
986 | * @param string $name the name of the default value constraint to be dropped. |
||
987 | * The name will be properly quoted by the method. |
||
988 | * @param string $table the table whose default value constraint is to be dropped. |
||
989 | * The name will be properly quoted by the method. |
||
990 | * @return string the SQL statement for dropping a default value constraint. |
||
991 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
992 | * @since 2.0.13 |
||
993 | */ |
||
994 | public function dropDefaultValue($name, $table) |
||
998 | |||
999 | /** |
||
1000 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
1001 | * The sequence will be reset such that the primary key of the next new row inserted |
||
1002 | * will have the specified value or 1. |
||
1003 | * @param string $table the name of the table whose primary key sequence will be reset |
||
1004 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
1005 | * the next new row's primary key will have a value 1. |
||
1006 | * @return string the SQL statement for resetting sequence |
||
1007 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
1008 | */ |
||
1009 | public function resetSequence($table, $value = null) |
||
1013 | |||
1014 | /** |
||
1015 | * Builds a SQL statement for enabling or disabling integrity check. |
||
1016 | * @param bool $check whether to turn on or off the integrity check. |
||
1017 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
1018 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
1019 | * @return string the SQL statement for checking integrity |
||
1020 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
1021 | */ |
||
1022 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
1026 | |||
1027 | /** |
||
1028 | * Builds a SQL command for adding comment to column. |
||
1029 | * |
||
1030 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1031 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
1032 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
1033 | * @return string the SQL statement for adding comment on column |
||
1034 | * @since 2.0.8 |
||
1035 | */ |
||
1036 | public function addCommentOnColumn($table, $column, $comment) |
||
1040 | |||
1041 | /** |
||
1042 | * Builds a SQL command for adding comment to table. |
||
1043 | * |
||
1044 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1045 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
1046 | * @return string the SQL statement for adding comment on table |
||
1047 | * @since 2.0.8 |
||
1048 | */ |
||
1049 | public function addCommentOnTable($table, $comment) |
||
1053 | |||
1054 | /** |
||
1055 | * Builds a SQL command for adding comment to column. |
||
1056 | * |
||
1057 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1058 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
1059 | * @return string the SQL statement for adding comment on column |
||
1060 | * @since 2.0.8 |
||
1061 | */ |
||
1062 | public function dropCommentFromColumn($table, $column) |
||
1066 | |||
1067 | /** |
||
1068 | * Builds a SQL command for adding comment to table. |
||
1069 | * |
||
1070 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1071 | * @return string the SQL statement for adding comment on column |
||
1072 | * @since 2.0.8 |
||
1073 | */ |
||
1074 | public function dropCommentFromTable($table) |
||
1078 | |||
1079 | /** |
||
1080 | * Creates a SQL View. |
||
1081 | * |
||
1082 | * @param string $viewName the name of the view to be created. |
||
1083 | * @param string|Query $subQuery the select statement which defines the view. |
||
1084 | * This can be either a string or a [[Query]] object. |
||
1085 | * @return string the `CREATE VIEW` SQL statement. |
||
1086 | * @since 2.0.14 |
||
1087 | */ |
||
1088 | public function createView($viewName, $subQuery) |
||
1103 | |||
1104 | /** |
||
1105 | * Drops a SQL View. |
||
1106 | * |
||
1107 | * @param string $viewName the name of the view to be dropped. |
||
1108 | * @return string the `DROP VIEW` SQL statement. |
||
1109 | * @since 2.0.14 |
||
1110 | */ |
||
1111 | public function dropView($viewName) |
||
1115 | |||
1116 | /** |
||
1117 | * Converts an abstract column type into a physical column type. |
||
1118 | * |
||
1119 | * The conversion is done using the type map specified in [[typeMap]]. |
||
1120 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
1121 | * physical types): |
||
1122 | * |
||
1123 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
1124 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
1125 | * - `upk`: an unsigned auto-incremental primary key type, will be converted into "int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
1126 | * - `char`: char type, will be converted into "char(1)" |
||
1127 | * - `string`: string type, will be converted into "varchar(255)" |
||
1128 | * - `text`: a long string type, will be converted into "text" |
||
1129 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
1130 | * - `integer`: integer type, will be converted into "int(11)" |
||
1131 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
1132 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
1133 | * - `float``: float number type, will be converted into "float" |
||
1134 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
1135 | * - `datetime`: datetime type, will be converted into "datetime" |
||
1136 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
1137 | * - `time`: time type, will be converted into "time" |
||
1138 | * - `date`: date type, will be converted into "date" |
||
1139 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
1140 | * - `binary`: binary data type, will be converted into "blob" |
||
1141 | * |
||
1142 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
1143 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
1144 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
1145 | * |
||
1146 | * For some of the abstract types you can also specify a length or precision constraint |
||
1147 | * by appending it in round brackets directly to the type. |
||
1148 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
1149 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
1150 | * be ignored. |
||
1151 | * |
||
1152 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
1153 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
1154 | * @return string physical column type. |
||
1155 | */ |
||
1156 | public function getColumnType($type) |
||
1176 | |||
1177 | /** |
||
1178 | * @param array $columns |
||
1179 | * @param array $params the binding parameters to be populated |
||
1180 | * @param bool $distinct |
||
1181 | * @param string $selectOption |
||
1182 | * @return string the SELECT clause built from [[Query::$select]]. |
||
1183 | */ |
||
1184 | 3 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
1221 | |||
1222 | /** |
||
1223 | * @param array $tables |
||
1224 | * @param array $params the binding parameters to be populated |
||
1225 | * @return string the FROM clause built from [[Query::$from]]. |
||
1226 | */ |
||
1227 | 3 | public function buildFrom($tables, &$params) |
|
1228 | { |
||
1229 | 3 | if (empty($tables)) { |
|
1230 | return ''; |
||
1231 | } |
||
1232 | |||
1233 | 3 | $tables = $this->quoteTableNames($tables, $params); |
|
1234 | |||
1235 | 3 | return 'FROM ' . implode(', ', $tables); |
|
1236 | } |
||
1237 | |||
1238 | /** |
||
1239 | * @param array $joins |
||
1240 | * @param array $params the binding parameters to be populated |
||
1241 | * @return string the JOIN clause built from [[Query::$join]]. |
||
1242 | * @throws Exception if the $joins parameter is not in proper format |
||
1243 | */ |
||
1244 | 3 | public function buildJoin($joins, &$params) |
|
1269 | |||
1270 | /** |
||
1271 | * Quotes table names passed. |
||
1272 | * |
||
1273 | * @param array $tables |
||
1274 | * @param array $params |
||
1275 | * @return array |
||
1276 | */ |
||
1277 | 3 | private function quoteTableNames($tables, &$params) |
|
1278 | { |
||
1279 | 3 | foreach ($tables as $i => $table) { |
|
1280 | 3 | if ($table instanceof Query) { |
|
1281 | [$sql, $params] = $this->build($table, $params); |
||
1282 | $tables[$i] = "($sql) " . $this->db->quoteTableName($i); |
||
1283 | 3 | } elseif (is_string($i)) { |
|
1284 | 3 | if (strpos($table, '(') === false) { |
|
1285 | $table = $this->db->quoteTableName($table); |
||
1286 | } |
||
1287 | 3 | $tables[$i] = "$table " . $this->db->quoteTableName($i); |
|
1288 | } elseif (strpos($table, '(') === false) { |
||
1289 | if (preg_match('/^(.*?)(?i:\s+as|)\s+([^ ]+)$/', $table, $matches)) { // with alias |
||
1290 | $tables[$i] = $this->db->quoteTableName($matches[1]) . ' ' . $this->db->quoteTableName($matches[2]); |
||
1291 | } else { |
||
1292 | 3 | $tables[$i] = $this->db->quoteTableName($table); |
|
1293 | } |
||
1294 | } |
||
1295 | } |
||
1296 | |||
1297 | 3 | return $tables; |
|
1298 | } |
||
1299 | |||
1300 | /** |
||
1301 | * @param string|array $condition |
||
1302 | * @param array $params the binding parameters to be populated |
||
1303 | * @return string the WHERE clause built from [[Query::$where]]. |
||
1304 | */ |
||
1305 | 3 | public function buildWhere($condition, &$params) |
|
1311 | |||
1312 | /** |
||
1313 | * @param array $columns |
||
1314 | * @param array $params the binding parameters to be populated |
||
1315 | * @return string the GROUP BY clause |
||
1316 | */ |
||
1317 | 3 | public function buildGroupBy($columns, &$params) |
|
1333 | |||
1334 | /** |
||
1335 | * @param string|array $condition |
||
1336 | * @param array $params the binding parameters to be populated |
||
1337 | * @return string the HAVING clause built from [[Query::$having]]. |
||
1338 | */ |
||
1339 | 3 | public function buildHaving($condition, &$params) |
|
1345 | |||
1346 | /** |
||
1347 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
1348 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
1349 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
1350 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
1351 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
1352 | * @param array $params the binding parameters to be populated |
||
1353 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
1354 | */ |
||
1355 | 3 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset, &$params) |
|
1368 | |||
1369 | /** |
||
1370 | * @param array $columns |
||
1371 | * @param array $params the binding parameters to be populated |
||
1372 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
1373 | */ |
||
1374 | 3 | public function buildOrderBy($columns, &$params) |
|
1391 | |||
1392 | /** |
||
1393 | * @param int $limit |
||
1394 | * @param int $offset |
||
1395 | * @return string the LIMIT and OFFSET clauses |
||
1396 | */ |
||
1397 | public function buildLimit($limit, $offset) |
||
1409 | |||
1410 | /** |
||
1411 | * Checks to see if the given limit is effective. |
||
1412 | * @param mixed $limit the given limit |
||
1413 | * @return bool whether the limit is effective |
||
1414 | */ |
||
1415 | 3 | protected function hasLimit($limit) |
|
1419 | |||
1420 | /** |
||
1421 | * Checks to see if the given offset is effective. |
||
1422 | * @param mixed $offset the given offset |
||
1423 | * @return bool whether the offset is effective |
||
1424 | */ |
||
1425 | 3 | protected function hasOffset($offset) |
|
1429 | |||
1430 | /** |
||
1431 | * @param array $unions |
||
1432 | * @param array $params the binding parameters to be populated |
||
1433 | * @return string the UNION clause built from [[Query::$union]]. |
||
1434 | */ |
||
1435 | public function buildUnion($unions, &$params) |
||
1454 | |||
1455 | /** |
||
1456 | * Processes columns and properly quotes them if necessary. |
||
1457 | * It will join all columns into a string with comma as separators. |
||
1458 | * @param string|array $columns the columns to be processed |
||
1459 | * @return string the processing result |
||
1460 | */ |
||
1461 | public function buildColumns($columns) |
||
1484 | |||
1485 | /** |
||
1486 | * Parses the condition specification and generates the corresponding SQL expression. |
||
1487 | * @param string|array|ExpressionInterface $condition the condition specification. Please refer to [[Query::where()]] |
||
1488 | * on how to specify a condition. |
||
1489 | * @param array $params the binding parameters to be populated |
||
1490 | * @return string the generated SQL expression |
||
1491 | */ |
||
1492 | 3 | public function buildCondition($condition, &$params) |
|
1508 | |||
1509 | /** |
||
1510 | * Transforms $condition defined in array format (as described in [[Query::where()]] |
||
1511 | * to instance of [[yii\db\condition\ConditionInterface|ConditionInterface]] according to |
||
1512 | * [[conditionClasses]] map. |
||
1513 | * |
||
1514 | * @param string|array $condition |
||
1515 | * @see conditionClasses |
||
1516 | * @return ConditionInterface |
||
1517 | * @since 2.0.14 |
||
1518 | */ |
||
1519 | public function createConditionFromArray($condition) |
||
1535 | |||
1536 | /** |
||
1537 | * Creates a SELECT EXISTS() SQL statement. |
||
1538 | * @param string $rawSql the subquery in a raw form to select from. |
||
1539 | * @return string the SELECT EXISTS() SQL statement. |
||
1540 | * @since 2.0.8 |
||
1541 | */ |
||
1542 | public function selectExists($rawSql) |
||
1546 | |||
1547 | /** |
||
1548 | * Helper method to add $value to $params array using [[PARAM_PREFIX]]. |
||
1549 | * |
||
1550 | * @param string|null $value |
||
1551 | * @param array $params passed by reference |
||
1552 | * @return string the placeholder name in $params array |
||
1553 | * |
||
1554 | * @since 2.0.14 |
||
1555 | */ |
||
1556 | public function bindParam($value, &$params) |
||
1563 | } |
||
1564 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.