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\Object |
||
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 | |||
70 | /** |
||
71 | * Constructor. |
||
72 | * @param Connection $connection the database connection. |
||
73 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
74 | */ |
||
75 | 853 | public function __construct($connection, $config = []) |
|
80 | |||
81 | /** |
||
82 | * Generates a SELECT SQL statement from a [[Query]] object. |
||
83 | * @param Query $query the [[Query]] object from which the SQL statement will be generated. |
||
84 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will |
||
85 | * be included in the result with the additional parameters generated during the query building process. |
||
86 | * @return array the generated SQL statement (the first array element) and the corresponding |
||
87 | * parameters to be bound to the SQL statement (the second array element). The parameters returned |
||
88 | * include those provided in `$params`. |
||
89 | */ |
||
90 | 559 | public function build($query, $params = []) |
|
130 | |||
131 | /** |
||
132 | * Creates an INSERT SQL statement. |
||
133 | * For example, |
||
134 | * |
||
135 | * ```php |
||
136 | * $sql = $queryBuilder->insert('user', [ |
||
137 | * 'name' => 'Sam', |
||
138 | * 'age' => 30, |
||
139 | * ], $params); |
||
140 | * ``` |
||
141 | * |
||
142 | * The method will properly escape the table and column names. |
||
143 | * |
||
144 | * @param string $table the table that new rows will be inserted into. |
||
145 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
||
146 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
147 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
148 | * @param array $params the binding parameters that will be generated by this method. |
||
149 | * They should be bound to the DB command later. |
||
150 | * @return string the INSERT SQL |
||
151 | */ |
||
152 | 118 | public function insert($table, $columns, &$params) |
|
153 | { |
||
154 | 118 | $schema = $this->db->getSchema(); |
|
155 | 118 | if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
|
156 | 113 | $columnSchemas = $tableSchema->columns; |
|
157 | 113 | } else { |
|
158 | 19 | $columnSchemas = []; |
|
159 | } |
||
160 | 118 | $names = []; |
|
161 | 118 | $placeholders = []; |
|
162 | 118 | $values = ' DEFAULT VALUES'; |
|
163 | 118 | if ($columns instanceof \yii\db\Query) { |
|
164 | 10 | list($names, $values) = $this->prepareInsertSelectSubQuery($columns, $schema); |
|
165 | 4 | } else { |
|
166 | 112 | foreach ($columns as $name => $value) { |
|
167 | 110 | $names[] = $schema->quoteColumnName($name); |
|
168 | 110 | if ($value instanceof Expression) { |
|
169 | 4 | $placeholders[] = $value->expression; |
|
170 | 4 | foreach ($value->params as $n => $v) { |
|
171 | 1 | $params[$n] = $v; |
|
172 | 4 | } |
|
173 | 111 | } elseif ($value instanceof \yii\db\Query) { |
|
174 | 2 | list($sql, $params) = $this->build($value, $params); |
|
175 | 2 | $placeholders[] = "($sql)"; |
|
176 | 2 | } else { |
|
177 | 108 | $phName = self::PARAM_PREFIX . count($params); |
|
178 | 108 | $placeholders[] = $phName; |
|
179 | 108 | $params[$phName] = !is_array($value) && isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value; |
|
180 | } |
||
181 | 112 | } |
|
182 | } |
||
183 | |||
184 | 112 | return 'INSERT INTO ' . $schema->quoteTableName($table) |
|
185 | 112 | . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '') |
|
186 | 112 | . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values); |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. |
||
191 | * |
||
192 | * @param \yii\db\Query $columns Object, which represents select query |
||
193 | * @param \yii\db\Schema $schema Schema object to qoute column name |
||
194 | * @return array |
||
195 | * @since 2.0.11 |
||
196 | */ |
||
197 | 15 | protected function prepareInsertSelectSubQuery($columns, $schema) |
|
218 | |||
219 | /** |
||
220 | * Generates a batch INSERT SQL statement. |
||
221 | * For example, |
||
222 | * |
||
223 | * ```php |
||
224 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
225 | * ['Tom', 30], |
||
226 | * ['Jane', 20], |
||
227 | * ['Linda', 25], |
||
228 | * ]); |
||
229 | * ``` |
||
230 | * |
||
231 | * Note that the values in each row must match the corresponding column names. |
||
232 | * |
||
233 | * The method will properly escape the column names, and quote the values to be inserted. |
||
234 | * |
||
235 | * @param string $table the table that new rows will be inserted into. |
||
236 | * @param array $columns the column names |
||
237 | * @param array $rows the rows to be batch inserted into the table |
||
238 | * @return string the batch INSERT SQL statement |
||
239 | */ |
||
240 | 18 | public function batchInsert($table, $columns, $rows) |
|
282 | |||
283 | /** |
||
284 | * Creates an UPDATE SQL statement. |
||
285 | * For example, |
||
286 | * |
||
287 | * ```php |
||
288 | * $params = []; |
||
289 | * $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
||
290 | * ``` |
||
291 | * |
||
292 | * The method will properly escape the table and column names. |
||
293 | * |
||
294 | * @param string $table the table to be updated. |
||
295 | * @param array $columns the column data (name => value) to be updated. |
||
296 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
297 | * refer to [[Query::where()]] on how to specify condition. |
||
298 | * @param array $params the binding parameters that will be modified by this method |
||
299 | * so that they can be bound to the DB command later. |
||
300 | * @return string the UPDATE SQL |
||
301 | */ |
||
302 | 74 | public function update($table, $columns, $condition, &$params) |
|
329 | |||
330 | /** |
||
331 | * Creates a DELETE SQL statement. |
||
332 | * For example, |
||
333 | * |
||
334 | * ```php |
||
335 | * $sql = $queryBuilder->delete('user', 'status = 0'); |
||
336 | * ``` |
||
337 | * |
||
338 | * The method will properly escape the table and column names. |
||
339 | * |
||
340 | * @param string $table the table where the data will be deleted from. |
||
341 | * @param array|string $condition the condition that will be put in the WHERE part. Please |
||
342 | * refer to [[Query::where()]] on how to specify condition. |
||
343 | * @param array $params the binding parameters that will be modified by this method |
||
344 | * so that they can be bound to the DB command later. |
||
345 | * @return string the DELETE SQL |
||
346 | */ |
||
347 | 152 | public function delete($table, $condition, &$params) |
|
348 | 1 | { |
|
349 | 151 | $sql = 'DELETE FROM ' . $this->db->quoteTableName($table); |
|
350 | 152 | $where = $this->buildWhere($condition, $params); |
|
351 | |||
352 | 151 | return $where === '' ? $sql : $sql . ' ' . $where; |
|
353 | } |
||
354 | |||
355 | /** |
||
356 | * Builds a SQL statement for creating a new DB table. |
||
357 | * |
||
358 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
359 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
360 | * stands for the column type which can contain an abstract DB type. |
||
361 | * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
362 | * |
||
363 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
364 | * inserted into the generated SQL. |
||
365 | * |
||
366 | * For example, |
||
367 | * |
||
368 | * ```php |
||
369 | * $sql = $queryBuilder->createTable('user', [ |
||
370 | * 'id' => 'pk', |
||
371 | * 'name' => 'string', |
||
372 | * 'age' => 'integer', |
||
373 | * ]); |
||
374 | * ``` |
||
375 | * |
||
376 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
377 | * @param array $columns the columns (name => definition) in the new table. |
||
378 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
379 | * @return string the SQL statement for creating a new DB table. |
||
380 | */ |
||
381 | 68 | public function createTable($table, $columns, $options = null) |
|
395 | |||
396 | /** |
||
397 | * Builds a SQL statement for renaming a DB table. |
||
398 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
399 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
400 | * @return string the SQL statement for renaming a DB table. |
||
401 | */ |
||
402 | 1 | public function renameTable($oldName, $newName) |
|
406 | |||
407 | /** |
||
408 | * Builds a SQL statement for dropping a DB table. |
||
409 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
410 | * @return string the SQL statement for dropping a DB table. |
||
411 | */ |
||
412 | 11 | public function dropTable($table) |
|
416 | |||
417 | /** |
||
418 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
419 | * @param string $name the name of the primary key constraint. |
||
420 | * @param string $table the table that the primary key constraint will be added to. |
||
421 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
422 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
423 | */ |
||
424 | 2 | public function addPrimaryKey($name, $table, $columns) |
|
438 | |||
439 | /** |
||
440 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
441 | * @param string $name the name of the primary key constraint to be removed. |
||
442 | * @param string $table the table that the primary key constraint will be removed from. |
||
443 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
444 | */ |
||
445 | 1 | public function dropPrimaryKey($name, $table) |
|
450 | |||
451 | /** |
||
452 | * Builds a SQL statement for truncating a DB table. |
||
453 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
454 | * @return string the SQL statement for truncating a DB table. |
||
455 | */ |
||
456 | 6 | public function truncateTable($table) |
|
460 | |||
461 | /** |
||
462 | * Builds a SQL statement for adding a new DB column. |
||
463 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
464 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
465 | * @param string $type the column type. The [[getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
466 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
467 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
468 | * @return string the SQL statement for adding a new column. |
||
469 | */ |
||
470 | 4 | public function addColumn($table, $column, $type) |
|
476 | |||
477 | /** |
||
478 | * Builds a SQL statement for dropping a DB column. |
||
479 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
480 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
481 | * @return string the SQL statement for dropping a DB column. |
||
482 | */ |
||
483 | public function dropColumn($table, $column) |
||
488 | |||
489 | /** |
||
490 | * Builds a SQL statement for renaming a column. |
||
491 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
492 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
493 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
494 | * @return string the SQL statement for renaming a DB column. |
||
495 | */ |
||
496 | public function renameColumn($table, $oldName, $newName) |
||
502 | |||
503 | /** |
||
504 | * Builds a SQL statement for changing the definition of a column. |
||
505 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
506 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
507 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
508 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
509 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
510 | * will become 'varchar(255) not null'. |
||
511 | * @return string the SQL statement for changing the definition of a column. |
||
512 | */ |
||
513 | 1 | public function alterColumn($table, $column, $type) |
|
520 | |||
521 | /** |
||
522 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
523 | * The method will properly quote the table and column names. |
||
524 | * @param string $name the name of the foreign key constraint. |
||
525 | * @param string $table the table that the foreign key constraint will be added to. |
||
526 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
527 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
528 | * @param string $refTable the table that the foreign key references to. |
||
529 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
530 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
531 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
532 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
533 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
534 | */ |
||
535 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
551 | |||
552 | /** |
||
553 | * Builds a SQL statement for dropping a foreign key constraint. |
||
554 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
555 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
556 | * @return string the SQL statement for dropping a foreign key constraint. |
||
557 | */ |
||
558 | public function dropForeignKey($name, $table) |
||
563 | |||
564 | /** |
||
565 | * Builds a SQL statement for creating a new index. |
||
566 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
567 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
568 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
569 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
570 | * by the method, unless a parenthesis is found in the name. |
||
571 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
572 | * @return string the SQL statement for creating a new index. |
||
573 | */ |
||
574 | 1 | public function createIndex($name, $table, $columns, $unique = false) |
|
581 | |||
582 | /** |
||
583 | * Builds a SQL statement for dropping an index. |
||
584 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
585 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
586 | * @return string the SQL statement for dropping an index. |
||
587 | */ |
||
588 | public function dropIndex($name, $table) |
||
592 | |||
593 | /** |
||
594 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
595 | * The sequence will be reset such that the primary key of the next new row inserted |
||
596 | * will have the specified value or 1. |
||
597 | * @param string $table the name of the table whose primary key sequence will be reset |
||
598 | * @param array|string $value the value for the primary key of the next new row inserted. If this is not set, |
||
599 | * the next new row's primary key will have a value 1. |
||
600 | * @return string the SQL statement for resetting sequence |
||
601 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
602 | */ |
||
603 | public function resetSequence($table, $value = null) |
||
607 | |||
608 | /** |
||
609 | * Builds a SQL statement for enabling or disabling integrity check. |
||
610 | * @param bool $check whether to turn on or off the integrity check. |
||
611 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
612 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
613 | * @return string the SQL statement for checking integrity |
||
614 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
615 | */ |
||
616 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
620 | |||
621 | /** |
||
622 | * Builds a SQL command for adding comment to column |
||
623 | * |
||
624 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
625 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
626 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
627 | * @return string the SQL statement for adding comment on column |
||
628 | * @since 2.0.8 |
||
629 | */ |
||
630 | 1 | public function addCommentOnColumn($table, $column, $comment) |
|
635 | |||
636 | /** |
||
637 | * Builds a SQL command for adding comment to table |
||
638 | * |
||
639 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
640 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
641 | * @return string the SQL statement for adding comment on table |
||
642 | * @since 2.0.8 |
||
643 | */ |
||
644 | 1 | public function addCommentOnTable($table, $comment) |
|
648 | |||
649 | /** |
||
650 | * Builds a SQL command for adding comment to column |
||
651 | * |
||
652 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
653 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
654 | * @return string the SQL statement for adding comment on column |
||
655 | * @since 2.0.8 |
||
656 | */ |
||
657 | 1 | public function dropCommentFromColumn($table, $column) |
|
661 | |||
662 | /** |
||
663 | * Builds a SQL command for adding comment to table |
||
664 | * |
||
665 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
666 | * @return string the SQL statement for adding comment on column |
||
667 | * @since 2.0.8 |
||
668 | */ |
||
669 | 1 | public function dropCommentFromTable($table) |
|
673 | |||
674 | /** |
||
675 | * Converts an abstract column type into a physical column type. |
||
676 | * The conversion is done using the type map specified in [[typeMap]]. |
||
677 | * The following abstract column types are supported (using MySQL as an example to explain the corresponding |
||
678 | * physical types): |
||
679 | * |
||
680 | * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
681 | * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
682 | * - `unsignedpk`: an unsigned auto-incremental primary key type, will be converted into "int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" |
||
683 | * - `char`: char type, will be converted into "char(1)" |
||
684 | * - `string`: string type, will be converted into "varchar(255)" |
||
685 | * - `text`: a long string type, will be converted into "text" |
||
686 | * - `smallint`: a small integer type, will be converted into "smallint(6)" |
||
687 | * - `integer`: integer type, will be converted into "int(11)" |
||
688 | * - `bigint`: a big integer type, will be converted into "bigint(20)" |
||
689 | * - `boolean`: boolean type, will be converted into "tinyint(1)" |
||
690 | * - `float``: float number type, will be converted into "float" |
||
691 | * - `decimal`: decimal number type, will be converted into "decimal" |
||
692 | * - `datetime`: datetime type, will be converted into "datetime" |
||
693 | * - `timestamp`: timestamp type, will be converted into "timestamp" |
||
694 | * - `time`: time type, will be converted into "time" |
||
695 | * - `date`: date type, will be converted into "date" |
||
696 | * - `money`: money type, will be converted into "decimal(19,4)" |
||
697 | * - `binary`: binary data type, will be converted into "blob" |
||
698 | * |
||
699 | * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only |
||
700 | * the first part will be converted, and the rest of the parts will be appended to the converted result. |
||
701 | * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. |
||
702 | * |
||
703 | * For some of the abstract types you can also specify a length or precision constraint |
||
704 | * by appending it in round brackets directly to the type. |
||
705 | * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. |
||
706 | * If the underlying DBMS does not support these kind of constraints for a type it will |
||
707 | * be ignored. |
||
708 | * |
||
709 | * If a type cannot be found in [[typeMap]], it will be returned without any change. |
||
710 | * @param string|ColumnSchemaBuilder $type abstract column type |
||
711 | * @return string physical column type. |
||
712 | */ |
||
713 | 71 | public function getColumnType($type) |
|
733 | |||
734 | /** |
||
735 | * @param array $columns |
||
736 | * @param array $params the binding parameters to be populated |
||
737 | * @param bool $distinct |
||
738 | * @param string $selectOption |
||
739 | * @return string the SELECT clause built from [[Query::$select]]. |
||
740 | */ |
||
741 | 800 | public function buildSelect($columns, &$params, $distinct = false, $selectOption = null) |
|
779 | |||
780 | /** |
||
781 | * @param array $tables |
||
782 | * @param array $params the binding parameters to be populated |
||
783 | * @return string the FROM clause built from [[Query::$from]]. |
||
784 | */ |
||
785 | 800 | public function buildFrom($tables, &$params) |
|
795 | |||
796 | /** |
||
797 | * @param array $joins |
||
798 | * @param array $params the binding parameters to be populated |
||
799 | * @return string the JOIN clause built from [[Query::$join]]. |
||
800 | * @throws Exception if the $joins parameter is not in proper format |
||
801 | */ |
||
802 | 800 | public function buildJoin($joins, &$params) |
|
827 | |||
828 | /** |
||
829 | * Quotes table names passed |
||
830 | * |
||
831 | * @param array $tables |
||
832 | * @param array $params |
||
833 | * @return array |
||
834 | */ |
||
835 | 551 | private function quoteTableNames($tables, &$params) |
|
856 | |||
857 | /** |
||
858 | * @param string|array $condition |
||
859 | * @param array $params the binding parameters to be populated |
||
860 | * @return string the WHERE clause built from [[Query::$where]]. |
||
861 | */ |
||
862 | 818 | public function buildWhere($condition, &$params) |
|
868 | |||
869 | /** |
||
870 | * @param array $columns |
||
871 | * @return string the GROUP BY clause |
||
872 | */ |
||
873 | 800 | public function buildGroupBy($columns) |
|
887 | |||
888 | /** |
||
889 | * @param string|array $condition |
||
890 | * @param array $params the binding parameters to be populated |
||
891 | * @return string the HAVING clause built from [[Query::$having]]. |
||
892 | */ |
||
893 | 800 | public function buildHaving($condition, &$params) |
|
899 | |||
900 | /** |
||
901 | * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. |
||
902 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
903 | * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. |
||
904 | * @param int $limit the limit number. See [[Query::limit]] for more details. |
||
905 | * @param int $offset the offset number. See [[Query::offset]] for more details. |
||
906 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
907 | */ |
||
908 | 800 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
920 | |||
921 | /** |
||
922 | * @param array $columns |
||
923 | * @return string the ORDER BY clause built from [[Query::$orderBy]]. |
||
924 | */ |
||
925 | 800 | public function buildOrderBy($columns) |
|
941 | |||
942 | /** |
||
943 | * @param int $limit |
||
944 | * @param int $offset |
||
945 | * @return string the LIMIT and OFFSET clauses |
||
946 | */ |
||
947 | 233 | public function buildLimit($limit, $offset) |
|
959 | |||
960 | /** |
||
961 | * Checks to see if the given limit is effective. |
||
962 | * @param mixed $limit the given limit |
||
963 | * @return bool whether the limit is effective |
||
964 | */ |
||
965 | 474 | protected function hasLimit($limit) |
|
969 | |||
970 | /** |
||
971 | * Checks to see if the given offset is effective. |
||
972 | * @param mixed $offset the given offset |
||
973 | * @return bool whether the offset is effective |
||
974 | */ |
||
975 | 474 | protected function hasOffset($offset) |
|
979 | |||
980 | /** |
||
981 | * @param array $unions |
||
982 | * @param array $params the binding parameters to be populated |
||
983 | * @return string the UNION clause built from [[Query::$union]]. |
||
984 | */ |
||
985 | 559 | public function buildUnion($unions, &$params) |
|
1004 | |||
1005 | /** |
||
1006 | * Processes columns and properly quotes them if necessary. |
||
1007 | * It will join all columns into a string with comma as separators. |
||
1008 | * @param string|array $columns the columns to be processed |
||
1009 | * @return string the processing result |
||
1010 | */ |
||
1011 | 3 | public function buildColumns($columns) |
|
1030 | |||
1031 | /** |
||
1032 | * Parses the condition specification and generates the corresponding SQL expression. |
||
1033 | * @param string|array|Expression $condition the condition specification. Please refer to [[Query::where()]] |
||
1034 | * on how to specify a condition. |
||
1035 | * @param array $params the binding parameters to be populated |
||
1036 | * @return string the generated SQL expression |
||
1037 | */ |
||
1038 | 818 | public function buildCondition($condition, &$params) |
|
1064 | |||
1065 | /** |
||
1066 | * Creates a condition based on column-value pairs. |
||
1067 | * @param array $condition the condition specification. |
||
1068 | * @param array $params the binding parameters to be populated |
||
1069 | * @return string the generated SQL expression |
||
1070 | */ |
||
1071 | 414 | public function buildHashCondition($condition, &$params) |
|
1098 | |||
1099 | /** |
||
1100 | * Connects two or more SQL expressions with the `AND` or `OR` operator. |
||
1101 | * @param string $operator the operator to use for connecting the given operands |
||
1102 | * @param array $operands the SQL expressions to connect. |
||
1103 | * @param array $params the binding parameters to be populated |
||
1104 | * @return string the generated SQL expression |
||
1105 | */ |
||
1106 | 151 | public function buildAndCondition($operator, $operands, &$params) |
|
1129 | |||
1130 | /** |
||
1131 | * Inverts an SQL expressions with `NOT` operator. |
||
1132 | * @param string $operator the operator to use for connecting the given operands |
||
1133 | * @param array $operands the SQL expressions to connect. |
||
1134 | * @param array $params the binding parameters to be populated |
||
1135 | * @return string the generated SQL expression |
||
1136 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1137 | */ |
||
1138 | 3 | public function buildNotCondition($operator, $operands, &$params) |
|
1154 | |||
1155 | /** |
||
1156 | * Creates an SQL expressions with the `BETWEEN` operator. |
||
1157 | * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||
1158 | * @param array $operands the first operand is the column name. The second and third operands |
||
1159 | * describe the interval that column value should be in. |
||
1160 | * @param array $params the binding parameters to be populated |
||
1161 | * @return string the generated SQL expression |
||
1162 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1163 | */ |
||
1164 | 21 | public function buildBetweenCondition($operator, $operands, &$params) |
|
1196 | |||
1197 | /** |
||
1198 | * Creates an SQL expressions with the `IN` operator. |
||
1199 | * @param string $operator the operator to use (e.g. `IN` or `NOT IN`) |
||
1200 | * @param array $operands the first operand is the column name. If it is an array |
||
1201 | * a composite IN condition will be generated. |
||
1202 | * The second operand is an array of values that column value should be among. |
||
1203 | * If it is an empty array the generated expression will be a `false` value if |
||
1204 | * operator is `IN` and empty if operator is `NOT IN`. |
||
1205 | * @param array $params the binding parameters to be populated |
||
1206 | * @return string the generated SQL expression |
||
1207 | * @throws Exception if wrong number of operands have been given. |
||
1208 | */ |
||
1209 | 214 | public function buildInCondition($operator, $operands, &$params) |
|
1270 | |||
1271 | /** |
||
1272 | * Builds SQL for IN condition |
||
1273 | * |
||
1274 | * @param string $operator |
||
1275 | * @param array $columns |
||
1276 | * @param Query $values |
||
1277 | * @param array $params |
||
1278 | * @return string SQL |
||
1279 | */ |
||
1280 | 14 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
1297 | |||
1298 | /** |
||
1299 | * Builds SQL for IN condition |
||
1300 | * |
||
1301 | * @param string $operator |
||
1302 | * @param array|\Traversable $columns |
||
1303 | * @param array $values |
||
1304 | * @param array $params |
||
1305 | * @return string SQL |
||
1306 | */ |
||
1307 | 10 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
1335 | |||
1336 | /** |
||
1337 | * Creates an SQL expressions with the `LIKE` operator. |
||
1338 | * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`) |
||
1339 | * @param array $operands an array of two or three operands |
||
1340 | * |
||
1341 | * - The first operand is the column name. |
||
1342 | * - The second operand is a single value or an array of values that column value |
||
1343 | * should be compared with. If it is an empty array the generated expression will |
||
1344 | * be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator |
||
1345 | * is `NOT LIKE` or `OR NOT LIKE`. |
||
1346 | * - An optional third operand can also be provided to specify how to escape special characters |
||
1347 | * in the value(s). The operand should be an array of mappings from the special characters to their |
||
1348 | * escaped counterparts. If this operand is not provided, a default escape mapping will be used. |
||
1349 | * You may use `false` or an empty array to indicate the values are already escaped and no escape |
||
1350 | * should be applied. Note that when using an escape mapping (or the third operand is not provided), |
||
1351 | * the values will be automatically enclosed within a pair of percentage characters. |
||
1352 | * @param array $params the binding parameters to be populated |
||
1353 | * @return string the generated SQL expression |
||
1354 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1355 | */ |
||
1356 | 72 | public function buildLikeCondition($operator, $operands, &$params) |
|
1402 | |||
1403 | /** |
||
1404 | * Creates an SQL expressions with the `EXISTS` operator. |
||
1405 | * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`) |
||
1406 | * @param array $operands contains only one element which is a [[Query]] object representing the sub-query. |
||
1407 | * @param array $params the binding parameters to be populated |
||
1408 | * @return string the generated SQL expression |
||
1409 | * @throws InvalidParamException if the operand is not a [[Query]] object. |
||
1410 | */ |
||
1411 | 18 | public function buildExistsCondition($operator, $operands, &$params) |
|
1420 | |||
1421 | /** |
||
1422 | * Creates an SQL expressions like `"column" operator value`. |
||
1423 | * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc. |
||
1424 | * @param array $operands contains two column names. |
||
1425 | * @param array $params the binding parameters to be populated |
||
1426 | * @return string the generated SQL expression |
||
1427 | * @throws InvalidParamException if wrong number of operands have been given. |
||
1428 | */ |
||
1429 | 30 | public function buildSimpleCondition($operator, $operands, &$params) |
|
1457 | |||
1458 | /** |
||
1459 | * Creates a SELECT EXISTS() SQL statement. |
||
1460 | * @param string $rawSql the subquery in a raw form to select from. |
||
1461 | * @return string the SELECT EXISTS() SQL statement. |
||
1462 | * @since 2.0.8 |
||
1463 | */ |
||
1464 | 53 | public function selectExists($rawSql) |
|
1468 | } |
||
1469 |