|
|
|
@ -11,12 +11,12 @@
|
|
|
|
|
namespace yii\db\dao; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Query represents a SQL statement in a way that is independent of DBMS. |
|
|
|
|
* BaseQuery 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, |
|
|
|
|
* BaseQuery 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. |
|
|
|
|
* BaseQuery provides a set of methods to facilitate the specification of different clauses. |
|
|
|
|
* These methods can be chained together. For example, |
|
|
|
|
* |
|
|
|
|
* ~~~ |
|
|
|
@ -120,27 +120,36 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* Columns can contain table prefixes (e.g. "tbl_user.id") and/or column aliases (e.g. "tbl_user.id AS user_id"). |
|
|
|
|
* The method will automatically quote the column names unless a column contains some parenthesis |
|
|
|
|
* (which means the column contains a DB expression). |
|
|
|
|
* @param boolean $distinct whether to use 'SELECT DISTINCT'. |
|
|
|
|
* @param string $option additional option that should be appended to the 'SELECT' keyword. For example, |
|
|
|
|
* in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function select($columns = '*', $distinct = false, $option = '') |
|
|
|
|
public function select($columns = '*', $option = '') |
|
|
|
|
{ |
|
|
|
|
$this->select = $columns; |
|
|
|
|
$this->distinct = $distinct; |
|
|
|
|
$this->selectOption = $option; |
|
|
|
|
return $this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the value indicating whether to SELECT DISTINCT or not. |
|
|
|
|
* @param bool $value whether to SELECT DISTINCT or not. |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function distinct($value = true) |
|
|
|
|
{ |
|
|
|
|
$this->distinct = $value; |
|
|
|
|
return $this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the FROM part of the query. |
|
|
|
|
* @param mixed $tables the table(s) to be selected from. This can be either a string (e.g. 'tbl_user') |
|
|
|
|
* or an array (e.g. array('tbl_user', 'tbl_profile')) specifying one or several table names. |
|
|
|
|
* Table names can contain schema prefixes (e.g. 'public.tbl_user') and/or table aliases (e.g. 'tbl_user u'). |
|
|
|
|
* The method will automatically quote the table names unless it contains some parenthesis |
|
|
|
|
* (which means the table is given as a sub-query or DB expression). |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function from($tables) |
|
|
|
|
{ |
|
|
|
@ -215,7 +224,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* @param array $params the parameters (name=>value) to be bound to the query. |
|
|
|
|
* For anonymous parameters, they can alternatively be specified as separate parameters to this method. |
|
|
|
|
* For example, `where('type=? AND status=?', 100, 1)`. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see andWhere() |
|
|
|
|
* @see orWhere() |
|
|
|
|
*/ |
|
|
|
@ -237,7 +246,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* on how to specify this parameter. |
|
|
|
|
* @param array $params the parameters (name=>value) to be bound to the query. |
|
|
|
|
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see where() |
|
|
|
|
* @see orWhere() |
|
|
|
|
*/ |
|
|
|
@ -263,7 +272,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* on how to specify this parameter. |
|
|
|
|
* @param array $params the parameters (name=>value) to be bound to the query. |
|
|
|
|
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see where() |
|
|
|
|
* @see andWhere() |
|
|
|
|
*/ |
|
|
|
@ -292,7 +301,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* Please refer to [[where()]] on how to specify this parameter. |
|
|
|
|
* @param array $params the parameters (name=>value) to be bound to the query. |
|
|
|
|
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function join($table, $condition, $params = array()) |
|
|
|
|
{ |
|
|
|
@ -314,7 +323,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* @param string|array $condition the join condition that should appear in the ON part. |
|
|
|
|
* Please refer to [[where()]] on how to specify this parameter. |
|
|
|
|
* @param array $params the parameters (name=>value) to be bound to the query |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function leftJoin($table, $condition, $params = array()) |
|
|
|
|
{ |
|
|
|
@ -336,7 +345,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* @param string|array $condition the join condition that should appear in the ON part. |
|
|
|
|
* Please refer to [[where()]] on how to specify this parameter. |
|
|
|
|
* @param array $params the parameters (name=>value) to be bound to the query |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function rightJoin($table, $condition, $params = array()) |
|
|
|
|
{ |
|
|
|
@ -356,7 +365,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u'). |
|
|
|
|
* The method will automatically quote the table name unless it contains some parenthesis |
|
|
|
|
* (which means the table is given as a sub-query or DB expression). |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function crossJoin($table) |
|
|
|
|
{ |
|
|
|
@ -371,7 +380,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u'). |
|
|
|
|
* The method will automatically quote the table name unless it contains some parenthesis |
|
|
|
|
* (which means the table is given as a sub-query or DB expression). |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function naturalJoin($table) |
|
|
|
|
{ |
|
|
|
@ -385,7 +394,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. array('id', 'name')). |
|
|
|
|
* The method will automatically quote the column names unless a column contains some parenthesis |
|
|
|
|
* (which means the column contains a DB expression). |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see addGroupBy() |
|
|
|
|
*/ |
|
|
|
|
public function groupBy($columns) |
|
|
|
@ -400,7 +409,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. array('id', 'name')). |
|
|
|
|
* The method will automatically quote the column names unless a column contains some parenthesis |
|
|
|
|
* (which means the column contains a DB expression). |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see groupBy() |
|
|
|
|
*/ |
|
|
|
|
public function addGroupBy($columns) |
|
|
|
@ -425,7 +434,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* Please refer to [[where()]] on how to specify this parameter. |
|
|
|
|
* @param array $params the parameters (name=>value) to be bound to the query. |
|
|
|
|
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see andHaving() |
|
|
|
|
* @see orHaving() |
|
|
|
|
*/ |
|
|
|
@ -447,7 +456,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* on how to specify this parameter. |
|
|
|
|
* @param array $params the parameters (name=>value) to be bound to the query. |
|
|
|
|
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see having() |
|
|
|
|
* @see orHaving() |
|
|
|
|
*/ |
|
|
|
@ -473,7 +482,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* on how to specify this parameter. |
|
|
|
|
* @param array $params the parameters (name=>value) to be bound to the query. |
|
|
|
|
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see having() |
|
|
|
|
* @see andHaving() |
|
|
|
|
*/ |
|
|
|
@ -498,7 +507,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array (e.g. array('id ASC', 'name DESC')). |
|
|
|
|
* The method will automatically quote the column names unless a column contains some parenthesis |
|
|
|
|
* (which means the column contains a DB expression). |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see addOrderBy() |
|
|
|
|
*/ |
|
|
|
|
public function orderBy($columns) |
|
|
|
@ -513,7 +522,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array (e.g. array('id ASC', 'name DESC')). |
|
|
|
|
* The method will automatically quote the column names unless a column contains some parenthesis |
|
|
|
|
* (which means the column contains a DB expression). |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see orderBy() |
|
|
|
|
*/ |
|
|
|
|
public function addOrderBy($columns) |
|
|
|
@ -535,7 +544,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
/** |
|
|
|
|
* Sets the LIMIT part of the query. |
|
|
|
|
* @param integer $limit the limit |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function limit($limit) |
|
|
|
|
{ |
|
|
|
@ -546,7 +555,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
/** |
|
|
|
|
* Sets the OFFSET part of the query. |
|
|
|
|
* @param integer $offset the offset |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function offset($offset) |
|
|
|
|
{ |
|
|
|
@ -557,7 +566,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
/** |
|
|
|
|
* Appends a SQL statement using UNION operator. |
|
|
|
|
* @param string $sql the SQL statement to be appended using UNION |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function union($sql) |
|
|
|
|
{ |
|
|
|
@ -570,7 +579,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* @param array list of query parameter values indexed by parameter placeholders. |
|
|
|
|
* For example, `array(':name'=>'Dan', ':age'=>31)`. |
|
|
|
|
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see addParams() |
|
|
|
|
*/ |
|
|
|
|
public function params($params) |
|
|
|
@ -584,7 +593,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
* @param array list of query parameter values indexed by parameter placeholders. |
|
|
|
|
* For example, `array(':name'=>'Dan', ':age'=>31)`. |
|
|
|
|
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
* @see params() |
|
|
|
|
*/ |
|
|
|
|
public function addParams($params) |
|
|
|
@ -703,7 +712,7 @@ class BaseQuery extends \yii\base\Object
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Resets the query object to its original state. |
|
|
|
|
* @return Query the query object itself |
|
|
|
|
* @return BaseQuery the query object itself |
|
|
|
|
*/ |
|
|
|
|
public function reset() |
|
|
|
|
{ |
|
|
|
|