|
|
@ -253,17 +253,13 @@ class Command extends \yii\base\Component |
|
|
|
* Executes the SQL statement. |
|
|
|
* Executes the SQL statement. |
|
|
|
* This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs. |
|
|
|
* This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs. |
|
|
|
* No result set will be returned. |
|
|
|
* No result set will be returned. |
|
|
|
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative |
|
|
|
|
|
|
|
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]] |
|
|
|
|
|
|
|
* or [[bindValue()]] will be ignored. |
|
|
|
|
|
|
|
* @return integer number of rows affected by the execution. |
|
|
|
* @return integer number of rows affected by the execution. |
|
|
|
* @throws Exception execution failed |
|
|
|
* @throws Exception execution failed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function execute($params = array()) |
|
|
|
public function execute() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$sql = $this->getSql(); |
|
|
|
$sql = $this->getSql(); |
|
|
|
|
|
|
|
|
|
|
|
$this->_params = array_merge($this->_params, $params); |
|
|
|
|
|
|
|
if ($this->_params === array()) { |
|
|
|
if ($this->_params === array()) { |
|
|
|
$paramLog = ''; |
|
|
|
$paramLog = ''; |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -282,11 +278,7 @@ class Command extends \yii\base\Component |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->prepare(); |
|
|
|
$this->prepare(); |
|
|
|
if ($params === array()) { |
|
|
|
|
|
|
|
$this->pdoStatement->execute(); |
|
|
|
$this->pdoStatement->execute(); |
|
|
|
} else { |
|
|
|
|
|
|
|
$this->pdoStatement->execute($params); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$n = $this->pdoStatement->rowCount(); |
|
|
|
$n = $this->pdoStatement->rowCount(); |
|
|
|
|
|
|
|
|
|
|
|
if ($this->db->enableProfiling) { |
|
|
|
if ($this->db->enableProfiling) { |
|
|
@ -309,63 +301,51 @@ class Command extends \yii\base\Component |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Executes the SQL statement and returns query result. |
|
|
|
* Executes the SQL statement and returns query result. |
|
|
|
* This method is for executing a SQL query that returns result set, such as `SELECT`. |
|
|
|
* This method is for executing a SQL query that returns result set, such as `SELECT`. |
|
|
|
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative |
|
|
|
|
|
|
|
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]] |
|
|
|
|
|
|
|
* or [[bindValue()]] will be ignored. |
|
|
|
|
|
|
|
* @return DataReader the reader object for fetching the query result |
|
|
|
* @return DataReader the reader object for fetching the query result |
|
|
|
* @throws Exception execution failed |
|
|
|
* @throws Exception execution failed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function query($params = array()) |
|
|
|
public function query() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->queryInternal('', $params); |
|
|
|
return $this->queryInternal(''); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Executes the SQL statement and returns ALL rows at once. |
|
|
|
* Executes the SQL statement and returns ALL rows at once. |
|
|
|
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative |
|
|
|
|
|
|
|
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]] |
|
|
|
|
|
|
|
* or [[bindValue()]] will be ignored. |
|
|
|
|
|
|
|
* @param mixed $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
|
|
|
* @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. |
|
|
|
* 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. |
|
|
|
* @return array all rows of the query result. Each array element is an array representing a row of data. |
|
|
|
* An empty array is returned if the query results in nothing. |
|
|
|
* An empty array is returned if the query results in nothing. |
|
|
|
* @throws Exception execution failed |
|
|
|
* @throws Exception execution failed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function queryAll($params = array(), $fetchMode = null) |
|
|
|
public function queryAll($fetchMode = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->queryInternal('fetchAll', $params, $fetchMode); |
|
|
|
return $this->queryInternal('fetchAll', $fetchMode); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Executes the SQL statement and returns the first row of the result. |
|
|
|
* Executes the SQL statement and returns the first row of the result. |
|
|
|
* This method is best used when only the first row of result is needed for a query. |
|
|
|
* This method is best used when only the first row of result is needed for a query. |
|
|
|
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative |
|
|
|
|
|
|
|
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]] |
|
|
|
|
|
|
|
* or [[bindValue()]] will be ignored. |
|
|
|
|
|
|
|
* @param mixed $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
|
|
|
* @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. |
|
|
|
* 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 |
|
|
|
* @return array|boolean the first row (in terms of an array) of the query result. False is returned if the query |
|
|
|
* results in nothing. |
|
|
|
* results in nothing. |
|
|
|
* @throws Exception execution failed |
|
|
|
* @throws Exception execution failed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function queryRow($params = array(), $fetchMode = null) |
|
|
|
public function queryRow($fetchMode = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->queryInternal('fetch', $params, $fetchMode); |
|
|
|
return $this->queryInternal('fetch', $fetchMode); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Executes the SQL statement and returns the value of the first column in the first row of data. |
|
|
|
* Executes the SQL statement and returns the value of the first column in the first row of data. |
|
|
|
* This method is best used when only a single value is needed for a query. |
|
|
|
* This method is best used when only a single value is needed for a query. |
|
|
|
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative |
|
|
|
|
|
|
|
* 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. |
|
|
|
* @return string|boolean the value of the first column in the first row of the query result. |
|
|
|
* False is returned if there is no value. |
|
|
|
* False is returned if there is no value. |
|
|
|
* @throws Exception execution failed |
|
|
|
* @throws Exception execution failed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function queryScalar($params = array()) |
|
|
|
public function queryScalar() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$result = $this->queryInternal('fetchColumn', $params, 0); |
|
|
|
$result = $this->queryInternal('fetchColumn', 0); |
|
|
|
if (is_resource($result) && get_resource_type($result) === 'stream') { |
|
|
|
if (is_resource($result) && get_resource_type($result) === 'stream') { |
|
|
|
return stream_get_contents($result); |
|
|
|
return stream_get_contents($result); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -377,33 +357,26 @@ class Command extends \yii\base\Component |
|
|
|
* Executes the SQL statement and returns the first column of the result. |
|
|
|
* Executes the SQL statement and returns the first column of the result. |
|
|
|
* This method is best used when only the first column of result (i.e. the first element in each row) |
|
|
|
* 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. |
|
|
|
* is needed for a query. |
|
|
|
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative |
|
|
|
|
|
|
|
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]] |
|
|
|
|
|
|
|
* or [[bindValue()]] will be ignored. |
|
|
|
|
|
|
|
* @return array the first column of the query result. Empty array is returned if the query results in nothing. |
|
|
|
* @return array the first column of the query result. Empty array is returned if the query results in nothing. |
|
|
|
* @throws Exception execution failed |
|
|
|
* @throws Exception execution failed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function queryColumn($params = array()) |
|
|
|
public function queryColumn() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->queryInternal('fetchAll', $params, \PDO::FETCH_COLUMN); |
|
|
|
return $this->queryInternal('fetchAll', \PDO::FETCH_COLUMN); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Performs the actual DB query of a SQL statement. |
|
|
|
* Performs the actual DB query of a SQL statement. |
|
|
|
* @param string $method method of PDOStatement to be called |
|
|
|
* @param string $method method of PDOStatement to be called |
|
|
|
* @param array $params input parameters (name=>value) for the SQL execution. This is an alternative |
|
|
|
|
|
|
|
* to [[bindValues()]]. Note that if you pass parameters in this way, any previous call to [[bindParam()]] |
|
|
|
|
|
|
|
* or [[bindValue()]] will be ignored. |
|
|
|
|
|
|
|
* @param mixed $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
|
|
|
* @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. |
|
|
|
* for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
|
|
|
* @return mixed the method execution result |
|
|
|
* @return mixed the method execution result |
|
|
|
* @throws Exception if the query causes any problem |
|
|
|
* @throws Exception if the query causes any problem |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private function queryInternal($method, $params, $fetchMode = null) |
|
|
|
private function queryInternal($method, $fetchMode = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$db = $this->db; |
|
|
|
$db = $this->db; |
|
|
|
$sql = $this->getSql(); |
|
|
|
$sql = $this->getSql(); |
|
|
|
$this->_params = array_merge($this->_params, $params); |
|
|
|
|
|
|
|
if ($this->_params === array()) { |
|
|
|
if ($this->_params === array()) { |
|
|
|
$paramLog = ''; |
|
|
|
$paramLog = ''; |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -431,11 +404,7 @@ class Command extends \yii\base\Component |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->prepare(); |
|
|
|
$this->prepare(); |
|
|
|
if ($params === array()) { |
|
|
|
|
|
|
|
$this->pdoStatement->execute(); |
|
|
|
$this->pdoStatement->execute(); |
|
|
|
} else { |
|
|
|
|
|
|
|
$this->pdoStatement->execute($params); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($method === '') { |
|
|
|
if ($method === '') { |
|
|
|
$result = new DataReader($this); |
|
|
|
$result = new DataReader($this); |
|
|
|