1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Database\Diagnostics;
13:
14: use Nette,
15: Nette\Database\Connection,
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: public $totalTime = 0;
32:
33:
34: public $queries = array();
35:
36:
37: public $name;
38:
39:
40: public $explain = TRUE;
41:
42:
43: public $disabled = FALSE;
44:
45:
46:
47: public static function initialize(Connection $connection)
48: {
49: $panel = new static;
50: Debugger::$blueScreen->addPanel(array($panel, 'renderException'), __CLASS__);
51: if (!Debugger::$productionMode) {
52: $connection->onQuery[] = callback($panel, 'logQuery');
53: Debugger::$bar->addPanel($panel);
54: }
55: }
56:
57:
58:
59: public function logQuery(Nette\Database\Statement $result, array $params = NULL)
60: {
61: if ($this->disabled) {
62: return;
63: }
64: $source = NULL;
65: foreach (debug_backtrace(FALSE) as $row) {
66: if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], NETTE_DIR . DIRECTORY_SEPARATOR) !== 0) {
67: $source = array($row['file'], (int) $row['line']);
68: break;
69: }
70: }
71: $this->totalTime += $result->time;
72: $this->queries[] = array($result->queryString, $params, $result->time, $result->rowCount(), $result->getConnection(), $source);
73: }
74:
75:
76:
77: public function renderException($e)
78: {
79: if ($e instanceof \PDOException && isset($e->queryString)) {
80: return array(
81: 'tab' => 'SQL',
82: 'panel' => Connection::highlightSql($e->queryString),
83: );
84: }
85: }
86:
87:
88:
89: public function getTab()
90: {
91: return '<span title="Nette\\Database ' . htmlSpecialChars($this->name) . '">'
92: . '<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" />'
93: . count($this->queries) . ' queries'
94: . ($this->totalTime ? ' / ' . sprintf('%0.1f', $this->totalTime * 1000) . 'ms' : '')
95: . '</span>';
96: }
97:
98:
99:
100: public function getPanel()
101: {
102: $this->disabled = TRUE;
103: $s = '';
104: $h = 'htmlSpecialChars';
105: foreach ($this->queries as $i => $query) {
106: list($sql, $params, $time, $rows, $connection, $source) = $query;
107:
108: $explain = NULL;
109: if ($this->explain && preg_match('#\s*SELECT\s#iA', $sql)) {
110: try {
111: $explain = $connection->queryArgs('EXPLAIN ' . $sql, $params)->fetchAll();
112: } catch (\PDOException $e) {}
113: }
114:
115: $s .= '<tr><td>' . sprintf('%0.3f', $time * 1000);
116: if ($explain) {
117: static $counter;
118: $counter++;
119: $s .= "<br /><a href='#' class='nette-toggler' rel='#nette-DbConnectionPanel-row-$counter'>explain ►</a>";
120: }
121:
122: $s .= '</td><td class="nette-DbConnectionPanel-sql">' . Connection::highlightSql(Nette\Utils\Strings::truncate($sql, self::$maxLength));
123: if ($explain) {
124: $s .= "<table id='nette-DbConnectionPanel-row-$counter' class='nette-collapsed'><tr>";
125: foreach ($explain[0] as $col => $foo) {
126: $s .= "<th>{$h($col)}</th>";
127: }
128: $s .= "</tr>";
129: foreach ($explain as $row) {
130: $s .= "<tr>";
131: foreach ($row as $col) {
132: $s .= "<td>{$h($col)}</td>";
133: }
134: $s .= "</tr>";
135: }
136: $s .= "</table>";
137: }
138: if ($source) {
139: $s .= Nette\Diagnostics\Helpers::editorLink($source[0], $source[1])->class('nette-DbConnectionPanel-source');
140: }
141:
142: $s .= '</td><td>';
143: foreach ($params as $param) {
144: $s .= Debugger::dump($param, TRUE);
145: }
146:
147: $s .= '</td><td>' . $rows . '</td></tr>';
148: }
149:
150: return empty($this->queries) ? '' :
151: '<style> #nette-debug td.nette-DbConnectionPanel-sql { background: white !important }
152: #nette-debug .nette-DbConnectionPanel-source { color: #BBB !important }
153: #nette-debug nette-DbConnectionPanel tr table { margin: 8px 0; max-height: 150px; overflow:auto } </style>
154: <h1>Queries: ' . count($this->queries) . ($this->totalTime ? ', time: ' . sprintf('%0.3f', $this->totalTime * 1000) . ' ms' : '') . '</h1>
155: <div class="nette-inner nette-DbConnectionPanel">
156: <table>
157: <tr><th>Time ms</th><th>SQL Statement</th><th>Params</th><th>Rows</th></tr>' . $s . '
158: </table>
159: </div>';
160: }
161:
162: }
163: