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.

685 lines
26 KiB

14 years ago
<?php
/**
13 years ago
* Command class file.
14 years ago
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\db;
14 years ago
14 years ago
/**
14 years ago
* Command represents a SQL statement to be executed against a database.
14 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.
14 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,
*
* ~~~
13 years ago
* $users = \Yii::$application->db->createCommand('SELECT * FROM tbl_user')->queryAll();
13 years ago
* ~~~
14 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.
14 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.
14 years ago
*
13 years ago
* @property string $sql the SQL statement to be executed
*
14 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
14 years ago
* @since 2.0
14 years ago
*/
14 years ago
class Command extends \yii\base\Component
14 years ago
{
/**
13 years ago
* @var Connection the DB connection that this command is associated with
*/
14 years ago
public $connection;
13 years ago
/**
* @var \PDOStatement the PDOStatement object that this command contains
*/
14 years ago
public $pdoStatement;
/**
13 years ago
* @var mixed the default fetch mode for this command.
14 years ago
* @see http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
*/
public $fetchMode = \PDO::FETCH_ASSOC;
14 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.
14 years ago
* @return string the SQL statement to be executed
*/
13 years ago
public function getSql()
14 years ago
{
14 years ago
return $this->_sql;
14 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.
14 years ago
* @return Command this command instance
14 years ago
*/
public function setSql($sql)
14 years ago
{
if ($sql !== $this->_sql) {
if ($this->connection->enableAutoQuoting && $sql != '') {
$sql = preg_replace_callback('/(\\{\\{(.*?)\\}\\}|\\[\\[(.*?)\\]\\])/', function($matches) {
if (isset($matches[3])) {
return $this->connection->quoteColumnName($matches[3]);
} else {
$name = str_replace('%', $this->connection->tablePrefix, $matches[2]);
return $this->connection->quoteTableName($name);
}
}, $sql);
}
$this->_sql = $sql;
$this->_params = array();
$this->cancel();
}
14 years ago
return $this;
}
/**
* 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
14 years ago
*/
public function prepare()
{
14 years ago
if ($this->pdoStatement == null) {
$sql = $this->getSql();
14 years ago
try {
13 years ago
$this->pdoStatement = $this->connection->pdo->prepare($sql);
13 years ago
} catch (\Exception $e) {
13 years ago
\Yii::error($e->getMessage() . "\nFailed to prepare SQL: $sql", __CLASS__);
13 years ago
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
13 years ago
throw new Exception($e->getMessage(), (int)$e->getCode(), $errorInfo);
14 years ago
}
}
}
/**
* Cancels the execution of the SQL statement.
13 years ago
* This method mainly sets [[pdoStatement]] to be null.
14 years ago
*/
public function cancel()
{
14 years ago
$this->pdoStatement = null;
14 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
14 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
14 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
14 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) {
$this->pdoStatement->bindParam($name, $value, $this->getPdoType(gettype($value)));
13 years ago
} elseif ($length === null) {
14 years ago
$this->pdoStatement->bindParam($name, $value, $dataType);
13 years ago
} elseif ($driverOptions === null) {
14 years ago
$this->pdoStatement->bindParam($name, $value, $dataType, $length);
13 years ago
} else {
14 years ago
$this->pdoStatement->bindParam($name, $value, $dataType, $length, $driverOptions);
13 years ago
}
13 years ago
$this->_params[$name] =& $value;
14 years ago
return $this;
}
/**
* Binds a value to a parameter.
13 years ago
* @param string|integer $name Parameter identifier. For a prepared statement
14 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
14 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
14 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) {
$this->pdoStatement->bindValue($name, $value, $this->getPdoType(gettype($value)));
13 years ago
} else {
14 years ago
$this->pdoStatement->bindValue($name, $value, $dataType);
13 years ago
}
13 years ago
$this->_params[$name] = $value;
14 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.
14 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))`.
14 years ago
* @return Command the current command being executed
14 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 {
$type = $this->getPdoType(gettype($value));
}
$this->pdoStatement->bindValue($name, $value, $type);
13 years ago
$this->_params[$name] = $value;
}
}
14 years ago
return $this;
}
/**
* Determines the PDO type for the give PHP data type.
* @param string $type The PHP type (obtained by `gettype()` call).
* @return integer the corresponding PDO type
* @see http://www.php.net/manual/en/pdo.constants.php
*/
private function getPdoType($type)
{
static $typeMap = array(
'boolean' => \PDO::PARAM_BOOL,
'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR,
'NULL' => \PDO::PARAM_NULL,
);
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;
}
/**
14 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.
14 years ago
* No result set will be returned.
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative
13 years ago
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]]
* or [[bindValue()]] will be ignored.
14 years ago
* @return integer number of rows affected by the execution.
13 years ago
* @throws Exception execution failed
14 years ago
*/
public function execute($params = array())
{
$sql = $this->getSql();
13 years ago
$this->_params = array_merge($this->_params, $params);
if ($this->_params === array()) {
$paramLog = '';
13 years ago
} else {
13 years ago
$paramLog = "\nParameters: " . var_export($this->_params, true);
14 years ago
}
13 years ago
\Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__);
13 years ago
13 years ago
try {
if ($this->connection->enableProfiling) {
\Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
}
14 years ago
$this->prepare();
13 years ago
if ($params === array()) {
14 years ago
$this->pdoStatement->execute();
13 years ago
} else {
14 years ago
$this->pdoStatement->execute($params);
13 years ago
}
14 years ago
$n = $this->pdoStatement->rowCount();
14 years ago
13 years ago
if ($this->connection->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
14 years ago
return $n;
13 years ago
} catch (\Exception $e) {
13 years ago
if ($this->connection->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
$message = $e->getMessage();
\Yii::error("$message\nFailed to execute SQL: {$sql}{$paramLog}", __CLASS__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, (int)$e->getCode(), $errorInfo);
14 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`.
14 years ago
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative
13 years ago
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]]
* or [[bindValue()]] will be ignored.
13 years ago
* @return DataReader the reader object for fetching the query result
* @throws Exception execution failed
14 years ago
*/
public function query($params = array())
{
14 years ago
return $this->queryInternal('', $params);
14 years ago
}
/**
13 years ago
* Executes the SQL statement and returns ALL rows at once.
13 years ago
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative
13 years ago
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]]
* or [[bindValue()]] will be ignored.
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.
* @return array all rows of the query result. Each array element is an array representing a row of data.
14 years ago
* An empty array is returned if the query results in nothing.
13 years ago
* @throws Exception execution failed
14 years ago
*/
14 years ago
public function queryAll($params = array(), $fetchMode = null)
14 years ago
{
14 years ago
return $this->queryInternal('fetchAll', $params, $fetchMode);
14 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.
13 years ago
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative
13 years ago
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]]
* or [[bindValue()]] will be ignored.
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.
* @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
14 years ago
*/
14 years ago
public function queryRow($params = array(), $fetchMode = null)
14 years ago
{
14 years ago
return $this->queryInternal('fetch', $params, $fetchMode);
14 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.
14 years ago
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative
13 years ago
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]]
* or [[bindValue()]] will be ignored.
* @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
14 years ago
*/
public function queryScalar($params = array())
{
13 years ago
$result = $this->queryInternal('fetchColumn', $params, 0);
14 years ago
if (is_resource($result) && get_resource_type($result) === 'stream') {
14 years ago
return stream_get_contents($result);
13 years ago
} else {
14 years ago
return $result;
14 years ago
}
14 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.
14 years ago
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative
13 years ago
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]]
* or [[bindValue()]] will be ignored.
13 years ago
* @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
14 years ago
*/
public function queryColumn($params = array())
{
14 years ago
return $this->queryInternal('fetchAll', $params, \PDO::FETCH_COLUMN);
14 years ago
}
/**
13 years ago
* Performs the actual DB query of a SQL statement.
14 years ago
* @param string $method method of PDOStatement to be called
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative
13 years ago
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]]
* or [[bindValue()]] will be ignored.
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.
14 years ago
* @return mixed the method execution result
*/
14 years ago
private function queryInternal($method, $params, $fetchMode = null)
14 years ago
{
13 years ago
$db = $this->connection;
$sql = $this->getSql();
13 years ago
$this->_params = array_merge($this->_params, $params);
if ($this->_params === array()) {
$paramLog = '';
13 years ago
} else {
13 years ago
$paramLog = "\nParameters: " . var_export($this->_params, true);
14 years ago
}
13 years ago
\Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__);
12 years ago
/** @var $cache \yii\caching\Cache */
if ($db->enableQueryCache && $method !== '') {
13 years ago
$cache = \Yii::$application->getComponent($db->queryCacheID);
13 years ago
}
if (isset($cache)) {
$cacheKey = __CLASS__ . "/{$db->dsn}/{$db->username}/$sql/$paramLog";
13 years ago
if (($result = $cache->get($cacheKey)) !== false) {
13 years ago
\Yii::trace('Query result found in cache', __CLASS__);
14 years ago
return $result;
}
}
13 years ago
try {
if ($db->enableProfiling) {
\Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
}
14 years ago
$this->prepare();
13 years ago
if ($params === array()) {
14 years ago
$this->pdoStatement->execute();
13 years ago
} else {
14 years ago
$this->pdoStatement->execute($params);
13 years ago
}
14 years ago
13 years ago
if ($method === '') {
14 years ago
$result = new DataReader($this);
13 years ago
} else {
13 years ago
if ($fetchMode === null) {
$fetchMode = $this->fetchMode;
}
14 years ago
$result = call_user_func_array(array($this->pdoStatement, $method), (array)$fetchMode);
$this->pdoStatement->closeCursor();
14 years ago
}
13 years ago
if ($db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
14 years ago
12 years ago
if (isset($cache, $cacheKey)) {
$cache->set($cacheKey, $result, $db->queryCacheDuration, $db->queryCacheDependency);
13 years ago
\Yii::trace('Saved query result in cache', __CLASS__);
13 years ago
}
14 years ago
return $result;
13 years ago
} catch (\Exception $e) {
13 years ago
if ($db->enableProfiling) {
\Yii::endProfile(__METHOD__ . "($sql)", __CLASS__);
}
$message = $e->getMessage();
13 years ago
\Yii::error("$message\nCommand::$method() failed: {$sql}{$paramLog}", __CLASS__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, (int)$e->getCode(), $errorInfo);
14 years ago
}
}
12 years ago
/**
* Creates an INSERT command.
* For example,
*
* ~~~
* $db->createCommand()->insert('tbl_user', array(
* 'name' => 'Sam',
* 'age' => 30,
* ))->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.
* @param array $params the parameters to be bound to the command
* @return Command the command object itself
*/
public function insert($table, $columns, $params = array())
{
$sql = $this->connection->getQueryBuilder()->insert($table, $columns, $params);
return $this->setSql($sql)->bindValues($params);
}
/**
* Creates an UPDATE command.
* For example,
*
* ~~~
* $db->createCommand()->update('tbl_user', array(
* 'status' => 1,
* ), '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())
{
$sql = $this->connection->getQueryBuilder()->update($table, $columns, $condition, $params);
return $this->setSql($sql)->bindValues($params);
}
/**
* Creates a DELETE command.
* For example,
*
* ~~~
* $db->createCommand()->delete('tbl_user', 'status = 0')->execute();
* ~~~
*
* 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->connection->getQueryBuilder()->delete($table, $condition);
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.
* The method [[\yii\db\QueryBuilder::getColumnType()]] will be called
* 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)
{
$sql = $this->connection->getQueryBuilder()->createTable($table, $columns, $options);
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)
{
$sql = $this->connection->getQueryBuilder()->renameTable($table, $newName);
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)
{
$sql = $this->connection->getQueryBuilder()->dropTable($table);
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)
{
$sql = $this->connection->getQueryBuilder()->truncateTable($table);
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)
{
$sql = $this->connection->getQueryBuilder()->addColumn($table, $column, $type);
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)
{
$sql = $this->connection->getQueryBuilder()->dropColumn($table, $column);
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)
{
$sql = $this->connection->getQueryBuilder()->renameColumn($table, $oldName, $newName);
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)
{
$sql = $this->connection->getQueryBuilder()->alterColumn($table, $column, $type);
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)
{
$sql = $this->connection->getQueryBuilder()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update);
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)
{
$sql = $this->connection->getQueryBuilder()->dropForeignKey($name, $table);
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)
{
$sql = $this->connection->getQueryBuilder()->createIndex($name, $table, $columns, $unique);
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)
{
$sql = $this->connection->getQueryBuilder()->dropIndex($name, $table);
return $this->setSql($sql);
}
14 years ago
}