1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\DatabaseTracy;
9:
10: use Nette;
11: use Nette\Database\Helpers;
12: use Tracy;
13:
14:
15: 16: 17:
18: class ConnectionPanel extends Nette\Object implements Tracy\IBarPanel
19: {
20:
21: public $maxQueries = 100;
22:
23:
24: private $totalTime = 0;
25:
26:
27: private $count = 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: public function __construct(Nette\Database\Connection $connection)
43: {
44: $connection->onQuery[] = array($this, 'logQuery');
45: }
46:
47:
48: public function logQuery(Nette\Database\Connection $connection, $result)
49: {
50: if ($this->disabled) {
51: return;
52: }
53: $this->count++;
54:
55: $source = NULL;
56: $trace = $result instanceof \PDOException ? $result->getTrace() : debug_backtrace(PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_IGNORE_ARGS : FALSE);
57: foreach ($trace as $row) {
58: if (isset($row['file']) && is_file($row['file']) && !Tracy\Debugger::getBluescreen()->isCollapsed($row['file'])) {
59: if ((isset($row['function']) && strpos($row['function'], 'call_user_func') === 0)
60: || (isset($row['class']) && is_subclass_of($row['class'], '\\Nette\\Database\\Connection'))
61: ) {
62: continue;
63: }
64: $source = array($row['file'], (int) $row['line']);
65: break;
66: }
67: }
68: if ($result instanceof Nette\Database\ResultSet) {
69: $this->totalTime += $result->getTime();
70: if ($this->count < $this->maxQueries) {
71: $this->queries[] = array($connection, $result->getQueryString(), $result->getParameters(), $source, $result->getTime(), $result->getRowCount(), NULL);
72: }
73:
74: } elseif ($result instanceof \PDOException && $this->count < $this->maxQueries) {
75: $this->queries[] = array($connection, $result->queryString, NULL, $source, NULL, NULL, $result->getMessage());
76: }
77: }
78:
79:
80: public static function renderException($e)
81: {
82: if (!$e instanceof \PDOException) {
83: return;
84: }
85: if (isset($e->queryString)) {
86: $sql = $e->queryString;
87:
88: } elseif ($item = Tracy\Helpers::findTrace($e->getTrace(), 'PDO::prepare')) {
89: $sql = $item['args'][0];
90: }
91: return isset($sql) ? array(
92: 'tab' => 'SQL',
93: 'panel' => Helpers::dumpSql($sql),
94: ) : NULL;
95: }
96:
97:
98: public function getTab()
99: {
100: $name = $this->name;
101: $count = $this->count;
102: $totalTime = $this->totalTime;
103: ob_start(function () {});
104: require __DIR__ . '/templates/ConnectionPanel.tab.phtml';
105: return ob_get_clean();
106: }
107:
108:
109: public function getPanel()
110: {
111: $this->disabled = TRUE;
112: if (!$this->count) {
113: return;
114: }
115:
116: $name = $this->name;
117: $count = $this->count;
118: $totalTime = $this->totalTime;
119: $queries = array();
120: foreach ($this->queries as $query) {
121: list($connection, $sql, $params, $source, $time, $rows, $error) = $query;
122: $explain = NULL;
123: if (!$error && $this->explain && preg_match('#\s*\(?\s*SELECT\s#iA', $sql)) {
124: try {
125: $cmd = is_string($this->explain) ? $this->explain : 'EXPLAIN';
126: $explain = $connection->queryArgs("$cmd $sql", $params)->fetchAll();
127: } catch (\PDOException $e) {
128: }
129: }
130: $query[] = $explain;
131: $queries[] = $query;
132: }
133:
134: ob_start(function () {});
135: require __DIR__ . '/templates/ConnectionPanel.panel.phtml';
136: return ob_get_clean();
137: }
138:
139: }
140: