Yii2 framework backup
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.

308 lines
13 KiB

13 years ago
<?php
/**
* Query class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\dao;
/**
13 years ago
* Query represents a SQL statement in a way that is independent of DBMS.
*
* Query not only can represent a SELECT statement, it can also represent INSERT, UPDATE, DELETE,
* and other commonly used DDL statements, such as CREATE TABLE, CREATE INDEX, etc.
*
* Query provides a set of methods to facilitate the specification of different clauses.
* These methods can be chained together. For example,
*
* ~~~
* $query = new Query;
* $query->select('id, name')
* ->from('tbl_user')
* ->limit(10);
* // get the actual SQL statement
* echo $query->getSql();
* // or execute the query
* $users = $query->createCommand()->queryAll();
* ~~~
*
* By calling [[getSql()]], we can obtain the actual SQL statement from a Query object.
* And by calling [[createCommand()]], we can get a [[Command]] instance which can be further
* used to perform/execute the DB query against a database.
13 years ago
*
13 years ago
* @property string $sql the SQL statement represented by this query object.
*
13 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
13 years ago
class Query extends BaseQuery
13 years ago
{
/**
13 years ago
* @var array the operation that this query represents. This refers to the method call as well as
13 years ago
* the corresponding parameters for constructing a non-select SQL statement (e.g. INSERT, CREATE TABLE).
13 years ago
* This property is mainly maintained by methods such as [[insert()]], [[update()]], [[createTable()]].
13 years ago
* If this property is not set, it means this query represents a SELECT statement.
13 years ago
*/
public $operation;
13 years ago
13 years ago
/**
13 years ago
* Generates and returns the SQL statement according to this query.
* @param Connection $connection the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return string the generated SQL statement
13 years ago
*/
13 years ago
public function getSql($connection = null)
13 years ago
{
13 years ago
if ($connection === null) {
$connection = \Yii::$application->db;
13 years ago
}
13 years ago
return $connection->getQueryBuilder()->build($this);
13 years ago
}
/**
13 years ago
* Creates a DB command that can be used to execute this query.
* @param Connection $connection the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* @return Command the created DB command instance.
13 years ago
*/
13 years ago
public function createCommand($connection = null)
13 years ago
{
13 years ago
if ($connection === null) {
$connection = \Yii::$application->db;
13 years ago
}
13 years ago
return $connection->createCommand($this);
13 years ago
}
/**
13 years ago
* Creates and executes an INSERT SQL statement.
* The method will properly escape the column names, and bind the values to be inserted.
* @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 Query the query object itself
*/
public function insert($table, $columns)
{
13 years ago
$this->operation = array(__FUNCTION__, $table, $columns, array());
13 years ago
return $this;
}
/**
* Creates and executes an UPDATE SQL statement.
* The method will properly escape the column names and bind the values to be updated.
* @param string $table the table to be updated.
* @param array $columns the column data (name=>value) to be updated.
* @param string|array $condition the conditions that will be put in the WHERE part.
* Please refer to [[where()]] on how to specify this parameter.
13 years ago
* @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
13 years ago
* @return Query the query object itself
*/
public function update($table, $columns, $condition = '', $params = array())
{
13 years ago
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
array_shift($params);
13 years ago
unset($params[0]);
13 years ago
}
13 years ago
$this->addParams($params);
13 years ago
$this->operation = array(__FUNCTION__, $table, $columns, $condition, array());
13 years ago
return $this;
}
/**
* Creates and executes a DELETE SQL statement.
* @param string $table the table where the data will be deleted from.
* @param string|array $condition the conditions that will be put in the WHERE part.
* Please refer to [[where()]] on how to specify this parameter.
13 years ago
* @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
13 years ago
* @return Query the query object itself
*/
public function delete($table, $condition = '', $params = array())
{
13 years ago
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
13 years ago
unset($params[0]);
13 years ago
}
13 years ago
$this->operation = array(__FUNCTION__, $table, $condition);
13 years ago
return $this->addParams($params);
13 years ago
}
/**
* Builds and executes a SQL statement 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\dao\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 Query the query object itself
*/
public function createTable($table, $columns, $options = null)
{
13 years ago
$this->operation = array(__FUNCTION__, $table, $columns, $options);
13 years ago
return $this;
}
/**
* Builds and executes a SQL statement 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 Query the query object itself
*/
public function renameTable($table, $newName)
{
13 years ago
$this->operation = array(__FUNCTION__, $table, $newName);
13 years ago
return $this;
}
/**
* Builds and executes a SQL statement for dropping a DB table.
* @param string $table the table to be dropped. The name will be properly quoted by the method.
* @return Query the query object itself
*/
public function dropTable($table)
{
13 years ago
$this->operation = array(__FUNCTION__, $table);
13 years ago
return $this;
13 years ago
}
13 years ago
/**
* Builds and executes a SQL statement for truncating a DB table.
* @param string $table the table to be truncated. The name will be properly quoted by the method.
* @return Query the query object itself
*/
public function truncateTable($table)
{
13 years ago
$this->operation = array(__FUNCTION__, $table);
13 years ago
return $this;
}
/**
* Builds and executes a SQL statement 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\dao\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 Query the query object itself
*/
public function addColumn($table, $column, $type)
{
13 years ago
$this->operation = array(__FUNCTION__, $table, $column, $type);
13 years ago
return $this;
}
/**
* Builds and executes a SQL statement 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 Query the query object itself
*/
public function dropColumn($table, $column)
{
13 years ago
$this->operation = array(__FUNCTION__, $table, $column);
13 years ago
return $this;
}
/**
* Builds and executes a SQL statement for renaming a column.
* @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
13 years ago
* @param string $oldName the old name of the column. The name will be properly quoted by the method.
13 years ago
* @param string $newName the new name of the column. The name will be properly quoted by the method.
* @return Query the query object itself
*/
13 years ago
public function renameColumn($table, $oldName, $newName)
13 years ago
{
13 years ago
$this->operation = array(__FUNCTION__, $table, $oldName, $newName);
13 years ago
return $this;
}
/**
* Builds and executes a SQL statement 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\dao\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 Query the query object itself
*/
public function alterColumn($table, $column, $type)
{
13 years ago
$this->operation = array(__FUNCTION__, $table, $column, $type);
13 years ago
return $this;
}
/**
* Builds a SQL statement 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 Query the query object itself
*/
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
{
13 years ago
$this->operation = array(__FUNCTION__, $name, $table, $columns, $refTable, $refColumns, $delete, $update);
13 years ago
return $this;
}
/**
* Builds a SQL statement 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 Query the query object itself
*/
public function dropForeignKey($name, $table)
{
13 years ago
$this->operation = array(__FUNCTION__, $name, $table);
13 years ago
return $this;
}
/**
* Builds and executes a SQL statement 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 Query the query object itself
*/
public function createIndex($name, $table, $columns, $unique = false)
{
13 years ago
$this->operation = array(__FUNCTION__, $name, $table, $columns, $unique);
13 years ago
return $this;
}
/**
* Builds and executes a SQL statement 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 Query the query object itself
*/
public function dropIndex($name, $table)
{
13 years ago
$this->operation = array(__FUNCTION__, $name, $table);
13 years ago
return $this;
}
13 years ago
}