Index: branches/5.2.x/core/kernel/application.php
===================================================================
--- branches/5.2.x/core/kernel/application.php
+++ branches/5.2.x/core/kernel/application.php
@@ -1239,6 +1239,7 @@
 	{
 		global $start;
 
+		$this->Conn->noDebuggingState = true;
 		$script_time = microtime(true) - $start;
 		$query_statistics = $this->Conn->getQueryStatistics(); // time & count
 
@@ -1267,6 +1268,8 @@
 			$data['LastHit'] = adodb_mktime();
 			$this->Conn->doInsert($data, TABLE_PREFIX . 'StatisticsCapture');
 		}
+
+		$this->Conn->noDebuggingState = false;
 	}
 
 	/**
@@ -1301,12 +1304,13 @@
 	 */
 	public function logSlowQuery($slow_sql, $time)
 	{
+		$this->Conn->noDebuggingState = true;
 		$query_crc = kUtil::crc32($slow_sql);
 
 		$sql = 'SELECT *
 				FROM ' . TABLE_PREFIX . 'SlowSqlCapture
 				WHERE QueryCrc = ' . $query_crc;
-		$data = $this->Conn->Query($sql, null, true);
+		$data = $this->Conn->Query($sql);
 
 		if ( $data ) {
 			$data = array_shift($data); // Because "Query" method (supports $no_debug) is used instead of "GetRow".
@@ -1332,6 +1336,8 @@
 
 			$this->Conn->doInsert($data, TABLE_PREFIX . 'SlowSqlCapture');
 		}
+
+		$this->Conn->noDebuggingState = false;
 	}
 
 	/**
Index: branches/5.2.x/core/kernel/db/db_connection.php
===================================================================
--- branches/5.2.x/core/kernel/db/db_connection.php
+++ branches/5.2.x/core/kernel/db/db_connection.php
@@ -135,6 +135,13 @@
 		public $nextQueryCachable = false;
 
 		/**
+		 * The "no debugging" state of the SQL queries.
+		 *
+		 * @var boolean
+		 */
+		public $noDebuggingState = false;
+
+		/**
 		 * For backwards compatibility with kDBLoadBalancer class
 		 *
 		 * @var bool
@@ -320,14 +327,18 @@
 		 *
 		 * @param string $sql
 		 * @param string $key_field
-		 * @param bool $no_debug
+		 * @param boolean|null $no_debug
 		 * @return bool
 		 * @access protected
 		 */
-		protected function showError($sql = '', $key_field = null, $no_debug = false)
+		protected function showError($sql = '', $key_field = null, $no_debug = null)
 		{
 			static $retry_count = 0;
 
+			if ( $no_debug === null ) {
+				$no_debug = $this->noDebuggingState;
+			}
+
 			if ( !is_object($this->connectionID) ) {
 				// no connection while doing mysql_query
 				$this->errorCode = mysqli_connect_errno();
@@ -500,13 +511,20 @@
 		 *
 		 * @param string $sql
 		 * @param string $key_field
-		 * @param bool $no_debug
+		 * @param boolean|null $no_debug
 		 * @return Array
 		 * @access public
 		 */
-		public function Query($sql, $key_field = null, $no_debug = false)
+		public function Query($sql, $key_field = null, $no_debug = null)
 		{
-			$this->_queryCount++;
+			if ( $no_debug === null ) {
+				$no_debug = $this->noDebuggingState;
+			}
+
+			if ( !$no_debug ) {
+				$this->_queryCount++;
+			}
+
 			$this->lastQuery = $sql;
 
 			// set 1st checkpoint: begin
@@ -530,11 +548,13 @@
 					}
 				}
 
+				$this->Destroy();
+
 				// set 2nd checkpoint: begin
 				if ( $this->_captureStatistics ) {
 					$query_time = microtime(true) - $start_time;
 
-					if ( $query_time > DBG_MAX_SQL_TIME ) {
+					if ( $query_time > DBG_MAX_SQL_TIME && !$no_debug ) {
 						$this->Application->logSlowQuery($sql, $query_time);
 					}
 
@@ -542,8 +562,6 @@
 				}
 				// set 2nd checkpoint: end
 
-				$this->Destroy();
-
 				return $ret;
 			}
 			else {
@@ -565,14 +583,21 @@
 		 *
 		 * @param string $sql
 		 * @param string $key_field
-		 * @param bool   $no_debug
+		 * @param boolean|null   $no_debug
 		 * @param string $iterator_class
 		 * @return kMySQLQuery|bool
 		 * @access public
 		 */
-		public function GetIterator($sql, $key_field = null, $no_debug = false, $iterator_class = 'kMySQLQuery')
+		public function GetIterator($sql, $key_field = null, $no_debug = null, $iterator_class = 'kMySQLQuery')
 		{
-			$this->_queryCount++;
+			if ( $no_debug === null ) {
+				$no_debug = $this->noDebuggingState;
+			}
+
+			if ( !$no_debug ) {
+				$this->_queryCount++;
+			}
+
 			$this->lastQuery = $sql;
 
 			// set 1st checkpoint: begin
@@ -590,7 +615,7 @@
 				if ( $this->_captureStatistics ) {
 					$query_time = microtime(true) - $start_time;
 
-					if ( $query_time > DBG_MAX_SQL_TIME ) {
+					if ( $query_time > DBG_MAX_SQL_TIME && !$no_debug ) {
 						$this->Application->logSlowQuery($sql, $query_time);
 					}
 
@@ -968,12 +993,16 @@
 	 *
 	 * @param string $sql
 	 * @param string $key_field
-	 * @param bool $no_debug
+	 * @param boolean|null $no_debug
 	 * @return Array
 	 * @access public
 	 */
-	public function Query($sql, $key_field = null, $no_debug = false)
+	public function Query($sql, $key_field = null, $no_debug = null)
 	{
+		if ( $no_debug === null ) {
+			$no_debug = $this->noDebuggingState;
+		}
+
 		if ( $no_debug ) {
 			return parent::Query($sql, $key_field, $no_debug);
 		}
@@ -1047,15 +1076,19 @@
 	 *
 	 * @param string $sql
 	 * @param string $key_field
-	 * @param bool   $no_debug
+	 * @param boolean|null   $no_debug
 	 * @param string $iterator_class
 	 * @return kMySQLQuery|bool
 	 * @access public
 	 */
-	public function GetIterator($sql, $key_field = null, $no_debug = false, $iterator_class = 'kMySQLQuery')
+	public function GetIterator($sql, $key_field = null, $no_debug = null, $iterator_class = 'kMySQLQuery')
 	{
+		if ( $no_debug === null ) {
+			$no_debug = $this->noDebuggingState;
+		}
+
 		if ( $no_debug ) {
-			return parent::Query($sql, $key_field, $no_debug, $iterator_class);
+			return parent::GetIterator($sql, $key_field, $no_debug, $iterator_class);
 		}
 
 		global $debugger;
Index: branches/5.2.x/core/kernel/db/db_load_balancer.php
===================================================================
--- branches/5.2.x/core/kernel/db/db_load_balancer.php
+++ branches/5.2.x/core/kernel/db/db_load_balancer.php
@@ -106,6 +106,13 @@
 	public $nextQueryCachable = false;
 
 	/**
+	 * The "no debugging" tate of the SQL queries.
+	 *
+	 * @var boolean
+	 */
+	public $noDebuggingState = false;
+
+	/**
 	 * Indicates, that next query should be sent to maser database
 	 *
 	 * @var bool
@@ -442,9 +449,13 @@
 			}
 		}
 
-		if ( $this->nextQueryCachable && is_object($conn) ) {
-			$conn->nextQueryCachable = true;
-			$this->nextQueryCachable = false;
+		if ( is_object($conn) ) {
+			$conn->noDebuggingState = $this->noDebuggingState;
+
+			if ( $this->nextQueryCachable ) {
+				$conn->nextQueryCachable = true;
+				$this->nextQueryCachable = false;
+			}
 		}
 
 		return $conn;
@@ -569,11 +580,11 @@
 	 *
 	 * @param string $sql
 	 * @param string $key_field
-	 * @param bool $no_debug
+	 * @param boolean|null $no_debug
 	 * @return Array
 	 * @access public
 	 */
-	public function Query($sql, $key_field = null, $no_debug = false)
+	public function Query($sql, $key_field = null, $no_debug = null)
 	{
 		$conn =& $this->chooseConnection($sql);
 
@@ -588,12 +599,12 @@
 	 *
 	 * @param string $sql
 	 * @param string $key_field
-	 * @param bool   $no_debug
+	 * @param boolean|null   $no_debug
 	 * @param string $iterator_class
 	 * @return kMySQLQuery|bool
 	 * @access public
 	 */
-	public function GetIterator($sql, $key_field = null, $no_debug = false, $iterator_class = 'kMySQLQuery')
+	public function GetIterator($sql, $key_field = null, $no_debug = null, $iterator_class = 'kMySQLQuery')
 	{
 		$conn =& $this->chooseConnection($sql);
 
Index: branches/5.2.x/core/kernel/db/i_db_connection.php
===================================================================
--- branches/5.2.x/core/kernel/db/i_db_connection.php
+++ branches/5.2.x/core/kernel/db/i_db_connection.php
@@ -95,11 +95,11 @@
 		 *
 		 * @param string $sql
 		 * @param string $key_field
-		 * @param bool $no_debug
+		 * @param boolean|null $no_debug
 		 * @return Array
 		 * @access public
 		 */
-		public function Query($sql, $key_field = null, $no_debug = false);
+		public function Query($sql, $key_field = null, $no_debug = null);
 
 		/**
 		 * Returns iterator to a recordset, produced from running $sql query.
@@ -109,12 +109,12 @@
 		 *
 		 * @param string $sql
 		 * @param string $key_field
-		 * @param bool   $no_debug
+		 * @param boolean|null   $no_debug
 		 * @param string $iterator_class
 		 * @return kMySQLQuery|bool
 		 * @access public
 		 */
-		public function GetIterator($sql, $key_field = null, $no_debug = false, $iterator_class = 'kMySQLQuery');
+		public function GetIterator($sql, $key_field = null, $no_debug = null, $iterator_class = 'kMySQLQuery');
 
 		/**
 		 * Free memory used to hold recordset handle.