Complex classes like Command 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 Command, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class Command extends Component |
||
58 | { |
||
59 | /** |
||
60 | * @var Connection the DB connection that this command is associated with |
||
61 | */ |
||
62 | public $db; |
||
63 | /** |
||
64 | * @var \PDOStatement the PDOStatement object that this command is associated with |
||
65 | */ |
||
66 | public $pdoStatement; |
||
67 | /** |
||
68 | * @var int the default fetch mode for this command. |
||
69 | * @see http://www.php.net/manual/en/pdostatement.setfetchmode.php |
||
70 | */ |
||
71 | public $fetchMode = \PDO::FETCH_ASSOC; |
||
72 | /** |
||
73 | * @var array the parameters (name => value) that are bound to the current PDO statement. |
||
74 | * This property is maintained by methods such as [[bindValue()]]. It is mainly provided for logging purpose |
||
75 | * and is used to generate [[rawSql]]. Do not modify it directly. |
||
76 | */ |
||
77 | public $params = []; |
||
78 | /** |
||
79 | * @var int the default number of seconds that query results can remain valid in cache. |
||
80 | * Use 0 to indicate that the cached data will never expire. And use a negative number to indicate |
||
81 | * query cache should not be used. |
||
82 | * @see cache() |
||
83 | */ |
||
84 | public $queryCacheDuration; |
||
85 | /** |
||
86 | * @var \yii\caching\Dependency the dependency to be associated with the cached query result for this command |
||
87 | * @see cache() |
||
88 | */ |
||
89 | public $queryCacheDependency; |
||
90 | |||
91 | /** |
||
92 | * @var array pending parameters to be bound to the current PDO statement. |
||
93 | */ |
||
94 | private $_pendingParams = []; |
||
95 | /** |
||
96 | * @var string the SQL statement that this command represents |
||
97 | */ |
||
98 | private $_sql; |
||
99 | /** |
||
100 | * @var string name of the table, which schema, should be refreshed after command execution. |
||
101 | */ |
||
102 | private $_refreshTableName; |
||
103 | /** |
||
104 | * @var string|false|null the isolation level to use for this transaction. |
||
105 | * See [[Transaction::begin()]] for details. |
||
106 | */ |
||
107 | private $_isolationLevel = false; |
||
108 | /** |
||
109 | * @var callable a callable (e.g. anonymous function) that is called when [[\yii\db\Exception]] is thrown |
||
110 | * when executing the command. |
||
111 | */ |
||
112 | private $_retryHandler; |
||
113 | |||
114 | /** |
||
115 | * Enables query cache for this command. |
||
116 | * @param int $duration the number of seconds that query result of this command can remain valid in the cache. |
||
117 | * If this is not set, the value of [[Connection::queryCacheDuration]] will be used instead. |
||
118 | * Use 0 to indicate that the cached data will never expire. |
||
119 | * @param \yii\caching\Dependency $dependency the cache dependency associated with the cached query result. |
||
120 | * @return $this the command object itself |
||
121 | */ |
||
122 | 6 | public function cache($duration = null, $dependency = null) |
|
128 | |||
129 | /** |
||
130 | * Disables query cache for this command. |
||
131 | * @return $this the command object itself |
||
132 | */ |
||
133 | 3 | public function noCache() |
|
138 | |||
139 | /** |
||
140 | * Returns the SQL statement for this command. |
||
141 | * @return string the SQL statement to be executed |
||
142 | */ |
||
143 | 1313 | public function getSql() |
|
147 | |||
148 | /** |
||
149 | * Specifies the SQL statement to be executed. The SQL statement will be quoted using [[Connection::quoteSql()]]. |
||
150 | * The previous SQL (if any) will be discarded, and [[params]] will be cleared as well. See [[reset()]] |
||
151 | * for details. |
||
152 | * |
||
153 | * @param string $sql the SQL statement to be set. |
||
154 | * @return $this this command instance |
||
155 | * @see reset() |
||
156 | * @see cancel() |
||
157 | */ |
||
158 | 1334 | public function setSql($sql) |
|
168 | |||
169 | /** |
||
170 | * Specifies the SQL statement to be executed. The SQL statement will not be modified in any way. |
||
171 | * The previous SQL (if any) will be discarded, and [[params]] will be cleared as well. See [[reset()]] |
||
172 | * for details. |
||
173 | * |
||
174 | * @param string $sql the SQL statement to be set. |
||
175 | * @return $this this command instance |
||
176 | * @since 2.0.13 |
||
177 | * @see reset() |
||
178 | * @see cancel() |
||
179 | */ |
||
180 | 26 | public function setRawSql($sql) |
|
190 | |||
191 | /** |
||
192 | * Returns the raw SQL by inserting parameter values into the corresponding placeholders in [[sql]]. |
||
193 | * Note that the return value of this method should mainly be used for logging purpose. |
||
194 | * It is likely that this method returns an invalid SQL due to improper replacement of parameter placeholders. |
||
195 | * @return string the raw SQL with parameter values inserted into the corresponding placeholders in [[sql]]. |
||
196 | */ |
||
197 | 1316 | public function getRawSql() |
|
227 | |||
228 | /** |
||
229 | * Prepares the SQL statement to be executed. |
||
230 | * For complex SQL statement that is to be executed multiple times, |
||
231 | * this may improve performance. |
||
232 | * For SQL statement with binding parameters, this method is invoked |
||
233 | * automatically. |
||
234 | * @param bool $forRead whether this method is called for a read query. If null, it means |
||
235 | * the SQL statement should be used to determine whether it is for read or write. |
||
236 | * @throws Exception if there is any DB error |
||
237 | */ |
||
238 | 1301 | public function prepare($forRead = null) |
|
266 | |||
267 | /** |
||
268 | * Cancels the execution of the SQL statement. |
||
269 | * This method mainly sets [[pdoStatement]] to be null. |
||
270 | */ |
||
271 | 1334 | public function cancel() |
|
275 | |||
276 | /** |
||
277 | * Binds a parameter to the SQL statement to be executed. |
||
278 | * @param string|int $name parameter identifier. For a prepared statement |
||
279 | * using named placeholders, this will be a parameter name of |
||
280 | * the form `:name`. For a prepared statement using question mark |
||
281 | * placeholders, this will be the 1-indexed position of the parameter. |
||
282 | * @param mixed $value the PHP variable to bind to the SQL statement parameter (passed by reference) |
||
283 | * @param int $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
||
284 | * @param int $length length of the data type |
||
285 | * @param mixed $driverOptions the driver-specific options |
||
286 | * @return $this the current command being executed |
||
287 | * @see http://www.php.net/manual/en/function.PDOStatement-bindParam.php |
||
288 | */ |
||
289 | 3 | public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null) |
|
307 | |||
308 | /** |
||
309 | * Binds pending parameters that were registered via [[bindValue()]] and [[bindValues()]]. |
||
310 | * Note that this method requires an active [[pdoStatement]]. |
||
311 | */ |
||
312 | 1300 | protected function bindPendingParams() |
|
319 | |||
320 | /** |
||
321 | * Binds a value to a parameter. |
||
322 | * @param string|int $name Parameter identifier. For a prepared statement |
||
323 | * using named placeholders, this will be a parameter name of |
||
324 | * the form `:name`. For a prepared statement using question mark |
||
325 | * placeholders, this will be the 1-indexed position of the parameter. |
||
326 | * @param mixed $value The value to bind to the parameter |
||
327 | * @param int $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
||
328 | * @return $this the current command being executed |
||
329 | * @see http://www.php.net/manual/en/function.PDOStatement-bindValue.php |
||
330 | */ |
||
331 | 6 | public function bindValue($name, $value, $dataType = null) |
|
341 | |||
342 | /** |
||
343 | * Binds a list of values to the corresponding parameters. |
||
344 | * This is similar to [[bindValue()]] except that it binds multiple values at a time. |
||
345 | * Note that the SQL data type of each value is determined by its PHP type. |
||
346 | * @param array $values the values to be bound. This must be given in terms of an associative |
||
347 | * array with array keys being the parameter names, and array values the corresponding parameter values, |
||
348 | * e.g. `[':name' => 'John', ':age' => 25]`. By default, the PDO type of each value is determined |
||
349 | * by its PHP type. You may explicitly specify the PDO type by using a [[yii\db\PdoValue]] class: `new PdoValue(value, type)`, |
||
350 | * e.g. `[':name' => 'John', ':profile' => new PdoValue($profile, \PDO::PARAM_LOB)]`. |
||
351 | * @return $this the current command being executed |
||
352 | */ |
||
353 | 1334 | public function bindValues($values) |
|
373 | |||
374 | /** |
||
375 | * Executes the SQL statement and returns query result. |
||
376 | * This method is for executing a SQL query that returns result set, such as `SELECT`. |
||
377 | * @return DataReader the reader object for fetching the query result |
||
378 | * @throws Exception execution failed |
||
379 | */ |
||
380 | 9 | public function query() |
|
384 | |||
385 | /** |
||
386 | * Executes the SQL statement and returns ALL rows at once. |
||
387 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
||
388 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
||
389 | * @return array all rows of the query result. Each array element is an array representing a row of data. |
||
390 | * An empty array is returned if the query results in nothing. |
||
391 | * @throws Exception execution failed |
||
392 | */ |
||
393 | 1135 | public function queryAll($fetchMode = null) |
|
397 | |||
398 | /** |
||
399 | * Executes the SQL statement and returns the first row of the result. |
||
400 | * This method is best used when only the first row of result is needed for a query. |
||
401 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://php.net/manual/en/pdostatement.setfetchmode.php) |
||
402 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
||
403 | * @return array|false the first row (in terms of an array) of the query result. False is returned if the query |
||
404 | * results in nothing. |
||
405 | * @throws Exception execution failed |
||
406 | */ |
||
407 | 478 | public function queryOne($fetchMode = null) |
|
411 | |||
412 | /** |
||
413 | * Executes the SQL statement and returns the value of the first column in the first row of data. |
||
414 | * This method is best used when only a single value is needed for a query. |
||
415 | * @return string|null|false the value of the first column in the first row of the query result. |
||
416 | * False is returned if there is no value. |
||
417 | * @throws Exception execution failed |
||
418 | */ |
||
419 | 308 | public function queryScalar() |
|
428 | |||
429 | /** |
||
430 | * Executes the SQL statement and returns the first column of the result. |
||
431 | * This method is best used when only the first column of result (i.e. the first element in each row) |
||
432 | * is needed for a query. |
||
433 | * @return array the first column of the query result. Empty array is returned if the query results in nothing. |
||
434 | * @throws Exception execution failed |
||
435 | */ |
||
436 | 83 | public function queryColumn() |
|
440 | |||
441 | /** |
||
442 | * Creates an INSERT command. |
||
443 | * |
||
444 | * For example, |
||
445 | * |
||
446 | * ```php |
||
447 | * $connection->createCommand()->insert('user', [ |
||
448 | * 'name' => 'Sam', |
||
449 | * 'age' => 30, |
||
450 | * ])->execute(); |
||
451 | * ``` |
||
452 | * |
||
453 | * The method will properly escape the column names, and bind the values to be inserted. |
||
454 | * |
||
455 | * Note that the created command is not executed until [[execute()]] is called. |
||
456 | * |
||
457 | * @param string $table the table that new rows will be inserted into. |
||
458 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
||
459 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
460 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
461 | * @return $this the command object itself |
||
462 | */ |
||
463 | 448 | public function insert($table, $columns) |
|
470 | |||
471 | /** |
||
472 | * Creates a batch INSERT command. |
||
473 | * |
||
474 | * For example, |
||
475 | * |
||
476 | * ```php |
||
477 | * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
||
478 | * ['Tom', 30], |
||
479 | * ['Jane', 20], |
||
480 | * ['Linda', 25], |
||
481 | * ])->execute(); |
||
482 | * ``` |
||
483 | * |
||
484 | * The method will properly escape the column names, and quote the values to be inserted. |
||
485 | * |
||
486 | * Note that the values in each row must match the corresponding column names. |
||
487 | * |
||
488 | * Also note that the created command is not executed until [[execute()]] is called. |
||
489 | * |
||
490 | * @param string $table the table that new rows will be inserted into. |
||
491 | * @param array $columns the column names |
||
492 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
493 | * @return $this the command object itself |
||
494 | */ |
||
495 | 26 | public function batchInsert($table, $columns, $rows) |
|
510 | |||
511 | /** |
||
512 | * Creates a command to insert rows into a database table if |
||
513 | * they do not already exist (matching unique constraints), |
||
514 | * or update them if they do. |
||
515 | * |
||
516 | * For example, |
||
517 | * |
||
518 | * ```php |
||
519 | * $sql = $queryBuilder->upsert('pages', [ |
||
520 | * 'name' => 'Front page', |
||
521 | * 'url' => 'http://example.com/', // url is unique |
||
522 | * 'visits' => 0, |
||
523 | * ], [ |
||
524 | * 'visits' => new \yii\db\Expression('visits + 1'), |
||
525 | * ], $params); |
||
526 | * ``` |
||
527 | * |
||
528 | * The method will properly escape the table and column names. |
||
529 | * |
||
530 | * @param string $table the table that new rows will be inserted into/updated in. |
||
531 | * @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance |
||
532 | * of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement. |
||
533 | * @param array|bool $updateColumns the column data (name => value) to be updated if they already exist. |
||
534 | * If `true` is passed, the column data will be updated to match the insert column data. |
||
535 | * If `false` is passed, no update will be performed if the column data already exists. |
||
536 | * @param array $params the parameters to be bound to the command. |
||
537 | * @return $this the command object itself. |
||
538 | * @since 2.0.14 |
||
539 | */ |
||
540 | 33 | public function upsert($table, $insertColumns, $updateColumns = true, $params = []) |
|
546 | |||
547 | /** |
||
548 | * Creates an UPDATE command. |
||
549 | * |
||
550 | * For example, |
||
551 | * |
||
552 | * ```php |
||
553 | * $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute(); |
||
554 | * ``` |
||
555 | * |
||
556 | * or with using parameter binding for the condition: |
||
557 | * |
||
558 | * ```php |
||
559 | * $minAge = 30; |
||
560 | * $connection->createCommand()->update('user', ['status' => 1], 'age > :minAge', [':minAge' => $minAge])->execute(); |
||
561 | * ``` |
||
562 | * |
||
563 | * The method will properly escape the column names and bind the values to be updated. |
||
564 | * |
||
565 | * Note that the created command is not executed until [[execute()]] is called. |
||
566 | * |
||
567 | * @param string $table the table to be updated. |
||
568 | * @param array $columns the column data (name => value) to be updated. |
||
569 | * @param string|array $condition the condition that will be put in the WHERE part. Please |
||
570 | * refer to [[Query::where()]] on how to specify condition. |
||
571 | * @param array $params the parameters to be bound to the command |
||
572 | * @return $this the command object itself |
||
573 | */ |
||
574 | 111 | public function update($table, $columns, $condition = '', $params = []) |
|
580 | |||
581 | /** |
||
582 | * Creates a DELETE command. |
||
583 | * |
||
584 | * For example, |
||
585 | * |
||
586 | * ```php |
||
587 | * $connection->createCommand()->delete('user', 'status = 0')->execute(); |
||
588 | * ``` |
||
589 | * |
||
590 | * or with using parameter binding for the condition: |
||
591 | * |
||
592 | * ```php |
||
593 | * $status = 0; |
||
594 | * $connection->createCommand()->delete('user', 'status = :status', [':status' => $status])->execute(); |
||
595 | * ``` |
||
596 | * |
||
597 | * The method will properly escape the table and column names. |
||
598 | * |
||
599 | * Note that the created command is not executed until [[execute()]] is called. |
||
600 | * |
||
601 | * @param string $table the table where the data will be deleted from. |
||
602 | * @param string|array $condition the condition that will be put in the WHERE part. Please |
||
603 | * refer to [[Query::where()]] on how to specify condition. |
||
604 | * @param array $params the parameters to be bound to the command |
||
605 | * @return $this the command object itself |
||
606 | */ |
||
607 | 350 | public function delete($table, $condition = '', $params = []) |
|
613 | |||
614 | /** |
||
615 | * Creates a SQL command for creating a new DB table. |
||
616 | * |
||
617 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
618 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
619 | * stands for the column type which can contain an abstract DB type. |
||
620 | * The method [[QueryBuilder::getColumnType()]] will be called |
||
621 | * to convert the abstract column types to physical ones. For example, `string` will be converted |
||
622 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
623 | * |
||
624 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
625 | * inserted into the generated SQL. |
||
626 | * |
||
627 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
628 | * @param array $columns the columns (name => definition) in the new table. |
||
629 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
630 | * @return $this the command object itself |
||
631 | */ |
||
632 | 134 | public function createTable($table, $columns, $options = null) |
|
638 | |||
639 | /** |
||
640 | * Creates a SQL command for renaming a DB table. |
||
641 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
642 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
643 | * @return $this the command object itself |
||
644 | */ |
||
645 | 3 | public function renameTable($table, $newName) |
|
651 | |||
652 | /** |
||
653 | * Creates a SQL command for dropping a DB table. |
||
654 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
655 | * @return $this the command object itself |
||
656 | */ |
||
657 | 39 | public function dropTable($table) |
|
663 | |||
664 | /** |
||
665 | * Creates a SQL command for truncating a DB table. |
||
666 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
667 | * @return $this the command object itself |
||
668 | */ |
||
669 | 13 | public function truncateTable($table) |
|
675 | |||
676 | /** |
||
677 | * Creates a SQL command for adding a new DB column. |
||
678 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
679 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
680 | * @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
||
681 | * to convert the give column type to the physical one. For example, `string` will be converted |
||
682 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
683 | * @return $this the command object itself |
||
684 | */ |
||
685 | 4 | public function addColumn($table, $column, $type) |
|
691 | |||
692 | /** |
||
693 | * Creates a SQL command for dropping a DB column. |
||
694 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
695 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
696 | * @return $this the command object itself |
||
697 | */ |
||
698 | public function dropColumn($table, $column) |
||
704 | |||
705 | /** |
||
706 | * Creates a SQL command for renaming a column. |
||
707 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
708 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
709 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
710 | * @return $this the command object itself |
||
711 | */ |
||
712 | public function renameColumn($table, $oldName, $newName) |
||
718 | |||
719 | /** |
||
720 | * Creates a SQL command for changing the definition of a column. |
||
721 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
722 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
723 | * @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
||
724 | * to convert the give column type to the physical one. For example, `string` will be converted |
||
725 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
726 | * @return $this the command object itself |
||
727 | */ |
||
728 | 2 | public function alterColumn($table, $column, $type) |
|
734 | |||
735 | /** |
||
736 | * Creates a SQL command for adding a primary key constraint to an existing table. |
||
737 | * The method will properly quote the table and column names. |
||
738 | * @param string $name the name of the primary key constraint. |
||
739 | * @param string $table the table that the primary key constraint will be added to. |
||
740 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
741 | * @return $this the command object itself. |
||
742 | */ |
||
743 | 2 | public function addPrimaryKey($name, $table, $columns) |
|
749 | |||
750 | /** |
||
751 | * Creates a SQL command for removing a primary key constraint to an existing table. |
||
752 | * @param string $name the name of the primary key constraint to be removed. |
||
753 | * @param string $table the table that the primary key constraint will be removed from. |
||
754 | * @return $this the command object itself |
||
755 | */ |
||
756 | 2 | public function dropPrimaryKey($name, $table) |
|
762 | |||
763 | /** |
||
764 | * Creates a SQL command for adding a foreign key constraint to an existing table. |
||
765 | * The method will properly quote the table and column names. |
||
766 | * @param string $name the name of the foreign key constraint. |
||
767 | * @param string $table the table that the foreign key constraint will be added to. |
||
768 | * @param string|array $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas. |
||
769 | * @param string $refTable the table that the foreign key references to. |
||
770 | * @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas. |
||
771 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
772 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
773 | * @return $this the command object itself |
||
774 | */ |
||
775 | 4 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
781 | |||
782 | /** |
||
783 | * Creates a SQL command for dropping a foreign key constraint. |
||
784 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
785 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
786 | * @return $this the command object itself |
||
787 | */ |
||
788 | 4 | public function dropForeignKey($name, $table) |
|
794 | |||
795 | /** |
||
796 | * Creates a SQL command for creating a new index. |
||
797 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
798 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
799 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them |
||
800 | * by commas. The column names will be properly quoted by the method. |
||
801 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
802 | * @return $this the command object itself |
||
803 | */ |
||
804 | 12 | public function createIndex($name, $table, $columns, $unique = false) |
|
810 | |||
811 | /** |
||
812 | * Creates a SQL command for dropping an index. |
||
813 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
814 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
815 | * @return $this the command object itself |
||
816 | */ |
||
817 | 3 | public function dropIndex($name, $table) |
|
823 | |||
824 | /** |
||
825 | * Creates a SQL command for adding an unique constraint to an existing table. |
||
826 | * @param string $name the name of the unique constraint. |
||
827 | * The name will be properly quoted by the method. |
||
828 | * @param string $table the table that the unique constraint will be added to. |
||
829 | * The name will be properly quoted by the method. |
||
830 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
831 | * If there are multiple columns, separate them with commas. |
||
832 | * The name will be properly quoted by the method. |
||
833 | * @return $this the command object itself. |
||
834 | * @since 2.0.13 |
||
835 | */ |
||
836 | 2 | public function addUnique($name, $table, $columns) |
|
842 | |||
843 | /** |
||
844 | * Creates a SQL command for dropping an unique constraint. |
||
845 | * @param string $name the name of the unique constraint to be dropped. |
||
846 | * The name will be properly quoted by the method. |
||
847 | * @param string $table the table whose unique constraint is to be dropped. |
||
848 | * The name will be properly quoted by the method. |
||
849 | * @return $this the command object itself. |
||
850 | * @since 2.0.13 |
||
851 | */ |
||
852 | 2 | public function dropUnique($name, $table) |
|
858 | |||
859 | /** |
||
860 | * Creates a SQL command for adding a check constraint to an existing table. |
||
861 | * @param string $name the name of the check constraint. |
||
862 | * The name will be properly quoted by the method. |
||
863 | * @param string $table the table that the check constraint will be added to. |
||
864 | * The name will be properly quoted by the method. |
||
865 | * @param string $expression the SQL of the `CHECK` constraint. |
||
866 | * @return $this the command object itself. |
||
867 | * @since 2.0.13 |
||
868 | */ |
||
869 | 1 | public function addCheck($name, $table, $expression) |
|
875 | |||
876 | /** |
||
877 | * Creates a SQL command for dropping a check constraint. |
||
878 | * @param string $name the name of the check constraint to be dropped. |
||
879 | * The name will be properly quoted by the method. |
||
880 | * @param string $table the table whose check constraint is to be dropped. |
||
881 | * The name will be properly quoted by the method. |
||
882 | * @return $this the command object itself. |
||
883 | * @since 2.0.13 |
||
884 | */ |
||
885 | 1 | public function dropCheck($name, $table) |
|
891 | |||
892 | /** |
||
893 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
894 | * @param string $name the name of the default value constraint. |
||
895 | * The name will be properly quoted by the method. |
||
896 | * @param string $table the table that the default value constraint will be added to. |
||
897 | * The name will be properly quoted by the method. |
||
898 | * @param string $column the name of the column to that the constraint will be added on. |
||
899 | * The name will be properly quoted by the method. |
||
900 | * @param mixed $value default value. |
||
901 | * @return $this the command object itself. |
||
902 | * @since 2.0.13 |
||
903 | */ |
||
904 | public function addDefaultValue($name, $table, $column, $value) |
||
910 | |||
911 | /** |
||
912 | * Creates a SQL command for dropping a default value constraint. |
||
913 | * @param string $name the name of the default value constraint to be dropped. |
||
914 | * The name will be properly quoted by the method. |
||
915 | * @param string $table the table whose default value constraint is to be dropped. |
||
916 | * The name will be properly quoted by the method. |
||
917 | * @return $this the command object itself. |
||
918 | * @since 2.0.13 |
||
919 | */ |
||
920 | public function dropDefaultValue($name, $table) |
||
926 | |||
927 | /** |
||
928 | * Creates a SQL command for resetting the sequence value of a table's primary key. |
||
929 | * The sequence will be reset such that the primary key of the next new row inserted |
||
930 | * will have the specified value or 1. |
||
931 | * @param string $table the name of the table whose primary key sequence will be reset |
||
932 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
933 | * the next new row's primary key will have a value 1. |
||
934 | * @return $this the command object itself |
||
935 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
936 | */ |
||
937 | 16 | public function resetSequence($table, $value = null) |
|
943 | |||
944 | /** |
||
945 | * Builds a SQL command for enabling or disabling integrity check. |
||
946 | * @param bool $check whether to turn on or off the integrity check. |
||
947 | * @param string $schema the schema name of the tables. Defaults to empty string, meaning the current |
||
948 | * or default schema. |
||
949 | * @param string $table the table name. |
||
950 | * @return $this the command object itself |
||
951 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
952 | */ |
||
953 | 4 | public function checkIntegrity($check = true, $schema = '', $table = '') |
|
959 | |||
960 | /** |
||
961 | * Builds a SQL command for adding comment to column. |
||
962 | * |
||
963 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
964 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
965 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
966 | * @return $this the command object itself |
||
967 | * @since 2.0.8 |
||
968 | */ |
||
969 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
975 | |||
976 | /** |
||
977 | * Builds a SQL command for adding comment to table. |
||
978 | * |
||
979 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
980 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
981 | * @return $this the command object itself |
||
982 | * @since 2.0.8 |
||
983 | */ |
||
984 | public function addCommentOnTable($table, $comment) |
||
990 | |||
991 | /** |
||
992 | * Builds a SQL command for dropping comment from column. |
||
993 | * |
||
994 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
995 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
996 | * @return $this the command object itself |
||
997 | * @since 2.0.8 |
||
998 | */ |
||
999 | 2 | public function dropCommentFromColumn($table, $column) |
|
1005 | |||
1006 | /** |
||
1007 | * Builds a SQL command for dropping comment from table. |
||
1008 | * |
||
1009 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
1010 | * @return $this the command object itself |
||
1011 | * @since 2.0.8 |
||
1012 | */ |
||
1013 | public function dropCommentFromTable($table) |
||
1019 | |||
1020 | /** |
||
1021 | * Creates a SQL View. |
||
1022 | * |
||
1023 | * @param string $viewName the name of the view to be created. |
||
1024 | * @param string|Query $subquery the select statement which defines the view. |
||
1025 | * This can be either a string or a [[Query]] object. |
||
1026 | * @return $this the command object itself. |
||
1027 | * @since 2.0.14 |
||
1028 | */ |
||
1029 | 3 | public function createView($viewName, $subquery) |
|
1035 | |||
1036 | /** |
||
1037 | * Drops a SQL View. |
||
1038 | * |
||
1039 | * @param string $viewName the name of the view to be dropped. |
||
1040 | * @return $this the command object itself. |
||
1041 | * @since 2.0.14 |
||
1042 | */ |
||
1043 | 3 | public function dropView($viewName) |
|
1049 | |||
1050 | /** |
||
1051 | * Executes the SQL statement. |
||
1052 | * This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs. |
||
1053 | * No result set will be returned. |
||
1054 | * @return int number of rows affected by the execution. |
||
1055 | * @throws Exception execution failed |
||
1056 | */ |
||
1057 | 654 | public function execute() |
|
1084 | |||
1085 | /** |
||
1086 | * Logs the current database query if query logging is enabled and returns |
||
1087 | * the profiling token if profiling is enabled. |
||
1088 | * @param string $category the log category. |
||
1089 | * @return array array of two elements, the first is boolean of whether profiling is enabled or not. |
||
1090 | * The second is the rawSql if it has been created. |
||
1091 | */ |
||
1092 | 1298 | private function logQuery($category) |
|
1104 | |||
1105 | /** |
||
1106 | * Performs the actual DB query of a SQL statement. |
||
1107 | * @param string $method method of PDOStatement to be called |
||
1108 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
||
1109 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
||
1110 | * @return mixed the method execution result |
||
1111 | * @throws Exception if the query causes any problem |
||
1112 | * @since 2.0.1 this method is protected (was private before). |
||
1113 | */ |
||
1114 | 1257 | protected function queryInternal($method, $fetchMode = null) |
|
1169 | |||
1170 | /** |
||
1171 | * Marks a specified table schema to be refreshed after command execution. |
||
1172 | * @param string $name name of the table, which schema should be refreshed. |
||
1173 | * @return $this this command instance |
||
1174 | * @since 2.0.6 |
||
1175 | */ |
||
1176 | 143 | protected function requireTableSchemaRefresh($name) |
|
1181 | |||
1182 | /** |
||
1183 | * Refreshes table schema, which was marked by [[requireTableSchemaRefresh()]]. |
||
1184 | * @since 2.0.6 |
||
1185 | */ |
||
1186 | 648 | protected function refreshTableSchema() |
|
1192 | |||
1193 | /** |
||
1194 | * Marks the command to be executed in transaction. |
||
1195 | * @param string|null $isolationLevel The isolation level to use for this transaction. |
||
1196 | * See [[Transaction::begin()]] for details. |
||
1197 | * @return $this this command instance. |
||
1198 | * @since 2.0.14 |
||
1199 | */ |
||
1200 | 3 | protected function requireTransaction($isolationLevel = null) |
|
1205 | |||
1206 | /** |
||
1207 | * Sets a callable (e.g. anonymous function) that is called when [[Exception]] is thrown |
||
1208 | * when executing the command. The signature of the callable should be: |
||
1209 | * |
||
1210 | * ```php |
||
1211 | * function (\yii\db\Exception $e, $attempt) |
||
1212 | * { |
||
1213 | * // return true or false (whether to retry the command or rethrow $e) |
||
1214 | * } |
||
1215 | * ``` |
||
1216 | * |
||
1217 | * The callable will recieve a database exception thrown and a current attempt |
||
1218 | * (to execute the command) number starting from 1. |
||
1219 | * |
||
1220 | * @param callable $handler a PHP callback to handle database exceptions. |
||
1221 | * @return $this this command instance. |
||
1222 | * @since 2.0.14 |
||
1223 | */ |
||
1224 | 3 | protected function setRetryHandler(callable $handler) |
|
1229 | |||
1230 | /** |
||
1231 | * Executes a prepared statement. |
||
1232 | * |
||
1233 | * It's a wrapper around [[\PDOStatement::execute()]] to support transactions |
||
1234 | * and retry handlers. |
||
1235 | * |
||
1236 | * @param string|null $rawSql the rawSql if it has been created. |
||
1237 | * @throws Exception if execution failed. |
||
1238 | * @since 2.0.14 |
||
1239 | */ |
||
1240 | 1297 | protected function internalExecute($rawSql) |
|
1266 | |||
1267 | /** |
||
1268 | * Resets command properties to their initial state. |
||
1269 | * |
||
1270 | * @since 2.0.13 |
||
1271 | */ |
||
1272 | 1334 | protected function reset() |
|
1281 | } |
||
1282 |