Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
7aa80d86
Commit
7aa80d86
authored
Feb 01, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored execute() and query() methods in db/Command.
parent
0f6eda37
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
84 additions
and
55 deletions
+84
-55
HttpException.php
framework/base/HttpException.php
+69
-0
Command.php
framework/db/Command.php
+14
-45
QueryBuilder.php
framework/db/QueryBuilder.php
+1
-2
CommandTest.php
tests/unit/framework/db/CommandTest.php
+0
-8
No files found.
framework/base/HttpException.php
View file @
7aa80d86
...
@@ -41,4 +41,73 @@ class HttpException extends Exception
...
@@ -41,4 +41,73 @@ class HttpException extends Exception
$this
->
statusCode
=
$status
;
$this
->
statusCode
=
$status
;
parent
::
__construct
(
$message
,
$code
);
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'
);
}
}
}
framework/db/Command.php
View file @
7aa80d86
...
@@ -253,17 +253,13 @@ class Command extends \yii\base\Component
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
);
...
...
framework/db/QueryBuilder.php
View file @
7aa80d86
...
@@ -131,11 +131,10 @@ class QueryBuilder extends \yii\base\Object
...
@@ -131,11 +131,10 @@ class QueryBuilder extends \yii\base\Object
* @param string $table the table that new rows will be inserted into.
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names
* @param array $columns the column names
* @param array $rows the rows to be batch inserted into the table
* @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
* @return string the batch INSERT SQL statement
* @throws NotSupportedException if this is not supported by the underlying DBMS
* @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.'
);
throw
new
NotSupportedException
(
$this
->
db
->
getDriverName
()
.
' does not support batch insert.'
);
...
...
tests/unit/framework/db/CommandTest.php
View file @
7aa80d86
...
@@ -189,14 +189,6 @@ class CommandTest extends \yiiunit\MysqlTestCase
...
@@ -189,14 +189,6 @@ class CommandTest extends \yiiunit\MysqlTestCase
$command
=
$db
->
createCommand
(
$sql
);
$command
=
$db
->
createCommand
(
$sql
);
$command
->
bindValue
(
':name'
,
'user5'
);
$command
->
bindValue
(
':name'
,
'user5'
);
$this
->
assertEquals
(
'user5@example.com'
,
$command
->
queryScalar
());
$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
()
function
testFetchMode
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment