Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

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