-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Database plugin to trace queries
Extending Zend_Db_Profiler to generate a backtrace. This makes it easy to see, what method is responsible for each query
- Loading branch information
Showing
4 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
class ZFDebug_Db_Profiler extends Zend_Db_Profiler | ||
{ | ||
/** | ||
* Undocumented function | ||
* | ||
* @param string $queryText SQL statement | ||
* @param integer $queryType OPTIONAL Type of query, one of the Zend_Db_Profiler::* constants | ||
* @return integer|null | ||
*/ | ||
public function queryStart($queryText, $queryType = null) | ||
{ | ||
if (!$this->_enabled) { | ||
return null; | ||
} | ||
|
||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); | ||
$trace = array_filter( | ||
$trace, | ||
function ($t) { | ||
return strstr($t['file'], 'application/') !== false; | ||
} | ||
); | ||
$trace = array_values($trace); | ||
$trace = array_map( | ||
function ($t) { | ||
$t = $t['class'].$t['type'].$t['function'].'() ' | ||
. $t['file'] . ':'.$t['line']; | ||
return $t; | ||
}, | ||
$trace | ||
); | ||
|
||
// make sure we have a query type | ||
if (null === $queryType) { | ||
switch (strtolower(substr(ltrim($queryText), 0, 6))) { | ||
case 'insert': | ||
$queryType = self::INSERT; | ||
break; | ||
case 'update': | ||
$queryType = self::UPDATE; | ||
break; | ||
case 'delete': | ||
$queryType = self::DELETE; | ||
break; | ||
case 'select': | ||
$queryType = self::SELECT; | ||
break; | ||
default: | ||
$queryType = self::QUERY; | ||
break; | ||
} | ||
} | ||
|
||
$this->_queryProfiles[] = new ZFDebug_Db_Profiler_Query($queryText, $queryType, $trace); | ||
|
||
end($this->_queryProfiles); | ||
|
||
return key($this->_queryProfiles); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
class ZFDebug_Db_Profiler_Query extends Zend_Db_Profiler_Query | ||
{ | ||
protected $_trace = []; | ||
|
||
public function __construct($query, $queryType, $trace) | ||
{ | ||
$this->_trace = $trace; | ||
|
||
parent::__construct($query, $queryType); | ||
} | ||
|
||
public function getTrace() | ||
{ | ||
return $this->_trace; | ||
} | ||
} |