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 | use CacheableQueryTrait; |
||
60 | |||
61 | /** |
||
62 | * @var Connection the DB connection that this command is associated with |
||
63 | */ |
||
64 | public $db; |
||
65 | /** |
||
66 | * @var \PDOStatement the PDOStatement object that this command is associated with |
||
67 | */ |
||
68 | public $pdoStatement; |
||
69 | /** |
||
70 | * @var int the default fetch mode for this command. |
||
71 | * @see http://www.php.net/manual/en/pdostatement.setfetchmode.php |
||
72 | */ |
||
73 | public $fetchMode = \PDO::FETCH_ASSOC; |
||
74 | /** |
||
75 | * @var array the parameters (name => value) that are bound to the current PDO statement. |
||
76 | * This property is maintained by methods such as [[bindValue()]]. It is mainly provided for logging purpose |
||
77 | * and is used to generate [[rawSql]]. Do not modify it directly. |
||
78 | */ |
||
79 | public $params = []; |
||
80 | |||
81 | /** |
||
82 | * @var array pending parameters to be bound to the current PDO statement. |
||
83 | */ |
||
84 | private $_pendingParams = []; |
||
85 | /** |
||
86 | * @var string the SQL statement that this command represents |
||
87 | */ |
||
88 | private $_sql; |
||
89 | /** |
||
90 | * @var string name of the table, which schema, should be refreshed after command execution. |
||
91 | */ |
||
92 | private $_refreshTableName; |
||
93 | /** |
||
94 | * @var string|false|null the isolation level to use for this transaction. |
||
95 | * See [[Transaction::begin()]] for details. |
||
96 | */ |
||
97 | private $_isolationLevel = false; |
||
98 | /** |
||
99 | * @var callable a callable (e.g. anonymous function) that is called when [[\yii\db\Exception]] is thrown |
||
100 | * when executing the command. |
||
101 | */ |
||
102 | private $_retryHandler; |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Returns the SQL statement for this command. |
||
107 | * @return string the SQL statement to be executed |
||
108 | */ |
||
109 | public function getSql() |
||
113 | |||
114 | /** |
||
115 | * Specifies the SQL statement to be executed. The SQL statement will be quoted using [[Connection::quoteSql()]]. |
||
116 | * The previous SQL (if any) will be discarded, and [[params]] will be cleared as well. See [[reset()]] |
||
117 | * for details. |
||
118 | * |
||
119 | * @param string $sql the SQL statement to be set. |
||
120 | * @return $this this command instance |
||
121 | * @see reset() |
||
122 | * @see cancel() |
||
123 | 3 | */ |
|
124 | public function setSql($sql) |
||
134 | 3 | ||
135 | /** |
||
136 | 3 | * Specifies the SQL statement to be executed. The SQL statement will not be modified in any way. |
|
137 | 3 | * The previous SQL (if any) will be discarded, and [[params]] will be cleared as well. See [[reset()]] |
|
138 | * for details. |
||
139 | * |
||
140 | * @param string $sql the SQL statement to be set. |
||
141 | * @return $this this command instance |
||
142 | * @since 2.0.13 |
||
143 | * @see reset() |
||
144 | 1276 | * @see cancel() |
|
145 | */ |
||
146 | 1276 | public function setRawSql($sql) |
|
156 | |||
157 | /** |
||
158 | * Returns the raw SQL by inserting parameter values into the corresponding placeholders in [[sql]]. |
||
159 | 1297 | * Note that the return value of this method should mainly be used for logging purpose. |
|
160 | * It is likely that this method returns an invalid SQL due to improper replacement of parameter placeholders. |
||
161 | 1297 | * @return string the raw SQL with parameter values inserted into the corresponding placeholders in [[sql]]. |
|
162 | 1297 | */ |
|
163 | 1297 | public function getRawSql() |
|
193 | |||
194 | /** |
||
195 | * Prepares the SQL statement to be executed. |
||
196 | * For complex SQL statement that is to be executed multiple times, |
||
197 | * this may improve performance. |
||
198 | 1279 | * For SQL statement with binding parameters, this method is invoked |
|
199 | * automatically. |
||
200 | 1279 | * @param bool $forRead whether this method is called for a read query. If null, it means |
|
201 | 1121 | * the SQL statement should be used to determine whether it is for read or write. |
|
202 | * @throws Exception if there is any DB error |
||
203 | 976 | */ |
|
204 | 976 | public function prepare($forRead = null) |
|
232 | |||
233 | /** |
||
234 | * Cancels the execution of the SQL statement. |
||
235 | * This method mainly sets [[pdoStatement]] to be null. |
||
236 | */ |
||
237 | public function cancel() |
||
241 | 1264 | ||
242 | 53 | /** |
|
243 | 53 | * Binds a parameter to the SQL statement to be executed. |
|
244 | * @param string|int $name parameter identifier. For a prepared statement |
||
245 | * using named placeholders, this will be a parameter name of |
||
246 | 1264 | * the form `:name`. For a prepared statement using question mark |
|
247 | * placeholders, this will be the 1-indexed position of the parameter. |
||
248 | 1264 | * @param mixed $value the PHP variable to bind to the SQL statement parameter (passed by reference) |
|
249 | * @param int $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
||
250 | 21 | * @param int $length length of the data type |
|
251 | * @param mixed $driverOptions the driver-specific options |
||
252 | 1264 | * @return $this the current command being executed |
|
253 | 1219 | * @see http://www.php.net/manual/en/function.PDOStatement-bindParam.php |
|
254 | */ |
||
255 | 659 | public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null) |
|
273 | |||
274 | 1297 | /** |
|
275 | 1297 | * Binds pending parameters that were registered via [[bindValue()]] and [[bindValues()]]. |
|
276 | * Note that this method requires an active [[pdoStatement]]. |
||
277 | */ |
||
278 | protected function bindPendingParams() |
||
285 | |||
286 | /** |
||
287 | * Binds a value to a parameter. |
||
288 | * @param string|int $name Parameter identifier. For a prepared statement |
||
289 | * using named placeholders, this will be a parameter name of |
||
290 | 3 | * the form `:name`. For a prepared statement using question mark |
|
291 | * placeholders, this will be the 1-indexed position of the parameter. |
||
292 | 3 | * @param mixed $value The value to bind to the parameter |
|
293 | * @param int $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
||
294 | 3 | * @return $this the current command being executed |
|
295 | 3 | * @see http://www.php.net/manual/en/function.PDOStatement-bindValue.php |
|
296 | */ |
||
297 | 3 | public function bindValue($name, $value, $dataType = null) |
|
307 | |||
308 | /** |
||
309 | * Binds a list of values to the corresponding parameters. |
||
310 | * This is similar to [[bindValue()]] except that it binds multiple values at a time. |
||
311 | * Note that the SQL data type of each value is determined by its PHP type. |
||
312 | * @param array $values the values to be bound. This must be given in terms of an associative |
||
313 | 1263 | * array with array keys being the parameter names, and array values the corresponding parameter values, |
|
314 | * e.g. `[':name' => 'John', ':age' => 25]`. By default, the PDO type of each value is determined |
||
315 | 1263 | * by its PHP type. You may explicitly specify the PDO type by using a [[yii\db\PdoValue]] class: `new PdoValue(value, type)`, |
|
316 | 957 | * e.g. `[':name' => 'John', ':profile' => new PdoValue($profile, \PDO::PARAM_LOB)]`. |
|
317 | * @return $this the current command being executed |
||
318 | 1263 | */ |
|
319 | 1263 | public function bindValues($values) |
|
339 | |||
340 | 6 | /** |
|
341 | * Executes the SQL statement and returns query result. |
||
342 | * This method is for executing a SQL query that returns result set, such as `SELECT`. |
||
343 | * @return DataReader the reader object for fetching the query result |
||
344 | * @throws Exception execution failed |
||
345 | */ |
||
346 | public function query() |
||
350 | |||
351 | /** |
||
352 | * Executes the SQL statement and returns ALL rows at once. |
||
353 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
||
354 | 1297 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
|
355 | * @return array all rows of the query result. Each array element is an array representing a row of data. |
||
356 | 1297 | * An empty array is returned if the query results in nothing. |
|
357 | 1139 | * @throws Exception execution failed |
|
358 | */ |
||
359 | public function queryAll($fetchMode = null) |
||
363 | 90 | ||
364 | 90 | /** |
|
365 | * Executes the SQL statement and returns the first row of the result. |
||
366 | 976 | * This method is best used when only the first row of result is needed for a query. |
|
367 | 976 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://php.net/manual/en/pdostatement.setfetchmode.php) |
|
368 | 976 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
|
369 | * @return array|false the first row (in terms of an array) of the query result. False is returned if the query |
||
370 | * results in nothing. |
||
371 | * @throws Exception execution failed |
||
372 | 976 | */ |
|
373 | public function queryOne($fetchMode = null) |
||
377 | |||
378 | /** |
||
379 | * Executes the SQL statement and returns the value of the first column in the first row of data. |
||
380 | * This method is best used when only a single value is needed for a query. |
||
381 | 9 | * @return string|null|false the value of the first column in the first row of the query result. |
|
382 | * False is returned if there is no value. |
||
383 | 9 | * @throws Exception execution failed |
|
384 | */ |
||
385 | public function queryScalar() |
||
394 | 1101 | ||
395 | /** |
||
396 | 1101 | * Executes the SQL statement and returns the first column of the result. |
|
397 | * This method is best used when only the first column of result (i.e. the first element in each row) |
||
398 | * is needed for a query. |
||
399 | * @return array the first column of the query result. Empty array is returned if the query results in nothing. |
||
400 | * @throws Exception execution failed |
||
401 | */ |
||
402 | public function queryColumn() |
||
406 | |||
407 | /** |
||
408 | 457 | * Creates an INSERT command. |
|
409 | * |
||
410 | 457 | * For example, |
|
411 | * |
||
412 | * ```php |
||
413 | * $connection->createCommand()->insert('user', [ |
||
414 | * 'name' => 'Sam', |
||
415 | * 'age' => 30, |
||
416 | * ])->execute(); |
||
417 | * ``` |
||
418 | * |
||
419 | * The method will properly escape the column names, and bind the values to be inserted. |
||
420 | 299 | * |
|
421 | * Note that the created command is not executed until [[execute()]] is called. |
||
422 | 299 | * |
|
423 | 296 | * @param string $table the table that new rows will be inserted into. |
|
424 | 17 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
|
425 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
426 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
427 | 289 | * @return $this the command object itself |
|
428 | */ |
||
429 | public function insert($table, $columns) |
||
436 | |||
437 | 80 | /** |
|
438 | * Creates a batch INSERT command. |
||
439 | 80 | * |
|
440 | * For example, |
||
441 | * |
||
442 | * ```php |
||
443 | * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
||
444 | * ['Tom', 30], |
||
445 | * ['Jane', 20], |
||
446 | * ['Linda', 25], |
||
447 | * ])->execute(); |
||
448 | * ``` |
||
449 | * |
||
450 | * The method will properly escape the column names, and quote the values to be inserted. |
||
451 | * |
||
452 | * Note that the values in each row must match the corresponding column names. |
||
453 | * |
||
454 | * Also note that the created command is not executed until [[execute()]] is called. |
||
455 | * |
||
456 | * @param string $table the table that new rows will be inserted into. |
||
457 | * @param array $columns the column names |
||
458 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
459 | * @return $this the command object itself |
||
460 | */ |
||
461 | public function batchInsert($table, $columns, $rows) |
||
474 | |||
475 | /** |
||
476 | * Creates a command to insert rows into a database table if |
||
477 | * they do not already exist (matching unique constraints), |
||
478 | * or update them if they do. |
||
479 | * |
||
480 | * For example, |
||
481 | * |
||
482 | * ```php |
||
483 | * $sql = $queryBuilder->upsert('pages', [ |
||
484 | * 'name' => 'Front page', |
||
485 | * 'url' => 'http://example.com/', // url is unique |
||
486 | * 'visits' => 0, |
||
487 | * ], [ |
||
488 | * 'visits' => new \yii\db\Expression('visits + 1'), |
||
489 | * ], $params); |
||
490 | * ``` |
||
491 | * |
||
492 | * The method will properly escape the table and column names. |
||
493 | * |
||
494 | * @param string $table the table that new rows will be inserted into/updated in. |
||
495 | * @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance |
||
496 | 22 | * of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement. |
|
497 | * @param array|bool $updateColumns the column data (name => value) to be updated if they already exist. |
||
498 | 22 | * If `true` is passed, the column data will be updated to match the insert column data. |
|
499 | * If `false` is passed, no update will be performed if the column data already exists. |
||
500 | 22 | * @param array $params the parameters to be bound to the command. |
|
501 | 22 | * @return $this the command object itself. |
|
502 | * @since 2.0.14 |
||
503 | 22 | */ |
|
504 | public function upsert($table, $insertColumns, $updateColumns = true, $params = []) |
||
510 | |||
511 | /** |
||
512 | * Creates an UPDATE command. |
||
513 | * |
||
514 | * For example, |
||
515 | * |
||
516 | * ```php |
||
517 | * $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute(); |
||
518 | * ``` |
||
519 | * |
||
520 | * or with using parameter binding for the condition: |
||
521 | * |
||
522 | * ```php |
||
523 | * $minAge = 30; |
||
524 | * $connection->createCommand()->update('user', ['status' => 1], 'age > :minAge', [':minAge' => $minAge])->execute(); |
||
525 | * ``` |
||
526 | * |
||
527 | * The method will properly escape the column names and bind the values to be updated. |
||
528 | * |
||
529 | * Note that the created command is not executed until [[execute()]] is called. |
||
530 | * |
||
531 | * @param string $table the table to be updated. |
||
532 | * @param array $columns the column data (name => value) to be updated. |
||
533 | * @param string|array $condition the condition that will be put in the WHERE part. Please |
||
534 | * refer to [[Query::where()]] on how to specify condition. |
||
535 | * @param array $params the parameters to be bound to the command |
||
536 | * @return $this the command object itself |
||
537 | */ |
||
538 | public function update($table, $columns, $condition = '', $params = []) |
||
544 | |||
545 | /** |
||
546 | * Creates a DELETE command. |
||
547 | * |
||
548 | * For example, |
||
549 | * |
||
550 | * ```php |
||
551 | * $connection->createCommand()->delete('user', 'status = 0')->execute(); |
||
552 | * ``` |
||
553 | * |
||
554 | * or with using parameter binding for the condition: |
||
555 | * |
||
556 | * ```php |
||
557 | * $status = 0; |
||
558 | * $connection->createCommand()->delete('user', 'status = :status', [':status' => $status])->execute(); |
||
559 | * ``` |
||
560 | * |
||
561 | * The method will properly escape the table and column names. |
||
562 | * |
||
563 | * Note that the created command is not executed until [[execute()]] is called. |
||
564 | * |
||
565 | * @param string $table the table where the data will be deleted from. |
||
566 | * @param string|array $condition the condition that will be put in the WHERE part. Please |
||
567 | * refer to [[Query::where()]] on how to specify condition. |
||
568 | * @param array $params the parameters to be bound to the command |
||
569 | * @return $this the command object itself |
||
570 | */ |
||
571 | public function delete($table, $condition = '', $params = []) |
||
577 | 109 | ||
578 | /** |
||
579 | * Creates a SQL command for creating a new DB table. |
||
580 | * |
||
581 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
582 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
583 | * stands for the column type which can contain an abstract DB type. |
||
584 | * The method [[QueryBuilder::getColumnType()]] will be called |
||
585 | * to convert the abstract column types to physical ones. For example, `string` will be converted |
||
586 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
587 | * |
||
588 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
589 | * inserted into the generated SQL. |
||
590 | * |
||
591 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
592 | * @param array $columns the columns (name => definition) in the new table. |
||
593 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
594 | * @return $this the command object itself |
||
595 | */ |
||
596 | public function createTable($table, $columns, $options = null) |
||
602 | |||
603 | /** |
||
604 | * Creates a SQL command for renaming a DB table. |
||
605 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
606 | 347 | * @param string $newName the new table name. The name will be properly quoted by the method. |
|
607 | * @return $this the command object itself |
||
608 | 347 | */ |
|
609 | public function renameTable($table, $newName) |
||
615 | |||
616 | /** |
||
617 | * Creates a SQL command for dropping a DB table. |
||
618 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
619 | * @return $this the command object itself |
||
620 | */ |
||
621 | public function dropTable($table) |
||
627 | |||
628 | /** |
||
629 | * Creates a SQL command for truncating a DB table. |
||
630 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
631 | 128 | * @return $this the command object itself |
|
632 | */ |
||
633 | 128 | public function truncateTable($table) |
|
639 | |||
640 | /** |
||
641 | * Creates a SQL command for adding a new DB column. |
||
642 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
643 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
644 | 3 | * @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
|
645 | * to convert the give column type to the physical one. For example, `string` will be converted |
||
646 | 3 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
|
647 | * @return $this the command object itself |
||
648 | 3 | */ |
|
649 | public function addColumn($table, $column, $type) |
||
655 | |||
656 | 36 | /** |
|
657 | * Creates a SQL command for dropping a DB column. |
||
658 | 36 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
|
659 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
660 | 36 | * @return $this the command object itself |
|
661 | */ |
||
662 | public function dropColumn($table, $column) |
||
668 | 13 | ||
669 | /** |
||
670 | 13 | * Creates a SQL command for renaming a column. |
|
671 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
672 | 13 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
|
673 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
674 | * @return $this the command object itself |
||
675 | */ |
||
676 | public function renameColumn($table, $oldName, $newName) |
||
682 | |||
683 | /** |
||
684 | 4 | * Creates a SQL command for changing the definition of a column. |
|
685 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
686 | 4 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
|
687 | * @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
||
688 | 4 | * to convert the give column type to the physical one. For example, `string` will be converted |
|
689 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
690 | * @return $this the command object itself |
||
691 | */ |
||
692 | public function alterColumn($table, $column, $type) |
||
698 | |||
699 | /** |
||
700 | * Creates a SQL command for adding a primary key constraint to an existing table. |
||
701 | * The method will properly quote the table and column names. |
||
702 | * @param string $name the name of the primary key constraint. |
||
703 | * @param string $table the table that the primary key constraint will be added to. |
||
704 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
705 | * @return $this the command object itself. |
||
706 | */ |
||
707 | public function addPrimaryKey($name, $table, $columns) |
||
713 | |||
714 | /** |
||
715 | * Creates a SQL command for removing a primary key constraint to an existing table. |
||
716 | * @param string $name the name of the primary key constraint to be removed. |
||
717 | * @param string $table the table that the primary key constraint will be removed from. |
||
718 | * @return $this the command object itself |
||
719 | */ |
||
720 | public function dropPrimaryKey($name, $table) |
||
726 | |||
727 | 2 | /** |
|
728 | * Creates a SQL command for adding a foreign key constraint to an existing table. |
||
729 | 2 | * The method will properly quote the table and column names. |
|
730 | * @param string $name the name of the foreign key constraint. |
||
731 | 2 | * @param string $table the table that the foreign key constraint will be added to. |
|
732 | * @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. |
||
733 | * @param string $refTable the table that the foreign key references to. |
||
734 | * @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas. |
||
735 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
736 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
737 | * @return $this the command object itself |
||
738 | */ |
||
739 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
745 | |||
746 | 2 | /** |
|
747 | * Creates a SQL command for dropping a foreign key constraint. |
||
748 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
749 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
750 | * @return $this the command object itself |
||
751 | */ |
||
752 | public function dropForeignKey($name, $table) |
||
758 | |||
759 | 2 | /** |
|
760 | * Creates a SQL command for creating a new index. |
||
761 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
762 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
763 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them |
||
764 | * by commas. The column names will be properly quoted by the method. |
||
765 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
766 | * @return $this the command object itself |
||
767 | */ |
||
768 | public function createIndex($name, $table, $columns, $unique = false) |
||
774 | 4 | ||
775 | /** |
||
776 | 4 | * Creates a SQL command for dropping an index. |
|
777 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
778 | 4 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
|
779 | * @return $this the command object itself |
||
780 | */ |
||
781 | public function dropIndex($name, $table) |
||
787 | 4 | ||
788 | /** |
||
789 | 4 | * Creates a SQL command for adding an unique constraint to an existing table. |
|
790 | * @param string $name the name of the unique constraint. |
||
791 | 4 | * The name will be properly quoted by the method. |
|
792 | * @param string $table the table that the unique constraint will be added to. |
||
793 | * The name will be properly quoted by the method. |
||
794 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
795 | * If there are multiple columns, separate them with commas. |
||
796 | * The name will be properly quoted by the method. |
||
797 | * @return $this the command object itself. |
||
798 | * @since 2.0.13 |
||
799 | */ |
||
800 | public function addUnique($name, $table, $columns) |
||
806 | |||
807 | 12 | /** |
|
808 | * Creates a SQL command for dropping an unique constraint. |
||
809 | * @param string $name the name of the unique constraint to be dropped. |
||
810 | * The name will be properly quoted by the method. |
||
811 | * @param string $table the table whose unique constraint is to be dropped. |
||
812 | * The name will be properly quoted by the method. |
||
813 | * @return $this the command object itself. |
||
814 | * @since 2.0.13 |
||
815 | */ |
||
816 | 3 | public function dropUnique($name, $table) |
|
822 | |||
823 | /** |
||
824 | * Creates a SQL command for adding a check constraint to an existing table. |
||
825 | * @param string $name the name of the check constraint. |
||
826 | * The name will be properly quoted by the method. |
||
827 | * @param string $table the table that the check constraint will be added to. |
||
828 | * The name will be properly quoted by the method. |
||
829 | * @param string $expression the SQL of the `CHECK` constraint. |
||
830 | * @return $this the command object itself. |
||
831 | * @since 2.0.13 |
||
832 | */ |
||
833 | public function addCheck($name, $table, $expression) |
||
839 | 2 | ||
840 | /** |
||
841 | * Creates a SQL command for dropping a check constraint. |
||
842 | * @param string $name the name of the check constraint to be dropped. |
||
843 | * The name will be properly quoted by the method. |
||
844 | * @param string $table the table whose check constraint is to be dropped. |
||
845 | * The name will be properly quoted by the method. |
||
846 | * @return $this the command object itself. |
||
847 | * @since 2.0.13 |
||
848 | */ |
||
849 | public function dropCheck($name, $table) |
||
855 | 2 | ||
856 | /** |
||
857 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
858 | * @param string $name the name of the default value constraint. |
||
859 | * The name will be properly quoted by the method. |
||
860 | * @param string $table the table that the default value constraint will be added to. |
||
861 | * The name will be properly quoted by the method. |
||
862 | * @param string $column the name of the column to that the constraint will be added on. |
||
863 | * The name will be properly quoted by the method. |
||
864 | * @param mixed $value default value. |
||
865 | * @return $this the command object itself. |
||
866 | * @since 2.0.13 |
||
867 | */ |
||
868 | 1 | public function addDefaultValue($name, $table, $column, $value) |
|
874 | |||
875 | /** |
||
876 | * Creates a SQL command for dropping a default value constraint. |
||
877 | * @param string $name the name of the default value constraint to be dropped. |
||
878 | * The name will be properly quoted by the method. |
||
879 | * @param string $table the table whose default value constraint is to be dropped. |
||
880 | * The name will be properly quoted by the method. |
||
881 | * @return $this the command object itself. |
||
882 | * @since 2.0.13 |
||
883 | */ |
||
884 | 1 | public function dropDefaultValue($name, $table) |
|
890 | |||
891 | /** |
||
892 | * Creates a SQL command for resetting the sequence value of a table's primary key. |
||
893 | * The sequence will be reset such that the primary key of the next new row inserted |
||
894 | * will have the specified value or 1. |
||
895 | * @param string $table the name of the table whose primary key sequence will be reset |
||
896 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
897 | * the next new row's primary key will have a value 1. |
||
898 | * @return $this the command object itself |
||
899 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
900 | */ |
||
901 | public function resetSequence($table, $value = null) |
||
907 | |||
908 | /** |
||
909 | * Builds a SQL command for enabling or disabling integrity check. |
||
910 | * @param bool $check whether to turn on or off the integrity check. |
||
911 | * @param string $schema the schema name of the tables. Defaults to empty string, meaning the current |
||
912 | * or default schema. |
||
913 | * @param string $table the table name. |
||
914 | * @return $this the command object itself |
||
915 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
916 | */ |
||
917 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
923 | |||
924 | /** |
||
925 | * Builds a SQL command for adding comment to column. |
||
926 | * |
||
927 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
928 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
929 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
930 | * @return $this the command object itself |
||
931 | * @since 2.0.8 |
||
932 | */ |
||
933 | public function addCommentOnColumn($table, $column, $comment) |
||
939 | |||
940 | 16 | /** |
|
941 | * Builds a SQL command for adding comment to table. |
||
942 | * |
||
943 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
944 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
945 | * @return $this the command object itself |
||
946 | * @since 2.0.8 |
||
947 | */ |
||
948 | public function addCommentOnTable($table, $comment) |
||
954 | 4 | ||
955 | /** |
||
956 | 4 | * Builds a SQL command for dropping comment from column. |
|
957 | * |
||
958 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
959 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
960 | * @return $this the command object itself |
||
961 | * @since 2.0.8 |
||
962 | */ |
||
963 | public function dropCommentFromColumn($table, $column) |
||
969 | |||
970 | 2 | /** |
|
971 | * Builds a SQL command for dropping comment from table. |
||
972 | 2 | * |
|
973 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
974 | * @return $this the command object itself |
||
975 | * @since 2.0.8 |
||
976 | */ |
||
977 | public function dropCommentFromTable($table) |
||
983 | |||
984 | /** |
||
985 | * Creates a SQL View. |
||
986 | * |
||
987 | * @param string $viewName the name of the view to be created. |
||
988 | * @param string|Query $subquery the select statement which defines the view. |
||
989 | * This can be either a string or a [[Query]] object. |
||
990 | * @return $this the command object itself. |
||
991 | * @since 2.0.14 |
||
992 | */ |
||
993 | public function createView($viewName, $subquery) |
||
999 | |||
1000 | 2 | /** |
|
1001 | * Drops a SQL View. |
||
1002 | 2 | * |
|
1003 | * @param string $viewName the name of the view to be dropped. |
||
1004 | * @return $this the command object itself. |
||
1005 | * @since 2.0.14 |
||
1006 | */ |
||
1007 | public function dropView($viewName) |
||
1013 | |||
1014 | /** |
||
1015 | * Executes the SQL statement. |
||
1016 | * This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs. |
||
1017 | * No result set will be returned. |
||
1018 | * @return int number of rows affected by the execution. |
||
1019 | * @throws Exception execution failed |
||
1020 | */ |
||
1021 | public function execute() |
||
1048 | |||
1049 | /** |
||
1050 | * Logs the current database query if query logging is enabled and returns |
||
1051 | * the profiling token if profiling is enabled. |
||
1052 | * @param string $category the log category. |
||
1053 | * @return array array of two elements, the first is boolean of whether profiling is enabled or not. |
||
1054 | * The second is the rawSql if it has been created. |
||
1055 | */ |
||
1056 | 645 | private function logQuery($category) |
|
1068 | 642 | ||
1069 | /** |
||
1070 | 642 | * Performs the actual DB query of a SQL statement. |
|
1071 | 639 | * @param string $method method of PDOStatement to be called |
|
1072 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
||
1073 | 639 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
|
1074 | * @return mixed the method execution result |
||
1075 | 639 | * @throws Exception if the query causes any problem |
|
1076 | * @since 2.0.1 this method is protected (was private before). |
||
1077 | 639 | */ |
|
1078 | 18 | protected function queryInternal($method, $fetchMode = null) |
|
1133 | 3 | ||
1134 | /** |
||
1135 | * Marks a specified table schema to be refreshed after command execution. |
||
1136 | * @param string $name name of the table, which schema should be refreshed. |
||
1137 | * @return $this this command instance |
||
1138 | 1220 | * @since 2.0.6 |
|
1139 | */ |
||
1140 | protected function requireTableSchemaRefresh($name) |
||
1145 | 1216 | ||
1146 | 9 | /** |
|
1147 | * Refreshes table schema, which was marked by [[requireTableSchemaRefresh()]]. |
||
1148 | 1213 | * @since 2.0.6 |
|
1149 | 1120 | */ |
|
1150 | protected function refreshTableSchema() |
||
1156 | 22 | ||
1157 | 22 | /** |
|
1158 | 22 | * Marks the command to be executed in transaction. |
|
1159 | * @param string|null $isolationLevel The isolation level to use for this transaction. |
||
1160 | * See [[Transaction::begin()]] for details. |
||
1161 | 1216 | * @return $this this command instance. |
|
1162 | 3 | * @since 2.0.14 |
|
1163 | 3 | */ |
|
1164 | protected function requireTransaction($isolationLevel = null) |
||
1169 | |||
1170 | /** |
||
1171 | * Sets a callable (e.g. anonymous function) that is called when [[Exception]] is thrown |
||
1172 | * when executing the command. The signature of the callable should be: |
||
1173 | * |
||
1174 | * ```php |
||
1175 | 137 | * function (\yii\db\Exception $e, $attempt) |
|
1176 | * { |
||
1177 | 137 | * // return true or false (whether to retry the command or rethrow $e) |
|
1178 | 137 | * } |
|
1179 | * ``` |
||
1180 | * |
||
1181 | * The callable will recieve a database exception thrown and a current attempt |
||
1182 | * (to execute the command) number starting from 1. |
||
1183 | * |
||
1184 | * @param callable $handler a PHP callback to handle database exceptions. |
||
1185 | 639 | * @return $this this command instance. |
|
1186 | * @since 2.0.14 |
||
1187 | 639 | */ |
|
1188 | 137 | protected function setRetryHandler(callable $handler) |
|
1193 | |||
1194 | /** |
||
1195 | * Executes a prepared statement. |
||
1196 | * |
||
1197 | * It's a wrapper around [[\PDOStatement::execute()]] to support transactions |
||
1198 | * and retry handlers. |
||
1199 | 3 | * |
|
1200 | * @param string|null $rawSql the rawSql if it has been created. |
||
1201 | 3 | * @throws Exception if execution failed. |
|
1202 | 3 | * @since 2.0.14 |
|
1203 | */ |
||
1204 | protected function internalExecute($rawSql) |
||
1230 | |||
1231 | /** |
||
1232 | * Resets command properties to their initial state. |
||
1233 | * |
||
1234 | * @since 2.0.13 |
||
1235 | */ |
||
1236 | protected function reset() |
||
1245 | } |
||
1246 |