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

ef5584
ef5584
/**
ef5584
*
ef5584
* @package dbal
ef5584
* @version $Id: mysqli.php 8814 2008-09-04 12:01:47Z 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
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
ef5584
ef5584
/**
ef5584
* MySQLi Database Abstraction Layer
ef5584
* mysqli-extension has to be compiled with:
ef5584
* MySQL 4.1+ or MySQL 5.0+
ef5584
* @package dbal
ef5584
*/
ef5584
class dbal_mysqli extends dbal
ef5584
{
ef5584
	var $multi_insert = true;
ef5584
ef5584
	/**
ef5584
	* Connect to server
ef5584
	*/
ef5584
	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false , $new_link = false)
ef5584
	{
ef5584
		$this->persistency = $persistency;
ef5584
		$this->user = $sqluser;
ef5584
		$this->server = $sqlserver;
ef5584
		$this->dbname = $database;
ef5584
		$port = (!$port) ? NULL : $port;
ef5584
ef5584
		// Persistant connections not supported by the mysqli extension?
ef5584
		$this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port);
ef5584
ef5584
		if ($this->db_connect_id && $this->dbname != '')
ef5584
		{
ef5584
			@mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
ef5584
ef5584
			// enforce strict mode on databases that support it
ef5584
			if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
ef5584
			{
ef5584
				$result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
ef5584
				$row = @mysqli_fetch_assoc($result);
ef5584
				@mysqli_free_result($result);
ef5584
ef5584
				$modes = array_map('trim', explode(',', $row['sql_mode']));
ef5584
ef5584
				// TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
ef5584
				if (!in_array('TRADITIONAL', $modes))
ef5584
				{
ef5584
					if (!in_array('STRICT_ALL_TABLES', $modes))
ef5584
					{
ef5584
						$modes[] = 'STRICT_ALL_TABLES';
ef5584
					}
ef5584
ef5584
					if (!in_array('STRICT_TRANS_TABLES', $modes))
ef5584
					{
ef5584
						$modes[] = 'STRICT_TRANS_TABLES';
ef5584
					}
ef5584
				}
ef5584
ef5584
				$mode = implode(',', $modes);
ef5584
				@mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
ef5584
			}
ef5584
			return $this->db_connect_id;
ef5584
		}
ef5584
ef5584
		return $this->sql_error('');
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Version information about used database
ef5584
	* @param bool $raw if true, only return the fetched sql_server_version
ef5584
	* @return string sql server version
ef5584
	*/
ef5584
	function sql_server_info($raw = false)
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		if (empty($cache) || ($this->sql_server_version = $cache->get('mysqli_version')) === false)
ef5584
		{
ef5584
			$result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version');
ef5584
			$row = @mysqli_fetch_assoc($result);
ef5584
			@mysqli_free_result($result);
ef5584
ef5584
			$this->sql_server_version = $row['version'];
ef5584
ef5584
			if (!empty($cache))
ef5584
			{
ef5584
				$cache->put('mysqli_version', $this->sql_server_version);
ef5584
			}
ef5584
		}
ef5584
ef5584
		return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
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
				return @mysqli_autocommit($this->db_connect_id, false);
ef5584
			break;
ef5584
ef5584
			case 'commit':
ef5584
				$result = @mysqli_commit($this->db_connect_id);
ef5584
				@mysqli_autocommit($this->db_connect_id, true);
ef5584
				return $result;
ef5584
			break;
ef5584
ef5584
			case 'rollback':
ef5584
				$result = @mysqli_rollback($this->db_connect_id);
ef5584
				@mysqli_autocommit($this->db_connect_id, true);
ef5584
				return $result;
ef5584
			break;
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Base query method
ef5584
	*
ef5584
	* @param	string	$query		Contains the SQL query which shall be executed
ef5584
	* @param	int		$cache_ttl	Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
ef5584
	* @return	mixed				When casted to bool the returned value returns true on success and false on failure
ef5584
	*
ef5584
	* @access	public
ef5584
	*/
ef5584
	function sql_query($query = '', $cache_ttl = 0)
