.
namespace hoplite\data;
use \hoplite\base\Profiling;
require_once HOPLITE_ROOT . '/base/filter.php';
require_once HOPLITE_ROOT . '/base/profiling.php';
/*!
A wrapper class around PDO that profiles all calls to the database.
This can be used as a drop-in for \PDO, though it sets the
PDO::ATTR_STATEMENT_CLASS to a custom instance.
*/
class ProfilingPDO extends \PDO
{
private $traces = array();
private $statements = array();
public function __construct()
{
$args = func_get_args();
call_user_func_array('parent::__construct', $args);
$this->SetAttribute(\PDO::ATTR_STATEMENT_CLASS,
array('\\hoplite\\data\\ProfilingPDOStatement', array($this)));
}
public function exec()
{
$start = microtime(true);
$args = func_get_args();
$r = call_user_func_array('parent::exec', $args);
$this->traces[] = array(
'start' => $start,
'end' => microtime(true),
'trace' => debug_backtrace(),
'query' => $statement,
);
return $r;
}
public function query()
{
$start = microtime(true);
$args = func_get_args();
$r = call_user_func_array('parent::query', $args);
$this->traces[] = array(
'start' => $start,
'end' => microtime(true),
'trace' => debug_backtrace(),
'query' => $args[0],
);
return $r;
}
public function prepare()
{
$start = microtime(true);
$args = func_get_args();
$r = call_user_func_array('parent::prepare', $args);
$this->traces[] = array(
'start' => $start,
'end' => microtime(true),
'trace' => debug_backtrace(),
'query' => $args[0],
'prepare' => TRUE,
);
return $r;
}
/*!
Returns the array of all the traces.
*/
public function GetTraces()
{
return $this->traces;
}
/*!
Generates a block of HTML that displays information about the query traces.
*/
public function ConstructHTMLDebugBlock()
{
$debug = '';
$debug .= "
\n";
$debug .= '
Query Debug: ' . sizeof($this->traces) . ' Total |
";
$debug .= "\n\t\t\t$query[query]\n\n";
if (isset($query['params'])) {
$debug .= "\t\t\t
(" .
($query['end'] - $query['start']) . ") \n";
$debug .= "\n\t\t | \n\t