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 | 1282 | 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 | 1303 | 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 | 22 | 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 | 1285 | 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 | 1270 | 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 | 1303 | 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 | 1269 | 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 | 1303 | 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 | 1104 | 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 | 457 | 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 | 302 | 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 | 446 | 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 | 22 | public function batchInsert($table, $columns, $rows) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Creates a command to insert rows into a database table if |
||
| 511 | * they do not already exist (matching unique constraints), |
||
| 512 | * or update them if they do. |
||
| 513 | * |
||
| 514 | * For example, |
||
| 515 | * |
||
| 516 | * ```php |
||
| 517 | * $sql = $queryBuilder->upsert('pages', [ |
||
| 518 | * 'name' => 'Front page', |
||
| 519 | * 'url' => 'http://example.com/', // url is unique |
||
| 520 | * 'visits' => 0, |
||
| 521 | * ], [ |
||
| 522 | * 'visits' => new \yii\db\Expression('visits + 1'), |
||
| 523 | * ], $params); |
||
| 524 | * ``` |
||
| 525 | * |
||
| 526 | * The method will properly escape the table and column names. |
||
| 527 | * |
||
| 528 | * @param string $table the table that new rows will be inserted into/updated in. |
||
| 529 | * @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance |
||
| 530 | * of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement. |
||
| 531 | * @param array|bool $updateColumns the column data (name => value) to be updated if they already exist. |
||
| 532 | * If `true` is passed, the column data will be updated to match the insert column data. |
||
| 533 | * If `false` is passed, no update will be performed if the column data already exists. |
||
| 534 | * @param array $params the parameters to be bound to the command. |
||
| 535 | * @return $this the command object itself. |
||
| 536 | * @since 2.0.14 |
||
| 537 | */ |
||
| 538 | 33 | public function upsert($table, $insertColumns, $updateColumns = true, $params = []) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Creates an UPDATE command. |
||
| 547 | * |
||
| 548 | * For example, |
||
| 549 | * |
||
| 550 | * ```php |
||
| 551 | * $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute(); |
||
| 552 | * ``` |
||
| 553 | * |
||
| 554 | * or with using parameter binding for the condition: |
||
| 555 | * |
||
| 556 | * ```php |
||
| 557 | * $minAge = 30; |
||
| 558 | * $connection->createCommand()->update('user', ['status' => 1], 'age > :minAge', [':minAge' => $minAge])->execute(); |
||
| 559 | * ``` |
||
| 560 | * |
||
| 561 | * The method will properly escape the column names and bind the values to be updated. |
||
| 562 | * |
||
| 563 | * Note that the created command is not executed until [[execute()]] is called. |
||
| 564 | * |
||
| 565 | * @param string $table the table to be updated. |
||
| 566 | * @param array $columns the column data (name => value) to be updated. |
||
| 567 | * @param string|array $condition the condition that will be put in the WHERE part. Please |
||
| 568 | * refer to [[Query::where()]] on how to specify condition. |
||
| 569 | * @param array $params the parameters to be bound to the command |
||
| 570 | * @return $this the command object itself |
||
| 571 | */ |
||
| 572 | 109 | public function update($table, $columns, $condition = '', $params = []) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Creates a DELETE command. |
||
| 581 | * |
||
| 582 | * For example, |
||
| 583 | * |
||
| 584 | * ```php |
||
| 585 | * $connection->createCommand()->delete('user', 'status = 0')->execute(); |
||
| 586 | * ``` |
||
| 587 | * |
||
| 588 | * or with using parameter binding for the condition: |
||
| 589 | * |
||
| 590 | * ```php |
||
| 591 | * $status = 0; |
||
| 592 | * $connection->createCommand()->delete('user', 'status = :status', [':status' => $status])->execute(); |
||
| 593 | * ``` |
||
| 594 | * |
||
| 595 | * The method will properly escape the table and column names. |
||
| 596 | * |
||
| 597 | * Note that the created command is not executed until [[execute()]] is called. |
||
| 598 | * |
||
| 599 | * @param string $table the table where the data will be deleted from. |
||
| 600 | * @param string|array $condition the condition that will be put in the WHERE part. Please |
||
| 601 | * refer to [[Query::where()]] on how to specify condition. |
||
| 602 | * @param array $params the parameters to be bound to the command |
||
| 603 | * @return $this the command object itself |
||
| 604 | */ |
||
| 605 | 350 | public function delete($table, $condition = '', $params = []) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Creates a SQL command for creating a new DB table. |
||
| 614 | * |
||
| 615 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
| 616 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
| 617 | * stands for the column type which can contain an abstract DB type. |
||
| 618 | * The method [[QueryBuilder::getColumnType()]] will be called |
||
| 619 | * to convert the abstract column types to physical ones. For example, `string` will be converted |
||
| 620 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
| 621 | * |
||
| 622 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
| 623 | * inserted into the generated SQL. |
||
| 624 | * |
||
| 625 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
| 626 | * @param array $columns the columns (name => definition) in the new table. |
||
| 627 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
| 628 | * @return $this the command object itself |
||
| 629 | */ |
||
| 630 | 131 | public function createTable($table, $columns, $options = null) |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Creates a SQL command for renaming a DB table. |
||
| 639 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
| 640 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
| 641 | * @return $this the command object itself |
||
| 642 | */ |
||
| 643 | 3 | public function renameTable($table, $newName) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Creates a SQL command for dropping a DB table. |
||
| 652 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
| 653 | * @return $this the command object itself |
||
| 654 | */ |
||
| 655 | 39 | public function dropTable($table) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Creates a SQL command for truncating a DB table. |
||
| 664 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
| 665 | * @return $this the command object itself |
||
| 666 | */ |
||
| 667 | 13 | public function truncateTable($table) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Creates a SQL command for adding a new DB column. |
||
| 676 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
| 677 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
| 678 | * @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
||
| 679 | * to convert the give column type to the physical one. For example, `string` will be converted |
||
| 680 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
| 681 | * @return $this the command object itself |
||
| 682 | */ |
||
| 683 | 4 | public function addColumn($table, $column, $type) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Creates a SQL command for dropping a DB column. |
||
| 692 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
| 693 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
| 694 | * @return $this the command object itself |
||
| 695 | */ |
||
| 696 | public function dropColumn($table, $column) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Creates a SQL command for renaming a column. |
||
| 705 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
| 706 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
| 707 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
| 708 | * @return $this the command object itself |
||
| 709 | */ |
||
| 710 | public function renameColumn($table, $oldName, $newName) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Creates a SQL command for changing the definition of a column. |
||
| 719 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
| 720 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
| 721 | * @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
||
| 722 | * to convert the give column type to the physical one. For example, `string` will be converted |
||
| 723 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
| 724 | * @return $this the command object itself |
||
| 725 | */ |
||
| 726 | 2 | public function alterColumn($table, $column, $type) |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Creates a SQL command for adding a primary key constraint to an existing table. |
||
| 735 | * The method will properly quote the table and column names. |
||
| 736 | * @param string $name the name of the primary key constraint. |
||
| 737 | * @param string $table the table that the primary key constraint will be added to. |
||
| 738 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
| 739 | * @return $this the command object itself. |
||
| 740 | */ |
||
| 741 | 2 | public function addPrimaryKey($name, $table, $columns) |
|
| 747 | |||
| 748 | /** |
||
| 749 | * Creates a SQL command for removing a primary key constraint to an existing table. |
||
| 750 | * @param string $name the name of the primary key constraint to be removed. |
||
| 751 | * @param string $table the table that the primary key constraint will be removed from. |
||
| 752 | * @return $this the command object itself |
||
| 753 | */ |
||
| 754 | 2 | public function dropPrimaryKey($name, $table) |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Creates a SQL command for adding a foreign key constraint to an existing table. |
||
| 763 | * The method will properly quote the table and column names. |
||
| 764 | * @param string $name the name of the foreign key constraint. |
||
| 765 | * @param string $table the table that the foreign key constraint will be added to. |
||
| 766 | * @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. |
||
| 767 | * @param string $refTable the table that the foreign key references to. |
||
| 768 | * @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas. |
||
| 769 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
| 770 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
| 771 | * @return $this the command object itself |
||
| 772 | */ |
||
| 773 | 4 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Creates a SQL command for dropping a foreign key constraint. |
||
| 782 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
| 783 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
| 784 | * @return $this the command object itself |
||
| 785 | */ |
||
| 786 | 4 | public function dropForeignKey($name, $table) |
|
| 792 | |||
| 793 | /** |
||
| 794 | * Creates a SQL command for creating a new index. |
||
| 795 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
| 796 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
| 797 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them |
||
| 798 | * by commas. The column names will be properly quoted by the method. |
||
| 799 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
| 800 | * @return $this the command object itself |
||
| 801 | */ |
||
| 802 | 12 | public function createIndex($name, $table, $columns, $unique = false) |
|
| 808 | |||
| 809 | /** |
||
| 810 | * Creates a SQL command for dropping an index. |
||
| 811 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
| 812 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
| 813 | * @return $this the command object itself |
||
| 814 | */ |
||
| 815 | 3 | public function dropIndex($name, $table) |
|
| 821 | |||
| 822 | /** |
||
| 823 | * Creates a SQL command for adding an unique constraint to an existing table. |
||
| 824 | * @param string $name the name of the unique constraint. |
||
| 825 | * The name will be properly quoted by the method. |
||
| 826 | * @param string $table the table that the unique constraint will be added to. |
||
| 827 | * The name will be properly quoted by the method. |
||
| 828 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
| 829 | * If there are multiple columns, separate them with commas. |
||
| 830 | * The name will be properly quoted by the method. |
||
| 831 | * @return $this the command object itself. |
||
| 832 | * @since 2.0.13 |
||
| 833 | */ |
||
| 834 | 2 | public function addUnique($name, $table, $columns) |
|
| 840 | |||
| 841 | /** |
||
| 842 | * Creates a SQL command for dropping an unique constraint. |
||
| 843 | * @param string $name the name of the unique constraint to be dropped. |
||
| 844 | * The name will be properly quoted by the method. |
||
| 845 | * @param string $table the table whose unique constraint is to be dropped. |
||
| 846 | * The name will be properly quoted by the method. |
||
| 847 | * @return $this the command object itself. |
||
| 848 | * @since 2.0.13 |
||
| 849 | */ |
||
| 850 | 2 | public function dropUnique($name, $table) |
|
| 856 | |||
| 857 | /** |
||
| 858 | * Creates a SQL command for adding a check constraint to an existing table. |
||
| 859 | * @param string $name the name of the check constraint. |
||
| 860 | * The name will be properly quoted by the method. |
||
| 861 | * @param string $table the table that the check constraint will be added to. |
||
| 862 | * The name will be properly quoted by the method. |
||
| 863 | * @param string $expression the SQL of the `CHECK` constraint. |
||
| 864 | * @return $this the command object itself. |
||
| 865 | * @since 2.0.13 |
||
| 866 | */ |
||
| 867 | 1 | public function addCheck($name, $table, $expression) |
|
| 873 | |||
| 874 | /** |
||
| 875 | * Creates a SQL command for dropping a check constraint. |
||
| 876 | * @param string $name the name of the check constraint to be dropped. |
||
| 877 | * The name will be properly quoted by the method. |
||
| 878 | * @param string $table the table whose check constraint is to be dropped. |
||
| 879 | * The name will be properly quoted by the method. |
||
| 880 | * @return $this the command object itself. |
||
| 881 | * @since 2.0.13 |
||
| 882 | */ |
||
| 883 | 1 | public function dropCheck($name, $table) |
|
| 889 | |||
| 890 | /** |
||
| 891 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
| 892 | * @param string $name the name of the default value constraint. |
||
| 893 | * The name will be properly quoted by the method. |
||
| 894 | * @param string $table the table that the default value constraint will be added to. |
||
| 895 | * The name will be properly quoted by the method. |
||
| 896 | * @param string $column the name of the column to that the constraint will be added on. |
||
| 897 | * The name will be properly quoted by the method. |
||
| 898 | * @param mixed $value default value. |
||
| 899 | * @return $this the command object itself. |
||
| 900 | * @since 2.0.13 |
||
| 901 | */ |
||
| 902 | public function addDefaultValue($name, $table, $column, $value) |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Creates a SQL command for dropping a default value constraint. |
||
| 911 | * @param string $name the name of the default value constraint to be dropped. |
||
| 912 | * The name will be properly quoted by the method. |
||
| 913 | * @param string $table the table whose default value constraint is to be dropped. |
||
| 914 | * The name will be properly quoted by the method. |
||
| 915 | * @return $this the command object itself. |
||
| 916 | * @since 2.0.13 |
||
| 917 | */ |
||
| 918 | public function dropDefaultValue($name, $table) |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Creates a SQL command for resetting the sequence value of a table's primary key. |
||
| 927 | * The sequence will be reset such that the primary key of the next new row inserted |
||
| 928 | * will have the specified value or 1. |
||
| 929 | * @param string $table the name of the table whose primary key sequence will be reset |
||
| 930 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
| 931 | * the next new row's primary key will have a value 1. |
||
| 932 | * @return $this the command object itself |
||
| 933 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
| 934 | */ |
||
| 935 | 16 | public function resetSequence($table, $value = null) |
|
| 941 | |||
| 942 | /** |
||
| 943 | * Builds a SQL command for enabling or disabling integrity check. |
||
| 944 | * @param bool $check whether to turn on or off the integrity check. |
||
| 945 | * @param string $schema the schema name of the tables. Defaults to empty string, meaning the current |
||
| 946 | * or default schema. |
||
| 947 | * @param string $table the table name. |
||
| 948 | * @return $this the command object itself |
||
| 949 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
| 950 | */ |
||
| 951 | 4 | public function checkIntegrity($check = true, $schema = '', $table = '') |
|
| 957 | |||
| 958 | /** |
||
| 959 | * Builds a SQL command for adding comment to column. |
||
| 960 | * |
||
| 961 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 962 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
| 963 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
| 964 | * @return $this the command object itself |
||
| 965 | * @since 2.0.8 |
||
| 966 | */ |
||
| 967 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
| 973 | |||
| 974 | /** |
||
| 975 | * Builds a SQL command for adding comment to table. |
||
| 976 | * |
||
| 977 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 978 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
| 979 | * @return $this the command object itself |
||
| 980 | * @since 2.0.8 |
||
| 981 | */ |
||
| 982 | public function addCommentOnTable($table, $comment) |
||
| 988 | |||
| 989 | /** |
||
| 990 | * Builds a SQL command for dropping comment from column. |
||
| 991 | * |
||
| 992 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 993 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
| 994 | * @return $this the command object itself |
||
| 995 | * @since 2.0.8 |
||
| 996 | */ |
||
| 997 | 2 | public function dropCommentFromColumn($table, $column) |
|
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Builds a SQL command for dropping comment from table. |
||
| 1006 | * |
||
| 1007 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 1008 | * @return $this the command object itself |
||
| 1009 | * @since 2.0.8 |
||
| 1010 | */ |
||
| 1011 | public function dropCommentFromTable($table) |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Creates a SQL View. |
||
| 1020 | * |
||
| 1021 | * @param string $viewName the name of the view to be created. |
||
| 1022 | * @param string|Query $subquery the select statement which defines the view. |
||
| 1023 | * This can be either a string or a [[Query]] object. |
||
| 1024 | * @return $this the command object itself. |
||
| 1025 | * @since 2.0.14 |
||
| 1026 | */ |
||
| 1027 | 3 | public function createView($viewName, $subquery) |
|
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Drops a SQL View. |
||
| 1036 | * |
||
| 1037 | * @param string $viewName the name of the view to be dropped. |
||
| 1038 | * @return $this the command object itself. |
||
| 1039 | * @since 2.0.14 |
||
| 1040 | */ |
||
| 1041 | 3 | public function dropView($viewName) |
|
| 1047 | |||
| 1048 | /** |
||
| 1049 | * Executes the SQL statement. |
||
| 1050 | * This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs. |
||
| 1051 | * No result set will be returned. |
||
| 1052 | * @return int number of rows affected by the execution. |
||
| 1053 | * @throws Exception execution failed |
||
| 1054 | */ |
||
| 1055 | 651 | public function execute() |
|
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Logs the current database query if query logging is enabled and returns |
||
| 1085 | * the profiling token if profiling is enabled. |
||
| 1086 | * @param string $category the log category. |
||
| 1087 | * @return array array of two elements, the first is boolean of whether profiling is enabled or not. |
||
| 1088 | * The second is the rawSql if it has been created. |
||
| 1089 | */ |
||
| 1090 | 1267 | private function logQuery($category) |
|
| 1102 | |||
| 1103 | /** |
||
| 1104 | * Performs the actual DB query of a SQL statement. |
||
| 1105 | * @param string $method method of PDOStatement to be called |
||
| 1106 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
||
| 1107 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
||
| 1108 | * @return mixed the method execution result |
||
| 1109 | * @throws Exception if the query causes any problem |
||
| 1110 | * @since 2.0.1 this method is protected (was private before). |
||
| 1111 | */ |
||
| 1112 | 1226 | protected function queryInternal($method, $fetchMode = null) |
|
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Marks a specified table schema to be refreshed after command execution. |
||
| 1170 | * @param string $name name of the table, which schema should be refreshed. |
||
| 1171 | * @return $this this command instance |
||
| 1172 | * @since 2.0.6 |
||
| 1173 | */ |
||
| 1174 | 140 | protected function requireTableSchemaRefresh($name) |
|
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Refreshes table schema, which was marked by [[requireTableSchemaRefresh()]]. |
||
| 1182 | * @since 2.0.6 |
||
| 1183 | */ |
||
| 1184 | 645 | protected function refreshTableSchema() |
|
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Marks the command to be executed in transaction. |
||
| 1193 | * @param string|null $isolationLevel The isolation level to use for this transaction. |
||
| 1194 | * See [[Transaction::begin()]] for details. |
||
| 1195 | * @return $this this command instance. |
||
| 1196 | * @since 2.0.14 |
||
| 1197 | */ |
||
| 1198 | 3 | protected function requireTransaction($isolationLevel = null) |
|
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Sets a callable (e.g. anonymous function) that is called when [[Exception]] is thrown |
||
| 1206 | * when executing the command. The signature of the callable should be: |
||
| 1207 | * |
||
| 1208 | * ```php |
||
| 1209 | * function (\yii\db\Exception $e, $attempt) |
||
| 1210 | * { |
||
| 1211 | * // return true or false (whether to retry the command or rethrow $e) |
||
| 1212 | * } |
||
| 1213 | * ``` |
||
| 1214 | * |
||
| 1215 | * The callable will recieve a database exception thrown and a current attempt |
||
| 1216 | * (to execute the command) number starting from 1. |
||
| 1217 | * |
||
| 1218 | * @param callable $handler a PHP callback to handle database exceptions. |
||
| 1219 | * @return $this this command instance. |
||
| 1220 | * @since 2.0.14 |
||
| 1221 | */ |
||
| 1222 | 3 | protected function setRetryHandler(callable $handler) |
|
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Executes a prepared statement. |
||
| 1230 | * |
||
| 1231 | * It's a wrapper around [[\PDOStatement::execute()]] to support transactions |
||
| 1232 | * and retry handlers. |
||
| 1233 | * |
||
| 1234 | * @param string|null $rawSql the rawSql if it has been created. |
||
| 1235 | * @throws Exception if execution failed. |
||
| 1236 | * @since 2.0.14 |
||
| 1237 | */ |
||
| 1238 | 1266 | protected function internalExecute($rawSql) |
|
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Resets command properties to their initial state. |
||
| 1267 | * |
||
| 1268 | * @since 2.0.13 |
||
| 1269 | */ |
||
| 1270 | 1303 | protected function reset() |
|
| 1279 | } |
||
| 1280 |