ef5584
	{
ef5584
		if ($query != '')
ef5584
		{
ef5584
			global $cache;
ef5584
ef5584
			// EXPLAIN only in extra debug mode
ef5584
			if (defined('DEBUG_EXTRA'))
ef5584
			{
ef5584
				$this->sql_report('start', $query);
ef5584
			}
ef5584
ef5584
			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
ef5584
			$this->sql_add_num_queries($this->query_result);
ef5584
ef5584
			if ($this->query_result === false)
ef5584
			{
ef5584
				if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
ef5584
				{
ef5584
					$this->sql_error($query);
ef5584
				}
ef5584
ef5584
				if (defined('DEBUG_EXTRA'))
ef5584
				{
ef5584
					$this->sql_report('stop', $query);
ef5584
				}
ef5584
ef5584
				if ($cache_ttl && method_exists($cache, 'sql_save'))
ef5584
				{
ef5584
					$cache->sql_save($query, $this->query_result, $cache_ttl);
ef5584
				}
ef5584
			}
ef5584
			else if (defined('DEBUG_EXTRA'))
ef5584
			{
ef5584
				$this->sql_report('fromcache', $query);
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		return $this->query_result;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build LIMIT query
ef5584
	*/
ef5584
	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
ef5584
	{
ef5584
		$this->query_result = false;
ef5584
ef5584
		// if $total is set to 0 we do not want to limit the number of rows
ef5584
		if ($total == 0)
ef5584
		{
ef5584
			// MySQL 4.1+ no longer supports -1 in limit queries
ef5584
			$total = '18446744073709551615';
ef5584
		}
ef5584
ef5584
		$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
ef5584
ef5584
		return $this->sql_query($query, $cache_ttl);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Return number of affected rows
ef5584
	*/
ef5584
	function sql_affectedrows()
ef5584
	{
ef5584
		return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Fetch current row
ef5584
	*/
ef5584
	function sql_fetchrow($query_id = false)
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		if ($query_id === false)
ef5584
		{
ef5584
			$query_id = $this->query_result;
ef5584
		}
ef5584
ef5584
		if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
ef5584
		{
ef5584
			return $cache->sql_fetchrow($query_id);
ef5584
		}
ef5584
ef5584
		return ($query_id !== false) ? @mysqli_fetch_assoc($query_id) : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Seek to given row number
ef5584
	* rownum is zero-based
ef5584
	*/
ef5584
	function sql_rowseek($rownum, &$query_id)
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		if ($query_id === false)
ef5584
		{
ef5584
			$query_id = $this->query_result;
ef5584
		}
ef5584
ef5584
		if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
ef5584
		{
ef5584
			return $cache->sql_rowseek($rownum, $query_id);
ef5584
		}
ef5584
ef5584
		return ($query_id !== false) ? @mysqli_data_seek($query_id, $rownum) : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get last inserted id after insert statement
ef5584
	*/
ef5584
	function sql_nextid()
ef5584
	{
ef5584
		return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Free sql result
ef5584
	*/
ef5584
	function sql_freeresult($query_id = false)
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		if ($query_id === false)
ef5584
		{
ef5584
			$query_id = $this->query_result;
ef5584
		}
ef5584
ef5584
		if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
ef5584
		{
ef5584
			return $cache->sql_freeresult($query_id);
ef5584
		}
ef5584
ef5584
		return @mysqli_free_result($query_id);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Escape string used in sql query
ef5584
	*/
ef5584
	function sql_escape($msg)
ef5584
	{
ef5584
		return @mysqli_real_escape_string($this->db_connect_id, $msg);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build LIKE expression
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_like_expression($expression)
ef5584
	{
ef5584
		return $expression;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build db-specific query data
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_custom_build($stage, $data)
ef5584
	{
ef5584
		switch ($stage)
ef5584
		{
ef5584
			case 'FROM':
ef5584
				$data = '(' . $data . ')';
ef5584
			break;
ef5584
		}
ef5584
ef5584
		return $data;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* return sql error array
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_error()
ef5584
	{
ef5584
		if (!$this->db_connect_id)
ef5584
		{
ef5584
			return array(
ef5584
				'message'	=> @mysqli_connect_error(),
ef5584
				'code'		=> @mysqli_connect_errno()
ef5584
			);
ef5584
		}
ef5584
ef5584
		return array(
ef5584
			'message'	=> @mysqli_error($this->db_connect_id),
ef5584
			'code'		=> @mysqli_errno($this->db_connect_id)
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Close sql connection
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_close()
ef5584
	{
ef5584
		return @mysqli_close($this->db_connect_id);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build db-specific report
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_report($mode, $query = '')
ef5584
	{
ef5584
		static $test_prof;
ef5584
ef5584
		// current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
ef5584
		if ($test_prof === null)
ef5584
		{
ef5584
			$test_prof = false;
ef5584
			if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false)
ef5584
			{
ef5584
				$ver = mysqli_get_server_version($this->db_connect_id);
ef5584
				if ($ver >= 50037 && $ver < 50100)
ef5584
				{
ef5584
					$test_prof = true;
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'start':
ef5584
ef5584
				$explain_query = $query;
ef5584
				if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
ef5584
				{
ef5584
					$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
ef5584
				}
ef5584
				else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
ef5584
				{
ef5584
					$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
ef5584
				}
ef5584
ef5584
				if (preg_match('/^SELECT/', $explain_query))
ef5584
				{
ef5584
					$html_table = false;
ef5584
ef5584
					// begin profiling
ef5584
					if ($test_prof)
ef5584
					{
ef5584
						@mysqli_query($this->db_connect_id, 'SET profiling = 1;');
ef5584
					}
ef5584
ef5584
					if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
ef5584
					{
ef5584
						while ($row = @mysqli_fetch_assoc($result))
ef5584
						{
ef5584
							$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
ef5584
						}
ef5584
					}
ef5584
					@mysqli_free_result($result);
ef5584
ef5584
					if ($html_table)
ef5584
					{
ef5584
						$this->html_hold .= '';
ef5584
					}
ef5584
ef5584
					if ($test_prof)
ef5584
					{
ef5584
						$html_table = false;
ef5584
ef5584
						// get the last profile
ef5584
						if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
ef5584
						{
ef5584
							$this->html_hold .= '
';
ef5584
							while ($row = @mysqli_fetch_assoc($result))
ef5584
							{
ef5584
								// make <unknown> HTML safe
ef5584
								if (!empty($row['Source_function']))
ef5584
								{
ef5584
									$row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']);
ef5584
								}
ef5584
ef5584
								// remove unsupported features
ef5584
								foreach ($row as $key => $val)
ef5584
								{
ef5584
									if ($val === null)
ef5584
									{
ef5584
										unset($row[$key]);
ef5584
									}
ef5584
								}
ef5584
								$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
ef5584
							}
ef5584
						}
ef5584
						@mysqli_free_result($result);
ef5584
ef5584
						if ($html_table)
ef5584
						{
ef5584
							$this->html_hold .= '';
ef5584
						}
ef5584
ef5584
						@mysqli_query($this->db_connect_id, 'SET profiling = 0;');
ef5584
					}
ef5584
				}
ef5584
ef5584
			break;
ef5584
ef5584
			case 'fromcache':
ef5584
				$endtime = explode(' ', microtime());
ef5584
				$endtime = $endtime[0] + $endtime[1];
ef5584
ef5584
				$result = @mysqli_query($this->db_connect_id, $query);
ef5584
				while ($void = @mysqli_fetch_assoc($result))
ef5584
				{
ef5584
					// Take the time spent on parsing rows into account
ef5584
				}
ef5584
				@mysqli_free_result($result);
ef5584
ef5584
				$splittime = explode(' ', microtime());
ef5584
				$splittime = $splittime[0] + $splittime[1];
ef5584
ef5584
				$this->sql_report('record_fromcache', $query, $endtime, $splittime);
ef5584
ef5584
			break;
ef5584
		}
ef5584
	}
ef5584
}
ef5584
ef5584
?>