1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Database\Diagnostics;
13:
14: use Nette,
15: Nette\Database\Helpers,
16: Nette\Diagnostics\Debugger;
17:
18:
19:
20: 21: 22: 23: 24:
25: class ConnectionPanel extends Nette\Object implements Nette\Diagnostics\IBarPanel
26: {
27:
28: static public $maxLength = 1000;
29:
30:
31: private $totalTime = 0;
32:
33:
34: private $queries = array();
35:
36:
37: public $name;
38:
39:
40: public $explain = TRUE;
41:
42:
43: public $disabled = FALSE;
44:
45:
46:
47: public function logQuery(Nette\Database\Statement $result, array $params = NULL)
48: {
49: if ($this->disabled) {
50: return;
51: }
52: $source = NULL;
53: foreach (debug_backtrace(FALSE) as $row) {
54: if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], NETTE_DIR . DIRECTORY_SEPARATOR) !== 0) {
55: if (isset($row['function']) && strpos($row['function'], 'call_user_func') === 0) continue;
56: if (isset($row['class']) && is_subclass_of($row['class'], '\\Nette\\Database\\Connection')) continue;
57: $source = array($row['file'], (int) $row['line']);
58: break;
59: }
60: }
61: $this->totalTime += $result->getTime();
62: $this->queries[] = array($result->queryString, $params, $result->getTime(), $result->rowCount(), $result->getConnection(), $source);
63: }
64:
65:
66:
67: public static function renderException($e)
68: {
69: if (!$e instanceof \PDOException) {
70: return;
71: }
72: if (isset($e->queryString)) {
73: $sql = $e->queryString;
74:
75: } elseif ($item = Nette\Diagnostics\Helpers::findTrace($e->getTrace(), 'PDO::prepare')) {
76: $sql = $item['args'][0];
77: }
78: return isset($sql) ? array(
79: 'tab' => 'SQL',
80: 'panel' => Helpers::dumpSql($sql),
81: ) : NULL;
82: }
83:
84:
85:
86: public function getTab()
87: {
88: return '<span title="Nette\\Database ' . htmlSpecialChars($this->name) . '">'
89: . '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEYSURBVBgZBcHPio5hGAfg6/2+R980k6wmJgsJ5U/ZOAqbSc2GnXOwUg7BESgLUeIQ1GSjLFnMwsKGGg1qxJRmPM97/1zXFAAAAEADdlfZzr26miup2svnelq7d2aYgt3rebl585wN6+K3I1/9fJe7O/uIePP2SypJkiRJ0vMhr55FLCA3zgIAOK9uQ4MS361ZOSX+OrTvkgINSjS/HIvhjxNNFGgQsbSmabohKDNoUGLohsls6BaiQIMSs2FYmnXdUsygQYmumy3Nhi6igwalDEOJEjPKP7CA2aFNK8Bkyy3fdNCg7r9/fW3jgpVJbDmy5+PB2IYp4MXFelQ7izPrhkPHB+P5/PjhD5gCgCenx+VR/dODEwD+A3T7nqbxwf1HAAAAAElFTkSuQmCC" />'
90: . count($this->queries) . ' ' . (count($this->queries) === 1 ? 'query' : 'queries')
91: . ($this->totalTime ? ' / ' . sprintf('%0.1f', $this->totalTime * 1000) . 'ms' : '')
92: . '</span>';
93: }
94:
95:
96:
97: public function getPanel()
98: {
99: $this->disabled = TRUE;
100: $s = '';
101: $h = 'htmlSpecialChars';
102: foreach ($this->queries as $i => $query) {
103: list($sql, $params, $time, $rows, $connection, $source) = $query;
104:
105: $explain = NULL;
106: if ($this->explain && preg_match('#\s*\(?\s*SELECT\s#iA', $sql)) {
107: try {
108: $cmd = is_string($this->explain) ? $this->explain : 'EXPLAIN';
109: $explain = $connection->queryArgs("$cmd $sql", $params)->fetchAll();
110: } catch (\PDOException $e) {}
111: }
112:
113: $s .= '<tr><td>' . sprintf('%0.3f', $time * 1000);
114: if ($explain) {
115: static $counter;
116: $counter++;
117: $s .= "<br /><a href='#' class='nette-toggler' rel='#nette-DbConnectionPanel-row-$counter'>explain ►</a>";
118: }
119:
120: $s .= '</td><td class="nette-DbConnectionPanel-sql">' . Helpers::dumpSql(self::$maxLength ? Nette\Utils\Strings::truncate($sql, self::$maxLength) : $sql);
121: if ($explain) {
122: $s .= "<table id='nette-DbConnectionPanel-row-$counter' class='nette-collapsed'><tr>";
123: foreach ($explain[0] as $col => $foo) {
124: $s .= "<th>{$h($col)}</th>";
125: }
126: $s .= "</tr>";
127: foreach ($explain as $row) {
128: $s .= "<tr>";
129: foreach ($row as $col) {
130: $s .= "<td>{$h($col)}</td>";
131: }
132: $s .= "</tr>";
133: }
134: $s .= "</table>";
135: }
136: if ($source) {
137: $s .= Nette\Diagnostics\Helpers::editorLink($source[0], $source[1])->class('nette-DbConnectionPanel-source');
138: }
139:
140: $s .= '</td><td>';
141: foreach ($params as $param) {
142: $s .= Debugger::dump($param, TRUE);
143: }
144:
145: $s .= '</td><td>' . $rows . '</td></tr>';
146: }
147:
148: return empty($this->queries) ? '' :
149: '<style> #nette-debug td.nette-DbConnectionPanel-sql { background: white !important }
150: #nette-debug .nette-DbConnectionPanel-source { color: #BBB !important } </style>
151: <h1>Queries: ' . count($this->queries) . ($this->totalTime ? ', time: ' . sprintf('%0.3f', $this->totalTime * 1000) . ' ms' : '') . '</h1>
152: <div class="nette-inner nette-DbConnectionPanel">
153: <table>
154: <tr><th>Time ms</th><th>SQL Statement</th><th>Params</th><th>Rows</th></tr>' . $s . '
155: </table>
156: </div>';
157: }
158:
159: }
160: