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