Index: branches/RC/core/kernel/db/db_connection.php =================================================================== diff -u -N -r10491 -r10494 --- branches/RC/core/kernel/db/db_connection.php (.../db_connection.php) (revision 10491) +++ branches/RC/core/kernel/db/db_connection.php (.../db_connection.php) (revision 10494) @@ -7,6 +7,14 @@ class kDBConnection { /** + * Holds reference to global KernelApplication instance + * @access public + * @var kApplication + */ + var $Application; + + + /** * Current database type * * @var string @@ -72,6 +80,13 @@ var $debugMode = false; /** + * Save query execution statistics + * + * @var bool + */ + var $_captureStatistics = false; + + /** * Last query to database * * @var string @@ -85,7 +100,12 @@ */ var $_queryCount = 0; -// var $_queryLog = Array (); + /** + * Total time, used for serving queries + * + * @var Array + */ + var $_queryTime = 0; /** * Initializes connection class with @@ -97,6 +117,8 @@ */ function kDBConnection($dbType, $errorHandler = '') { + $this->Application =& kApplication::Instance(); + $this->dbType = $dbType; // $this->initMetaFunctions(); if (!$errorHandler) { @@ -105,6 +127,8 @@ else { $this->errorHandler = $errorHandler; } + + $this->_captureStatistics = defined('DBG_CAPTURE_STATISTICS') && DBG_CAPTURE_STATISTICS && !(defined('ADMIN') && ADMIN); } /** @@ -352,17 +376,17 @@ $this->_queryCount++; } - /*if (!array_key_exists($sql, $this->_queryLog)) { - $this->_queryLog[$sql] = 1; - } - else { - $this->_queryLog[$sql]++; - }*/ - if ($this->debugMode && !$no_debug) { return $this->debugQuery($sql,$key_field); } $query_func = $this->getMetaFunction('query'); + + // set 1st checkpoint: begin + if ($this->_captureStatistics) { + $start_time = getmicrotime(); + } + // set 1st checkpoint: end + $this->queryID = $query_func($sql,$this->connectionID); if (is_resource($this->queryID)) { $ret = Array(); @@ -377,9 +401,27 @@ $ret[] = $row; } } + + // set 2nd checkpoint: begin + if ($this->_captureStatistics) { + $query_time = getmicrotime() - $start_time; + if ($query_time > DBG_MAX_SQL_TIME && !$no_debug) { + $this->Application->logSlowQuery($sql, $query_time); + } + $this->_queryTime += $query_time; + } + // set 2nd checkpoint: end + $this->Destroy(); return $ret; } + else { + // set 2nd checkpoint: begin + if ($this->_captureStatistics) { + $this->_queryTime += getmicrotime() - $start_time; + } + // set 2nd checkpoint: end + } $this->showError($sql); return false; } @@ -423,14 +465,16 @@ $ret[] = $row; } } + // set 2nd checkpoint: begin $first_cell = count($ret) == 1 && count(current($ret)) == 1 ? current(current($ret)) : null; if ($profileSQLs) { $debugger->profileFinish('sql_'.$queryID, null, null, $this->getAffectedRows(), $first_cell, $this->_queryCount); $debugger->profilerAddTotal('sql', 'sql_'.$queryID); } - $this->Destroy(); // set 2nd checkpoint: end + + $this->Destroy(); return $ret; } else { @@ -617,5 +661,15 @@ return $table_found[$table_name]; } + + /** + * Returns query processing statistics + * + * @return Array + */ + function getQueryStatistics() + { + return Array ('time' => $this->_queryTime, 'count' => $this->_queryCount); + } } ?> \ No newline at end of file