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