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
  • PHP

Classes

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