Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • ConnectionPanel
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Bridges\DatabaseTracy;
  9: 
 10: use Nette,
 11:     Nette\Database\Helpers,
 12:     Tracy;
 13: 
 14: 
 15: /**
 16:  * Debug panel for Nette\Database.
 17:  *
 18:  * @author     David Grudl
 19:  */
 20: class ConnectionPanel extends Nette\Object implements Tracy\IBarPanel
 21: {
 22:     /** @var int */
 23:     public $maxQueries = 100;
 24: 
 25:     /** @var int logged time */
 26:     private $totalTime = 0;
 27: 
 28:     /** @var int */
 29:     private $count = 0;
 30: 
 31:     /** @var array */
 32:     private $queries = array();
 33: 
 34:     /** @var string */
 35:     public $name;
 36: 
 37:     /** @var bool|string explain queries? */
 38:     public $explain = TRUE;
 39: 
 40:     /** @var bool */
 41:     public $disabled = FALSE;
 42: 
 43: 
 44:     public function __construct(Nette\Database\Connection $connection)
 45:     {
 46:         $connection->onQuery[] = array($this, 'logQuery');
 47:     }
 48: 
 49: 
 50:     public function logQuery(Nette\Database\Connection $connection, $result)
 51:     {
 52:         if ($this->disabled) {
 53:             return;
 54:         }
 55:         $this->count++;
 56: 
 57:         $source = NULL;
 58:         $trace = $result instanceof \PDOException ? $result->getTrace() : debug_backtrace(PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_IGNORE_ARGS : FALSE);
 59:         foreach ($trace as $row) {
 60:             if (isset($row['file']) && is_file($row['file']) && !Tracy\Debugger::getBluescreen()->isCollapsed($row['file'])) {
 61:                 if ((isset($row['function']) && strpos($row['function'], 'call_user_func') === 0)
 62:                     || (isset($row['class']) && is_subclass_of($row['class'], '\\Nette\\Database\\Connection'))
 63:                 ) {
 64:                     continue;
 65:                 }
 66:                 $source = array($row['file'], (int) $row['line']);
 67:                 break;
 68:             }
 69:         }
 70:         if ($result instanceof Nette\Database\ResultSet) {
 71:             $this->totalTime += $result->getTime();
 72:             if ($this->count < $this->maxQueries) {
 73:                 $this->queries[] = array($connection, $result->getQueryString(), $result->getParameters(), $source, $result->getTime(), $result->getRowCount(), NULL);
 74:             }
 75: 
 76:         } elseif ($result instanceof \PDOException && $this->count < $this->maxQueries) {
 77:             $this->queries[] = array($connection, $result->queryString, NULL, $source, NULL, NULL, $result->getMessage());
 78:         }
 79:     }
 80: 
 81: 
 82:     public static function renderException($e)
 83:     {
 84:         if (!$e instanceof \PDOException) {
 85:             return;
 86:         }
 87:         if (isset($e->queryString)) {
 88:             $sql = $e->queryString;
 89: 
 90:         } elseif ($item = Tracy\Helpers::findTrace($e->getTrace(), 'PDO::prepare')) {
 91:             $sql = $item['args'][0];
 92:         }
 93:         return isset($sql) ? array(
 94:             'tab' => 'SQL',
 95:             'panel' => Helpers::dumpSql($sql),
 96:         ) : NULL;
 97:     }
 98: 
 99: 
100:     public function getTab()
101:     {
102:         return '<span title="Nette\\Database ' . htmlSpecialChars($this->name) . '">'
103:             . '<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" />'
104:             . $this->count . ' ' . ($this->count === 1 ? 'query' : 'queries')
105:             . ($this->totalTime ? sprintf(' / %0.1f ms', $this->totalTime * 1000) : '')
106:             . '</span>';
107:     }
108: 
109: 
110:     public function getPanel()
111:     {
112:         $this->disabled = TRUE;
113:         $s = '';
114:         foreach ($this->queries as $query) {
115:             list($connection, $sql, $params, $source, $time, $rows, $error) = $query;
116: 
117:             $explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS()
118:             if (!$error && $this->explain && preg_match('#\s*\(?\s*SELECT\s#iA', $sql)) {
119:                 try {
120:                     $cmd = is_string($this->explain) ? $this->explain : 'EXPLAIN';
121:                     $explain = $connection->queryArgs("$cmd $sql", $params)->fetchAll();
122:                 } catch (\PDOException $e) {}
123:             }
124: 
125:             $s .= '<tr><td>';
126:             if ($error) {
127:                 $s .= '<span title="' . htmlSpecialChars($error, ENT_IGNORE | ENT_QUOTES) . '">ERROR</span>';
128:             } elseif ($time !== NULL) {
129:                 $s .= sprintf('%0.3f', $time * 1000);
130:             }
131:             if ($explain) {
132:                 static $counter;
133:                 $counter++;
134:                 $s .= "<br /><a class='tracy-toggle tracy-collapsed' href='#nette-DbConnectionPanel-row-$counter'>explain</a>";
135:             }
136: 
137:             $s .= '</td><td class="nette-DbConnectionPanel-sql">' . Helpers::dumpSql($sql, $params);
138:             if ($explain) {
139:                 $s .= "<table id='nette-DbConnectionPanel-row-$counter' class='tracy-collapsed'><tr>";
140:                 foreach ($explain[0] as $col => $foo) {
141:                     $s .= '<th>' . htmlSpecialChars($col) . '</th>';
142:                 }
143:                 $s .= '</tr>';
144:                 foreach ($explain as $row) {
145:                     $s .= '<tr>';
146:                     foreach ($row as $col) {
147:                         $s .= '<td>' . htmlSpecialChars($col) . '</td>';
148:                     }
149:                     $s .= '</tr>';
150:                 }
151:                 $s .= '</table>';
152:             }
153:             if ($source) {
154:                 $s .= substr_replace(Tracy\Helpers::editorLink($source[0], $source[1]), ' class="nette-DbConnectionPanel-source"', 2, 0);
155:             }
156: 
157:             $s .= '</td><td>' . $rows . '</td></tr>';
158:         }
159: 
160:         return $this->count ?
161:             '<style class="tracy-debug"> #tracy-debug td.nette-DbConnectionPanel-sql { background: white !important }
162:             #tracy-debug .nette-DbConnectionPanel-source { color: #BBB !important } </style>
163:             <h1 title="' . htmlSpecialChars($connection->getDsn()) . '">Queries: ' . $this->count
164:             . ($this->totalTime ? sprintf(', time: %0.3f ms', $this->totalTime * 1000) : '') . ', ' . htmlSpecialChars($this->name) . '</h1>
165:             <div class="tracy-inner nette-DbConnectionPanel">
166:             <table>
167:                 <tr><th>Time&nbsp;ms</th><th>SQL Query</th><th>Rows</th></tr>' . $s . '
168:             </table>'
169:             . (count($this->queries) < $this->count ? '<p>...and more</p>' : '')
170:             . '</div>' : '';
171:     }
172: 
173: }
174: 
Nette 2.2.2 API API documentation generated by ApiGen 2.8.0