Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

751 lines
27 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\db;
13 years ago
use Yii;
12 years ago
use yii\base\NotSupportedException;
use yii\caching\Cache;
12 years ago
13 years ago
/**
13 years ago
* Command represents a SQL statement to be executed against a database.
13 years ago
*
13 years ago
* A command object is usually created by calling [[Connection::createCommand()]].
13 years ago
* The SQL statement it represents can be set via the [[sql]] property.
13 years ago
*
13 years ago
* To execute a non-query SQL (such as INSERT, DELETE, UPDATE), call [[execute()]].
13 years ago
* To execute a SQL statement that returns result data set (such as SELECT),
13 years ago
* use [[queryAll()]], [[queryRow()]], [[queryColumn()]], [[queryScalar()]], or [[query()]].
13 years ago
* For example,
*
* ~~~
12 years ago
* $users = $connection->createCommand('SELECT * FROM tbl_user')->queryAll();
13 years ago
* ~~~
13 years ago
*
13 years ago
* Command supports SQL statement preparation and parameter binding.
13 years ago
* Call [[bindValue()]] to bind a value to a SQL parameter;
* Call [[bindParam()]] to bind a PHP variable to a SQL parameter.
13 years ago
* When binding a parameter, the SQL statement is automatically prepared.
13 years ago
* You may also call [[prepare()]] explicitly to prepare a SQL statement.
13 years ago
*
12 years ago
* Command also supports building SQL statements by providing methods such as [[insert()]],
* [[update()]], etc. For example,
*
* ~~~
* $connection->createCommand()->insert('tbl_user', array(
* 'name' => 'Sam',
* 'age' => 30,
* ))->execute();
* ~~~
*
* To build SELECT SQL statements, please use [[QueryBuilder]] instead.
*
13 years ago
* @property string $sql the SQL statement to be executed
*
13 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class Command extends \yii\base\Component
13 years ago
{
/**
13 years ago
* @var Connection the DB connection that this command is associated with
*/
12 years ago
public $db;
13 years ago
/**
12 years ago
* @var \PDOStatement the PDOStatement object that this command is associated with
13 years ago
*/
13 years ago
public $pdoStatement;
/**
13 years ago
* @var mixed the default fetch mode for this command.
13 years ago
* @see http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
*/
public $fetchMode = \PDO::FETCH_ASSOC;
13 years ago
/**
13 years ago
* @var string the SQL statement that this command represents
*/
private $_sql;
/**
13 years ago
* @var array the parameter log information (name=>value)
13 years ago
*/
13 years ago
private $_params = array();
13 years ago
/**
13 years ago
* Returns the SQL statement for this command.
13 years ago
* @return string the SQL statement to be executed
*/
13 years ago
public function getSql()
13 years ago
{
13 years ago
return $this->_sql;
13 years ago
}
/**
* Specifies the SQL statement to be executed.
13 years ago
* Any previous execution will be terminated or cancelled.
* @param string $sql the SQL statement to be set.
13 years ago
* @return Command this command instance
13 years ago
*/
public function setSql($sql)
13 years ago
{
if ($sql !== $this->_sql) {
12 years ago
if ($this->db->enableAutoQuoting && $sql != '') {
12 years ago
$sql = $this->expandSql($sql);
}
12 years ago
$this->cancel();
$this->_sql = $sql;
$this->_params = array();
}
13 years ago
return $this;
}
/**
12 years ago
* Expands a SQL statement by quoting table and column names and replacing table prefixes.
* @param string $sql the SQL to be expanded
* @return string the expanded SQL
*/
protected function expandSql($sql)
{
12 years ago
$db = $this->db;
return preg_replace_callback('/(\\{\\{(.*?)\\}\\}|\\[\\[(.*?)\\]\\])/', function($matches) use($db) {
12 years ago
if (isset($matches[3])) {
12 years ago
return $db->quoteColumnName($matches[3]);
12 years ago
} else {
12 years ago
$name = str_replace('%', $db->tablePrefix, $matches[2]);
return $db->quoteTableName($name);
12 years ago
}
}, $sql);
}
/**
13 years ago
* Prepares the SQL statement to be executed.
* For complex SQL statement that is to be executed multiple times,
* this may improve performance.
* For SQL statement with binding parameters, this method is invoked
* automatically.
13 years ago
* @throws Exception if there is any DB error
13 years ago
*/
public function prepare()
{
13 years ago
if ($this->pdoStatement == null) {
$sql = $this->getSql();
13 years ago
try {
12 years ago
$this->pdoStatement = $this->db->pdo->prepare($sql);
13 years ago
} catch (\Exception $e) {
Yii::error($e->getMessage() . "\nFailed to prepare SQL: $sql", __METHOD__);
13 years ago
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($e->getMessage(), $errorInfo, (int)$e->getCode());
13 years ago
}
}
}
/**
* Cancels the execution of the SQL statement.
13 years ago
* This method mainly sets [[pdoStatement]] to be null.
13 years ago
*/
public function cancel()
{
13 years ago
$this->pdoStatement = null;
13 years ago
}
/**
* Binds a parameter to the SQL statement to be executed.
13 years ago
* @param string|integer $name parameter identifier. For a prepared statement
13 years ago
* using named placeholders, this will be a parameter name of
13 years ago
* the form `:name`. For a prepared statement using question mark
13 years ago
* placeholders, this will be the 1-indexed position of the parameter.
* @param mixed $value Name of the PHP variable to bind to the SQL statement parameter
* @param integer $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value.
* @param integer $length length of the data type
13 years ago
* @param mixed $driverOptions the driver-specific options
* @return Command the current command being executed
13 years ago
* @see http://www.php.net/manual/en/function.PDOStatement-bindParam.php
*/
public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null)
{
$this->prepare();
13 years ago
if ($dataType === null) {
12 years ago
$this->pdoStatement->bindParam($name, $value, $this->getPdoType($value));
13 years ago
} elseif ($length === null) {
13 years ago
$this->pdoStatement->bindParam($name, $value, $dataType);
13 years ago
} elseif ($driverOptions === null) {
13 years ago
$this->pdoStatement->bindParam($name, $value, $dataType, $length);
13 years ago
} else {
13 years ago
$this->pdoStatement->bindParam($name, $value, $dataType, $length, $driverOptions);
13 years ago
}
13 years ago
$this->_params[$name] =& $value;
13 years ago
return $this;
}
/**
* Binds a value to a parameter.
13 years ago
* @param string|integer $name Parameter identifier. For a prepared statement
13 years ago
* using named placeholders, this will be a parameter name of
13 years ago
* the form `:name`. For a prepared statement using question mark
13 years ago
* placeholders, this will be the 1-indexed position of the parameter.
* @param mixed $value The value to bind to the parameter
* @param integer $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value.
13 years ago
* @return Command the current command being executed
13 years ago
* @see http://www.php.net/manual/en/function.PDOStatement-bindValue.php
*/
public function bindValue($name, $value, $dataType = null)
{
$this->prepare();
13 years ago
if ($dataType === null) {
12 years ago
$this->pdoStatement->bindValue($name, $value, $this->getPdoType($value));
13 years ago
} else {
13 years ago
$this->pdoStatement->bindValue($name, $value, $dataType);
13 years ago
}
13 years ago
$this->_params[$name] = $value;
13 years ago
return $this;
}
/**
* Binds a list of values to the corresponding parameters.
13 years ago
* This is similar to [[bindValue()]] except that it binds multiple values at a time.
13 years ago
* Note that the SQL data type of each value is determined by its PHP type.
* @param array $values the values to be bound. This must be given in terms of an associative
13 years ago
* array with array keys being the parameter names, and array values the corresponding parameter values,
* e.g. `array(':name'=>'John', ':age'=>25)`. By default, the PDO type of each value is determined
* by its PHP type. You may explicitly specify the PDO type by using an array: `array(value, type)`,
* e.g. `array(':name'=>'John', ':profile'=>array($profile, \PDO::PARAM_LOB))`.
13 years ago
* @return Command the current command being executed
13 years ago
*/
public function bindValues($values)
{
13 years ago
if (!empty($values)) {
13 years ago
$this->prepare();
foreach ($values as $name => $value) {
if (is_array($value)) {
$type = $value[1];
$value = $value[0];
} else {
12 years ago
$type = $this->getPdoType($value);
}
$this->pdoStatement->bindValue($name, $value, $type);
13 years ago
$this->_params[$name] = $value;
}
}
13 years ago
return $this;
}
/**
12 years ago
* Determines the PDO type for the give PHP data value.
* @param mixed $data the data whose PDO type is to be determined
* @return integer the PDO type
* @see http://www.php.net/manual/en/pdo.constants.php
*/
12 years ago
private function getPdoType($data)
{
static $typeMap = array(
'boolean' => \PDO::PARAM_BOOL,
'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR,
'NULL' => \PDO::PARAM_NULL,
);
12 years ago
$type = gettype($data);
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;
}
/**
13 years ago
* Executes the SQL statement.
13 years ago
* This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs.
13 years ago
* No result set will be returned.
* @return integer number of rows affected by the execution.
13 years ago
* @throws Exception execution failed
13 years ago
*/
public function execute()
13 years ago
{
$sql = $this->getSql();
12 years ago
13 years ago
if ($this->_params === array()) {
$paramLog = '';
13 years ago
} else {
13 years ago
$paramLog = "\nParameters: " . var_export($this->_params, true);
13 years ago
}
13 years ago
Yii::trace("Executing SQL: {$sql}{$paramLog}", __METHOD__);
13 years ago
12 years ago
if ($sql == '') {
return 0;
}
13 years ago
try {
$token = "SQL: $sql";
12 years ago
if ($this->db->enableProfiling) {
Yii::beginProfile($token, __METHOD__);
13 years ago
}
13 years ago
$this->prepare();
$this->pdoStatement->execute();
13 years ago
$n = $this->pdoStatement->rowCount();
13 years ago
12 years ago
if ($this->db->enableProfiling) {
Yii::endProfile($token, __METHOD__);
13 years ago
}
13 years ago
return $n;
13 years ago
} catch (\Exception $e) {
12 years ago
if ($this->db->enableProfiling) {
Yii::endProfile($token, __METHOD__);
13 years ago
}
$message = $e->getMessage();
12 years ago
Yii::error("$message\nFailed to execute SQL: {$sql}{$paramLog}", __METHOD__);
12 years ago
13 years ago
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode());
13 years ago
}
}
/**
* Executes the SQL statement and returns query result.
13 years ago
* This method is for executing a SQL query that returns result set, such as `SELECT`.
* @return DataReader the reader object for fetching the query result
* @throws Exception execution failed
13 years ago
*/
public function query()
13 years ago
{
return $this->queryInternal('');
13 years ago
}
/**
13 years ago
* Executes the SQL statement and returns ALL rows at once.
* @param mixed $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php)
* for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used.
* @return array all rows of the query result. Each array element is an array representing a row of data.
13 years ago
* An empty array is returned if the query results in nothing.
13 years ago
* @throws Exception execution failed
13 years ago
*/
public function queryAll($fetchMode = null)
13 years ago
{
return $this->queryInternal('fetchAll', $fetchMode);
13 years ago
}
/**
* Executes the SQL statement and returns the first row of the result.
13 years ago
* This method is best used when only the first row of result is needed for a query.
* @param mixed $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php)
* for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used.
* @return array|boolean the first row (in terms of an array) of the query result. False is returned if the query
* results in nothing.
13 years ago
* @throws Exception execution failed
13 years ago
*/
public function queryRow($fetchMode = null)
13 years ago
{
return $this->queryInternal('fetch', $fetchMode);
13 years ago
}
/**
* Executes the SQL statement and returns the value of the first column in the first row of data.
13 years ago
* This method is best used when only a single value is needed for a query.
13 years ago
* @return string|boolean the value of the first column in the first row of the query result.
13 years ago
* False is returned if there is no value.
13 years ago
* @throws Exception execution failed
13 years ago
*/
public function queryScalar()
13 years ago
{
$result = $this->queryInternal('fetchColumn', 0);
13 years ago
if (is_resource($result) && get_resource_type($result) === 'stream') {
13 years ago
return stream_get_contents($result);
13 years ago
} else {
13 years ago
return $result;
13 years ago
}
13 years ago
}
/**
* Executes the SQL statement and returns the first column of the result.
13 years ago
* This method is best used when only the first column of result (i.e. the first element in each row)
* is needed for a query.
* @return array the first column of the query result. Empty array is returned if the query results in nothing.
13 years ago
* @throws Exception execution failed
13 years ago
*/
public function queryColumn()
13 years ago
{
return $this->queryInternal('fetchAll', \PDO::FETCH_COLUMN);
13 years ago
}
/**
13 years ago
* Performs the actual DB query of a SQL statement.
13 years ago
* @param string $method method of PDOStatement to be called
13 years ago
* @param mixed $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php)
* for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used.
13 years ago
* @return mixed the method execution result
12 years ago
* @throws Exception if the query causes any problem
13 years ago
*/
private function queryInternal($method, $fetchMode = null)
13 years ago
{
12 years ago
$db = $this->db;
$sql = $this->getSql();
13 years ago
if ($this->_params === array()) {
$paramLog = '';
13 years ago
} else {
13 years ago
$paramLog = "\nParameters: " . var_export($this->_params, true);
13 years ago
}
13 years ago
Yii::trace("Querying SQL: {$sql}{$paramLog}", __METHOD__);
12 years ago
/** @var $cache \yii\caching\Cache */
if ($db->enableQueryCache && $method !== '') {
$cache = is_string($db->queryCache) ? Yii::$app->getComponent($db->queryCache) : $db->queryCache;
13 years ago
}
if (isset($cache) && $cache instanceof Cache) {
12 years ago
$cacheKey = $cache->buildKey(array(
__CLASS__,
$db->dsn,
$db->username,
$sql,
$paramLog,
));
13 years ago
if (($result = $cache->get($cacheKey)) !== false) {
Yii::trace('Query result served from cache', __METHOD__);
13 years ago
return $result;
}
}
13 years ago
try {
$token = "SQL: $sql";
13 years ago
if ($db->enableProfiling) {
Yii::beginProfile($token, __METHOD__);
13 years ago
}
13 years ago
$this->prepare();
$this->pdoStatement->execute();
13 years ago
13 years ago
if ($method === '') {
13 years ago
$result = new DataReader($this);
13 years ago
} else {
13 years ago
if ($fetchMode === null) {
$fetchMode = $this->fetchMode;
}
13 years ago
$result = call_user_func_array(array($this->pdoStatement, $method), (array)$fetchMode);
$this->pdoStatement->closeCursor();
13 years ago
}
13 years ago
if ($db->enableProfiling) {
Yii::endProfile($token, __METHOD__);
13 years ago
}
13 years ago
if (isset($cache, $cacheKey) && $cache instanceof Cache) {
$cache->set($cacheKey, $result, $db->queryCacheDuration, $db->queryCacheDependency);
Yii::trace('Saved query result in cache', __METHOD__);
13 years ago
}
13 years ago
return $result;
13 years ago
} catch (\Exception $e) {
13 years ago
if ($db->enableProfiling) {
Yii::endProfile($token, __METHOD__);
13 years ago
}
$message = $e->getMessage();
Yii::error("$message\nCommand::$method() failed: {$sql}{$paramLog}", __METHOD__);
13 years ago
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode());
13 years ago
}
}
12 years ago
/**
* Creates an INSERT command.
* For example,
*
* ~~~
12 years ago
* $connection->createCommand()->insert('tbl_user', array(
* 'name' => 'Sam',
* 'age' => 30,
12 years ago
* ))->execute();
* ~~~
*
* The method will properly escape the column names, and bind the values to be inserted.
*
* Note that the created command is not executed until [[execute()]] is called.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column data (name=>value) to be inserted into the table.
* @return Command the command object itself
*/
public function insert($table, $columns)
12 years ago
{
$params = array();
12 years ago
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
12 years ago
return $this->setSql($sql)->bindValues($params);
}
/**
* Creates a batch INSERT command.
* For example,
*
* ~~~
* $connection->createCommand()->batchInsert('tbl_user', array('name', 'age'), array(
* array('Tom', 30),
* array('Jane', 20),
* array('Linda', 25),
* ))->execute();
* ~~~
*
* Not that the values in each row must match the corresponding column names.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names
* @param array $rows the rows to be batch inserted into the table
* @return Command the command object itself
*/
public function batchInsert($table, $columns, $rows)
{
$sql = $this->db->getQueryBuilder()->batchInsert($table, $columns, $rows);
return $this->setSql($sql);
}
/**
12 years ago
* Creates an UPDATE command.
* For example,
*
* ~~~
12 years ago
* $connection->createCommand()->update('tbl_user', array(
* 'status' => 1,
12 years ago
* ), 'age > 30')->execute();
* ~~~
*
* The method will properly escape the column names and bind the values to be updated.
*
* Note that the created command is not executed until [[execute()]] is called.
*
* @param string $table the table to be updated.
* @param array $columns the column data (name=>value) to be updated.
* @param mixed $condition the condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition.
* @param array $params the parameters to be bound to the command
* @return Command the command object itself
*/
public function update($table, $columns, $condition = '', $params = array())
{
12 years ago
$sql = $this->db->getQueryBuilder()->update($table, $columns, $condition, $params);
12 years ago
return $this->setSql($sql)->bindValues($params);
}
/**
* Creates a DELETE command.
* For example,
*
* ~~~
12 years ago
* $connection->createCommand()->delete('tbl_user', 'status = 0')->execute();
12 years ago
* ~~~
*
* The method will properly escape the table and column names.
*
* Note that the created command is not executed until [[execute()]] is called.
*
* @param string $table the table where the data will be deleted from.
* @param mixed $condition the condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition.
* @param array $params the parameters to be bound to the command
* @return Command the command object itself
*/
public function delete($table, $condition = '', $params = array())
{
$sql = $this->db->getQueryBuilder()->delete($table, $condition, $params);
12 years ago
return $this->setSql($sql)->bindValues($params);
}
/**
* Creates a SQL command for creating a new DB table.
*
* The columns in the new table should be specified as name-definition pairs (e.g. 'name'=>'string'),
* where name stands for a column name which will be properly quoted by the method, and definition
* stands for the column type which can contain an abstract DB type.
12 years ago
* The method [[QueryBuilder::getColumnType()]] will be called
12 years ago
* to convert the abstract column types to physical ones. For example, `string` will be converted
* as `varchar(255)`, and `string not null` becomes `varchar(255) not null`.
*
* If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
* inserted into the generated SQL.
*
* @param string $table the name of the table to be created. The name will be properly quoted by the method.
* @param array $columns the columns (name=>definition) in the new table.
* @param string $options additional SQL fragment that will be appended to the generated SQL.
* @return Command the command object itself
*/
public function createTable($table, $columns, $options = null)
{
12 years ago
$sql = $this->db->getQueryBuilder()->createTable($table, $columns, $options);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for renaming a DB table.
* @param string $table the table to be renamed. The name will be properly quoted by the method.
* @param string $newName the new table name. The name will be properly quoted by the method.
* @return Command the command object itself
*/
public function renameTable($table, $newName)
{
12 years ago
$sql = $this->db->getQueryBuilder()->renameTable($table, $newName);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for dropping a DB table.
* @param string $table the table to be dropped. The name will be properly quoted by the method.
* @return Command the command object itself
*/
public function dropTable($table)
{
12 years ago
$sql = $this->db->getQueryBuilder()->dropTable($table);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for truncating a DB table.
* @param string $table the table to be truncated. The name will be properly quoted by the method.
* @return Command the command object itself
*/
public function truncateTable($table)
{
12 years ago
$sql = $this->db->getQueryBuilder()->truncateTable($table);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for adding a new DB column.
* @param string $table the table that the new column will be added to. The table name will be properly quoted by the method.
* @param string $column the name of the new column. The name will be properly quoted by the method.
* @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called
* to convert the give column type to the physical one. For example, `string` will be converted
* as `varchar(255)`, and `string not null` becomes `varchar(255) not null`.
* @return Command the command object itself
*/
public function addColumn($table, $column, $type)
{
12 years ago
$sql = $this->db->getQueryBuilder()->addColumn($table, $column, $type);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for dropping a DB column.
* @param string $table the table whose column is to be dropped. The name will be properly quoted by the method.
* @param string $column the name of the column to be dropped. The name will be properly quoted by the method.
* @return Command the command object itself
*/
public function dropColumn($table, $column)
{
12 years ago
$sql = $this->db->getQueryBuilder()->dropColumn($table, $column);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for renaming a column.
* @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
* @param string $oldName the old name of the column. The name will be properly quoted by the method.
* @param string $newName the new name of the column. The name will be properly quoted by the method.
* @return Command the command object itself
*/
public function renameColumn($table, $oldName, $newName)
{
12 years ago
$sql = $this->db->getQueryBuilder()->renameColumn($table, $oldName, $newName);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for changing the definition of a column.
* @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
* @param string $column the name of the column to be changed. The name will be properly quoted by the method.
* @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called
* to convert the give column type to the physical one. For example, `string` will be converted
* as `varchar(255)`, and `string not null` becomes `varchar(255) not null`.
* @return Command the command object itself
*/
public function alterColumn($table, $column, $type)
{
12 years ago
$sql = $this->db->getQueryBuilder()->alterColumn($table, $column, $type);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for adding a foreign key constraint to an existing table.
* The method will properly quote the table and column names.
* @param string $name the name of the foreign key constraint.
* @param string $table the table that the foreign key constraint will be added to.
* @param string $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas.
* @param string $refTable the table that the foreign key references to.
* @param string $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas.
* @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
* @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
* @return Command the command object itself
*/
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
{
12 years ago
$sql = $this->db->getQueryBuilder()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for dropping a foreign key constraint.
* @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
* @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method.
* @return Command the command object itself
*/
public function dropForeignKey($name, $table)
{
12 years ago
$sql = $this->db->getQueryBuilder()->dropForeignKey($name, $table);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for creating a new index.
* @param string $name the name of the index. The name will be properly quoted by the method.
* @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
* @param string $columns the column(s) that should be included in the index. If there are multiple columns, please separate them
* by commas. The column names will be properly quoted by the method.
* @param boolean $unique whether to add UNIQUE constraint on the created index.
* @return Command the command object itself
*/
public function createIndex($name, $table, $columns, $unique = false)
{
12 years ago
$sql = $this->db->getQueryBuilder()->createIndex($name, $table, $columns, $unique);
12 years ago
return $this->setSql($sql);
}
/**
* Creates a SQL command for dropping an index.
* @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
* @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
* @return Command the command object itself
*/
public function dropIndex($name, $table)
{
12 years ago
$sql = $this->db->getQueryBuilder()->dropIndex($name, $table);
12 years ago
return $this->setSql($sql);
}
12 years ago
/**
* Creates a SQL command for resetting the sequence value of a table's primary key.
* The sequence will be reset such that the primary key of the next new row inserted
* will have the specified value or 1.
* @param string $table the name of the table whose primary key sequence will be reset
* @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
* the next new row's primary key will have a value 1.
* @return Command the command object itself
* @throws NotSupportedException if this is not supported by the underlying DBMS
*/
public function resetSequence($table, $value = null)
{
12 years ago
$sql = $this->db->getQueryBuilder()->resetSequence($table, $value);
12 years ago
return $this->setSql($sql);
}
/**
* Builds a SQL command for enabling or disabling integrity check.
* @param boolean $check whether to turn on or off the integrity check.
* @param string $schema the schema name of the tables. Defaults to empty string, meaning the current
* or default schema.
* @return Command the command object itself
* @throws NotSupportedException if this is not supported by the underlying DBMS
*/
public function checkIntegrity($check = true, $schema = '')
{
12 years ago
$sql = $this->db->getQueryBuilder()->checkIntegrity($check, $schema);
12 years ago
return $this->setSql($sql);
}
13 years ago
}