Complex classes like QueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class QueryBuilder extends \yii\base\BaseObject |
||
27 | { |
||
28 | /** |
||
29 | * The prefix for automatically generated query binding parameters. |
||
30 | */ |
||
31 | const PARAM_PREFIX = ':qp'; |
||
32 | |||
33 | /** |
||
34 | * @var Connection the database connection. |
||
35 | */ |
||
36 | public $db; |
||
37 | /** |
||
38 | * @var string the separator between different fragments of a SQL statement. |
||
39 | * Defaults to an empty space. This is mainly used by [[build()]] when generating a SQL statement. |
||
40 | */ |
||
41 | public $separator = ' '; |
||
42 | /** |
||
43 | * @var array the abstract column types mapped to physical column types. |
||
44 | * This is mainly used to support creating/modifying tables using DB-independent data type specifications. |
||
45 | * Child classes should override this property to declare supported type mappings. |
||
46 | */ |
||
47 | public $typeMap = []; |
||
48 | |||
49 | /** |
||
50 | * @var array map of query condition to builder methods. |
||
51 | * These methods are used by [[buildCondition]] to build SQL conditions from array syntax. |
||
52 | */ |
||
53 | protected $conditionBuilders = [ |
||
54 | 'NOT' => 'buildNotCondition', |
||
55 | 'AND' => 'buildAndCondition', |
||
56 | 'OR' => 'buildAndCondition', |
||
57 | 'BETWEEN' => 'buildBetweenCondition', |
||
58 | 'NOT BETWEEN' => 'buildBetweenCondition', |
||
59 | 'IN' => 'buildInCondition', |
||
60 | 'NOT IN' => 'buildInCondition', |
||
61 | 'LIKE' => 'buildLikeCondition', |
||
62 | 'NOT LIKE' => 'buildLikeCondition', |
||
63 | 'OR LIKE' => 'buildLikeCondition', |
||
64 | 'OR NOT LIKE' => 'buildLikeCondition', |
||
65 | 'EXISTS' => 'buildExistsCondition', |
||
66 | 'NOT EXISTS' => 'buildExistsCondition', |
||
67 | ]; |
||
68 | /** |
||
69 | * @var array map of chars to their replacements in LIKE conditions. |
||
70 | * By default it's configured to escape `%`, `_` and `\` with `\`. |
||
71 | * @since 2.0.12. |
||
72 | */ |
||
73 | protected $likeEscapingReplacements = [ |
||
74 | '%' => '\%', |
||
75 | '_' => '\_', |
||
76 | '\\' => '\\\\', |
||
77 | ]; |
||
78 | /** |
||
79 | * @var string|null character used to escape special characters in LIKE conditions. |
||
80 | * By default it's assumed to be `\`. |
||
81 | * @since 2.0.12 |
||
82 | */ |
||
83 | protected $likeEscapeCharacter; |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Constructor. |
||
88 | * @param Connection $connection the database connection. |
||
89 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
90 | */ |
||
91 | public function __construct($connection, $config = []) |
||
96 | |||
97 | /** |
||
98 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
99 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
100 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
101 | * be included in the result with the additional parameters generated during the query building process. |
||
102 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
103 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
104 | * include those provided in `$params`. |
||
105 | */ |
||
106 | public function build($query, $params = []) |
||
146 | |||
147 | /** |
||
148 | * Creates an INSERT SQL statement. |
||
149 | * For example, |
||
150 | * |
||
151 | * ```php |
||
152 | * $sql = $queryBuilder->insert('user', [ |
||
153 | * 'name' => 'Sam', |
||
154 | * 'age' => 30, |
||
155 | * ], $params); |
||
156 | * ``` |
||
157 | * |
||
158 | * The method will properly escape the table and column names. |
||
159 | * |
||
160 | * @param string $table the table that new rows will be inserted into. |
||
161 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
||
162 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
163 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
164 | * @param array $params the binding parameters that will be generated by this method. |
||
165 | * They should be bound to the DB command later. |
||
166 | * @return string the INSERT SQL |
||
167 | */ |
||
168 | public function insert($table, $columns, &$params) |
||
204 | |||
205 | /** |
||
206 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
207 | * |
||
208 | * @param \yii\db\Query $columns Object, which represents select query. |
||
209 | * @param \yii\db\Schema $schema Schema object to quote column name. |
||
210 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
211 | * be included in the result with the additional parameters generated during the query building process. |
||
212 | * @return array |
||
213 | * @throws InvalidParamException if query's select does not contain named parameters only. |
||
214 | * @since 2.0.11 |
||
215 | */ |
||
216 | protected function prepareInsertSelectSubQuery($columns, $schema, $params = []) |
||
237 | |||
238 | /** |
||
239 | * Generates a batch INSERT SQL statement. |
||
240 | * For example, |
||
241 | * |
||
242 | * ```php |
||
243 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
244 | * ['Tom', 30], |
||
245 | * ['Jane', 20], |
||
246 | * ['Linda', 25], |
||
247 | * ]); |
||
248 | * ``` |
||
249 | * |
||
250 | * Note that the values in each row must match the corresponding column names. |
||
251 | * |
||
252 | * The method will properly escape the column names, and quote the values to be inserted. |
||
253 | * |
||
254 | * @param string $table the table that new rows will be inserted into. |
||
255 | * @param array $columns the column names |
||
256 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
257 | * @return string the batch INSERT SQL statement |
||
258 | */ |
||
259 | public function batchInsert($table, $columns, $rows) |
||
301 | |||
302 | /** |
||
303 | * Creates an UPDATE SQL statement. |
||
304 | * For example, |
||
305 | * |
||
306 | * ```php |
||
307 | * $params = []; |
||
308 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
309 | * ``` |
||
310 | * |
||
311 | * The method will properly escape the table and column names. |
||
312 | * |
||
313 | * @param string $table the table to be updated. |
||
314 | * @param array $columns the column data (name => value) to be updated. |
||
315 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
316 | * refer to [[Query::where()]] on how to specify condition. |
||
317 | * @param array $params the binding parameters that will be modified by this method |
||
318 | * so that they can be bound to the DB command later. |
||
319 | * @return string the UPDATE SQL |
||
320 | */ |
||
321 | public function update($table, $columns, $condition, &$params) |
||
348 | |||
349 | /** |
||
350 | * Creates a DELETE SQL statement. |
||
351 | * For example, |
||
352 | * |
||
353 | * ```php |
||
354 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
355 | * ``` |
||
356 | * |
||
357 | * The method will properly escape the table and column names. |
||
358 | * |
||
359 | * @param string $table the table where the data will be deleted from. |
||
360 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
361 | * refer to [[Query::where()]] on how to specify condition. |
||
362 | * @param array $params the binding parameters that will be modified by this method |
||
363 | * so that they can be bound to the DB command later. |
||
364 | * @return string the DELETE SQL |
||
365 | */ |
||
366 | public function delete($table, $condition, &$params) |
||
373 | |||
374 | /** |
||
375 | * Builds a SQL statement for creating a new DB table. |
||
376 | * |
||
377 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
378 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
379 | * stands for the column type which can contain an abstract DB type. |
||
380 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
381 | * |
||
382 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
383 | * inserted into the generated SQL. |
||
384 | * |
||
385 | * For example, |
||
386 | * |
||
387 | * ```php |
||
388 | * $sql = $queryBuilder->createTable('user', [ |
||
389 | * 'id' => 'pk', |
||
390 | * 'name' => 'string', |
||
391 | * 'age' => 'integer', |
||
392 | * ]); |
||
393 | * ``` |
||
394 | * |
||
395 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
396 | * @param array $columns the columns (name => definition) in the new table. |
||
397 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
398 | * @return string the SQL statement for creating a new DB table. |
||
399 | */ |
||
400 | public function createTable($table, $columns, $options = null) |
||
414 | |||
415 | /** |
||
416 | * Builds a SQL statement for renaming a DB table. |
||
417 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
418 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
419 | * @return string the SQL statement for renaming a DB table. |
||
420 | */ |
||
421 | public function renameTable($oldName, $newName) |
||
425 | |||
426 | /** |
||
427 | * Builds a SQL statement for dropping a DB table. |
||
428 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
429 | * @return string the SQL statement for dropping a DB table. |
||
430 | */ |
||
431 | public function dropTable($table) |
||
435 | |||
436 | /** |
||
437 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
438 | * @param string $name the name of the primary key constraint. |
||
439 | * @param string $table the table that the primary key constraint will be added to. |
||
440 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
441 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
442 | */ |
||
443 | public function addPrimaryKey($name, $table, $columns) |
||
457 | |||
458 | /** |
||
459 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
460 | * @param string $name the name of the primary key constraint to be removed. |
||
461 | * @param string $table the table that the primary key constraint will be removed from. |
||
462 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
463 | */ |
||
464 | public function dropPrimaryKey($name, $table) |
||
469 | |||
470 | /** |
||
471 | * Builds a SQL statement for truncating a DB table. |
||
472 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
473 | * @return string the SQL statement for truncating a DB table. |
||
474 | */ |
||
475 | public function truncateTable($table) |
||
479 | |||
480 | /** |
||
481 | * Builds a SQL statement for adding a new DB column. |
||
482 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
483 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
484 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
485 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
486 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
487 | * @return string the SQL statement for adding a new column. |
||
488 | */ |
||
489 | public function addColumn($table, $column, $type) |
||
495 | |||
496 | /** |
||
497 | * Builds a SQL statement for dropping a DB column. |
||
498 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
499 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
500 | * @return string the SQL statement for dropping a DB column. |
||
501 | */ |
||
502 | public function dropColumn($table, $column) |
||
507 | |||
508 | /** |
||
509 | * Builds a SQL statement for renaming a column. |
||
510 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
511 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
512 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
513 | * @return string the SQL statement for renaming a DB column. |
||
514 | */ |
||
515 | public function renameColumn($table, $oldName, $newName) |
||
521 | |||
522 | /** |
||
523 | * Builds a SQL statement for changing the definition of a column. |
||
524 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
525 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
526 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
527 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
528 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
529 | * will become 'varchar(255) not null'. |
||
530 | * @return string the SQL statement for changing the definition of a column. |
||
531 | */ |
||
532 | public function alterColumn($table, $column, $type) |
||
539 | |||
540 | /** |
||
541 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
542 | * The method will properly quote the table and column names. |
||
543 | * @param string $name the name of the foreign key constraint. |
||
544 | * @param string $table the table that the foreign key constraint will be added to. |
||
545 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
546 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
547 | * @param string $refTable the table that the foreign key references to. |
||
548 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
549 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
550 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
551 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
552 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
553 | */ |
||
554 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
570 | |||
571 | /** |
||
572 | * Builds a SQL statement for dropping a foreign key constraint. |
||
573 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
574 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
575 | * @return string the SQL statement for dropping a foreign key constraint. |
||
576 | */ |
||
577 | public function dropForeignKey($name, $table) |
||
582 | |||
583 | /** |
||
584 | * Builds a SQL statement for creating a new index. |
||
585 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
586 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
587 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
588 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
589 | * by the method, unless a parenthesis is found in the name. |
||
590 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
591 | * @return string the SQL statement for creating a new index. |
||
592 | */ |
||
593 | public function createIndex($name, $table, $columns, $unique = false) |
||
600 | |||
601 | /** |
||
602 | * Builds a SQL statement for dropping an index. |
||
603 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
604 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
605 | * @return string the SQL statement for dropping an index. |
||
606 | */ |
||
607 | public function dropIndex($name, $table) |
||
611 | |||
612 | /** |
||
613 | * Creates a SQL command for adding an unique constraint to an existing table. |
||
614 | * @param string $name the name of the unique constraint. |
||
615 | * The name will be properly quoted by the method. |
||
616 | * @param string $table the table that the unique constraint will be added to. |
||
617 | * The name will be properly quoted by the method. |
||
618 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
619 | * If there are multiple columns, separate them with commas. |
||
620 | * The name will be properly quoted by the method. |
||
621 | * @return string the SQL statement for adding an unique constraint to an existing table. |
||
622 | * @since 2.0.13 |
||
623 | */ |
||
624 | public function addUnique($name, $table, $columns) |
||
636 | |||
637 | /** |
||
638 | * Creates a SQL command for dropping an unique constraint. |
||
639 | * @param string $name the name of the unique constraint to be dropped. |
||
640 | * The name will be properly quoted by the method. |
||
641 | * @param string $table the table whose unique constraint is to be dropped. |
||
642 | * The name will be properly quoted by the method. |
||
643 | * @return string the SQL statement for dropping an unique constraint. |
||
644 | * @since 2.0.13 |
||
645 | */ |
||
646 | public function dropUnique($name, $table) |
||
651 | |||
652 | /** |
||
653 | * Creates a SQL command for adding a check constraint to an existing table. |
||
654 | * @param string $name the name of the check constraint. |
||
655 | * The name will be properly quoted by the method. |
||
656 | * @param string $table the table that the check constraint will be added to. |
||
657 | * The name will be properly quoted by the method. |
||
658 | * @param string $expression the SQL of the `CHECK` constraint. |
||
659 | * @return string the SQL statement for adding a check constraint to an existing table. |
||
660 | * @since 2.0.13 |
||
661 | */ |
||
662 | public function addCheck($name, $table, $expression) |
||
667 | |||
668 | /** |
||
669 | * Creates a SQL command for dropping a check constraint. |
||
670 | * @param string $name the name of the check constraint to be dropped. |
||
671 | * The name will be properly quoted by the method. |
||
672 | * @param string $table the table whose check constraint is to be dropped. |
||
673 | * The name will be properly quoted by the method. |
||
674 | * @return string the SQL statement for dropping a check constraint. |
||
675 | * @since 2.0.13 |
||
676 | */ |
||
677 | public function dropCheck($name, $table) |
||
682 | |||
683 | /** |
||
684 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
685 | * @param string $name the name of the default value constraint. |
||
686 | * The name will be properly quoted by the method. |
||
687 | * @param string $table the table that the default value constraint will be added to. |
||
688 | * The name will be properly quoted by the method. |
||
689 | * @param string $column the name of the column to that the constraint will be added on. |
||
690 | * The name will be properly quoted by the method. |
||
691 | * @param mixed $value default value. |
||
692 | * @return string the SQL statement for adding a default value constraint to an existing table. |
||
693 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
694 | * @since 2.0.13 |
||
695 | */ |
||
696 | public function addDefaultValue($name, $table, $column, $value) |
||
700 | |||
701 | /** |
||
702 | * Creates a SQL command for dropping a default value constraint. |
||
703 | * @param string $name the name of the default value constraint to be dropped. |
||
704 | * The name will be properly quoted by the method. |
||
705 | * @param string $table the table whose default value constraint is to be dropped. |
||
706 | * The name will be properly quoted by the method. |
||
707 | * @return string the SQL statement for dropping a default value constraint. |
||
708 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
709 | * @since 2.0.13 |
||
710 | */ |
||
711 | public function dropDefaultValue($name, $table) |
||
715 | |||
716 | /** |
||
717 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
718 | * The sequence will be reset such that the primary key of the next new row inserted |
||
719 | * will have the specified value or 1. |
||
720 | * @param string $table the name of the table whose primary key sequence will be reset |
||
721 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
722 | * the next new row's primary key will have a value 1. |
||
723 | * @return string the SQL statement for resetting sequence |
||
724 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
725 | */ |
||
726 | public function resetSequence($table, $value = null) |
||
730 | |||
731 | /** |
||
732 | * Builds a SQL statement for enabling or disabling integrity check. |
||
733 | * @param bool $check whether to turn on or off the integrity check. |
||
734 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
735 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
736 | * @return string the SQL statement for checking integrity |
||
737 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
738 | */ |
||
739 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
743 | |||
744 | /** |
||
745 | * Builds a SQL command for adding comment to column |
||
746 | * |
||
747 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
748 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
749 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
750 | * @return string the SQL statement for adding comment on column |
||
751 | * @since 2.0.8 |
||
752 | */ |
||
753 | public function addCommentOnColumn($table, $column, $comment) |
||
757 | |||
758 | /** |
||
759 | * Builds a SQL command for adding comment to table |
||
760 | * |
||
761 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
762 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
763 | * @return string the SQL statement for adding comment on table |
||
764 | * @since 2.0.8 |
||
765 | */ |
||
766 | public function addCommentOnTable($table, $comment) |
||
770 | |||
771 | /** |
||
772 | * Builds a SQL command for adding comment to column |
||
773 | * |
||
774 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
775 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
776 | * @return string the SQL statement for adding comment on column |
||
777 | * @since 2.0.8 |
||
778 | */ |
||
779 | public function dropCommentFromColumn($table, $column) |
||
783 | |||
784 | /** |
||
785 | * Builds a SQL command for adding comment to table |
||
786 | * |
||
787 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
788 | * @return string the SQL statement for adding comment on column |
||
789 | * @since 2.0.8 |
||
790 | */ |
||
791 | public function dropCommentFromTable($table) |
||
795 | |||
796 | /** |
||
797 | * Converts an abstract column type into a physical column type. |
||
798 | * The conversion is done using the type map specified in [[typeMap]]. |
||
799 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
800 | * physical types): |
||
801 | * |
||
802 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
803 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
804 | * - `unsignedpk`: an unsigned auto-incremental primary key type, will be converted into "int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
805 | * - `char`: char type, will be converted into "char(1)" |
||
806 | * - `string`: string type, will be converted into "varchar(255)" |
||
807 | * - `text`: a long string type, will be converted into "text" |
||
808 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
809 | * - `integer`: integer type, will be converted into "int(11)" |
||
810 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
811 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
812 | * - `float``: float number type, will be converted into "float" |
||
813 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
814 | * - `datetime`: datetime type, will be converted into "datetime" |
||
815 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
816 | * - `time`: time type, will be converted into "time" |
||
817 | * - `date`: date type, will be converted into "date" |
||
818 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
819 | * - `binary`: binary data type, will be converted into "blob" |
||
820 | * |
||
821 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
822 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
823 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
824 | * |
||
825 | * For some of the abstract types you can also specify a length or precision constraint |
||
826 | * by appending it in round brackets directly to the type. |
||
827 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
828 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
829 | * be ignored. |
||
830 | * |
||
831 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
832 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
833 | * @return string physical column type. |
||
834 | */ |
||
835 | public function getColumnType($type) |
||
855 | |||
856 | /** |
||
857 | * @param array $columns |
||
858 | * @param array $params the binding parameters to be populated |
||
859 | * @param bool $distinct |
||
860 | * @param string $selectOption |
||
861 | * @return string the SELECT clause built from [[Query::$select]]. |
||
862 | */ |
||
863 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
||
901 | |||
902 | /** |
||
903 | * @param array $tables |
||
904 | * @param array $params the binding parameters to be populated |
||
905 | * @return string the FROM clause built from [[Query::$from]]. |
||
906 | */ |
||
907 | public function buildFrom($tables, &$params) |
||
917 | |||
918 | /** |
||
919 | * @param array $joins |
||
920 | * @param array $params the binding parameters to be populated |
||
921 | * @return string the JOIN clause built from [[Query::$join]]. |
||
922 | * @throws Exception if the $joins parameter is not in proper format |
||
923 | */ |
||
924 | public function buildJoin($joins, &$params) |
||
949 | |||
950 | /** |
||
951 | * Quotes table names passed |
||
952 | * |
||
953 | * @param array $tables |
||
954 | * @param array $params |
||
955 | * @return array |
||
956 | */ |
||
957 | private function quoteTableNames($tables, &$params) |
||
978 | |||
979 | /** |
||
980 | * @param string|array $condition |
||
981 | * @param array $params the binding parameters to be populated |
||
982 | * @return string the WHERE clause built from [[Query::$where]]. |
||
983 | */ |
||
984 | public function buildWhere($condition, &$params) |
||
990 | |||
991 | /** |
||
992 | * @param array $columns |
||
993 | * @return string the GROUP BY clause |
||
994 | */ |
||
995 | public function buildGroupBy($columns) |
||
1009 | |||
1010 | /** |
||
1011 | * @param string|array $condition |
||
1012 | * @param array $params the binding parameters to be populated |
||
1013 | * @return string the HAVING clause built from [[Query::$having]]. |
||
1014 | */ |
||
1015 | public function buildHaving($condition, &$params) |
||
1021 | |||
1022 | /** |
||
1023 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
1024 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
1025 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
1026 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
1027 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
1028 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
1029 | */ |
||
1030 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
1042 | |||
1043 | /** |
||
1044 | * @param array $columns |
||
1045 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
1046 | */ |
||
1047 | public function buildOrderBy($columns) |
||
1063 | |||
1064 | /** |
||
1065 | * @param int $limit |
||
1066 | * @param int $offset |
||
1067 | * @return string the LIMIT and OFFSET clauses |
||
1068 | */ |
||
1069 | public function buildLimit($limit, $offset) |
||
1081 | |||
1082 | /** |
||
1083 | * Checks to see if the given limit is effective. |
||
1084 | * @param mixed $limit the given limit |
||
1085 | * @return bool whether the limit is effective |
||
1086 | */ |
||
1087 | protected function hasLimit($limit) |
||
1091 | |||
1092 | /** |
||
1093 | * Checks to see if the given offset is effective. |
||
1094 | * @param mixed $offset the given offset |
||
1095 | * @return bool whether the offset is effective |
||
1096 | */ |
||
1097 | protected function hasOffset($offset) |
||
1101 | |||
1102 | /** |
||
1103 | * @param array $unions |
||
1104 | * @param array $params the binding parameters to be populated |
||
1105 | * @return string the UNION clause built from [[Query::$union]]. |
||
1106 | */ |
||
1107 | public function buildUnion($unions, &$params) |
||
1126 | |||
1127 | /** |
||
1128 | * Processes columns and properly quotes them if necessary. |
||
1129 | * It will join all columns into a string with comma as separators. |
||
1130 | * @param string|array $columns the columns to be processed |
||
1131 | * @return string the processing result |
||
1132 | */ |
||
1133 | public function buildColumns($columns) |
||
1152 | |||
1153 | /** |
||
1154 | * Parses the condition specification and generates the corresponding SQL expression. |
||
1155 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
1156 | * on how to specify a condition. |
||
1157 | * @param array $params the binding parameters to be populated |
||
1158 | * @return string the generated SQL expression |
||
1159 | */ |
||
1160 | public function buildCondition($condition, &$params) |
||
1187 | |||
1188 | /** |
||
1189 | * Creates a condition based on column-value pairs. |
||
1190 | * @param array $condition the condition specification. |
||
1191 | * @param array $params the binding parameters to be populated |
||
1192 | * @return string the generated SQL expression |
||
1193 | */ |
||
1194 | public function buildHashCondition($condition, &$params) |
||
1221 | |||
1222 | /** |
||
1223 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1224 | * @param string $operator the operator to use for connecting the given operands |
||
1225 | * @param array $operands the SQL expressions to connect. |
||
1226 | * @param array $params the binding parameters to be populated |
||
1227 | * @return string the generated SQL expression |
||
1228 | */ |
||
1229 | public function buildAndCondition($operator, $operands, &$params) |
||
1252 | |||
1253 | /** |
||
1254 | * Inverts an SQL expressions with `NOT` operator. |
||
1255 | * @param string $operator the operator to use for connecting the given operands |
||
1256 | * @param array $operands the SQL expressions to connect. |
||
1257 | * @param array $params the binding parameters to be populated |
||
1258 | * @return string the generated SQL expression |
||
1259 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1260 | */ |
||
1261 | public function buildNotCondition($operator, $operands, &$params) |
||
1277 | |||
1278 | /** |
||
1279 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1280 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1281 | * @param array $operands the first operand is the column name. The second and third operands |
||
1282 | * describe the interval that column value should be in. |
||
1283 | * @param array $params the binding parameters to be populated |
||
1284 | * @return string the generated SQL expression |
||
1285 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1286 | */ |
||
1287 | public function buildBetweenCondition($operator, $operands, &$params) |
||
1319 | |||
1320 | /** |
||
1321 | * Creates an SQL expressions with the `IN` operator. |
||
1322 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1323 | * @param array $operands the first operand is the column name. If it is an array |
||
1324 | * a composite IN condition will be generated. |
||
1325 | * The second operand is an array of values that column value should be among. |
||
1326 | * If it is an empty array the generated expression will be a `false` value if |
||
1327 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1328 | * @param array $params the binding parameters to be populated |
||
1329 | * @return string the generated SQL expression |
||
1330 | * @throws Exception if wrong number of operands have been given. |
||
1331 | */ |
||
1332 | public function buildInCondition($operator, $operands, &$params) |
||
1393 | |||
1394 | /** |
||
1395 | * Builds SQL for IN condition |
||
1396 | * |
||
1397 | * @param string $operator |
||
1398 | * @param array $columns |
||
1399 | * @param Query $values |
||
1400 | * @param array $params |
||
1401 | * @return string SQL |
||
1402 | */ |
||
1403 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
||
1421 | |||
1422 | /** |
||
1423 | * Builds SQL for IN condition |
||
1424 | * |
||
1425 | * @param string $operator |
||
1426 | * @param array|\Traversable $columns |
||
1427 | * @param array $values |
||
1428 | * @param array $params |
||
1429 | * @return string SQL |
||
1430 | */ |
||
1431 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
||
1459 | |||
1460 | /** |
||
1461 | * Creates an SQL expressions with the `LIKE` operator. |
||
1462 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1463 | * @param array $operands an array of two or three operands |
||
1464 | * |
||
1465 | * - The first operand is the column name. |
||
1466 | * - The second operand is a single value or an array of values that column value |
||
1467 | * should be compared with. If it is an empty array the generated expression will |
||
1468 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1469 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1470 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1471 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1472 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1473 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1474 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1475 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1476 | * @param array $params the binding parameters to be populated |
||
1477 | * @return string the generated SQL expression |
||
1478 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1479 | */ |
||
1480 | public function buildLikeCondition($operator, $operands, &$params) |
||
1530 | |||
1531 | /** |
||
1532 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1533 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1534 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1535 | * @param array $params the binding parameters to be populated |
||
1536 | * @return string the generated SQL expression |
||
1537 | * @throws InvalidParamException if the operand is not a [[Query]] object. |
||
1538 | */ |
||
1539 | public function buildExistsCondition($operator, $operands, &$params) |
||
1548 | |||
1549 | /** |
||
1550 | * Creates an SQL expressions like `"column" operator value`. |
||
1551 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1552 | * @param array $operands contains two column names. |
||
1553 | * @param array $params the binding parameters to be populated |
||
1554 | * @return string the generated SQL expression |
||
1555 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1556 | */ |
||
1557 | public function buildSimpleCondition($operator, $operands, &$params) |
||
1585 | |||
1586 | /** |
||
1587 | * Creates a SELECT EXISTS() SQL statement. |
||
1588 | * @param string $rawSql the subquery in a raw form to select from. |
||
1589 | * @return string the SELECT EXISTS() SQL statement. |
||
1590 | * @since 2.0.8 |
||
1591 | */ |
||
1592 | public function selectExists($rawSql) |
||
1596 | } |
||
1597 |