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

Classes

  • Overview
  • Package
  • 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:  * @package Nette\Database\Diagnostics
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * Debug panel for Database.
 13:  *
 14:  * @author     David Grudl
 15:  * @package Nette\Database\Diagnostics
 16:  */
 17: class DatabasePanel extends Object implements IBarPanel
 18: {
 19:     /** @var int maximum SQL length */
 20:     static public $maxLength = 1000;
 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 logQuery(Statement $result, array $params = NULL)
 45:     {
 46:         if ($this->disabled) {
 47:             return;
 48:         }
 49: 
 50:         $source = NULL;
 51:         foreach (PHP_VERSION_ID < 50205 ? debug_backtrace() : 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: 
 60:         $this->count++;
 61:         $this->totalTime += $result->getTime();
 62:         if ($this->count < $this->maxQueries) {
 63:             $this->queries[] = array($result->queryString, $params, $result->getTime(), $result->rowCount(), $result->getConnection(), $source);
 64:         }
 65:     }
 66: 
 67: 
 68:     public static function renderException($e)
 69:     {
 70:         if (!$e instanceof PDOException) {
 71:             return;
 72:         }
 73:         if (isset($e->queryString)) {
 74:             $sql = $e->queryString;
 75: 
 76:         } elseif ($item = DebugHelpers::findTrace($e->getTrace(), 'PDO::prepare')) {
 77:             $sql = $item['args'][0];
 78:         }
 79:         return isset($sql) ? array(
 80:             'tab' => 'SQL',
 81:             'panel' => DatabaseHelpers::dumpSql($sql),
 82:         ) : NULL;
 83:     }
 84: 
 85: 
 86:     public function getTab()
 87:     {
 88:         return '<span title="Nette\\Database ' . htmlSpecialChars($this->name) . '">'
 89:             . '<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" />'
 90:             . $this->count . ' ' . ($this->count === 1 ? 'query' : 'queries')
 91:             . ($this->totalTime ? ' / ' . sprintf('%0.1f', $this->totalTime * 1000) . 'ms' : '')
 92:             . '</span>';
 93:     }
 94: 
 95: 
 96:     public function getPanel()
 97:     {
 98:         $this->disabled = TRUE;
 99:         $s = '';
100:         foreach ($this->queries as $query) {
101:             list($sql, $params, $time, $rows, $connection, $source) = $query;
102: 
103:             $explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS()
104:             if ($this->explain && preg_match('#\s*\(?\s*SELECT\s#iA', $sql)) {
105:                 try {
106:                     $cmd = is_string($this->explain) ? $this->explain : 'EXPLAIN';
107:                     $explain = $connection->queryArgs("$cmd $sql", $params)->fetchAll();
108:                 } catch (PDOException $e) {}
109:             }
110: 
111:             $s .= '<tr><td>' . sprintf('%0.3f', $time * 1000);
112:             if ($explain) {
113:                 static $counter;
114:                 $counter++;
115:                 $s .= "<br /><a href='#' class='nette-toggler' rel='#nette-DbConnectionPanel-row-$counter'>explain&nbsp;&#x25ba;</a>";
116:             }
117: 
118:             $s .= '</td><td class="nette-DbConnectionPanel-sql">' . DatabaseHelpers::dumpSql(self::$maxLength ? Strings::truncate($sql, self::$maxLength) : $sql);
119:             if ($explain) {
120:                 $s .= "<table id='nette-DbConnectionPanel-row-$counter' class='nette-collapsed'><tr>";
121:                 foreach ($explain[0] as $col => $foo) {
122:                     $s .= '<th>' . htmlSpecialChars($col) . '</th>';
123:                 }
124:                 $s .= "</tr>";
125:                 foreach ($explain as $row) {
126:                     $s .= "<tr>";
127:                     foreach ($row as $col) {
128:                         $s .= '<td>' . htmlSpecialChars($col) . '</td>';
129:                     }
130:                     $s .= "</tr>";
131:                 }
132:                 $s .= "</table>";
133:             }
134:             if ($source) {
135:                 $s .= DebugHelpers::editorLink($source[0], $source[1])->class('nette-DbConnectionPanel-source');
136:             }
137: 
138:             $s .= '</td><td>';
139:             foreach ($params as $param) {
140:                 $s .= Debugger::dump($param, TRUE);
141:             }
142: 
143:             $s .= '</td><td>' . $rows . '</td></tr>';
144:         }
145: 
146:         return $this->count ?
147:             '<style class="nette-debug"> #nette-debug td.nette-DbConnectionPanel-sql { background: white !important }
148:             #nette-debug .nette-DbConnectionPanel-source { color: #BBB !important } </style>
149:             <h1 title="' . htmlSpecialChars($connection->getDsn()) . '">Queries: ' . $this->count
150:             . ($this->totalTime ? ', time: ' . sprintf('%0.3f', $this->totalTime * 1000) . ' ms' : '') . ', ' . htmlSpecialChars($this->name) . '</h1>
151:             <div class="nette-inner nette-DbConnectionPanel">
152:             <table>
153:                 <tr><th>Time&nbsp;ms</th><th>SQL Statement</th><th>Params</th><th>Rows</th></tr>' . $s . '
154:             </table>'
155:             . (count($this->queries) < $this->count ? '<p>...and more</p>' : '')
156:             . '</div>' : '';
157:     }
158: 
159: }
160: 
Nette Framework 2.0.18 (for PHP 5.2, un-prefixed) API documentation generated by ApiGen 2.8.0