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
d06e785e
Commit
d06e785e
authored
May 17, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Always enable profiling for DB queries.
parent
80b3d564
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
41 deletions
+23
-41
Command.php
yii/db/Command.php
+0
-12
Connection.php
yii/db/Connection.php
+5
-29
Logger.php
yii/logging/Logger.php
+18
-0
No files found.
yii/db/Command.php
View file @
d06e785e
...
...
@@ -283,22 +283,16 @@ class Command extends \yii\base\Component
try
{
$token
=
"SQL:
$sql
"
;
if
(
$this
->
db
->
enableProfiling
)
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
}
$this
->
prepare
();
$this
->
pdoStatement
->
execute
();
$n
=
$this
->
pdoStatement
->
rowCount
();
if
(
$this
->
db
->
enableProfiling
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
}
return
$n
;
}
catch
(
\Exception
$e
)
{
if
(
$this
->
db
->
enableProfiling
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
}
$message
=
$e
->
getMessage
();
Yii
::
error
(
"
$message
\n
Failed to execute SQL:
$rawSql
"
,
__METHOD__
);
...
...
@@ -411,9 +405,7 @@ class Command extends \yii\base\Component
try
{
$token
=
"SQL:
$sql
"
;
if
(
$db
->
enableProfiling
)
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
}
$this
->
prepare
();
$this
->
pdoStatement
->
execute
();
...
...
@@ -428,9 +420,7 @@ class Command extends \yii\base\Component
$this
->
pdoStatement
->
closeCursor
();
}
if
(
$db
->
enableProfiling
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
}
if
(
isset
(
$cache
,
$cacheKey
)
&&
$cache
instanceof
Cache
)
{
$cache
->
set
(
$cacheKey
,
$result
,
$db
->
queryCacheDuration
,
$db
->
queryCacheDependency
);
...
...
@@ -439,9 +429,7 @@ class Command extends \yii\base\Component
return
$result
;
}
catch
(
\Exception
$e
)
{
if
(
$db
->
enableProfiling
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
}
$message
=
$e
->
getMessage
();
Yii
::
error
(
"
$message
\n
Command::
$method
() failed:
$rawSql
"
,
__METHOD__
);
$errorInfo
=
$e
instanceof
\PDOException
?
$e
->
errorInfo
:
null
;
...
...
yii/db/Connection.php
View file @
d06e785e
...
...
@@ -215,13 +215,6 @@ class Connection extends Component
*/
public
$emulatePrepare
;
/**
* @var boolean whether to enable profiling for the SQL statements being executed.
* Defaults to false. This should be mainly enabled and used during development
* to find out the bottleneck of SQL executions.
* @see getStats
*/
public
$enableProfiling
=
false
;
/**
* @var string the common prefix or suffix for table names. If a table name is given
* as `{{%TableName}}`, then the percentage character `%` will be replaced with this
* property value. For example, `{{%post}}` becomes `{{tbl_post}}` if this property is
...
...
@@ -314,12 +307,16 @@ class Connection extends Component
if
(
empty
(
$this
->
dsn
))
{
throw
new
InvalidConfigException
(
'Connection::dsn cannot be empty.'
);
}
$token
=
'Opening DB connection: '
.
$this
->
dsn
;
try
{
Yii
::
trace
(
'Opening DB connection: '
.
$this
->
dsn
,
__METHOD__
);
Yii
::
trace
(
$token
,
__METHOD__
);
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$this
->
pdo
=
$this
->
createPdoInstance
();
$this
->
initConnection
();
Yii
::
endProfile
(
$token
,
__METHOD__
);
}
catch
(
\PDOException
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
Yii
::
error
(
"Failed to open DB connection (
{
$this
->
dsn
}
): "
.
$e
->
getMessage
(),
__METHOD__
);
$message
=
YII_DEBUG
?
'Failed to open DB connection: '
.
$e
->
getMessage
()
:
'Failed to open DB connection.'
;
throw
new
Exception
(
$message
,
$e
->
errorInfo
,
(
int
)
$e
->
getCode
());
...
...
@@ -541,25 +538,4 @@ class Connection extends Component
return
strtolower
(
$this
->
pdo
->
getAttribute
(
PDO
::
ATTR_DRIVER_NAME
));
}
}
/**
* Returns the statistical results of SQL queries.
* The results returned include the number of SQL statements executed and
* the total time spent.
* In order to use this method, [[enableProfiling]] has to be set true.
* @return array the first element indicates the number of SQL statements executed,
* and the second element the total time spent in SQL execution.
* @see \yii\logging\Logger::getProfiling()
*/
public
function
getQuerySummary
()
{
$logger
=
Yii
::
getLogger
();
$timings
=
$logger
->
getProfiling
(
array
(
'yii\db\Command::query'
,
'yii\db\Command::execute'
));
$count
=
count
(
$timings
);
$time
=
0
;
foreach
(
$timings
as
$timing
)
{
$time
+=
$timing
[
1
];
}
return
array
(
$count
,
$time
);
}
}
yii/logging/Logger.php
View file @
d06e785e
...
...
@@ -225,6 +225,24 @@ class Logger extends Component
return
array_values
(
$timings
);
}
/**
* Returns the statistical results of DB queries.
* The results returned include the number of SQL statements executed and
* the total time spent.
* @return array the first element indicates the number of SQL statements executed,
* and the second element the total time spent in SQL execution.
*/
public
function
getDbProfiling
()
{
$timings
=
$this
->
getProfiling
(
array
(
'yii\db\Command::query'
,
'yii\db\Command::execute'
));
$count
=
count
(
$timings
);
$time
=
0
;
foreach
(
$timings
as
$timing
)
{
$time
+=
$timing
[
1
];
}
return
array
(
$count
,
$time
);
}
private
function
calculateTimings
()
{
$timings
=
array
();
...
...
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