Commit 7aa80d86 by Qiang Xue

refactored execute() and query() methods in db/Command.

parent 0f6eda37
......@@ -41,4 +41,73 @@ class HttpException extends Exception
$this->statusCode = $status;
parent::__construct($message, $code);
}
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
static $httpCodes = array(
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
118 => 'Connection timed out',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
210 => 'Content Different',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
310 => 'Too many Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested range unsatisfiable',
417 => 'Expectation failed',
418 => 'I’m a teapot',
422 => 'Unprocessable entity',
423 => 'Locked',
424 => 'Method failure',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
449 => 'Retry With',
450 => 'Blocked by Windows Parental Controls',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway ou Proxy Error',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
507 => 'Insufficient storage',
509 => 'Bandwidth Limit Exceeded',
);
if(isset($httpCodes[$this->statusCode]))
return $httpCodes[$this->statusCode];
else
return \Yii::t('yii', 'Error');
}
}
......@@ -253,17 +253,13 @@ class Command extends \yii\base\Component
* Executes the SQL statement.
* This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs.
* 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.
* @throws Exception execution failed
*/
public function execute($params = array())
public function execute()
{
$sql = $this->getSql();
$this->_params = array_merge($this->_params, $params);
if ($this->_params === array()) {
$paramLog = '';
} else {
......@@ -282,11 +278,7 @@ class Command extends \yii\base\Component
}
$this->prepare();
if ($params === array()) {
$this->pdoStatement->execute();
} else {
$this->pdoStatement->execute($params);
}
$this->pdoStatement->execute();
$n = $this->pdoStatement->rowCount();
if ($this->db->enableProfiling) {
......@@ -309,63 +301,51 @@ class Command extends \yii\base\Component
/**
* Executes the SQL statement and returns query result.
* 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
* @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.
* @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)
* 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.
* An empty array is returned if the query results in nothing.
* @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.
* 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)
* 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
* results in nothing.
* @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.
* 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.
* False is returned if there is no value.
* @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') {
return stream_get_contents($result);
} else {
......@@ -377,33 +357,26 @@ class Command extends \yii\base\Component
* 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)
* 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.
* @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.
* @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)
* for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used.
* @return mixed the method execution result
* @throws Exception if the query causes any problem
*/
private function queryInternal($method, $params, $fetchMode = null)
private function queryInternal($method, $fetchMode = null)
{
$db = $this->db;
$sql = $this->getSql();
$this->_params = array_merge($this->_params, $params);
if ($this->_params === array()) {
$paramLog = '';
} else {
......@@ -431,11 +404,7 @@ class Command extends \yii\base\Component
}
$this->prepare();
if ($params === array()) {
$this->pdoStatement->execute();
} else {
$this->pdoStatement->execute($params);
}
$this->pdoStatement->execute();
if ($method === '') {
$result = new DataReader($this);
......
......@@ -131,11 +131,10 @@ class QueryBuilder extends \yii\base\Object
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names
* @param array $rows the rows to be batch inserted into the table
* @param array $params the parameters to be bound to the command
* @return string the batch INSERT SQL statement
* @throws NotSupportedException if this is not supported by the underlying DBMS
*/
public function batchInsert($table, $columns, $rows, $params = array())
public function batchInsert($table, $columns, $rows)
{
throw new NotSupportedException($this->db->getDriverName() . ' does not support batch insert.');
......
......@@ -189,14 +189,6 @@ class CommandTest extends \yiiunit\MysqlTestCase
$command = $db->createCommand($sql);
$command->bindValue(':name', 'user5');
$this->assertEquals('user5@example.com', $command->queryScalar());
// bind value via query or execute method
$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user6\', \'address6\')';
$command = $db->createCommand($sql);
$command->execute(array(':email' => 'user6@example.com'));
$sql = 'SELECT email FROM tbl_customer WHERE name=:name';
$command = $db->createCommand($sql);
$this->assertEquals('user5@example.com', $command->queryScalar(array(':name' => 'user5')));
}
function testFetchMode()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment