3 // Copyright (c) 2013 Blue Static
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
17 namespace hoplite\data
;
19 use \hoplite
\base\Profiling
;
21 require_once HOPLITE_ROOT
. '/base/profiling.php';
24 A wrapper class around PDO that profiles all calls to the database.
26 This can be used as a drop-in for \PDO, though it sets the
27 PDO::ATTR_STATEMENT_CLASS to a custom instance.
29 class ProfilingPDO
extends \PDO
31 private $traces = array();
33 private $statements = array();
35 public function __construct()
37 $args = func_get_args();
38 call_user_func_array('parent::__construct', $args);
39 $this->SetAttribute(\PDO
::ATTR_STATEMENT_CLASS
,
40 array('\\hoplite\\data\\ProfilingPDOStatement', array($this)));
43 public function exec()
45 $start = microtime(true
);
47 $args = func_get_args();
48 $r = call_user_func_array('parent::exec', $args);
50 $this->traces
[] = array(
52 'end' => microtime(true
),
53 'trace' => debug_backtrace(),
54 'query' => $statement,
59 public function query()
61 $start = microtime(true
);
63 $args = func_get_args();
64 $r = call_user_func_array('parent::query', $args);
66 $this->traces
[] = array(
68 'end' => microtime(true
),
69 'trace' => debug_backtrace(),
76 public function prepare()
78 $start = microtime(true
);
80 $args = func_get_args();
81 $r = call_user_func_array('parent::prepare', $args);
83 $this->traces
[] = array(
85 'end' => microtime(true
),
86 'trace' => debug_backtrace(),
95 Returns the array of all the traces.
97 public function GetTraces()
103 Generates a block of HTML that displays information about the query traces.
105 public function ConstructHTMLDebugBlock()
109 $debug .= "<br />\n";
110 $debug .= '<table cellpadding="4" cellspacing="1" border="0" align="center" width="30%" ' .
111 'style="background-color: rgb(60, 60, 60); color: white">' . "\n\t";
112 $debug .= '<tr><td><strong>Query Debug: ' . sizeof($this->traces
) . ' Total </strong></td></tr>';
113 foreach ($this->traces
as $query) {
114 $italic = isset($query['prepare']) && $query['prepare'] ?
'font-style: italic' : '';
115 $debug .= "\n\t<tr style=\"background-color: rgb(230, 230, 230); color: black; $italic\">";
116 $debug .= "\n\t\t<td>";
117 $debug .= "\n\t\t\t$query[query]\n\n";
118 if (isset($query['params'])) {
119 $debug .= "\t\t\t<ol>\n\t\t\t\t<li>";
120 $debug .= implode("</li>\n\t\t\t\t<li>", filter_var_array($query['params'], FILTER_SANITIZE_SPECIAL_CHARS
));
121 $debug .= "</li>\n\t\t\t</ol>\n";
123 $debug .= "\n\t\t\t<div style=\"font-size: 9px;\">(" .
124 ($query['end'] - $query['start']) . ")</div>\n";
125 $debug .= "<!--\n" . implode("\n", Profiling
::FormatDebugBacktrace($query['trace'])) .
126 "\n-->\n\t\t</td>\n\t</tr>";
129 $debug .= "\n</table>\n\n\n";
135 Internal function that records a query trace. This is public only for use b
136 ProfilingPDOStatement.
138 public function RecordTrace(array $trace)
140 $this->traces
[] = $trace;
145 A companion to ProfilingPDO that profiles prepared statements.
147 class ProfilingPDOStatement
extends \PDOStatement
151 protected function __construct(ProfilingPDO
$pdo)
156 public function execute()
158 $start = microtime(true
);
160 $args = func_get_args();
161 $r = call_user_func_array('parent::execute', $args);
163 $this->pdo
->RecordTrace(array(
165 'end' => microtime(true
),
166 'trace' => debug_backtrace(),
167 'query' => $this->queryString
,
168 'params' => $args[0],