Packages

  • 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

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