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
b0cc86df
Commit
b0cc86df
authored
Mar 30, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added Command::getRawSql()
parent
597082a1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
19 deletions
+44
-19
Command.php
framework/db/Command.php
+41
-16
Schema.php
framework/db/Schema.php
+3
-3
No files found.
framework/db/Command.php
View file @
b0cc86df
...
...
@@ -99,6 +99,39 @@ class Command extends \yii\base\Component
}
/**
* Returns the raw SQL by inserting parameter values into the corresponding placeholders in [[sql]].
* Note that the return value of this method should mainly be used for logging purpose.
* It is likely that this method returns an invalid SQL due to improper replacement of parameter placeholders.
* @return string the raw SQL
*/
public
function
getRawSql
()
{
if
(
$this
->
_params
===
array
())
{
return
$this
->
_sql
;
}
else
{
$params
=
array
();
foreach
(
$this
->
_params
as
$name
=>
$value
)
{
if
(
is_string
(
$value
))
{
$params
[
$name
]
=
$this
->
db
->
quoteValue
(
$value
);
}
elseif
(
$value
===
null
)
{
$params
[
$name
]
=
'NULL'
;
}
else
{
$params
[
$name
]
=
$value
;
}
}
if
(
isset
(
$params
[
1
]))
{
$sql
=
''
;
foreach
(
explode
(
'?'
,
$this
->
_sql
)
as
$i
=>
$part
)
{
$sql
.=
(
isset
(
$params
[
$i
])
?
$params
[
$i
]
:
''
)
.
$part
;
}
return
$sql
;
}
else
{
return
strtr
(
$this
->
_sql
,
$params
);
}
}
}
/**
* Prepares the SQL statement to be executed.
* For complex SQL statement that is to be executed multiple times,
* this may improve performance.
...
...
@@ -222,6 +255,7 @@ class Command extends \yii\base\Component
'boolean'
=>
\PDO
::
PARAM_BOOL
,
'integer'
=>
\PDO
::
PARAM_INT
,
'string'
=>
\PDO
::
PARAM_STR
,
'resource'
=>
\PDO
::
PARAM_LOB
,
'NULL'
=>
\PDO
::
PARAM_NULL
,
);
$type
=
gettype
(
$data
);
...
...
@@ -239,13 +273,9 @@ class Command extends \yii\base\Component
{
$sql
=
$this
->
getSql
();
if
(
$this
->
_params
===
array
())
{
$paramLog
=
''
;
}
else
{
$paramLog
=
"
\n
Parameters: "
.
var_export
(
$this
->
_params
,
true
);
}
$rawSql
=
$this
->
getRawSql
();
Yii
::
trace
(
"Executing SQL:
{
$sql
}{
$paramLog
}
"
,
__METHOD__
);
Yii
::
trace
(
"Executing SQL:
$rawSql
"
,
__METHOD__
);
if
(
$sql
==
''
)
{
return
0
;
...
...
@@ -271,7 +301,7 @@ class Command extends \yii\base\Component
}
$message
=
$e
->
getMessage
();
Yii
::
error
(
"
$message
\n
Failed to execute SQL:
{
$sql
}{
$paramLog
}
"
,
__METHOD__
);
Yii
::
error
(
"
$message
\n
Failed to execute SQL:
$rawSql
"
,
__METHOD__
);
$errorInfo
=
$e
instanceof
\PDOException
?
$e
->
errorInfo
:
null
;
throw
new
Exception
(
$message
,
$errorInfo
,
(
int
)
$e
->
getCode
());
...
...
@@ -357,13 +387,9 @@ class Command extends \yii\base\Component
{
$db
=
$this
->
db
;
$sql
=
$this
->
getSql
();
if
(
$this
->
_params
===
array
())
{
$paramLog
=
''
;
}
else
{
$paramLog
=
"
\n
Parameters: "
.
var_export
(
$this
->
_params
,
true
);
}
$rawSql
=
$this
->
getRawSql
();
Yii
::
trace
(
"Querying SQL:
{
$sql
}{
$paramLog
}
"
,
__METHOD__
);
Yii
::
trace
(
"Querying SQL:
$rawSql
"
,
__METHOD__
);
/** @var $cache \yii\caching\Cache */
if
(
$db
->
enableQueryCache
&&
$method
!==
''
)
{
...
...
@@ -375,8 +401,7 @@ class Command extends \yii\base\Component
__CLASS__
,
$db
->
dsn
,
$db
->
username
,
$sql
,
$paramLog
,
$rawSql
,
));
if
((
$result
=
$cache
->
get
(
$cacheKey
))
!==
false
)
{
Yii
::
trace
(
'Query result served from cache'
,
__METHOD__
);
...
...
@@ -418,7 +443,7 @@ class Command extends \yii\base\Component
Yii
::
endProfile
(
$token
,
__METHOD__
);
}
$message
=
$e
->
getMessage
();
Yii
::
error
(
"
$message
\n
Command::
$method
() failed:
{
$sql
}{
$paramLog
}
"
,
__METHOD__
);
Yii
::
error
(
"
$message
\n
Command::
$method
() failed:
$rawSql
"
,
__METHOD__
);
$errorInfo
=
$e
instanceof
\PDOException
?
$e
->
errorInfo
:
null
;
throw
new
Exception
(
$message
,
$errorInfo
,
(
int
)
$e
->
getCode
());
}
...
...
framework/db/Schema.php
View file @
b0cc86df
...
...
@@ -83,7 +83,7 @@ abstract class Schema extends \yii\base\Object
}
$db
=
$this
->
db
;
$realName
=
$this
->
getR
eal
TableName
(
$name
);
$realName
=
$this
->
getR
aw
TableName
(
$name
);
if
(
$db
->
enableSchemaCache
&&
!
in_array
(
$name
,
$db
->
schemaCacheExclude
,
true
))
{
/** @var $cache Cache */
...
...
@@ -318,13 +318,13 @@ abstract class Schema extends \yii\base\Object
}
/**
* Returns the
real name of a
table name.
* Returns the
actual name of a given
table name.
* This method will strip off curly brackets from the given table name
* and replace the percentage character '%' with [[Connection::tablePrefix]].
* @param string $name the table name to be converted
* @return string the real name of the given table name
*/
public
function
getR
eal
TableName
(
$name
)
public
function
getR
aw
TableName
(
$name
)
{
if
(
strpos
(
$name
,
'{{'
)
!==
false
)
{
$name
=
preg_replace
(
'/\\{\\{(.*?)\\}\\}/'
,
'\1'
,
$name
);
...
...
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