diff --git a/core/database.class.php b/core/database.class.php index f0480699..637e7c69 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -413,6 +413,11 @@ class Database { */ public $transaction = false; + /** + * How many queries this DB object has run + */ + public $query_count = 0; + /** * For now, only connect to the cache, as we will pretty much certainly * need it. There are some pages where all the data is in cache, so the @@ -545,6 +550,25 @@ class Database { return $this->engine->name; } + private function count_execs($db, $sql, $inputarray) { + if ((defined('DEBUG_SQL') && DEBUG_SQL === true) || (!defined('DEBUG_SQL') && @$_GET['DEBUG_SQL'])) { + $fp = @fopen("data/sql.log", "a"); + if($fp) { + if(isset($inputarray) && is_array($inputarray)) { + fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)." -- ".join(", ", $inputarray)."\n"); + } + else { + fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)."\n"); + } + fclose($fp); + } + } + if(!is_array($inputarray)) $this->query_count++; + # handle 2-dimensional input arrays + else if(is_array(reset($inputarray))) $this->query_count += sizeof($inputarray); + else $this->query_count++; + } + /** * Execute an SQL query and return an PDO result-set. * @@ -556,7 +580,7 @@ class Database { public function execute($query, $args=array()) { try { if(is_null($this->db)) $this->connect_db(); - _count_execs($this->db, $query, $args); + $this->count_execs($this->db, $query, $args); $stmt = $this->db->prepare($query); if (!array_key_exists(0, $args)) { foreach($args as $name=>$value) { diff --git a/core/util.inc.php b/core/util.inc.php index 3a37bbe0..51cf99bb 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -699,44 +699,6 @@ function getExtension ($mime_type){ return ($ext ? $ext : false); } - -$_shm_query_count = 0; -/** - * $db is the connection object - * - * @private - */ -function _count_execs($db, $sql, $inputarray) { - global $_shm_query_count; - if ((defined('DEBUG_SQL') && DEBUG_SQL === true) || (!defined('DEBUG_SQL') && @$_GET['DEBUG_SQL'])) { - $fp = @fopen("data/sql.log", "a"); - if($fp) { - if(isset($inputarray) && is_array($inputarray)) { - fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)." -- ".join(", ", $inputarray)."\n"); - } - else { - fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)."\n"); - } - fclose($fp); - } - else { - # WARNING: - # SQL queries happen before the event system is fully initialised - # (eg, "select theme from config" happens before "load themes"), - # so using the event system to report an error will create some - # really weird looking bugs. - # - #log_error("core", "failed to open sql.log for appending"); - } - } - if (!is_array($inputarray)) $_shm_query_count++; - # handle 2-dimensional input arrays - else if (is_array(reset($inputarray))) $_shm_query_count += sizeof($inputarray); - else $_shm_query_count++; - # in PHP4.4 and PHP5, we need to return a value by reference - $null = null; return $null; -} - /** * Compare two Block objects, used to sort them before being displayed * @@ -1501,7 +1463,7 @@ $_shm_load_start = microtime(true); * @return string debug info to add to the page. */ function get_debug_info() { - global $config, $_shm_event_count, $database, $_shm_query_count, $_shm_load_start; + global $config, $_shm_event_count, $database, $_shm_load_start; $i_mem = sprintf("%5.2f", ((memory_get_peak_usage(true)+512)/1024)/1024); @@ -1518,7 +1480,7 @@ function get_debug_info() { $miss = $database->cache->get_misses(); $debug = "
Took $time seconds (db:$dbtime) and {$i_mem}MB of RAM"; - $debug .= "; Used $i_files files and $_shm_query_count queries"; + $debug .= "; Used $i_files files and {$database->query_count} queries"; $debug .= "; Sent $_shm_event_count events"; $debug .= "; $hits cache hits and $miss misses"; $debug .= "; Shimmie version ". VERSION . $commit; // .", SCore Version ". SCORE_VERSION; diff --git a/ext/statsd/main.php b/ext/statsd/main.php index 73306d44..1cec97af 100644 --- a/ext/statsd/main.php +++ b/ext/statsd/main.php @@ -19,14 +19,14 @@ class StatsDInterface extends Extension { public static $stats = array(); private function _stats($type) { - global $config, $_shm_event_count, $database, $_shm_query_count, $_shm_load_start; + global $config, $_shm_event_count, $database, $_shm_load_start; $time = microtime(true) - $_shm_load_start; StatsDInterface::$stats["shimmie.$type.hits"] = "1|c"; StatsDInterface::$stats["shimmie.$type.time"] = "$time|ms"; StatsDInterface::$stats["shimmie.$type.time-db"] = "{$database->dbtime}|ms"; StatsDInterface::$stats["shimmie.$type.memory"] = memory_get_peak_usage(true)."|c"; StatsDInterface::$stats["shimmie.$type.files"] = count(get_included_files())."|c"; - StatsDInterface::$stats["shimmie.$type.queries"] = $_shm_query_count."|c"; + StatsDInterface::$stats["shimmie.$type.queries"] = $database->query_count."|c"; StatsDInterface::$stats["shimmie.$type.events"] = $_shm_event_count."|c"; StatsDInterface::$stats["shimmie.$type.cache-hits"] = $database->cache->get_hits()."|c"; StatsDInterface::$stats["shimmie.$type.cache-misses"] = $database->cache->get_misses()."|c";