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*\(?\s*SELECT\s#iA', $sql)) {
96: try {
97: $cmd = is_string($this->explain) ? $this->explain : 'EXPLAIN';
98: $explain = $connection->queryArgs("$cmd $sql", $params)->fetchAll();
99: } catch (PDOException $e) {}
100: }
101:
102: $s .= '<tr><td>' . sprintf('%0.3f', $time * 1000);
103: if ($explain) {
104: static $counter;
105: $counter++;
106: $s .= "<br /><a href='#' class='nette-toggler' rel='#nette-DbConnectionPanel-row-$counter'>explain ►</a>";
107: }
108:
109: $s .= '</td><td class="nette-DbConnectionPanel-sql">' . NDatabaseHelpers::dumpSql(self::$maxLength ? NStrings::truncate($sql, self::$maxLength) : $sql);
110: if ($explain) {
111: $s .= "<table id='nette-DbConnectionPanel-row-$counter' class='nette-collapsed'><tr>";
112: foreach ($explain[0] as $col => $foo) {
113: $s .= "<th>{$h($col)}</th>";
114: }
115: $s .= "</tr>";
116: foreach ($explain as $row) {
117: $s .= "<tr>";
118: foreach ($row as $col) {
119: $s .= "<td>{$h($col)}</td>";
120: }
121: $s .= "</tr>";
122: }
123: $s .= "</table>";
124: }
125: if ($source) {
126: $s .= NDebugHelpers::editorLink($source[0], $source[1])->class('nette-DbConnectionPanel-source');
127: }
128:
129: $s .= '</td><td>';
130: foreach ($params as $param) {
131: $s .= NDebugger::dump($param, TRUE);
132: }
133:
134: $s .= '</td><td>' . $rows . '</td></tr>';
135: }
136:
137: return empty($this->queries) ? '' :
138: '<style> #nette-debug td.nette-DbConnectionPanel-sql { background: white !important }
139: #nette-debug .nette-DbConnectionPanel-source { color: #BBB !important } </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: