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