includes/db/mysqli.php
changeset 0 ceef6d64c21d
equal deleted inserted replaced
-1:000000000000 0:ceef6d64c21d
       
     1 <?php
       
     2 /**
       
     3 *
       
     4 * @package dbal
       
     5 * @version $Id$
       
     6 * @copyright (c) 2005 phpBB Group
       
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
       
     8 *
       
     9 */
       
    10 
       
    11 /**
       
    12 * @ignore
       
    13 */
       
    14 if (!defined('IN_PHPBB'))
       
    15 {
       
    16 	exit;
       
    17 }
       
    18 
       
    19 include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
       
    20 
       
    21 /**
       
    22 * MySQLi Database Abstraction Layer
       
    23 * mysqli-extension has to be compiled with:
       
    24 * MySQL 4.1+ or MySQL 5.0+
       
    25 * @package dbal
       
    26 */
       
    27 class dbal_mysqli extends dbal
       
    28 {
       
    29 	var $multi_insert = true;
       
    30 
       
    31 	/**
       
    32 	* Connect to server
       
    33 	*/
       
    34 	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false , $new_link = false)
       
    35 	{
       
    36 		// Mysqli extension supports persistent connection since PHP 5.3.0
       
    37 		$this->persistency = (version_compare(PHP_VERSION, '5.3.0', '>=')) ? $persistency : false;
       
    38 		$this->user = $sqluser;
       
    39 
       
    40 		// If persistent connection, set dbhost to localhost when empty and prepend it with 'p:' prefix
       
    41 		$this->server = ($this->persistency) ? 'p:' . (($sqlserver) ? $sqlserver : 'localhost') : $sqlserver;
       
    42 
       
    43 		$this->dbname = $database;
       
    44 		$port = (!$port) ? NULL : $port;
       
    45 
       
    46 		// If port is set and it is not numeric, most likely mysqli socket is set.
       
    47 		// Try to map it to the $socket parameter.
       
    48 		$socket = NULL;
       
    49 		if ($port)
       
    50 		{
       
    51 			if (is_numeric($port))
       
    52 			{
       
    53 				$port = (int) $port;
       
    54 			}
       
    55 			else
       
    56 			{
       
    57 				$socket = $port;
       
    58 				$port = NULL;
       
    59 			}
       
    60 		}
       
    61 
       
    62 		$this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket);
       
    63 
       
    64 		if ($this->db_connect_id && $this->dbname != '')
       
    65 		{
       
    66 			@mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
       
    67 
       
    68 			// enforce strict mode on databases that support it
       
    69 			if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
       
    70 			{
       
    71 				$result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
       
    72 				$row = @mysqli_fetch_assoc($result);
       
    73 				@mysqli_free_result($result);
       
    74 
       
    75 				$modes = array_map('trim', explode(',', $row['sql_mode']));
       
    76 
       
    77 				// TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
       
    78 				if (!in_array('TRADITIONAL', $modes))
       
    79 				{
       
    80 					if (!in_array('STRICT_ALL_TABLES', $modes))
       
    81 					{
       
    82 						$modes[] = 'STRICT_ALL_TABLES';
       
    83 					}
       
    84 
       
    85 					if (!in_array('STRICT_TRANS_TABLES', $modes))
       
    86 					{
       
    87 						$modes[] = 'STRICT_TRANS_TABLES';
       
    88 					}
       
    89 				}
       
    90 
       
    91 				$mode = implode(',', $modes);
       
    92 				@mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
       
    93 			}
       
    94 			return $this->db_connect_id;
       
    95 		}
       
    96 
       
    97 		return $this->sql_error('');
       
    98 	}
       
    99 
       
   100 	/**
       
   101 	* Version information about used database
       
   102 	* @param bool $use_cache If true, it is safe to retrieve the value from the cache
       
   103 	* @return string sql server version
       
   104 	*/
       
   105 	function sql_server_info($raw = false, $use_cache = true)
       
   106 	{
       
   107 		global $cache;
       
   108 
       
   109 		if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysqli_version')) === false)
       
   110 		{
       
   111 			$result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version');
       
   112 			$row = @mysqli_fetch_assoc($result);
       
   113 			@mysqli_free_result($result);
       
   114 
       
   115 			$this->sql_server_version = $row['version'];
       
   116 
       
   117 			if (!empty($cache) && $use_cache)
       
   118 			{
       
   119 				$cache->put('mysqli_version', $this->sql_server_version);
       
   120 			}
       
   121 		}
       
   122 
       
   123 		return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
       
   124 	}
       
   125 
       
   126 	/**
       
   127 	* SQL Transaction
       
   128 	* @access private
       
   129 	*/
       
   130 	function _sql_transaction($status = 'begin')
       
   131 	{
       
   132 		switch ($status)
       
   133 		{
       
   134 			case 'begin':
       
   135 				return @mysqli_autocommit($this->db_connect_id, false);
       
   136 			break;
       
   137 
       
   138 			case 'commit':
       
   139 				$result = @mysqli_commit($this->db_connect_id);
       
   140 				@mysqli_autocommit($this->db_connect_id, true);
       
   141 				return $result;
       
   142 			break;
       
   143 
       
   144 			case 'rollback':
       
   145 				$result = @mysqli_rollback($this->db_connect_id);
       
   146 				@mysqli_autocommit($this->db_connect_id, true);
       
   147 				return $result;
       
   148 			break;
       
   149 		}
       
   150 
       
   151 		return true;
       
   152 	}
       
   153 
       
   154 	/**
       
   155 	* Base query method
       
   156 	*
       
   157 	* @param	string	$query		Contains the SQL query which shall be executed
       
   158 	* @param	int		$cache_ttl	Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
       
   159 	* @return	mixed				When casted to bool the returned value returns true on success and false on failure
       
   160 	*
       
   161 	* @access	public
       
   162 	*/
       
   163 	function sql_query($query = '', $cache_ttl = 0)
       
   164 	{
       
   165 		if ($query != '')
       
   166 		{
       
   167 			global $cache;
       
   168 
       
   169 			// EXPLAIN only in extra debug mode
       
   170 			if (defined('DEBUG_EXTRA'))
       
   171 			{
       
   172 				$this->sql_report('start', $query);
       
   173 			}
       
   174 
       
   175 			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
       
   176 			$this->sql_add_num_queries($this->query_result);
       
   177 
       
   178 			if ($this->query_result === false)
       
   179 			{
       
   180 				if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
       
   181 				{
       
   182 					$this->sql_error($query);
       
   183 				}
       
   184 
       
   185 				if (defined('DEBUG_EXTRA'))
       
   186 				{
       
   187 					$this->sql_report('stop', $query);
       
   188 				}
       
   189 
       
   190 				if ($cache_ttl && method_exists($cache, 'sql_save'))
       
   191 				{
       
   192 					$cache->sql_save($query, $this->query_result, $cache_ttl);
       
   193 				}
       
   194 			}
       
   195 			else if (defined('DEBUG_EXTRA'))
       
   196 			{
       
   197 				$this->sql_report('fromcache', $query);
       
   198 			}
       
   199 		}
       
   200 		else
       
   201 		{
       
   202 			return false;
       
   203 		}
       
   204 
       
   205 		return $this->query_result;
       
   206 	}
       
   207 
       
   208 	/**
       
   209 	* Build LIMIT query
       
   210 	*/
       
   211 	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
       
   212 	{
       
   213 		$this->query_result = false;
       
   214 
       
   215 		// if $total is set to 0 we do not want to limit the number of rows
       
   216 		if ($total == 0)
       
   217 		{
       
   218 			// MySQL 4.1+ no longer supports -1 in limit queries
       
   219 			$total = '18446744073709551615';
       
   220 		}
       
   221 
       
   222 		$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
       
   223 
       
   224 		return $this->sql_query($query, $cache_ttl);
       
   225 	}
       
   226 
       
   227 	/**
       
   228 	* Return number of affected rows
       
   229 	*/
       
   230 	function sql_affectedrows()
       
   231 	{
       
   232 		return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
       
   233 	}
       
   234 
       
   235 	/**
       
   236 	* Fetch current row
       
   237 	*/
       
   238 	function sql_fetchrow($query_id = false)
       
   239 	{
       
   240 		global $cache;
       
   241 
       
   242 		if ($query_id === false)
       
   243 		{
       
   244 			$query_id = $this->query_result;
       
   245 		}
       
   246 
       
   247 		if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
       
   248 		{
       
   249 			return $cache->sql_fetchrow($query_id);
       
   250 		}
       
   251 
       
   252 		if ($query_id !== false)
       
   253 		{
       
   254 			$result = @mysqli_fetch_assoc($query_id);
       
   255 			return $result !== null ? $result : false;
       
   256 		}
       
   257 
       
   258 		return false;
       
   259 	}
       
   260 
       
   261 	/**
       
   262 	* Seek to given row number
       
   263 	* rownum is zero-based
       
   264 	*/
       
   265 	function sql_rowseek($rownum, &$query_id)
       
   266 	{
       
   267 		global $cache;
       
   268 
       
   269 		if ($query_id === false)
       
   270 		{
       
   271 			$query_id = $this->query_result;
       
   272 		}
       
   273 
       
   274 		if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
       
   275 		{
       
   276 			return $cache->sql_rowseek($rownum, $query_id);
       
   277 		}
       
   278 
       
   279 		return ($query_id !== false) ? @mysqli_data_seek($query_id, $rownum) : false;
       
   280 	}
       
   281 
       
   282 	/**
       
   283 	* Get last inserted id after insert statement
       
   284 	*/
       
   285 	function sql_nextid()
       
   286 	{
       
   287 		return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
       
   288 	}
       
   289 
       
   290 	/**
       
   291 	* Free sql result
       
   292 	*/
       
   293 	function sql_freeresult($query_id = false)
       
   294 	{
       
   295 		global $cache;
       
   296 
       
   297 		if ($query_id === false)
       
   298 		{
       
   299 			$query_id = $this->query_result;
       
   300 		}
       
   301 
       
   302 		if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
       
   303 		{
       
   304 			return $cache->sql_freeresult($query_id);
       
   305 		}
       
   306 
       
   307 		return @mysqli_free_result($query_id);
       
   308 	}
       
   309 
       
   310 	/**
       
   311 	* Escape string used in sql query
       
   312 	*/
       
   313 	function sql_escape($msg)
       
   314 	{
       
   315 		return @mysqli_real_escape_string($this->db_connect_id, $msg);
       
   316 	}
       
   317 
       
   318 	/**
       
   319 	* Build LIKE expression
       
   320 	* @access private
       
   321 	*/
       
   322 	function _sql_like_expression($expression)
       
   323 	{
       
   324 		return $expression;
       
   325 	}
       
   326 
       
   327 	/**
       
   328 	* Build db-specific query data
       
   329 	* @access private
       
   330 	*/
       
   331 	function _sql_custom_build($stage, $data)
       
   332 	{
       
   333 		switch ($stage)
       
   334 		{
       
   335 			case 'FROM':
       
   336 				$data = '(' . $data . ')';
       
   337 			break;
       
   338 		}
       
   339 
       
   340 		return $data;
       
   341 	}
       
   342 
       
   343 	/**
       
   344 	* return sql error array
       
   345 	* @access private
       
   346 	*/
       
   347 	function _sql_error()
       
   348 	{
       
   349 		if (!$this->db_connect_id)
       
   350 		{
       
   351 			return array(
       
   352 				'message'	=> @mysqli_connect_error(),
       
   353 				'code'		=> @mysqli_connect_errno()
       
   354 			);
       
   355 		}
       
   356 
       
   357 		return array(
       
   358 			'message'	=> @mysqli_error($this->db_connect_id),
       
   359 			'code'		=> @mysqli_errno($this->db_connect_id)
       
   360 		);
       
   361 	}
       
   362 
       
   363 	/**
       
   364 	* Close sql connection
       
   365 	* @access private
       
   366 	*/
       
   367 	function _sql_close()
       
   368 	{
       
   369 		return @mysqli_close($this->db_connect_id);
       
   370 	}
       
   371 
       
   372 	/**
       
   373 	* Build db-specific report
       
   374 	* @access private
       
   375 	*/
       
   376 	function _sql_report($mode, $query = '')
       
   377 	{
       
   378 		static $test_prof;
       
   379 
       
   380 		// current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
       
   381 		if ($test_prof === null)
       
   382 		{
       
   383 			$test_prof = false;
       
   384 			if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false)
       
   385 			{
       
   386 				$ver = mysqli_get_server_version($this->db_connect_id);
       
   387 				if ($ver >= 50037 && $ver < 50100)
       
   388 				{
       
   389 					$test_prof = true;
       
   390 				}
       
   391 			}
       
   392 		}
       
   393 
       
   394 		switch ($mode)
       
   395 		{
       
   396 			case 'start':
       
   397 
       
   398 				$explain_query = $query;
       
   399 				if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
       
   400 				{
       
   401 					$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
       
   402 				}
       
   403 				else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
       
   404 				{
       
   405 					$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
       
   406 				}
       
   407 
       
   408 				if (preg_match('/^SELECT/', $explain_query))
       
   409 				{
       
   410 					$html_table = false;
       
   411 
       
   412 					// begin profiling
       
   413 					if ($test_prof)
       
   414 					{
       
   415 						@mysqli_query($this->db_connect_id, 'SET profiling = 1;');
       
   416 					}
       
   417 
       
   418 					if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
       
   419 					{
       
   420 						while ($row = @mysqli_fetch_assoc($result))
       
   421 						{
       
   422 							$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
       
   423 						}
       
   424 					}
       
   425 					@mysqli_free_result($result);
       
   426 
       
   427 					if ($html_table)
       
   428 					{
       
   429 						$this->html_hold .= '</table>';
       
   430 					}
       
   431 
       
   432 					if ($test_prof)
       
   433 					{
       
   434 						$html_table = false;
       
   435 
       
   436 						// get the last profile
       
   437 						if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
       
   438 						{
       
   439 							$this->html_hold .= '<br />';
       
   440 							while ($row = @mysqli_fetch_assoc($result))
       
   441 							{
       
   442 								// make <unknown> HTML safe
       
   443 								if (!empty($row['Source_function']))
       
   444 								{
       
   445 									$row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
       
   446 								}
       
   447 
       
   448 								// remove unsupported features
       
   449 								foreach ($row as $key => $val)
       
   450 								{
       
   451 									if ($val === null)
       
   452 									{
       
   453 										unset($row[$key]);
       
   454 									}
       
   455 								}
       
   456 								$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
       
   457 							}
       
   458 						}
       
   459 						@mysqli_free_result($result);
       
   460 
       
   461 						if ($html_table)
       
   462 						{
       
   463 							$this->html_hold .= '</table>';
       
   464 						}
       
   465 
       
   466 						@mysqli_query($this->db_connect_id, 'SET profiling = 0;');
       
   467 					}
       
   468 				}
       
   469 
       
   470 			break;
       
   471 
       
   472 			case 'fromcache':
       
   473 				$endtime = explode(' ', microtime());
       
   474 				$endtime = $endtime[0] + $endtime[1];
       
   475 
       
   476 				$result = @mysqli_query($this->db_connect_id, $query);
       
   477 				while ($void = @mysqli_fetch_assoc($result))
       
   478 				{
       
   479 					// Take the time spent on parsing rows into account
       
   480 				}
       
   481 				@mysqli_free_result($result);
       
   482 
       
   483 				$splittime = explode(' ', microtime());
       
   484 				$splittime = $splittime[0] + $splittime[1];
       
   485 
       
   486 				$this->sql_report('record_fromcache', $query, $endtime, $splittime);
       
   487 
       
   488 			break;
       
   489 		}
       
   490 	}
       
   491 }
       
   492 
       
   493 ?>