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

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