From 182703894adcd8cb50a4e3a7142ecc18a745f0d8 Mon Sep 17 00:00:00 2001
From: Robert Sesek <rsesek@bluestatic.org>
Date: Wed, 20 Dec 2006 00:48:08 +0000
Subject: [PATCH] Adding Db::_formatBacktrace() and Db::getHistory()

---
 Db.php | 98 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 77 insertions(+), 21 deletions(-)

diff --git a/Db.php b/Db.php
index 2877d9a..7431953 100644
--- a/Db.php
+++ b/Db.php
@@ -87,6 +87,17 @@ abstract class BSDb
 	*/
 	protected $history = array();
 	
+	// ###################################################################
+	/**
+	* Returns the history information array
+	*
+	* @return	array	History record
+	*/
+	public function getHistory()
+	{
+		return $this->history;
+	}
+	
 	// ###################################################################
 	/**
 	* Connect to a the specified database
@@ -161,7 +172,7 @@ abstract class BSDb
 			$this->_error('Invalid SQL query');
 		}
 		
-		$this->history[] = $history = array('query' => $string, 'time' => BSFunctions::FetchMicrotimeDiff($time), 'trace' => $this->registry->format_debug_trace(debug_backtrace()));
+		$this->history[] = $history = array('query' => $string, 'time' => BSFunctions::FetchMicrotimeDiff($time), 'trace' => $this->_formatBacktrace(debug_backtrace()));
 		
 		if (defined('ISSO_SHOW_QUERIES_LIVE'))
 		{
@@ -442,26 +453,6 @@ abstract class BSDb
 	*/
 	public abstract function transactionCommit();
 	
-	// ###################################################################
-	/**
-	* Constructs a table of query information output that is used in some
-	* other modules to display a list of queries. This merely formats
-	* a DB->history array entry nicely
-	*
-	* @param	array	An entry from DB->history
-	*
-	* @return	string	A formatted table block
-	*/
-	protected function _constructDebugQuery($query)
-	{
-		$block = "<strong>Query:</strong>\n\n<div>" . htmlspecialchars($query['query']) . "</div>\n";
-		$block .= "<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
-		$block .= "<strong>Time:</strong> $query[time]<br />\n\t\t<br />\n\t\t";
-		$block .= "<strong>Backtrace:</strong>\n\t\t<div>" . implode("<br />\n", $query['trace']) . "</div>\n\t</td>\n</tr>";
-		
-		return BSRegister::Message('Query Debug', $block, 1, true, false, 0);
-	}
-	
 	// ###################################################################
 	/**
 	* Error wrapper for ISSO->message()
@@ -492,6 +483,71 @@ abstract class BSDb
 			exit;
 		}
 	}
+	
+	// ###################################################################
+	/**
+	* A backtrace formatter.
+	*
+	* This is very slightly modified from PEAR/PHP_Compat (PHP license)
+	*
+	* @author	Laurent Laville <pear@laurent-laville.org>
+	* @author	Aidan Lister <aidan@php.net>
+	*
+	* @param	array	The backtrace from debug_backtrace() to format
+	*
+	* @return	string	Formatted output
+	*/
+	private function _formatBacktrace($backtrace)
+	{
+		// Unset call to debug_print_backtrace
+		array_shift($backtrace);
+		if (empty($backtrace))
+		{
+			return '';
+		}
+
+		// Iterate backtrace
+		$calls = array();
+		foreach ($backtrace AS $i => $call)
+		{
+			if (!isset($call['file']))
+			{
+				$call['file'] = '(null)';
+			}
+			if (!isset($call['line']))
+			{
+				$call['line'] = '0';
+			}
+			$location = $call['file'] . ':' . $call['line'];
+			$function = (isset($call['class'])) ? call['class'] . (isset($call['type']) ? $call['type'] : '.') . $call['function'] : $call['function'];
+			
+			$params = '';
+			if (isset($call['args']))
+			{
+				$args = array();
+				foreach ($call['args'] AS $arg)
+				{
+					if (is_array($arg))
+					{
+						$args[] = print_r($arg, true);
+					}
+					elseif (is_object($arg))
+					{
+						$args[] = get_class($arg);
+					}
+					else
+					{
+						$args[] = $arg;
+					}
+				}
+				$params = implode(', ', $args);
+			}
+
+			$calls[] = sprintf('#%d	 %s(%s) called at [%s]', $i, $function, $params, $location);
+		}
+
+		return implode("\n", $calls), "\n";
+	}
 }
 
 /*=====================================================================*\
-- 
2.43.5