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