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.

310 lines
13 KiB

14 years ago
<?php
/**
* Query class file.
*
* @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.
14 years ago
*
13 years ago
* @property string $sql the SQL statement represented by this query object.
*
14 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
13 years ago
class Query extends BaseQuery
14 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
/**
* @var string the SQL statement that this query represents. This is directly set by user.
*/
public $sql;
13 years ago
/**
13 years ago
* Generates and returns the SQL statement according to this query.
13 years ago
* Note that after calling this method, [[params]] may be modified with additional
* parameters generated by the query builder.
13 years ago
* @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 ($this->sql !== null) {
return $this->sql;
}
13 years ago
if ($connection === null) {
$connection = \Yii::$application->db;
13 years ago
}
13 years ago
$qb = $connection->getQueryBuilder();
if ($this->operation !== null) {
$params = $this->operation;
$method = array_shift($params);
$qb->query = $this;
return call_user_func_array(array($qb, $method), $params);
} else {
return $qb->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
$sql = $this->getSql($connection);
return $connection->createCommand($sql, $this->params);
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);
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.
13 years ago
* @return Query the query object itself
*/
public function update($table, $columns, $condition = '', $params = array())
{
13 years ago
$this->operation = array(__FUNCTION__, $table, $columns, $condition, $params);
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.
13 years ago
* @return Query the query object itself
*/
public function delete($table, $condition = '', $params = array())
{
13 years ago
$this->operation = array(__FUNCTION__, $table, $condition, $params);
return $this;
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;
14 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;
}
14 years ago
}