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 | 1169 | 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 | 658 | public function build($query, $params = []) |
|
131 | |||
132 | /** |
||
133 | * Creates an INSERT SQL statement. |
||
134 | * |
||
135 | * For example, |
||
136 | * |
||
137 | * ```php |
||
138 | * $sql = $queryBuilder->insert('user', [ |
||
139 | * 'name' => 'Sam', |
||
140 | * 'age' => 30, |
||
141 | * ], $params); |
||
142 | * ``` |
||
143 | * |
||
144 | * The method will properly escape the table and column names. |
||
145 | * |
||
146 | * @param string $table the table that new rows will be inserted into. |
||
147 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
||
148 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
149 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
150 | * @param array $params the binding parameters that will be generated by this method. |
||
151 | * They should be bound to the DB command later. |
||
152 | * @return string the INSERT SQL |
||
153 | */ |
||
154 | 266 | public function insert($table, $columns, &$params) |
|
190 | |||
191 | /** |
||
192 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
193 | * |
||
194 | * @param \yii\db\Query $columns Object, which represents select query. |
||
195 | * @param \yii\db\Schema $schema Schema object to quote column name. |
||
196 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
197 | * be included in the result with the additional parameters generated during the query building process. |
||
198 | * @return array |
||
199 | * @throws InvalidArgumentException if query's select does not contain named parameters only. |
||
200 | * @since 2.0.11 |
||
201 | */ |
||
202 | 15 | protected function prepareInsertSelectSubQuery($columns, $schema, $params = []) |
|
223 | |||
224 | /** |
||
225 | * Generates a batch INSERT SQL statement. |
||
226 | * |
||
227 | * For example, |
||
228 | * |
||
229 | * ```php |
||
230 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
231 | * ['Tom', 30], |
||
232 | * ['Jane', 20], |
||
233 | * ['Linda', 25], |
||
234 | * ]); |
||
235 | * ``` |
||
236 | * |
||
237 | * Note that the values in each row must match the corresponding column names. |
||
238 | * |
||
239 | * The method will properly escape the column names, and quote the values to be inserted. |
||
240 | * |
||
241 | * @param string $table the table that new rows will be inserted into. |
||
242 | * @param array $columns the column names |
||
243 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
244 | * @return string the batch INSERT SQL statement |
||
245 | */ |
||
246 | 22 | public function batchInsert($table, $columns, $rows) |
|
291 | |||
292 | /** |
||
293 | * Creates an UPDATE SQL statement. |
||
294 | * |
||
295 | * For example, |
||
296 | * |
||
297 | * ```php |
||
298 | * $params = []; |
||
299 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
300 | * ``` |
||
301 | * |
||
302 | * The method will properly escape the table and column names. |
||
303 | * |
||
304 | * @param string $table the table to be updated. |
||
305 | * @param array $columns the column data (name => value) to be updated. |
||
306 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
307 | * refer to [[Query::where()]] on how to specify condition. |
||
308 | * @param array $params the binding parameters that will be modified by this method |
||
309 | * so that they can be bound to the DB command later. |
||
310 | * @return string the UPDATE SQL |
||
311 | */ |
||
312 | 92 | public function update($table, $columns, $condition, &$params) |
|
339 | |||
340 | /** |
||
341 | * Creates a DELETE SQL statement. |
||
342 | * |
||
343 | * For example, |
||
344 | * |
||
345 | * ```php |
||
346 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
347 | * ``` |
||
348 | * |
||
349 | * The method will properly escape the table and column names. |
||
350 | * |
||
351 | * @param string $table the table where the data will be deleted from. |
||
352 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
353 | * refer to [[Query::where()]] on how to specify condition. |
||
354 | * @param array $params the binding parameters that will be modified by this method |
||
355 | * so that they can be bound to the DB command later. |
||
356 | * @return string the DELETE SQL |
||
357 | */ |
||
358 | 308 | public function delete($table, $condition, &$params) |
|
365 | |||
366 | /** |
||
367 | * Builds a SQL statement for creating a new DB table. |
||
368 | * |
||
369 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
370 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
371 | * stands for the column type which can contain an abstract DB type. |
||
372 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
373 | * |
||
374 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
375 | * inserted into the generated SQL. |
||
376 | * |
||
377 | * For example, |
||
378 | * |
||
379 | * ```php |
||
380 | * $sql = $queryBuilder->createTable('user', [ |
||
381 | * 'id' => 'pk', |
||
382 | * 'name' => 'string', |
||
383 | * 'age' => 'integer', |
||
384 | * ]); |
||
385 | * ``` |
||
386 | * |
||
387 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
388 | * @param array $columns the columns (name => definition) in the new table. |
||
389 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
390 | * @return string the SQL statement for creating a new DB table. |
||
391 | */ |
||
392 | 110 | public function createTable($table, $columns, $options = null) |
|
406 | |||
407 | /** |
||
408 | * Builds a SQL statement for renaming a DB table. |
||
409 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
410 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
411 | * @return string the SQL statement for renaming a DB table. |
||
412 | */ |
||
413 | 1 | public function renameTable($oldName, $newName) |
|
417 | |||
418 | /** |
||
419 | * Builds a SQL statement for dropping a DB table. |
||
420 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
421 | * @return string the SQL statement for dropping a DB table. |
||
422 | */ |
||
423 | 19 | public function dropTable($table) |
|
427 | |||
428 | /** |
||
429 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
430 | * @param string $name the name of the primary key constraint. |
||
431 | * @param string $table the table that the primary key constraint will be added to. |
||
432 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
433 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
434 | */ |
||
435 | 6 | public function addPrimaryKey($name, $table, $columns) |
|
449 | |||
450 | /** |
||
451 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
452 | * @param string $name the name of the primary key constraint to be removed. |
||
453 | * @param string $table the table that the primary key constraint will be removed from. |
||
454 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
455 | */ |
||
456 | 2 | public function dropPrimaryKey($name, $table) |
|
461 | |||
462 | /** |
||
463 | * Builds a SQL statement for truncating a DB table. |
||
464 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
465 | * @return string the SQL statement for truncating a DB table. |
||
466 | */ |
||
467 | 9 | public function truncateTable($table) |
|
471 | |||
472 | /** |
||
473 | * Builds a SQL statement for adding a new DB column. |
||
474 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
475 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
476 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
477 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
478 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
479 | * @return string the SQL statement for adding a new column. |
||
480 | */ |
||
481 | 4 | public function addColumn($table, $column, $type) |
|
487 | |||
488 | /** |
||
489 | * Builds a SQL statement for dropping a DB column. |
||
490 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
491 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
492 | * @return string the SQL statement for dropping a DB column. |
||
493 | */ |
||
494 | public function dropColumn($table, $column) |
||
499 | |||
500 | /** |
||
501 | * Builds a SQL statement for renaming a column. |
||
502 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
503 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
504 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
505 | * @return string the SQL statement for renaming a DB column. |
||
506 | */ |
||
507 | public function renameColumn($table, $oldName, $newName) |
||
513 | |||
514 | /** |
||
515 | * Builds a SQL statement for changing the definition of a column. |
||
516 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
517 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
518 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
519 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
520 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
521 | * will become 'varchar(255) not null'. |
||
522 | * @return string the SQL statement for changing the definition of a column. |
||
523 | */ |
||
524 | 1 | public function alterColumn($table, $column, $type) |
|
531 | |||
532 | /** |
||
533 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
534 | * The method will properly quote the table and column names. |
||
535 | * @param string $name the name of the foreign key constraint. |
||
536 | * @param string $table the table that the foreign key constraint will be added to. |
||
537 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
538 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
539 | * @param string $refTable the table that the foreign key references to. |
||
540 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
541 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
542 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
543 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
544 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
545 | */ |
||
546 | 8 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
562 | |||
563 | /** |
||
564 | * Builds a SQL statement for dropping a foreign key constraint. |
||
565 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
566 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
567 | * @return string the SQL statement for dropping a foreign key constraint. |
||
568 | */ |
||
569 | 3 | public function dropForeignKey($name, $table) |
|
574 | |||
575 | /** |
||
576 | * Builds a SQL statement for creating a new index. |
||
577 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
578 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
579 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
580 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
581 | * by the method, unless a parenthesis is found in the name. |
||
582 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
583 | * @return string the SQL statement for creating a new index. |
||
584 | */ |
||
585 | 6 | public function createIndex($name, $table, $columns, $unique = false) |
|
592 | |||
593 | /** |
||
594 | * Builds a SQL statement for dropping an index. |
||
595 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
596 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
597 | * @return string the SQL statement for dropping an index. |
||
598 | */ |
||
599 | 4 | public function dropIndex($name, $table) |
|
603 | |||
604 | /** |
||
605 | * Creates a SQL command for adding an unique constraint to an existing table. |
||
606 | * @param string $name the name of the unique constraint. |
||
607 | * The name will be properly quoted by the method. |
||
608 | * @param string $table the table that the unique constraint will be added to. |
||
609 | * The name will be properly quoted by the method. |
||
610 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
611 | * If there are multiple columns, separate them with commas. |
||
612 | * The name will be properly quoted by the method. |
||
613 | * @return string the SQL statement for adding an unique constraint to an existing table. |
||
614 | * @since 2.0.13 |
||
615 | */ |
||
616 | 6 | public function addUnique($name, $table, $columns) |
|
629 | |||
630 | /** |
||
631 | * Creates a SQL command for dropping an unique constraint. |
||
632 | * @param string $name the name of the unique constraint to be dropped. |
||
633 | * The name will be properly quoted by the method. |
||
634 | * @param string $table the table whose unique constraint is to be dropped. |
||
635 | * The name will be properly quoted by the method. |
||
636 | * @return string the SQL statement for dropping an unique constraint. |
||
637 | * @since 2.0.13 |
||
638 | */ |
||
639 | 2 | public function dropUnique($name, $table) |
|
644 | |||
645 | /** |
||
646 | * Creates a SQL command for adding a check constraint to an existing table. |
||
647 | * @param string $name the name of the check constraint. |
||
648 | * The name will be properly quoted by the method. |
||
649 | * @param string $table the table that the check constraint will be added to. |
||
650 | * The name will be properly quoted by the method. |
||
651 | * @param string $expression the SQL of the `CHECK` constraint. |
||
652 | * @return string the SQL statement for adding a check constraint to an existing table. |
||
653 | * @since 2.0.13 |
||
654 | */ |
||
655 | 2 | public function addCheck($name, $table, $expression) |
|
660 | |||
661 | /** |
||
662 | * Creates a SQL command for dropping a check constraint. |
||
663 | * @param string $name the name of the check constraint to be dropped. |
||
664 | * The name will be properly quoted by the method. |
||
665 | * @param string $table the table whose check constraint is to be dropped. |
||
666 | * The name will be properly quoted by the method. |
||
667 | * @return string the SQL statement for dropping a check constraint. |
||
668 | * @since 2.0.13 |
||
669 | */ |
||
670 | 2 | public function dropCheck($name, $table) |
|
675 | |||
676 | /** |
||
677 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
678 | * @param string $name the name of the default value constraint. |
||
679 | * The name will be properly quoted by the method. |
||
680 | * @param string $table the table that the default value constraint will be added to. |
||
681 | * The name will be properly quoted by the method. |
||
682 | * @param string $column the name of the column to that the constraint will be added on. |
||
683 | * The name will be properly quoted by the method. |
||
684 | * @param mixed $value default value. |
||
685 | * @return string the SQL statement for adding a default value constraint to an existing table. |
||
686 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
687 | * @since 2.0.13 |
||
688 | */ |
||
689 | public function addDefaultValue($name, $table, $column, $value) |
||
693 | |||
694 | /** |
||
695 | * Creates a SQL command for dropping a default value constraint. |
||
696 | * @param string $name the name of the default value constraint to be dropped. |
||
697 | * The name will be properly quoted by the method. |
||
698 | * @param string $table the table whose default value constraint is to be dropped. |
||
699 | * The name will be properly quoted by the method. |
||
700 | * @return string the SQL statement for dropping a default value constraint. |
||
701 | * @throws NotSupportedException if this is not supported by the underlying DBMS. |
||
702 | * @since 2.0.13 |
||
703 | */ |
||
704 | public function dropDefaultValue($name, $table) |
||
708 | |||
709 | /** |
||
710 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
711 | * The sequence will be reset such that the primary key of the next new row inserted |
||
712 | * will have the specified value or 1. |
||
713 | * @param string $table the name of the table whose primary key sequence will be reset |
||
714 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
715 | * the next new row's primary key will have a value 1. |
||
716 | * @return string the SQL statement for resetting sequence |
||
717 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
718 | */ |
||
719 | public function resetSequence($table, $value = null) |
||
723 | |||
724 | /** |
||
725 | * Builds a SQL statement for enabling or disabling integrity check. |
||
726 | * @param bool $check whether to turn on or off the integrity check. |
||
727 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
728 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
729 | * @return string the SQL statement for checking integrity |
||
730 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
731 | */ |
||
732 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
736 | |||
737 | /** |
||
738 | * Builds a SQL command for adding comment to column. |
||
739 | * |
||
740 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
741 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
742 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
743 | * @return string the SQL statement for adding comment on column |
||
744 | * @since 2.0.8 |
||
745 | */ |
||
746 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
750 | |||
751 | /** |
||
752 | * Builds a SQL command for adding comment to table. |
||
753 | * |
||
754 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
755 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
756 | * @return string the SQL statement for adding comment on table |
||
757 | * @since 2.0.8 |
||
758 | */ |
||
759 | 1 | public function addCommentOnTable($table, $comment) |
|
763 | |||
764 | /** |
||
765 | * Builds a SQL command for adding comment to column. |
||
766 | * |
||
767 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
768 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
769 | * @return string the SQL statement for adding comment on column |
||
770 | * @since 2.0.8 |
||
771 | */ |
||
772 | 2 | public function dropCommentFromColumn($table, $column) |
|
776 | |||
777 | /** |
||
778 | * Builds a SQL command for adding comment to table. |
||
779 | * |
||
780 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
781 | * @return string the SQL statement for adding comment on column |
||
782 | * @since 2.0.8 |
||
783 | */ |
||
784 | 1 | public function dropCommentFromTable($table) |
|
788 | |||
789 | /** |
||
790 | * Converts an abstract column type into a physical column type. |
||
791 | * |
||
792 | * The conversion is done using the type map specified in [[typeMap]]. |
||
793 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
794 | * physical types): |
||
795 | * |
||
796 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
797 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
798 | * - `upk`: an unsigned auto-incremental primary key type, will be converted into "int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
799 | * - `char`: char type, will be converted into "char(1)" |
||
800 | * - `string`: string type, will be converted into "varchar(255)" |
||
801 | * - `text`: a long string type, will be converted into "text" |
||
802 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
803 | * - `integer`: integer type, will be converted into "int(11)" |
||
804 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
805 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
806 | * - `float``: float number type, will be converted into "float" |
||
807 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
808 | * - `datetime`: datetime type, will be converted into "datetime" |
||
809 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
810 | * - `time`: time type, will be converted into "time" |
||
811 | * - `date`: date type, will be converted into "date" |
||
812 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
813 | * - `binary`: binary data type, will be converted into "blob" |
||
814 | * |
||
815 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
816 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
817 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
818 | * |
||
819 | * For some of the abstract types you can also specify a length or precision constraint |
||
820 | * by appending it in round brackets directly to the type. |
||
821 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
822 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
823 | * be ignored. |
||
824 | * |
||
825 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
826 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
827 | * @return string physical column type. |
||
828 | */ |
||
829 | 114 | public function getColumnType($type) |
|
849 | |||
850 | /** |
||
851 | * @param array $columns |
||
852 | * @param array $params the binding parameters to be populated |
||
853 | * @param bool $distinct |
||
854 | * @param string $selectOption |
||
855 | * @return string the SELECT clause built from [[Query::$select]]. |
||
856 | */ |
||
857 | 956 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
895 | |||
896 | /** |
||
897 | * @param array $tables |
||
898 | * @param array $params the binding parameters to be populated |
||
899 | * @return string the FROM clause built from [[Query::$from]]. |
||
900 | */ |
||
901 | 956 | public function buildFrom($tables, &$params) |
|
911 | |||
912 | /** |
||
913 | * @param array $joins |
||
914 | * @param array $params the binding parameters to be populated |
||
915 | * @return string the JOIN clause built from [[Query::$join]]. |
||
916 | * @throws Exception if the $joins parameter is not in proper format |
||
917 | */ |
||
918 | 956 | public function buildJoin($joins, &$params) |
|
943 | |||
944 | /** |
||
945 | * Quotes table names passed. |
||
946 | * |
||
947 | * @param array $tables |
||
948 | * @param array $params |
||
949 | * @return array |
||
950 | */ |
||
951 | 701 | private function quoteTableNames($tables, &$params) |
|
973 | |||
974 | /** |
||
975 | * @param string|array $condition |
||
976 | * @param array $params the binding parameters to be populated |
||
977 | * @return string the WHERE clause built from [[Query::$where]]. |
||
978 | */ |
||
979 | 1019 | public function buildWhere($condition, &$params) |
|
985 | |||
986 | /** |
||
987 | * @param array $columns |
||
988 | * @param array $params the binding parameters to be populated |
||
989 | * @return string the GROUP BY clause |
||
990 | */ |
||
991 | 956 | public function buildGroupBy($columns, &$params) |
|
1007 | |||
1008 | /** |
||
1009 | * @param string|array $condition |
||
1010 | * @param array $params the binding parameters to be populated |
||
1011 | * @return string the HAVING clause built from [[Query::$having]]. |
||
1012 | */ |
||
1013 | 956 | public function buildHaving($condition, &$params) |
|
1019 | |||
1020 | /** |
||
1021 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
1022 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
1023 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
1024 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
1025 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
1026 | * @param array $params the binding parameters to be populated |
||
1027 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
1028 | */ |
||
1029 | 956 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset, &$params) |
|
1042 | |||
1043 | /** |
||
1044 | * @param array $columns |
||
1045 | * @param array $params the binding parameters to be populated |
||
1046 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
1047 | */ |
||
1048 | 956 | public function buildOrderBy($columns, &$params) |
|
1065 | |||
1066 | /** |
||
1067 | * @param int $limit |
||
1068 | * @param int $offset |
||
1069 | * @return string the LIMIT and OFFSET clauses |
||
1070 | */ |
||
1071 | 320 | public function buildLimit($limit, $offset) |
|
1083 | |||
1084 | /** |
||
1085 | * Checks to see if the given limit is effective. |
||
1086 | * @param mixed $limit the given limit |
||
1087 | * @return bool whether the limit is effective |
||
1088 | */ |
||
1089 | 618 | protected function hasLimit($limit) |
|
1093 | |||
1094 | /** |
||
1095 | * Checks to see if the given offset is effective. |
||
1096 | * @param mixed $offset the given offset |
||
1097 | * @return bool whether the offset is effective |
||
1098 | */ |
||
1099 | 618 | protected function hasOffset($offset) |
|
1103 | |||
1104 | /** |
||
1105 | * @param array $unions |
||
1106 | * @param array $params the binding parameters to be populated |
||
1107 | * @return string the UNION clause built from [[Query::$union]]. |
||
1108 | */ |
||
1109 | 658 | public function buildUnion($unions, &$params) |
|
1128 | |||
1129 | /** |
||
1130 | * Processes columns and properly quotes them if necessary. |
||
1131 | * It will join all columns into a string with comma as separators. |
||
1132 | * @param string|array $columns the columns to be processed |
||
1133 | * @return string the processing result |
||
1134 | */ |
||
1135 | 32 | public function buildColumns($columns) |
|
1154 | |||
1155 | /** |
||
1156 | * Parses the condition specification and generates the corresponding SQL expression. |
||
1157 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
1158 | * on how to specify a condition. |
||
1159 | * @param array $params the binding parameters to be populated |
||
1160 | * @return string the generated SQL expression |
||
1161 | */ |
||
1162 | 1019 | public function buildCondition($condition, &$params) |
|
1190 | |||
1191 | /** |
||
1192 | * Creates a condition based on column-value pairs. |
||
1193 | * @param array $condition the condition specification. |
||
1194 | * @param array $params the binding parameters to be populated |
||
1195 | * @return string the generated SQL expression |
||
1196 | */ |
||
1197 | 568 | public function buildHashCondition($condition, &$params) |
|
1225 | |||
1226 | /** |
||
1227 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1228 | * @param string $operator the operator to use for connecting the given operands |
||
1229 | * @param array $operands the SQL expressions to connect. |
||
1230 | * @param array $params the binding parameters to be populated |
||
1231 | * @return string the generated SQL expression |
||
1232 | */ |
||
1233 | 198 | public function buildAndCondition($operator, $operands, &$params) |
|
1256 | |||
1257 | /** |
||
1258 | * Inverts an SQL expressions with `NOT` operator. |
||
1259 | * @param string $operator the operator to use for connecting the given operands |
||
1260 | * @param array $operands the SQL expressions to connect. |
||
1261 | * @param array $params the binding parameters to be populated |
||
1262 | * @return string the generated SQL expression |
||
1263 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1264 | */ |
||
1265 | 6 | public function buildNotCondition($operator, $operands, &$params) |
|
1281 | |||
1282 | /** |
||
1283 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1284 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1285 | * @param array $operands the first operand is the column name. The second and third operands |
||
1286 | * describe the interval that column value should be in. |
||
1287 | * @param array $params the binding parameters to be populated |
||
1288 | * @return string the generated SQL expression |
||
1289 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1290 | */ |
||
1291 | 21 | public function buildBetweenCondition($operator, $operands, &$params) |
|
1323 | |||
1324 | /** |
||
1325 | * Creates an SQL expressions with the `IN` operator. |
||
1326 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1327 | * @param array $operands the first operand is the column name. If it is an array |
||
1328 | * a composite IN condition will be generated. |
||
1329 | * The second operand is an array of values that column value should be among. |
||
1330 | * If it is an empty array the generated expression will be a `false` value if |
||
1331 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1332 | * @param array $params the binding parameters to be populated |
||
1333 | * @return string the generated SQL expression |
||
1334 | * @throws Exception if wrong number of operands have been given. |
||
1335 | */ |
||
1336 | 231 | public function buildInCondition($operator, $operands, &$params) |
|
1397 | |||
1398 | /** |
||
1399 | * Builds SQL for IN condition. |
||
1400 | * |
||
1401 | * @param string $operator |
||
1402 | * @param array $columns |
||
1403 | * @param Query $values |
||
1404 | * @param array $params |
||
1405 | * @return string SQL |
||
1406 | */ |
||
1407 | 14 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
1426 | |||
1427 | /** |
||
1428 | * Builds SQL for IN condition. |
||
1429 | * |
||
1430 | * @param string $operator |
||
1431 | * @param array|\Traversable $columns |
||
1432 | * @param array $values |
||
1433 | * @param array $params |
||
1434 | * @return string SQL |
||
1435 | */ |
||
1436 | 10 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
1464 | |||
1465 | /** |
||
1466 | * Creates an SQL expressions with the `LIKE` operator. |
||
1467 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1468 | * @param array $operands an array of two or three operands |
||
1469 | * |
||
1470 | * - The first operand is the column name. |
||
1471 | * - The second operand is a single value or an array of values that column value |
||
1472 | * should be compared with. If it is an empty array the generated expression will |
||
1473 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1474 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1475 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1476 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1477 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1478 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1479 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1480 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1481 | * @param array $params the binding parameters to be populated |
||
1482 | * @return string the generated SQL expression |
||
1483 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1484 | */ |
||
1485 | 75 | public function buildLikeCondition($operator, $operands, &$params) |
|
1535 | |||
1536 | /** |
||
1537 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1538 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1539 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1540 | * @param array $params the binding parameters to be populated |
||
1541 | * @return string the generated SQL expression |
||
1542 | * @throws InvalidArgumentException if the operand is not a [[Query]] object. |
||
1543 | */ |
||
1544 | 18 | public function buildExistsCondition($operator, $operands, &$params) |
|
1553 | |||
1554 | /** |
||
1555 | * Creates an SQL expressions like `"column" operator value`. |
||
1556 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1557 | * @param array $operands contains two column names. |
||
1558 | * @param array $params the binding parameters to be populated |
||
1559 | * @return string the generated SQL expression |
||
1560 | * @throws InvalidArgumentException if wrong number of operands have been given. |
||
1561 | */ |
||
1562 | 36 | public function buildSimpleCondition($operator, $operands, &$params) |
|
1591 | |||
1592 | /** |
||
1593 | * Creates a SELECT EXISTS() SQL statement. |
||
1594 | * @param string $rawSql the subquery in a raw form to select from. |
||
1595 | * @return string the SELECT EXISTS() SQL statement. |
||
1596 | * @since 2.0.8 |
||
1597 | */ |
||
1598 | 60 | public function selectExists($rawSql) |
|
1602 | } |
||
1603 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.