Blame Extras/phpBB/3.0.4/includes/db/dbal.php

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

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

SQL

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

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

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

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

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

SQL Report

4c79b5
									
4c79b5
									

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' : '') . '

4c79b5
4c79b5
									

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

4c79b5
4c79b5
									

4c79b5
									' . $this->sql_report . '
4c79b5
								
4c79b5
								
4c79b5
							
4c79b5
							
4c79b5
						
4c79b5
						
4c79b5
							Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
4c79b5
						
4c79b5
					
4c79b5
					</body>
4c79b5
					</html>';
4c79b5
4c79b5
				exit_handler();
4c79b5
4c79b5
			break;
4c79b5
4c79b5
			case 'stop':
4c79b5
				$endtime = explode(' ', microtime());
4c79b5
				$endtime = $endtime[0] + $endtime[1];
4c79b5
4c79b5
				$this->sql_report .= '
4c79b5
4c79b5
					
4c79b5
					
4c79b5
					
4c79b5
						Query #' . $this->num_queries['total'] . '
4c79b5
					
4c79b5
					
4c79b5
					
4c79b5
					
4c79b5
						<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>
4c79b5
					
4c79b5
					
4c79b5
					
4c79b5
4c79b5
					' . $this->html_hold . '
4c79b5
4c79b5
					

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



';
4c79b5
4c79b5
				$this->sql_time += $endtime - $this->curtime;
4c79b5
			break;
4c79b5
4c79b5
			case 'start':
4c79b5
				$this->query_hold = $query;
4c79b5
				$this->html_hold = '';
4c79b5
4c79b5
				$this->_sql_report($mode, $query);
4c79b5
4c79b5
				$this->curtime = explode(' ', microtime());
4c79b5
				$this->curtime = $this->curtime[0] + $this->curtime[1];
4c79b5
4c79b5
			break;
4c79b5
4c79b5
			case 'add_select_row':
4c79b5
4c79b5
				$html_table = func_get_arg(2);
4c79b5
				$row = func_get_arg(3);
4c79b5
4c79b5
				if (!$html_table && sizeof($row))
4c79b5
				{
4c79b5
					$html_table = true;
4c79b5
					$this->html_hold .= '';
4c79b5
4c79b5
					foreach (array_keys($row) as $val)
4c79b5
					{
4c79b5
						$this->html_hold .= '' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '';
4c79b5
					}
4c79b5
					$this->html_hold .= '';
4c79b5
				}
4c79b5
				$this->html_hold .= '';
4c79b5
4c79b5
				$class = 'row1';
4c79b5
				foreach (array_values($row) as $val)
4c79b5
				{
4c79b5
					$class = ($class == 'row1') ? 'row2' : 'row1';
4c79b5
					$this->html_hold .= '' . (($val) ? $val : ' ') . '';
4c79b5
				}
4c79b5
				$this->html_hold .= '';
4c79b5
4c79b5
				return $html_table;
4c79b5
4c79b5
			break;
4c79b5
4c79b5
			case 'fromcache':
4c79b5
4c79b5
				$this->_sql_report($mode, $query);
4c79b5
4c79b5
			break;
4c79b5
4c79b5
			case 'record_fromcache':
4c79b5
4c79b5
				$endtime = func_get_arg(2);
4c79b5
				$splittime = func_get_arg(3);
4c79b5
4c79b5
				$time_cache = $endtime - $this->curtime;
4c79b5
				$time_db = $splittime - $endtime;
4c79b5
				$color = ($time_db > $time_cache) ? 'green' : 'red';
4c79b5
4c79b5
				$this->sql_report .= '';
Query results obtained from the cache
4c79b5
				$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>';
4c79b5
				$this->sql_report .= '

';

4c79b5
				$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



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