Blame Identity/Webenv/phpBB/3.0.4/includes/db/dbal.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package dbal
ef5584
* @version $Id: dbal.php 9178 2008-12-06 11:11:10Z acydburn $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
* Database Abstraction Layer
ef5584
* @package dbal
ef5584
*/
ef5584
class dbal
ef5584
{
ef5584
	var $db_connect_id;
ef5584
	var $query_result;
ef5584
	var $return_on_error = false;
ef5584
	var $transaction = false;
ef5584
	var $sql_time = 0;
ef5584
	var $num_queries = array();
ef5584
	var $open_queries = array();
ef5584
ef5584
	var $curtime = 0;
ef5584
	var $query_hold = '';
ef5584
	var $html_hold = '';
ef5584
	var $sql_report = '';
ef5584
ef5584
	var $persistency = false;
ef5584
	var $user = '';
ef5584
	var $server = '';
ef5584
	var $dbname = '';
ef5584
ef5584
	// Set to true if error triggered
ef5584
	var $sql_error_triggered = false;
ef5584
ef5584
	// Holding the last sql query on sql error
ef5584
	var $sql_error_sql = '';
ef5584
	// Holding the error information - only populated if sql_error_triggered is set
ef5584
	var $sql_error_returned = array();
ef5584
ef5584
	// Holding transaction count
ef5584
	var $transactions = 0;
ef5584
ef5584
	// Supports multi inserts?
ef5584
	var $multi_insert = false;
ef5584
ef5584
	/**
ef5584
	* Current sql layer
ef5584
	*/
ef5584
	var $sql_layer = '';
ef5584
ef5584
	/**
ef5584
	* Wildcards for matching any (%) or exactly one (_) character within LIKE expressions
ef5584
	*/
ef5584
	var $any_char;
ef5584
	var $one_char;
ef5584
ef5584
	/**
ef5584
	* Exact version of the DBAL, directly queried
ef5584
	*/
ef5584
	var $sql_server_version = false;
ef5584
ef5584
	/**
ef5584
	* Constructor
ef5584
	*/
ef5584
	function dbal()
ef5584
	{
ef5584
		$this->num_queries = array(
ef5584
			'cached'		=> 0,
ef5584
			'normal'		=> 0,
ef5584
			'total'			=> 0,
ef5584
		);
ef5584
ef5584
		// Fill default sql layer based on the class being called.
ef5584
		// This can be changed by the specified layer itself later if needed.
ef5584
		$this->sql_layer = substr(get_class($this), 5);
ef5584
ef5584
		// Do not change this please! This variable is used to easy the use of it - and is hardcoded.
ef5584
		$this->any_char = chr(0) . '%';
ef5584
		$this->one_char = chr(0) . '_';
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* return on error or display error message
ef5584
	*/
ef5584
	function sql_return_on_error($fail = false)
ef5584
	{
ef5584
		$this->sql_error_triggered = false;
ef5584
		$this->sql_error_sql = '';
ef5584
ef5584
		$this->return_on_error = $fail;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Return number of sql queries and cached sql queries used
ef5584
	*/
ef5584
	function sql_num_queries($cached = false)
ef5584
	{
ef5584
		return ($cached) ? $this->num_queries['cached'] : $this->num_queries['normal'];
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Add to query count
ef5584
	*/
ef5584
	function sql_add_num_queries($cached = false)
ef5584
	{
ef5584
		$this->num_queries['cached'] += ($cached !== false) ? 1 : 0;
ef5584
		$this->num_queries['normal'] += ($cached !== false) ? 0 : 1;
ef5584
		$this->num_queries['total'] += 1;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* DBAL garbage collection, close sql connection
ef5584
	*/
ef5584
	function sql_close()
ef5584
	{
ef5584
		if (!$this->db_connect_id)
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		if ($this->transaction)
ef5584
		{
ef5584
			do
ef5584
			{
ef5584
				$this->sql_transaction('commit');
ef5584
			}
ef5584
			while ($this->transaction);
ef5584
		}
ef5584
ef5584
		foreach ($this->open_queries as $query_id)
ef5584
		{
ef5584
			$this->sql_freeresult($query_id);
ef5584
		}
ef5584
ef5584
		// Connection closed correctly. Set db_connect_id to false to prevent errors
ef5584
		if ($result = $this->_sql_close())
ef5584
		{
ef5584
			$this->db_connect_id = false;
ef5584
		}
ef5584
ef5584
		return $result;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build LIMIT query
ef5584
	* Doing some validation here.
ef5584
	*/
ef5584
	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
ef5584
	{
ef5584
		if (empty($query))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		// Never use a negative total or offset
ef5584
		$total = ($total < 0) ? 0 : $total;
ef5584
		$offset = ($offset < 0) ? 0 : $offset;
ef5584
ef5584
		return $this->_sql_query_limit($query, $total, $offset, $cache_ttl);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Fetch all rows
ef5584
	*/
ef5584
	function sql_fetchrowset($query_id = false)
ef5584
	{
ef5584
		if ($query_id === false)
ef5584
		{
ef5584
			$query_id = $this->query_result;
ef5584
		}
ef5584
ef5584
		if ($query_id !== false)
ef5584
		{
ef5584
			$result = array();
ef5584
			while ($row = $this->sql_fetchrow($query_id))
ef5584
			{
ef5584
				$result[] = $row;
ef5584
			}
ef5584
ef5584
			return $result;
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Fetch field
ef5584
	* if rownum is false, the current row is used, else it is pointing to the row (zero-based)
ef5584
	*/
ef5584
	function sql_fetchfield($field, $rownum = false, $query_id = false)
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		if ($query_id === false)
ef5584
		{
ef5584
			$query_id = $this->query_result;
ef5584
		}
ef5584
ef5584
		if ($query_id !== false)
ef5584
		{
ef5584
			if ($rownum !== false)
ef5584
			{
ef5584
				$this->sql_rowseek($rownum, $query_id);
ef5584
			}
ef5584
ef5584
			if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
ef5584
			{
ef5584
				return $cache->sql_fetchfield($query_id, $field);
ef5584
			}
ef5584
ef5584
			$row = $this->sql_fetchrow($query_id);
ef5584
			return (isset($row[$field])) ? $row[$field] : false;
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Correctly adjust LIKE expression for special characters
ef5584
	* Some DBMS are handling them in a different way
ef5584
	*
ef5584
	* @param string $expression The expression to use. Every wildcard is escaped, except $this->any_char and $this->one_char
ef5584
	* @return string LIKE expression including the keyword!
ef5584
	*/
ef5584
	function sql_like_expression($expression)
ef5584
	{
ef5584
		$expression = str_replace(array('_', '%'), array("\_", "\%"), $expression);
ef5584
		$expression = str_replace(array(chr(0) . "\_", chr(0) . "\%"), array('_', '%'), $expression);
ef5584
ef5584
		return $this->_sql_like_expression('LIKE \'' . $this->sql_escape($expression) . '\'');
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* SQL Transaction
ef5584
	* @access private
ef5584
	*/
ef5584
	function sql_transaction($status = 'begin')
ef5584
	{
ef5584
		switch ($status)
ef5584
		{
ef5584
			case 'begin':
ef5584
				// If we are within a transaction we will not open another one, but enclose the current one to not loose data (prevening auto commit)
ef5584
				if ($this->transaction)
ef5584
				{
ef5584
					$this->transactions++;
ef5584
					return true;
ef5584
				}
ef5584
ef5584
				$result = $this->_sql_transaction('begin');
ef5584
ef5584
				if (!$result)
ef5584
				{
ef5584
					$this->sql_error();
ef5584
				}
ef5584
ef5584
				$this->transaction = true;
ef5584
			break;
ef5584
ef5584
			case 'commit':
ef5584
				// If there was a previously opened transaction we do not commit yet... but count back the number of inner transactions
ef5584
				if ($this->transaction && $this->transactions)
ef5584
				{
ef5584
					$this->transactions--;
ef5584
					return true;
ef5584
				}
ef5584
ef5584
				// Check if there is a transaction (no transaction can happen if there was an error, with a combined rollback and error returning enabled)
ef5584
				// This implies we have transaction always set for autocommit db's
ef5584
				if (!$this->transaction)
ef5584
				{
ef5584
					return false;
ef5584
				}
ef5584
ef5584
				$result = $this->_sql_transaction('commit');
ef5584
ef5584
				if (!$result)
ef5584
				{
ef5584
					$this->sql_error();
ef5584
				}
ef5584
ef5584
				$this->transaction = false;
ef5584
				$this->transactions = 0;
ef5584
			break;
ef5584
ef5584
			case 'rollback':
ef5584
				$result = $this->_sql_transaction('rollback');
ef5584
				$this->transaction = false;
ef5584
				$this->transactions = 0;
ef5584
			break;
ef5584
ef5584
			default:
ef5584
				$result = $this->_sql_transaction($status);
ef5584
			break;
ef5584
		}
ef5584
ef5584
		return $result;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build sql statement from array for insert/update/select statements
ef5584
	*
ef5584
	* Idea for this from Ikonboard
ef5584
	* Possible query values: INSERT, INSERT_SELECT, UPDATE, SELECT
ef5584
	*
ef5584
	*/
ef5584
	function sql_build_array($query, $assoc_ary = false)
ef5584
	{
ef5584
		if (!is_array($assoc_ary))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$fields = $values = array();
ef5584
ef5584
		if ($query == 'INSERT' || $query == 'INSERT_SELECT')
ef5584
		{
ef5584
			foreach ($assoc_ary as $key => $var)
ef5584
			{
ef5584
				$fields[] = $key;
ef5584
ef5584
				if (is_array($var) && is_string($var[0]))
ef5584
				{
ef5584
					// This is used for INSERT_SELECT(s)
ef5584
					$values[] = $var[0];
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$values[] = $this->_sql_validate_value($var);
ef5584
				}
ef5584
			}
ef5584
ef5584
			$query = ($query == 'INSERT') ? ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')' : ' (' . implode(', ', $fields) . ') SELECT ' . implode(', ', $values) . ' ';
ef5584
		}
ef5584
		else if ($query == 'MULTI_INSERT')
ef5584
		{
ef5584
			trigger_error('The MULTI_INSERT query value is no longer supported. Please use sql_multi_insert() instead.', E_USER_ERROR);
ef5584
		}
ef5584
		else if ($query == 'UPDATE' || $query == 'SELECT')
ef5584
		{
ef5584
			$values = array();
ef5584
			foreach ($assoc_ary as $key => $var)
ef5584
			{
ef5584
				$values[] = "$key = " . $this->_sql_validate_value($var);
ef5584
			}
ef5584
			$query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values);
ef5584
		}
ef5584
ef5584
		return $query;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build IN or NOT IN sql comparison string, uses <> or = on single element
ef5584
	* arrays to improve comparison speed
ef5584
	*
ef5584
	* @access public
ef5584
	* @param	string	$field				name of the sql column that shall be compared
ef5584
	* @param	array	$array				array of values that are allowed (IN) or not allowed (NOT IN)
ef5584
	* @param	bool	$negate				true for NOT IN (), false for IN () (default)
ef5584
	* @param	bool	$allow_empty_set	If true, allow $array to be empty, this function will return 1=1 or 1=0 then. Default to false.
ef5584
	*/
ef5584
	function sql_in_set($field, $array, $negate = false, $allow_empty_set = false)
ef5584
	{
ef5584
		if (!sizeof($array))
ef5584
		{
ef5584
			if (!$allow_empty_set)
ef5584
			{
ef5584
				// Print the backtrace to help identifying the location of the problematic code
ef5584
				$this->sql_error('No values specified for SQL IN comparison');
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				// NOT IN () actually means everything so use a tautology
ef5584
				if ($negate)
ef5584
				{
ef5584
					return '1=1';
ef5584
				}
ef5584
				// IN () actually means nothing so use a contradiction
ef5584
				else
ef5584
				{
ef5584
					return '1=0';
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		if (!is_array($array))
ef5584
		{
ef5584
			$array = array($array);
ef5584
		}
ef5584
ef5584
		if (sizeof($array) == 1)
ef5584
		{
ef5584
			@reset($array);
ef5584
			$var = current($array);
ef5584
ef5584
			return $field . ($negate ? ' <> ' : ' = ') . $this->_sql_validate_value($var);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			return $field . ($negate ? ' NOT IN ' : ' IN ') . '(' . implode(', ', array_map(array($this, '_sql_validate_value'), $array)) . ')';
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Run more than one insert statement.
ef5584
	*
ef5584
	* @param string $table table name to run the statements on
ef5584
	* @param array &$sql_ary multi-dimensional array holding the statement data.
ef5584
	*
ef5584
	* @return bool false if no statements were executed.
ef5584
	* @access public
ef5584
	*/
ef5584
	function sql_multi_insert($table, &$sql_ary)
ef5584
	{
ef5584
		if (!sizeof($sql_ary))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		if ($this->multi_insert)
ef5584
		{
ef5584
			$ary = array();
ef5584
			foreach ($sql_ary as $id => $_sql_ary)
ef5584
			{
ef5584
				// If by accident the sql array is only one-dimensional we build a normal insert statement
ef5584
				if (!is_array($_sql_ary))
ef5584
				{
ef5584
					$this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $sql_ary));
ef5584
					return true;
ef5584
				}
ef5584
ef5584
				$values = array();
ef5584
				foreach ($_sql_ary as $key => $var)
ef5584
				{
ef5584
					$values[] = $this->_sql_validate_value($var);
ef5584
				}
ef5584
				$ary[] = '(' . implode(', ', $values) . ')';
ef5584
			}
ef5584
ef5584
			$this->sql_query('INSERT INTO ' . $table . ' ' . ' (' . implode(', ', array_keys($sql_ary[0])) . ') VALUES ' . implode(', ', $ary));
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			foreach ($sql_ary as $ary)
ef5584
			{
ef5584
				if (!is_array($ary))
ef5584
				{
ef5584
					return false;
ef5584
				}
ef5584
ef5584
				$this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $ary));
ef5584
			}
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Function for validating values
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_validate_value($var)
ef5584
	{
ef5584
		if (is_null($var))
ef5584
		{
ef5584
			return 'NULL';
ef5584
		}
ef5584
		else if (is_string($var))
ef5584
		{
ef5584
			return "'" . $this->sql_escape($var) . "'";
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			return (is_bool($var)) ? intval($var) : $var;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build sql statement from array for select and select distinct statements
ef5584
	*
ef5584
	* Possible query values: SELECT, SELECT_DISTINCT
ef5584
	*/
ef5584
	function sql_build_query($query, $array)
ef5584
	{
ef5584
		$sql = '';
ef5584
		switch ($query)
ef5584
		{
ef5584
			case 'SELECT':
ef5584
			case 'SELECT_DISTINCT';
ef5584
ef5584
				$sql = str_replace('_', ' ', $query) . ' ' . $array['SELECT'] . ' FROM ';
ef5584
ef5584
				// Build table array. We also build an alias array for later checks.
ef5584
				$table_array = $aliases = array();
ef5584
				$used_multi_alias = false;
ef5584
ef5584
				foreach ($array['FROM'] as $table_name => $alias)
ef5584
				{
ef5584
					if (is_array($alias))
ef5584
					{
ef5584
						$used_multi_alias = true;
ef5584
ef5584
						foreach ($alias as $multi_alias)
ef5584
						{
ef5584
							$table_array[] = $table_name . ' ' . $multi_alias;
ef5584
							$aliases[] = $multi_alias;
ef5584
						}
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$table_array[] = $table_name . ' ' . $alias;
ef5584
						$aliases[] = $alias;
ef5584
					}
ef5584
				}
ef5584
ef5584
				// We run the following code to determine if we need to re-order the table array. ;)
ef5584
				// The reason for this is that for multi-aliased tables (two equal tables) in the FROM statement the last table need to match the first comparison.
ef5584
				// DBMS who rely on this: Oracle, PostgreSQL and MSSQL. For all other DBMS it makes absolutely no difference in which order the table is.
ef5584
				if (!empty($array['LEFT_JOIN']) && sizeof($array['FROM']) > 1 && $used_multi_alias !== false)
ef5584
				{
ef5584
					// Take first LEFT JOIN
ef5584
					$join = current($array['LEFT_JOIN']);
ef5584
ef5584
					// Determine the table used there (even if there are more than one used, we only want to have one
ef5584
					preg_match('/(' . implode('|', $aliases) . ')\.[^\s]+/U', str_replace(array('(', ')', 'AND', 'OR', ' '), '', $join['ON']), $matches);
ef5584
ef5584
					// If there is a first join match, we need to make sure the table order is correct
ef5584
					if (!empty($matches[1]))
ef5584
					{
ef5584
						$first_join_match = trim($matches[1]);
ef5584
						$table_array = $last = array();
ef5584
ef5584
						foreach ($array['FROM'] as $table_name => $alias)
ef5584
						{
ef5584
							if (is_array($alias))
ef5584
							{
ef5584
								foreach ($alias as $multi_alias)
ef5584
								{
ef5584
									($multi_alias === $first_join_match) ? $last[] = $table_name . ' ' . $multi_alias : $table_array[] = $table_name . ' ' . $multi_alias;
ef5584
								}
ef5584
							}
ef5584
							else
ef5584
							{
ef5584
								($alias === $first_join_match) ? $last[] = $table_name . ' ' . $alias : $table_array[] = $table_name . ' ' . $alias;
ef5584
							}
ef5584
						}
ef5584
ef5584
						$table_array = array_merge($table_array, $last);
ef5584
					}
ef5584
				}
ef5584
ef5584
				$sql .= $this->_sql_custom_build('FROM', implode(', ', $table_array));
ef5584
ef5584
				if (!empty($array['LEFT_JOIN']))
ef5584
				{
ef5584
					foreach ($array['LEFT_JOIN'] as $join)
ef5584
					{
ef5584
						$sql .= ' LEFT JOIN ' . key($join['FROM']) . ' ' . current($join['FROM']) . ' ON (' . $join['ON'] . ')';
ef5584
					}
ef5584
				}
ef5584
ef5584
				if (!empty($array['WHERE']))
ef5584
				{
ef5584
					$sql .= ' WHERE ' . $this->_sql_custom_build('WHERE', $array['WHERE']);
ef5584
				}
ef5584
ef5584
				if (!empty($array['GROUP_BY']))
ef5584
				{
ef5584
					$sql .= ' GROUP BY ' . $array['GROUP_BY'];
ef5584
				}
ef5584
ef5584
				if (!empty($array['ORDER_BY']))
ef5584
				{
ef5584
					$sql .= ' ORDER BY ' . $array['ORDER_BY'];
ef5584
				}
ef5584
ef5584
			break;
ef5584
		}
ef5584
ef5584
		return $sql;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* display sql error page
ef5584
	*/
ef5584
	function sql_error($sql = '')
ef5584
	{
ef5584
		global $auth, $user, $config;
ef5584
ef5584
		// Set var to retrieve errored status
ef5584
		$this->sql_error_triggered = true;
ef5584
		$this->sql_error_sql = $sql;
ef5584
ef5584
		$this->sql_error_returned = $this->_sql_error();
ef5584
ef5584
		if (!$this->return_on_error)
ef5584
		{
ef5584
			$message = 'SQL ERROR [ ' . $this->sql_layer . ' ]

' . $this->sql_error_returned['message'] . ' [' . $this->sql_error_returned['code'] . ']';
ef5584
ef5584
			// Show complete SQL error and path to administrators only
ef5584
			// Additionally show complete error on installation or if extended debug mode is enabled
ef5584
			// The DEBUG_EXTRA constant is for development only!
ef5584
			if ((isset($auth) && $auth->acl_get('a_')) || defined('IN_INSTALL') || defined('DEBUG_EXTRA'))
ef5584
			{
ef5584
				// Print out a nice backtrace...
ef5584
				$backtrace = get_backtrace();
ef5584
ef5584
				$message .= ($sql) ? '

SQL

' . htmlspecialchars($sql) : '';
ef5584
				$message .= ($backtrace) ? '

BACKTRACE
' . $backtrace : '';
ef5584
				$message .= '
';
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				// If error occurs in initiating the session we need to use a pre-defined language string
ef5584
				// This could happen if the connection could not be established for example (then we are not able to grab the default language)
ef5584
				if (!isset($user->lang['SQL_ERROR_OCCURRED']))
ef5584
				{
ef5584
					$message .= '

An sql error occurred while fetching this page. Please contact an administrator if this problem persists.';
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					if (!empty($config['board_contact']))
ef5584
					{
ef5584
						$message .= '

' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '', '');
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$message .= '

' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '', '');
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
ef5584
			if ($this->transaction)
ef5584
			{
ef5584
				$this->sql_transaction('rollback');
ef5584
			}
ef5584
ef5584
			if (strlen($message) > 1024)
ef5584
			{
ef5584
				// We need to define $msg_long_text here to circumvent text stripping.
ef5584
				global $msg_long_text;
ef5584
				$msg_long_text = $message;
ef5584
ef5584
				trigger_error(false, E_USER_ERROR);
ef5584
			}
ef5584
ef5584
			trigger_error($message, E_USER_ERROR);
ef5584
		}
ef5584
ef5584
		if ($this->transaction)
ef5584
		{
ef5584
			$this->sql_transaction('rollback');
ef5584
		}
ef5584
ef5584
		return $this->sql_error_returned;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Explain queries
ef5584
	*/
ef5584
	function sql_report($mode, $query = '')
ef5584
	{
ef5584
		global $cache, $starttime, $phpbb_root_path, $user;
ef5584
ef5584
		if (empty($_REQUEST['explain']))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		if (!$query && $this->query_hold != '')
ef5584
		{
ef5584
			$query = $this->query_hold;
ef5584
		}
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'display':
ef5584
				if (!empty($cache))
ef5584
				{
ef5584
					$cache->unload();
ef5584
				}
ef5584
				$this->sql_close();
ef5584
ef5584
				$mtime = explode(' ', microtime());
ef5584
				$totaltime = $mtime[0] + $mtime[1] - $starttime;
ef5584
ef5584
				echo '
ef5584
					<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
ef5584
					<head>
ef5584
						<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
ef5584
						<meta http-equiv="Content-Style-Type" content="text/css" />
ef5584
						<meta http-equiv="imagetoolbar" content="no" />
ef5584
						<title>SQL Report</title>
ef5584
						<link href="' . $phpbb_root_path . 'adm/style/admin.css" rel="stylesheet" type="text/css" media="screen" />
ef5584
					</head>
ef5584
					<body id="errorpage">
ef5584
					
ef5584
						
ef5584
							Return to previous page
ef5584
						
ef5584
						
ef5584
							
ef5584
							
ef5584
								
ef5584
								
ef5584
									

SQL Report

ef5584
									
ef5584
									

Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries['normal']} queries" . (($this->num_queries['cached']) ? " + {$this->num_queries['cached']} " . (($this->num_queries['cached'] == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '

ef5584
ef5584
									

Time spent on ' . $this->sql_layer . ' queries: ' . round($this->sql_time, 5) . 's | Time spent on PHP: ' . round($totaltime - $this->sql_time, 5) . 's

ef5584
ef5584
									

ef5584
									' . $this->sql_report . '
ef5584
								
ef5584
								
ef5584
							
ef5584
							
ef5584
						
ef5584
						
ef5584
							Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
ef5584
						
ef5584
					
ef5584
					</body>
ef5584
					</html>';
ef5584
ef5584
				exit_handler();
ef5584
ef5584
			break;
ef5584
ef5584
			case 'stop':
ef5584
				$endtime = explode(' ', microtime());
ef5584
				$endtime = $endtime[0] + $endtime[1];
ef5584
ef5584
				$this->sql_report .= '
ef5584
ef5584
					
ef5584
					
ef5584
					
ef5584
						Query #' . $this->num_queries['total'] . '
ef5584
					
ef5584
					
ef5584
					
ef5584
					
ef5584
						<textarea style="font-family:\'Courier New\',monospace;width:99%" rows="5" cols="10">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea>
ef5584
					
ef5584
					
ef5584
					
ef5584
ef5584
					' . $this->html_hold . '
ef5584
ef5584
					

ef5584
				';
ef5584
ef5584
				if ($this->query_result)
ef5584
				{
ef5584
					if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query))
ef5584
					{
ef5584
						$this->sql_report .= 'Affected rows: ' . $this->sql_affectedrows($this->query_result) . ' | ';
ef5584
					}
ef5584
					$this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: ' . sprintf('%.5f', $endtime - $this->curtime) . 's';
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$error = $this->sql_error();
ef5584
					$this->sql_report .= 'FAILED - ' . $this->sql_layer . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']);
ef5584
				}
ef5584
ef5584
				$this->sql_report .= '



';
ef5584
ef5584
				$this->sql_time += $endtime - $this->curtime;
ef5584
			break;
ef5584
ef5584
			case 'start':
ef5584
				$this->query_hold = $query;
ef5584
				$this->html_hold = '';
ef5584
ef5584
				$this->_sql_report($mode, $query);
ef5584
ef5584
				$this->curtime = explode(' ', microtime());
ef5584
				$this->curtime = $this->curtime[0] + $this->curtime[1];
ef5584
ef5584
			break;
ef5584
ef5584
			case 'add_select_row':
ef5584
ef5584
				$html_table = func_get_arg(2);
ef5584
				$row = func_get_arg(3);
ef5584
ef5584
				if (!$html_table && sizeof($row))
ef5584
				{
ef5584
					$html_table = true;
ef5584
					$this->html_hold .= '';
ef5584
ef5584
					foreach (array_keys($row) as $val)
ef5584
					{
ef5584
						$this->html_hold .= '' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '';
ef5584
					}
ef5584
					$this->html_hold .= '';
ef5584
				}
ef5584
				$this->html_hold .= '';
ef5584
ef5584
				$class = 'row1';
ef5584
				foreach (array_values($row) as $val)
ef5584
				{
ef5584
					$class = ($class == 'row1') ? 'row2' : 'row1';
ef5584
					$this->html_hold .= '' . (($val) ? $val : ' ') . '';
ef5584
				}
ef5584
				$this->html_hold .= '';
ef5584
ef5584
				return $html_table;
ef5584
ef5584
			break;
ef5584
ef5584
			case 'fromcache':
ef5584
ef5584
				$this->_sql_report($mode, $query);
ef5584
ef5584
			break;
ef5584
ef5584
			case 'record_fromcache':
ef5584
ef5584
				$endtime = func_get_arg(2);
ef5584
				$splittime = func_get_arg(3);
ef5584
ef5584
				$time_cache = $endtime - $this->curtime;
ef5584
				$time_db = $splittime - $endtime;
ef5584
				$color = ($time_db > $time_cache) ? 'green' : 'red';
ef5584
ef5584
				$this->sql_report .= '';
Query results obtained from the cache
ef5584
				$this->sql_report .= '<textarea style="font-family:\'Courier New\',monospace;width:99%" rows="5" cols="10">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea>';
ef5584
				$this->sql_report .= '

';

ef5584
				$this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: ' . sprintf('%.5f', ($time_cache)) . 's | Elapsed [db]: ' . sprintf('%.5f', $time_db) . 's



';
ef5584
ef5584
				// Pad the start time to not interfere with page timing
ef5584
				$starttime += $time_db;
ef5584
ef5584
			break;
ef5584
ef5584
			default:
ef5584
ef5584
				$this->_sql_report($mode, $query);
ef5584
ef5584
			break;
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
}
ef5584
ef5584
/**
ef5584
* This variable holds the class name to use later
ef5584
*/
ef5584
$sql_db = (!empty($dbms)) ? 'dbal_' . basename($dbms) : 'dbal';
ef5584
ef5584
?>