Query.php 12.5 KB
Newer Older
w  
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12
<?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;

/**
Qiang Xue committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * 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.
w  
Qiang Xue committed
35
 *
Qiang Xue committed
36 37
 * @property string $sql the SQL statement represented by this query object.
 *
w  
Qiang Xue committed
38 39 40
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
41
class Query extends BaseQuery
w  
Qiang Xue committed
42
{
Qiang Xue committed
43 44
	/**
	 * @var array the operation that this query represents. This refers to the method call as well as
Qiang Xue committed
45
	 * the corresponding parameters for constructing a non-select SQL statement (e.g. INSERT, CREATE TABLE).
Qiang Xue committed
46
	 * This property is mainly maintained by methods such as [[insert()]], [[update()]], [[createTable()]].
Qiang Xue committed
47
	 * If this property is not set, it means this query represents a SELECT statement.
Qiang Xue committed
48 49 50 51
	 */
	public $operation;

	/**
Qiang Xue committed
52
	 * Generates and returns the SQL statement according to this query.
Qiang Xue committed
53 54
	 * Note that after calling this method, [[params]] may be modified with additional
	 * parameters generated by the query builder.
Qiang Xue committed
55 56 57
	 * @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
Qiang Xue committed
58
	 */
Qiang Xue committed
59
	public function getSql($connection = null)
Qiang Xue committed
60
	{
Qiang Xue committed
61 62
		if ($connection === null) {
			$connection = \Yii::$application->db;
Qiang Xue committed
63
		}
Qiang Xue committed
64 65 66 67 68 69 70 71 72
		$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);
		}
Qiang Xue committed
73 74 75
	}

	/**
Qiang Xue committed
76 77 78 79
	 * 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.
Qiang Xue committed
80
	 */
Qiang Xue committed
81
	public function createCommand($connection = null)
Qiang Xue committed
82
	{
Qiang Xue committed
83 84
		if ($connection === null) {
			$connection = \Yii::$application->db;
Qiang Xue committed
85
		}
Qiang Xue committed
86 87
		$sql = $this->getSql($connection);
		return $connection->createCommand($sql, $this->params);
Qiang Xue committed
88 89
	}

Qiang Xue committed
90 91 92 93 94 95 96 97 98
	/**
	 * 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)
	{
Qiang Xue committed
99
		$this->operation = array(__FUNCTION__, $table, $columns);
Qiang Xue committed
100 101 102 103 104 105 106 107 108 109
		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.
Qiang Xue committed
110
	 * @param array $params the parameters (name=>value) to be bound to the query.
Qiang Xue committed
111 112 113 114
	 * @return Query the query object itself
	 */
	public function update($table, $columns, $condition = '', $params = array())
	{
Qiang Xue committed
115
		$this->operation = array(__FUNCTION__, $table, $columns, $condition, $params);
Qiang Xue committed
116 117 118 119 120 121 122 123
		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.
Qiang Xue committed
124
	 * @param array $params the parameters (name=>value) to be bound to the query.
Qiang Xue committed
125 126 127 128
	 * @return Query the query object itself
	 */
	public function delete($table, $condition = '', $params = array())
	{
Qiang Xue committed
129 130
		$this->operation = array(__FUNCTION__, $table, $condition, $params);
		return $this;
Qiang Xue committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	}

	/**
	 * 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)
	{
Qiang Xue committed
153
		$this->operation = array(__FUNCTION__, $table, $columns, $options);
Qiang Xue committed
154 155 156 157 158 159 160 161 162 163 164
		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)
	{
Qiang Xue committed
165
		$this->operation = array(__FUNCTION__, $table, $newName);
Qiang Xue committed
166 167 168 169 170 171 172 173 174 175
		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)
	{
Qiang Xue committed
176
		$this->operation = array(__FUNCTION__, $table);
Qiang Xue committed
177
		return $this;
w  
Qiang Xue committed
178 179
	}

Qiang Xue committed
180 181 182 183 184 185 186
	/**
	 * 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)
	{
Qiang Xue committed
187
		$this->operation = array(__FUNCTION__, $table);
Qiang Xue committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201
		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)
	{
Qiang Xue committed
202
		$this->operation = array(__FUNCTION__, $table, $column, $type);
Qiang Xue committed
203 204 205 206 207 208 209 210 211 212 213
		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)
	{
Qiang Xue committed
214
		$this->operation = array(__FUNCTION__, $table, $column);
Qiang Xue committed
215 216 217 218 219 220
		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.
Qiang Xue committed
221
	 * @param string $oldName the old name of the column. The name will be properly quoted by the method.
Qiang Xue committed
222 223 224
	 * @param string $newName the new name of the column. The name will be properly quoted by the method.
	 * @return Query the query object itself
	 */
Qiang Xue committed
225
	public function renameColumn($table, $oldName, $newName)
Qiang Xue committed
226
	{
Qiang Xue committed
227
		$this->operation = array(__FUNCTION__, $table, $oldName, $newName);
Qiang Xue committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241
		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)
	{
Qiang Xue committed
242
		$this->operation = array(__FUNCTION__, $table, $column, $type);
Qiang Xue committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
		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)
	{
Qiang Xue committed
260
		$this->operation = array(__FUNCTION__, $name, $table, $columns, $refTable, $refColumns, $delete, $update);
Qiang Xue committed
261 262 263 264 265 266 267 268 269 270 271
		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)
	{
Qiang Xue committed
272
		$this->operation = array(__FUNCTION__, $name, $table);
Qiang Xue committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286
		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)
	{
Qiang Xue committed
287
		$this->operation = array(__FUNCTION__, $name, $table, $columns, $unique);
Qiang Xue committed
288 289 290 291 292 293 294 295 296 297 298
		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)
	{
Qiang Xue committed
299
		$this->operation = array(__FUNCTION__, $name, $table);
Qiang Xue committed
300 301
		return $this;
	}
w  
Qiang Xue committed
302
}