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 | /** |
||
| 116 | * Enables query cache for this command. |
||
| 117 | * @param int $duration the number of seconds that query result of this command can remain valid in the cache. |
||
| 118 | * If this is not set, the value of [[Connection::queryCacheDuration]] will be used instead. |
||
| 119 | * Use 0 to indicate that the cached data will never expire. |
||
| 120 | * @param \yii\caching\Dependency $dependency the cache dependency associated with the cached query result. |
||
| 121 | * @return $this the command object itself |
||
| 122 | */ |
||
| 123 | public function cache($duration = null, $dependency = null) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Disables query cache for this command. |
||
| 132 | * @return $this the command object itself |
||
| 133 | */ |
||
| 134 | public function noCache() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns the SQL statement for this command. |
||
| 142 | * @return string the SQL statement to be executed |
||
| 143 | */ |
||
| 144 | 3 | public function getSql() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Specifies the SQL statement to be executed. The SQL statement will be quoted using [[Connection::quoteSql()]]. |
||
| 151 | * The previous SQL (if any) will be discarded, and [[params]] will be cleared as well. See [[reset()]] |
||
| 152 | * for details. |
||
| 153 | * |
||
| 154 | * @param string $sql the SQL statement to be set. |
||
| 155 | * @return $this this command instance |
||
| 156 | * @see reset() |
||
| 157 | * @see cancel() |
||
| 158 | */ |
||
| 159 | 3 | public function setSql($sql) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Specifies the SQL statement to be executed. The SQL statement will not be modified in any way. |
||
| 172 | * The previous SQL (if any) will be discarded, and [[params]] will be cleared as well. See [[reset()]] |
||
| 173 | * for details. |
||
| 174 | * |
||
| 175 | * @param string $sql the SQL statement to be set. |
||
| 176 | * @return $this this command instance |
||
| 177 | * @since 2.0.13 |
||
| 178 | * @see reset() |
||
| 179 | * @see cancel() |
||
| 180 | */ |
||
| 181 | public function setRawSql($sql) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the raw SQL by inserting parameter values into the corresponding placeholders in [[sql]]. |
||
| 194 | * Note that the return value of this method should mainly be used for logging purpose. |
||
| 195 | * It is likely that this method returns an invalid SQL due to improper replacement of parameter placeholders. |
||
| 196 | * @return string the raw SQL with parameter values inserted into the corresponding placeholders in [[sql]]. |
||
| 197 | */ |
||
| 198 | 3 | public function getRawSql() |
|
| 199 | { |
||
| 200 | 3 | if (empty($this->params)) { |
|
| 201 | 2 | return $this->_sql; |
|
| 202 | } |
||
| 203 | 1 | $params = []; |
|
| 204 | 1 | foreach ($this->params as $name => $value) { |
|
| 205 | 1 | if (is_string($name) && strncmp(':', $name, 1)) { |
|
| 206 | $name = ':' . $name; |
||
| 207 | } |
||
| 208 | 1 | if (is_string($value)) { |
|
| 209 | $params[$name] = $this->db->quoteValue($value); |
||
| 210 | 1 | } elseif (is_bool($value)) { |
|
| 211 | $params[$name] = ($value ? 'TRUE' : 'FALSE'); |
||
| 212 | 1 | } elseif ($value === null) { |
|
| 213 | $params[$name] = 'NULL'; |
||
| 214 | 1 | } elseif ((!is_object($value) && !is_resource($value)) || $value instanceof Expression) { |
|
| 215 | 1 | $params[$name] = $value; |
|
| 216 | } |
||
| 217 | } |
||
| 218 | 1 | if (!isset($params[1])) { |
|
| 219 | 1 | return strtr($this->_sql, $params); |
|
| 220 | } |
||
| 221 | $sql = ''; |
||
| 222 | foreach (explode('?', $this->_sql) as $i => $part) { |
||
| 223 | $sql .= ($params[$i] ?? '') . $part; |
||
| 224 | } |
||
| 225 | |||
| 226 | return $sql; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Prepares the SQL statement to be executed. |
||
| 231 | * For complex SQL statement that is to be executed multiple times, |
||
| 232 | * this may improve performance. |
||
| 233 | * For SQL statement with binding parameters, this method is invoked |
||
| 234 | * automatically. |
||
| 235 | * @param bool $forRead whether this method is called for a read query. If null, it means |
||
| 236 | * the SQL statement should be used to determine whether it is for read or write. |
||
| 237 | * @throws Exception if there is any DB error |
||
| 238 | */ |
||
| 239 | 3 | public function prepare($forRead = null) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Cancels the execution of the SQL statement. |
||
| 270 | * This method mainly sets [[pdoStatement]] to be null. |
||
| 271 | */ |
||
| 272 | 3 | public function cancel() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Binds a parameter to the SQL statement to be executed. |
||
| 279 | * @param string|int $name parameter identifier. For a prepared statement |
||
| 280 | * using named placeholders, this will be a parameter name of |
||
| 281 | * the form `:name`. For a prepared statement using question mark |
||
| 282 | * placeholders, this will be the 1-indexed position of the parameter. |
||
| 283 | * @param mixed $value the PHP variable to bind to the SQL statement parameter (passed by reference) |
||
| 284 | * @param int $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
||
| 285 | * @param int $length length of the data type |
||
| 286 | * @param mixed $driverOptions the driver-specific options |
||
| 287 | * @return $this the current command being executed |
||
| 288 | * @see http://www.php.net/manual/en/function.PDOStatement-bindParam.php |
||
| 289 | */ |
||
| 290 | public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Binds pending parameters that were registered via [[bindValue()]] and [[bindValues()]]. |
||
| 311 | * Note that this method requires an active [[pdoStatement]]. |
||
| 312 | */ |
||
| 313 | 3 | protected function bindPendingParams() |
|
| 314 | { |
||
| 315 | 3 | foreach ($this->_pendingParams as $name => $value) { |
|
| 316 | 1 | $this->pdoStatement->bindValue($name, $value[0], $value[1]); |
|
| 317 | } |
||
| 318 | 3 | $this->_pendingParams = []; |
|
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Binds a value to a parameter. |
||
| 323 | * @param string|int $name Parameter identifier. For a prepared statement |
||
| 324 | * using named placeholders, this will be a parameter name of |
||
| 325 | * the form `:name`. For a prepared statement using question mark |
||
| 326 | * placeholders, this will be the 1-indexed position of the parameter. |
||
| 327 | * @param mixed $value The value to bind to the parameter |
||
| 328 | * @param int $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
||
| 329 | * @return $this the current command being executed |
||
| 330 | * @see http://www.php.net/manual/en/function.PDOStatement-bindValue.php |
||
| 331 | */ |
||
| 332 | public function bindValue($name, $value, $dataType = null) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Binds a list of values to the corresponding parameters. |
||
| 345 | * This is similar to [[bindValue()]] except that it binds multiple values at a time. |
||
| 346 | * Note that the SQL data type of each value is determined by its PHP type. |
||
| 347 | * @param array $values the values to be bound. This must be given in terms of an associative |
||
| 348 | * array with array keys being the parameter names, and array values the corresponding parameter values, |
||
| 349 | * e.g. `[':name' => 'John', ':age' => 25]`. By default, the PDO type of each value is determined |
||
| 350 | * by its PHP type. You may explicitly specify the PDO type by using a [[yii\db\PdoValue]] class: `new PdoValue(value, type)`, |
||
| 351 | * e.g. `[':name' => 'John', ':profile' => new PdoValue($profile, \PDO::PARAM_LOB)]`. |
||
| 352 | * @return $this the current command being executed |
||
| 353 | */ |
||
| 354 | 3 | public function bindValues($values) |
|
| 355 | { |
||
| 356 | 3 | if (empty($values)) { |
|
| 357 | 2 | return $this; |
|
| 358 | } |
||
| 359 | |||
| 360 | 1 | $schema = $this->db->getSchema(); |
|
| 361 | 1 | foreach ($values as $name => $value) { |
|
| 362 | 1 | if (is_array($value)) { // TODO: Drop in Yii 2.1 |
|
| 363 | $this->_pendingParams[$name] = $value; |
||
| 364 | $this->params[$name] = $value[0]; |
||
| 365 | 1 | } elseif ($value instanceof PdoValue) { |
|
| 366 | $this->_pendingParams[$name] = [$value->getValue(), $value->getType()]; |
||
| 367 | $this->params[$name] = $value->getValue(); |
||
| 368 | } else { |
||
| 369 | 1 | $type = $schema->getPdoType($value); |
|
| 370 | 1 | $this->_pendingParams[$name] = [$value, $type]; |
|
| 371 | 1 | $this->params[$name] = $value; |
|
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | 1 | return $this; |
|
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Executes the SQL statement and returns query result. |
||
| 380 | * This method is for executing a SQL query that returns result set, such as `SELECT`. |
||
| 381 | * @return DataReader the reader object for fetching the query result |
||
| 382 | * @throws Exception execution failed |
||
| 383 | */ |
||
| 384 | public function query() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Executes the SQL statement and returns ALL rows at once. |
||
| 391 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
||
| 392 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
||
| 393 | * @return array all rows of the query result. Each array element is an array representing a row of data. |
||
| 394 | * An empty array is returned if the query results in nothing. |
||
| 395 | * @throws Exception execution failed |
||
| 396 | */ |
||
| 397 | 1 | public function queryAll($fetchMode = null) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Executes the SQL statement and returns the first row of the result. |
||
| 404 | * This method is best used when only the first row of result is needed for a query. |
||
| 405 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://php.net/manual/en/pdostatement.setfetchmode.php) |
||
| 406 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
||
| 407 | * @return array|false the first row (in terms of an array) of the query result. False is returned if the query |
||
| 408 | * results in nothing. |
||
| 409 | * @throws Exception execution failed |
||
| 410 | */ |
||
| 411 | public function queryOne($fetchMode = null) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Executes the SQL statement and returns the value of the first column in the first row of data. |
||
| 418 | * This method is best used when only a single value is needed for a query. |
||
| 419 | * @return string|null|false the value of the first column in the first row of the query result. |
||
| 420 | * False is returned if there is no value. |
||
| 421 | * @throws Exception execution failed |
||
| 422 | */ |
||
| 423 | 3 | public function queryScalar() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Executes the SQL statement and returns the first column of the result. |
||
| 435 | * This method is best used when only the first column of result (i.e. the first element in each row) |
||
| 436 | * is needed for a query. |
||
| 437 | * @return array the first column of the query result. Empty array is returned if the query results in nothing. |
||
| 438 | * @throws Exception execution failed |
||
| 439 | */ |
||
| 440 | public function queryColumn() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Creates an INSERT command. |
||
| 447 | * |
||
| 448 | * For example, |
||
| 449 | * |
||
| 450 | * ```php |
||
| 451 | * $connection->createCommand()->insert('user', [ |
||
| 452 | * 'name' => 'Sam', |
||
| 453 | * 'age' => 30, |
||
| 454 | * ])->execute(); |
||
| 455 | * ``` |
||
| 456 | * |
||
| 457 | * The method will properly escape the column names, and bind the values to be inserted. |
||
| 458 | * |
||
| 459 | * Note that the created command is not executed until [[execute()]] is called. |
||
| 460 | * |
||
| 461 | * @param string $table the table that new rows will be inserted into. |
||
| 462 | * @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
||
| 463 | * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
||
| 464 | * Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
||
| 465 | * @return $this the command object itself |
||
| 466 | */ |
||
| 467 | public function insert($table, $columns) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Creates a batch INSERT command. |
||
| 477 | * |
||
| 478 | * For example, |
||
| 479 | * |
||
| 480 | * ```php |
||
| 481 | * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
||
| 482 | * ['Tom', 30], |
||
| 483 | * ['Jane', 20], |
||
| 484 | * ['Linda', 25], |
||
| 485 | * ])->execute(); |
||
| 486 | * ``` |
||
| 487 | * |
||
| 488 | * The method will properly escape the column names, and quote the values to be inserted. |
||
| 489 | * |
||
| 490 | * Note that the values in each row must match the corresponding column names. |
||
| 491 | * |
||
| 492 | * Also note that the created command is not executed until [[execute()]] is called. |
||
| 493 | * |
||
| 494 | * @param string $table the table that new rows will be inserted into. |
||
| 495 | * @param array $columns the column names |
||
| 496 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
| 497 | * @return $this the command object itself |
||
| 498 | */ |
||
| 499 | public function batchInsert($table, $columns, $rows) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Creates a command to insert rows into a database table if |
||
| 517 | * they do not already exist (matching unique constraints), |
||
| 518 | * or update them if they do. |
||
| 519 | * |
||
| 520 | * For example, |
||
| 521 | * |
||
| 522 | * ```php |
||
| 523 | * $sql = $queryBuilder->upsert('pages', [ |
||
| 524 | * 'name' => 'Front page', |
||
| 525 | * 'url' => 'http://example.com/', // url is unique |
||
| 526 | * 'visits' => 0, |
||
| 527 | * ], [ |
||
| 528 | * 'visits' => new \yii\db\Expression('visits + 1'), |
||
| 529 | * ], $params); |
||
| 530 | * ``` |
||
| 531 | * |
||
| 532 | * The method will properly escape the table and column names. |
||
| 533 | * |
||
| 534 | * @param string $table the table that new rows will be inserted into/updated in. |
||
| 535 | * @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance |
||
| 536 | * of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement. |
||
| 537 | * @param array|bool $updateColumns the column data (name => value) to be updated if they already exist. |
||
| 538 | * If `true` is passed, the column data will be updated to match the insert column data. |
||
| 539 | * If `false` is passed, no update will be performed if the column data already exists. |
||
| 540 | * @param array $params the parameters to be bound to the command. |
||
| 541 | * @return $this the command object itself. |
||
| 542 | * @since 2.0.14 |
||
| 543 | */ |
||
| 544 | public function upsert($table, $insertColumns, $updateColumns = true, $params = []) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Creates an UPDATE command. |
||
| 553 | * |
||
| 554 | * For example, |
||
| 555 | * |
||
| 556 | * ```php |
||
| 557 | * $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute(); |
||
| 558 | * ``` |
||
| 559 | * |
||
| 560 | * or with using parameter binding for the condition: |
||
| 561 | * |
||
| 562 | * ```php |
||
| 563 | * $minAge = 30; |
||
| 564 | * $connection->createCommand()->update('user', ['status' => 1], 'age > :minAge', [':minAge' => $minAge])->execute(); |
||
| 565 | * ``` |
||
| 566 | * |
||
| 567 | * The method will properly escape the column names and bind the values to be updated. |
||
| 568 | * |
||
| 569 | * Note that the created command is not executed until [[execute()]] is called. |
||
| 570 | * |
||
| 571 | * @param string $table the table to be updated. |
||
| 572 | * @param array $columns the column data (name => value) to be updated. |
||
| 573 | * @param string|array $condition the condition that will be put in the WHERE part. Please |
||
| 574 | * refer to [[Query::where()]] on how to specify condition. |
||
| 575 | * @param array $params the parameters to be bound to the command |
||
| 576 | * @return $this the command object itself |
||
| 577 | */ |
||
| 578 | public function update($table, $columns, $condition = '', $params = []) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Creates a DELETE command. |
||
| 587 | * |
||
| 588 | * For example, |
||
| 589 | * |
||
| 590 | * ```php |
||
| 591 | * $connection->createCommand()->delete('user', 'status = 0')->execute(); |
||
| 592 | * ``` |
||
| 593 | * |
||
| 594 | * or with using parameter binding for the condition: |
||
| 595 | * |
||
| 596 | * ```php |
||
| 597 | * $status = 0; |
||
| 598 | * $connection->createCommand()->delete('user', 'status = :status', [':status' => $status])->execute(); |
||
| 599 | * ``` |
||
| 600 | * |
||
| 601 | * The method will properly escape the table and column names. |
||
| 602 | * |
||
| 603 | * Note that the created command is not executed until [[execute()]] is called. |
||
| 604 | * |
||
| 605 | * @param string $table the table where the data will be deleted from. |
||
| 606 | * @param string|array $condition the condition that will be put in the WHERE part. Please |
||
| 607 | * refer to [[Query::where()]] on how to specify condition. |
||
| 608 | * @param array $params the parameters to be bound to the command |
||
| 609 | * @return $this the command object itself |
||
| 610 | */ |
||
| 611 | public function delete($table, $condition = '', $params = []) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Creates a SQL command for creating a new DB table. |
||
| 620 | * |
||
| 621 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
| 622 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
| 623 | * stands for the column type which can contain an abstract DB type. |
||
| 624 | * The method [[QueryBuilder::getColumnType()]] will be called |
||
| 625 | * to convert the abstract column types to physical ones. For example, `string` will be converted |
||
| 626 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
| 627 | * |
||
| 628 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
| 629 | * inserted into the generated SQL. |
||
| 630 | * |
||
| 631 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
| 632 | * @param array $columns the columns (name => definition) in the new table. |
||
| 633 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
| 634 | * @return $this the command object itself |
||
| 635 | */ |
||
| 636 | public function createTable($table, $columns, $options = null) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Creates a SQL command for renaming a DB table. |
||
| 645 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
| 646 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
| 647 | * @return $this the command object itself |
||
| 648 | */ |
||
| 649 | public function renameTable($table, $newName) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Creates a SQL command for dropping a DB table. |
||
| 658 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
| 659 | * @return $this the command object itself |
||
| 660 | */ |
||
| 661 | public function dropTable($table) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Creates a SQL command for truncating a DB table. |
||
| 670 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
| 671 | * @return $this the command object itself |
||
| 672 | */ |
||
| 673 | public function truncateTable($table) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Creates a SQL command for adding a new DB column. |
||
| 682 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
| 683 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
| 684 | * @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
||
| 685 | * to convert the give column type to the physical one. For example, `string` will be converted |
||
| 686 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
| 687 | * @return $this the command object itself |
||
| 688 | */ |
||
| 689 | public function addColumn($table, $column, $type) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Creates a SQL command for dropping a DB column. |
||
| 698 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
| 699 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
| 700 | * @return $this the command object itself |
||
| 701 | */ |
||
| 702 | public function dropColumn($table, $column) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Creates a SQL command for renaming a column. |
||
| 711 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
| 712 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
| 713 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
| 714 | * @return $this the command object itself |
||
| 715 | */ |
||
| 716 | public function renameColumn($table, $oldName, $newName) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Creates a SQL command for changing the definition of a column. |
||
| 725 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
| 726 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
| 727 | * @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
||
| 728 | * to convert the give column type to the physical one. For example, `string` will be converted |
||
| 729 | * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
||
| 730 | * @return $this the command object itself |
||
| 731 | */ |
||
| 732 | public function alterColumn($table, $column, $type) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Creates a SQL command for adding a primary key constraint to an existing table. |
||
| 741 | * The method will properly quote the table and column names. |
||
| 742 | * @param string $name the name of the primary key constraint. |
||
| 743 | * @param string $table the table that the primary key constraint will be added to. |
||
| 744 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
| 745 | * @return $this the command object itself. |
||
| 746 | */ |
||
| 747 | public function addPrimaryKey($name, $table, $columns) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Creates a SQL command for removing a primary key constraint to an existing table. |
||
| 756 | * @param string $name the name of the primary key constraint to be removed. |
||
| 757 | * @param string $table the table that the primary key constraint will be removed from. |
||
| 758 | * @return $this the command object itself |
||
| 759 | */ |
||
| 760 | public function dropPrimaryKey($name, $table) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Creates a SQL command for adding a foreign key constraint to an existing table. |
||
| 769 | * The method will properly quote the table and column names. |
||
| 770 | * @param string $name the name of the foreign key constraint. |
||
| 771 | * @param string $table the table that the foreign key constraint will be added to. |
||
| 772 | * @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. |
||
| 773 | * @param string $refTable the table that the foreign key references to. |
||
| 774 | * @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas. |
||
| 775 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
| 776 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
| 777 | * @return $this the command object itself |
||
| 778 | */ |
||
| 779 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Creates a SQL command for dropping a foreign key constraint. |
||
| 788 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
| 789 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
| 790 | * @return $this the command object itself |
||
| 791 | */ |
||
| 792 | public function dropForeignKey($name, $table) |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Creates a SQL command for creating a new index. |
||
| 801 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
| 802 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
| 803 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them |
||
| 804 | * by commas. The column names will be properly quoted by the method. |
||
| 805 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
| 806 | * @return $this the command object itself |
||
| 807 | */ |
||
| 808 | public function createIndex($name, $table, $columns, $unique = false) |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Creates a SQL command for dropping an index. |
||
| 817 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
| 818 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
| 819 | * @return $this the command object itself |
||
| 820 | */ |
||
| 821 | public function dropIndex($name, $table) |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Creates a SQL command for adding an unique constraint to an existing table. |
||
| 830 | * @param string $name the name of the unique constraint. |
||
| 831 | * The name will be properly quoted by the method. |
||
| 832 | * @param string $table the table that the unique constraint will be added to. |
||
| 833 | * The name will be properly quoted by the method. |
||
| 834 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
| 835 | * If there are multiple columns, separate them with commas. |
||
| 836 | * The name will be properly quoted by the method. |
||
| 837 | * @return $this the command object itself. |
||
| 838 | * @since 2.0.13 |
||
| 839 | */ |
||
| 840 | public function addUnique($name, $table, $columns) |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Creates a SQL command for dropping an unique constraint. |
||
| 849 | * @param string $name the name of the unique constraint to be dropped. |
||
| 850 | * The name will be properly quoted by the method. |
||
| 851 | * @param string $table the table whose unique constraint is to be dropped. |
||
| 852 | * The name will be properly quoted by the method. |
||
| 853 | * @return $this the command object itself. |
||
| 854 | * @since 2.0.13 |
||
| 855 | */ |
||
| 856 | public function dropUnique($name, $table) |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Creates a SQL command for adding a check constraint to an existing table. |
||
| 865 | * @param string $name the name of the check constraint. |
||
| 866 | * The name will be properly quoted by the method. |
||
| 867 | * @param string $table the table that the check constraint will be added to. |
||
| 868 | * The name will be properly quoted by the method. |
||
| 869 | * @param string $expression the SQL of the `CHECK` constraint. |
||
| 870 | * @return $this the command object itself. |
||
| 871 | * @since 2.0.13 |
||
| 872 | */ |
||
| 873 | public function addCheck($name, $table, $expression) |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Creates a SQL command for dropping a check constraint. |
||
| 882 | * @param string $name the name of the check constraint to be dropped. |
||
| 883 | * The name will be properly quoted by the method. |
||
| 884 | * @param string $table the table whose check constraint is to be dropped. |
||
| 885 | * The name will be properly quoted by the method. |
||
| 886 | * @return $this the command object itself. |
||
| 887 | * @since 2.0.13 |
||
| 888 | */ |
||
| 889 | public function dropCheck($name, $table) |
||
| 895 | |||
| 896 | /** |
||
| 897 | * Creates a SQL command for adding a default value constraint to an existing table. |
||
| 898 | * @param string $name the name of the default value constraint. |
||
| 899 | * The name will be properly quoted by the method. |
||
| 900 | * @param string $table the table that the default value constraint will be added to. |
||
| 901 | * The name will be properly quoted by the method. |
||
| 902 | * @param string $column the name of the column to that the constraint will be added on. |
||
| 903 | * The name will be properly quoted by the method. |
||
| 904 | * @param mixed $value default value. |
||
| 905 | * @return $this the command object itself. |
||
| 906 | * @since 2.0.13 |
||
| 907 | */ |
||
| 908 | public function addDefaultValue($name, $table, $column, $value) |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Creates a SQL command for dropping a default value constraint. |
||
| 917 | * @param string $name the name of the default value constraint to be dropped. |
||
| 918 | * The name will be properly quoted by the method. |
||
| 919 | * @param string $table the table whose default value constraint is to be dropped. |
||
| 920 | * The name will be properly quoted by the method. |
||
| 921 | * @return $this the command object itself. |
||
| 922 | * @since 2.0.13 |
||
| 923 | */ |
||
| 924 | public function dropDefaultValue($name, $table) |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Creates a SQL command for resetting the sequence value of a table's primary key. |
||
| 933 | * The sequence will be reset such that the primary key of the next new row inserted |
||
| 934 | * will have the specified value or 1. |
||
| 935 | * @param string $table the name of the table whose primary key sequence will be reset |
||
| 936 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
| 937 | * the next new row's primary key will have a value 1. |
||
| 938 | * @return $this the command object itself |
||
| 939 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
| 940 | */ |
||
| 941 | public function resetSequence($table, $value = null) |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Builds a SQL command for enabling or disabling integrity check. |
||
| 950 | * @param bool $check whether to turn on or off the integrity check. |
||
| 951 | * @param string $schema the schema name of the tables. Defaults to empty string, meaning the current |
||
| 952 | * or default schema. |
||
| 953 | * @param string $table the table name. |
||
| 954 | * @return $this the command object itself |
||
| 955 | * @throws NotSupportedException if this is not supported by the underlying DBMS |
||
| 956 | */ |
||
| 957 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Builds a SQL command for adding comment to column. |
||
| 966 | * |
||
| 967 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 968 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
| 969 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
| 970 | * @return $this the command object itself |
||
| 971 | * @since 2.0.8 |
||
| 972 | */ |
||
| 973 | public function addCommentOnColumn($table, $column, $comment) |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Builds a SQL command for adding comment to table. |
||
| 982 | * |
||
| 983 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 984 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
| 985 | * @return $this the command object itself |
||
| 986 | * @since 2.0.8 |
||
| 987 | */ |
||
| 988 | public function addCommentOnTable($table, $comment) |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Builds a SQL command for dropping comment from column. |
||
| 997 | * |
||
| 998 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 999 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
| 1000 | * @return $this the command object itself |
||
| 1001 | * @since 2.0.8 |
||
| 1002 | */ |
||
| 1003 | public function dropCommentFromColumn($table, $column) |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Builds a SQL command for dropping comment from table. |
||
| 1012 | * |
||
| 1013 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
| 1014 | * @return $this the command object itself |
||
| 1015 | * @since 2.0.8 |
||
| 1016 | */ |
||
| 1017 | public function dropCommentFromTable($table) |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Creates a SQL View. |
||
| 1026 | * |
||
| 1027 | * @param string $viewName the name of the view to be created. |
||
| 1028 | * @param string|Query $subquery the select statement which defines the view. |
||
| 1029 | * This can be either a string or a [[Query]] object. |
||
| 1030 | * @return $this the command object itself. |
||
| 1031 | * @since 2.0.14 |
||
| 1032 | */ |
||
| 1033 | public function createView($viewName, $subquery) |
||
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Drops a SQL View. |
||
| 1042 | * |
||
| 1043 | * @param string $viewName the name of the view to be dropped. |
||
| 1044 | * @return $this the command object itself. |
||
| 1045 | * @since 2.0.14 |
||
| 1046 | */ |
||
| 1047 | public function dropView($viewName) |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Executes the SQL statement. |
||
| 1056 | * This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs. |
||
| 1057 | * No result set will be returned. |
||
| 1058 | * @return int number of rows affected by the execution. |
||
| 1059 | * @throws Exception execution failed |
||
| 1060 | */ |
||
| 1061 | public function execute() |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Logs the current database query if query logging is enabled and returns |
||
| 1091 | * the profiling token if profiling is enabled. |
||
| 1092 | * @param string $category the log category. |
||
| 1093 | * @return array array of two elements, the first is boolean of whether profiling is enabled or not. |
||
| 1094 | * The second is the rawSql if it has been created. |
||
| 1095 | */ |
||
| 1096 | 3 | private function logQuery($category) |
|
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Performs the actual DB query of a SQL statement. |
||
| 1111 | * @param string $method method of PDOStatement to be called |
||
| 1112 | * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
||
| 1113 | * for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
||
| 1114 | * @return mixed the method execution result |
||
| 1115 | * @throws Exception if the query causes any problem |
||
| 1116 | * @since 2.0.1 this method is protected (was private before). |
||
| 1117 | */ |
||
| 1118 | 3 | protected function queryInternal($method, $fetchMode = null) |
|
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Marks a specified table schema to be refreshed after command execution. |
||
| 1176 | * @param string $name name of the table, which schema should be refreshed. |
||
| 1177 | * @return $this this command instance |
||
| 1178 | * @since 2.0.6 |
||
| 1179 | */ |
||
| 1180 | protected function requireTableSchemaRefresh($name) |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Refreshes table schema, which was marked by [[requireTableSchemaRefresh()]]. |
||
| 1188 | * @since 2.0.6 |
||
| 1189 | */ |
||
| 1190 | protected function refreshTableSchema() |
||
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Marks the command to be executed in transaction. |
||
| 1199 | * @param string|null $isolationLevel The isolation level to use for this transaction. |
||
| 1200 | * See [[Transaction::begin()]] for details. |
||
| 1201 | * @return $this this command instance. |
||
| 1202 | * @since 2.0.14 |
||
| 1203 | */ |
||
| 1204 | protected function requireTransaction($isolationLevel = null) |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Sets a callable (e.g. anonymous function) that is called when [[Exception]] is thrown |
||
| 1212 | * when executing the command. The signature of the callable should be: |
||
| 1213 | * |
||
| 1214 | * ```php |
||
| 1215 | * function (\yii\db\Exception $e, $attempt) |
||
| 1216 | * { |
||
| 1217 | * // return true or false (whether to retry the command or rethrow $e) |
||
| 1218 | * } |
||
| 1219 | * ``` |
||
| 1220 | * |
||
| 1221 | * The callable will recieve a database exception thrown and a current attempt |
||
| 1222 | * (to execute the command) number starting from 1. |
||
| 1223 | * |
||
| 1224 | * @param callable $handler a PHP callback to handle database exceptions. |
||
| 1225 | * @return $this this command instance. |
||
| 1226 | * @since 2.0.14 |
||
| 1227 | */ |
||
| 1228 | protected function setRetryHandler(callable $handler) |
||
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Executes a prepared statement. |
||
| 1236 | * |
||
| 1237 | * It's a wrapper around [[\PDOStatement::execute()]] to support transactions |
||
| 1238 | * and retry handlers. |
||
| 1239 | * |
||
| 1240 | * @param string|null $rawSql the rawSql if it has been created. |
||
| 1241 | * @throws Exception if execution failed. |
||
| 1242 | * @since 2.0.14 |
||
| 1243 | */ |
||
| 1244 | 3 | protected function internalExecute($rawSql) |
|
| 1270 | |||
| 1271 | /** |
||
| 1272 | * Resets command properties to their initial state. |
||
| 1273 | * |
||
| 1274 | * @since 2.0.13 |
||
| 1275 | */ |
||
| 1276 | 3 | protected function reset() |
|
| 1285 | } |
||
| 1286 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.