Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • ConnectionPanel
  • Overview
  • Namespace
  • 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 (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Bridges\DatabaseTracy;
  9: 
 10: use Nette;
 11: use Nette\Database\Helpers;
 12: use Tracy;
 13: 
 14: 
 15: /**
 16:  * Debug panel for Nette\Database.
 17:  */
 18: class ConnectionPanel extends Nette\Object implements Tracy\IBarPanel
 19: {
 20:     /** @var int */
 21:     public $maxQueries = 100;
 22: 
 23:     /** @var int logged time */
 24:     private $totalTime = 0;
 25: 
 26:     /** @var int */
 27:     private $count = 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 __construct(Nette\Database\Connection $connection)
 43:     {
 44:         $connection->onQuery[] = array($this, 'logQuery');
 45:     }
 46: 
 47: 
 48:     public function logQuery(Nette\Database\Connection $connection, $result)
 49:     {
 50:         if ($this->disabled) {
 51:             return;
 52:         }
 53:         $this->count++;
 54: 
 55:         $source = NULL;
 56:         $trace = $result instanceof \PDOException ? $result->getTrace() : debug_backtrace(PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_IGNORE_ARGS : FALSE);
 57:         foreach ($trace as $row) {
 58:             if (isset($row['file']) && is_file($row['file']) && !Tracy\Debugger::getBluescreen()->isCollapsed($row['file'])) {
 59:                 if ((isset($row['function']) && strpos($row['function'], 'call_user_func') === 0)
 60:                     || (isset($row['class']) && is_subclass_of($row['class'], '\\Nette\\Database\\Connection'))
 61:                 ) {
 62:                     continue;
 63:                 }
 64:                 $source = array($row['file'], (int) $row['line']);
 65:                 break;
 66:             }
 67:         }
 68:         if ($result instanceof Nette\Database\ResultSet) {
 69:             $this->totalTime += $result->getTime();
 70:             if ($this->count < $this->maxQueries) {
 71:                 $this->queries[] = array($connection, $result->getQueryString(), $result->getParameters(), $source, $result->getTime(), $result->getRowCount(), NULL);
 72:             }
 73: 
 74:         } elseif ($result instanceof \PDOException && $this->count < $this->maxQueries) {
 75:             $this->queries[] = array($connection, $result->queryString, NULL, $source, NULL, NULL, $result->getMessage());
 76:         }
 77:     }
 78: 
 79: 
 80:     public static function renderException($e)
 81:     {
 82:         if (!$e instanceof \PDOException) {
 83:             return;
 84:         }
 85:         if (isset($e->queryString)) {
 86:             $sql = $e->queryString;
 87: 
 88:         } elseif ($item = Tracy\Helpers::findTrace($e->getTrace(), 'PDO::prepare')) {
 89:             $sql = $item['args'][0];
 90:         }
 91:         return isset($sql) ? array(
 92:             'tab' => 'SQL',
 93:             'panel' => Helpers::dumpSql($sql),
 94:         ) : NULL;
 95:     }
 96: 
 97: 
 98:     public function getTab()
 99:     {
100:         $name = $this->name;
101:         $count = $this->count;
102:         $totalTime = $this->totalTime;
103:         ob_start(function () {});
104:         require __DIR__ . '/templates/ConnectionPanel.tab.phtml';
105:         return ob_get_clean();
106:     }
107: 
108: 
109:     public function getPanel()
110:     {
111:         $this->disabled = TRUE;
112:         if (!$this->count) {
113:             return;
114:         }
115: 
116:         $name = $this->name;
117:         $count = $this->count;
118:         $totalTime = $this->totalTime;
119:         $queries = array();
120:         foreach ($this->queries as $query) {
121:             list($connection, $sql, $params, $source, $time, $rows, $error) = $query;
122:             $explain = NULL;
123:             if (!$error && $this->explain && preg_match('#\s*\(?\s*SELECT\s#iA', $sql)) {
124:                 try {
125:                     $cmd = is_string($this->explain) ? $this->explain : 'EXPLAIN';
126:                     $explain = $connection->queryArgs("$cmd $sql", $params)->fetchAll();
127:                 } catch (\PDOException $e) {
128:                 }
129:             }
130:             $query[] = $explain;
131:             $queries[] = $query;
132:         }
133: 
134:         ob_start(function () {});
135:         require __DIR__ . '/templates/ConnectionPanel.panel.phtml';
136:         return ob_get_clean();
137:     }
138: 
139: }
140: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0