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