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