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

  • NDatabasePanel
  • Overview
  • Package
  • 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:  * @package Nette\Database\Diagnostics
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Debug panel for NDatabase.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Database\Diagnostics
 20:  */
 21: class NDatabasePanel extends NObject 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(NStatement $result, array $params = NULL)
 44:     {
 45:         if ($this->disabled) {
 46:             return;
 47:         }
 48:         $source = NULL;
 49:         foreach (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 && isset($e->queryString)) {
 66:             return array(
 67:                 'tab' => 'SQL',
 68:                 'panel' => NDatabaseHelpers::dumpSql($e->queryString),
 69:             );
 70:         }
 71:     }
 72: 
 73: 
 74: 
 75:     public function getTab()
 76:     {
 77:         return '<span title="Nette\\Database ' . htmlSpecialChars($this->name) . '">'
 78:             . '<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" />'
 79:             . count($this->queries) . ' queries'
 80:             . ($this->totalTime ? ' / ' . sprintf('%0.1f', $this->totalTime * 1000) . 'ms' : '')
 81:             . '</span>';
 82:     }
 83: 
 84: 
 85: 
 86:     public function getPanel()
 87:     {
 88:         $this->disabled = TRUE;
 89:         $s = '';
 90:         $h = 'htmlSpecialChars';
 91:         foreach ($this->queries as $i => $query) {
 92:             list($sql, $params, $time, $rows, $connection, $source) = $query;
 93: 
 94:             $explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS()
 95:             if ($this->explain && preg_match('#\s*\(?\s*SELECT\s#iA', $sql)) {
 96:                 try {
 97:                     $cmd = is_string($this->explain) ? $this->explain : 'EXPLAIN';
 98:                     $explain = $connection->queryArgs("$cmd $sql", $params)->fetchAll();
 99:                 } catch (PDOException $e) {}
100:             }
101: 
102:             $s .= '<tr><td>' . sprintf('%0.3f', $time * 1000);
103:             if ($explain) {
104:                 static $counter;
105:                 $counter++;
106:                 $s .= "<br /><a href='#' class='nette-toggler' rel='#nette-DbConnectionPanel-row-$counter'>explain&nbsp;&#x25ba;</a>";
107:             }
108: 
109:             $s .= '</td><td class="nette-DbConnectionPanel-sql">' . NDatabaseHelpers::dumpSql(self::$maxLength ? NStrings::truncate($sql, self::$maxLength) : $sql);
110:             if ($explain) {
111:                 $s .= "<table id='nette-DbConnectionPanel-row-$counter' class='nette-collapsed'><tr>";
112:                 foreach ($explain[0] as $col => $foo) {
113:                     $s .= "<th>{$h($col)}</th>";
114:                 }
115:                 $s .= "</tr>";
116:                 foreach ($explain as $row) {
117:                     $s .= "<tr>";
118:                     foreach ($row as $col) {
119:                         $s .= "<td>{$h($col)}</td>";
120:                     }
121:                     $s .= "</tr>";
122:                 }
123:                 $s .= "</table>";
124:             }
125:             if ($source) {
126:                 $s .= NDebugHelpers::editorLink($source[0], $source[1])->class('nette-DbConnectionPanel-source');
127:             }
128: 
129:             $s .= '</td><td>';
130:             foreach ($params as $param) {
131:                 $s .= NDebugger::dump($param, TRUE);
132:             }
133: 
134:             $s .= '</td><td>' . $rows . '</td></tr>';
135:         }
136: 
137:         return empty($this->queries) ? '' :
138:             '<style> #nette-debug td.nette-DbConnectionPanel-sql { background: white !important }
139:             #nette-debug .nette-DbConnectionPanel-source { color: #BBB !important } </style>
140:             <h1>Queries: ' . count($this->queries) . ($this->totalTime ? ', time: ' . sprintf('%0.3f', $this->totalTime * 1000) . ' ms' : '') . '</h1>
141:             <div class="nette-inner nette-DbConnectionPanel">
142:             <table>
143:                 <tr><th>Time&nbsp;ms</th><th>SQL Statement</th><th>Params</th><th>Rows</th></tr>' . $s . '
144:             </table>
145:             </div>';
146:     }
147: 
148: }
149: 
Nette Framework 2.0.0 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0