1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/db/mysqli.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,493 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package dbal
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
1.23 +
1.24 +/**
1.25 +* MySQLi Database Abstraction Layer
1.26 +* mysqli-extension has to be compiled with:
1.27 +* MySQL 4.1+ or MySQL 5.0+
1.28 +* @package dbal
1.29 +*/
1.30 +class dbal_mysqli extends dbal
1.31 +{
1.32 + var $multi_insert = true;
1.33 +
1.34 + /**
1.35 + * Connect to server
1.36 + */
1.37 + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false , $new_link = false)
1.38 + {
1.39 + // Mysqli extension supports persistent connection since PHP 5.3.0
1.40 + $this->persistency = (version_compare(PHP_VERSION, '5.3.0', '>=')) ? $persistency : false;
1.41 + $this->user = $sqluser;
1.42 +
1.43 + // If persistent connection, set dbhost to localhost when empty and prepend it with 'p:' prefix
1.44 + $this->server = ($this->persistency) ? 'p:' . (($sqlserver) ? $sqlserver : 'localhost') : $sqlserver;
1.45 +
1.46 + $this->dbname = $database;
1.47 + $port = (!$port) ? NULL : $port;
1.48 +
1.49 + // If port is set and it is not numeric, most likely mysqli socket is set.
1.50 + // Try to map it to the $socket parameter.
1.51 + $socket = NULL;
1.52 + if ($port)
1.53 + {
1.54 + if (is_numeric($port))
1.55 + {
1.56 + $port = (int) $port;
1.57 + }
1.58 + else
1.59 + {
1.60 + $socket = $port;
1.61 + $port = NULL;
1.62 + }
1.63 + }
1.64 +
1.65 + $this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket);
1.66 +
1.67 + if ($this->db_connect_id && $this->dbname != '')
1.68 + {
1.69 + @mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
1.70 +
1.71 + // enforce strict mode on databases that support it
1.72 + if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
1.73 + {
1.74 + $result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
1.75 + $row = @mysqli_fetch_assoc($result);
1.76 + @mysqli_free_result($result);
1.77 +
1.78 + $modes = array_map('trim', explode(',', $row['sql_mode']));
1.79 +
1.80 + // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
1.81 + if (!in_array('TRADITIONAL', $modes))
1.82 + {
1.83 + if (!in_array('STRICT_ALL_TABLES', $modes))
1.84 + {
1.85 + $modes[] = 'STRICT_ALL_TABLES';
1.86 + }
1.87 +
1.88 + if (!in_array('STRICT_TRANS_TABLES', $modes))
1.89 + {
1.90 + $modes[] = 'STRICT_TRANS_TABLES';
1.91 + }
1.92 + }
1.93 +
1.94 + $mode = implode(',', $modes);
1.95 + @mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
1.96 + }
1.97 + return $this->db_connect_id;
1.98 + }
1.99 +
1.100 + return $this->sql_error('');
1.101 + }
1.102 +
1.103 + /**
1.104 + * Version information about used database
1.105 + * @param bool $use_cache If true, it is safe to retrieve the value from the cache
1.106 + * @return string sql server version
1.107 + */
1.108 + function sql_server_info($raw = false, $use_cache = true)
1.109 + {
1.110 + global $cache;
1.111 +
1.112 + if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysqli_version')) === false)
1.113 + {
1.114 + $result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version');
1.115 + $row = @mysqli_fetch_assoc($result);
1.116 + @mysqli_free_result($result);
1.117 +
1.118 + $this->sql_server_version = $row['version'];
1.119 +
1.120 + if (!empty($cache) && $use_cache)
1.121 + {
1.122 + $cache->put('mysqli_version', $this->sql_server_version);
1.123 + }
1.124 + }
1.125 +
1.126 + return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
1.127 + }
1.128 +
1.129 + /**
1.130 + * SQL Transaction
1.131 + * @access private
1.132 + */
1.133 + function _sql_transaction($status = 'begin')
1.134 + {
1.135 + switch ($status)
1.136 + {
1.137 + case 'begin':
1.138 + return @mysqli_autocommit($this->db_connect_id, false);
1.139 + break;
1.140 +
1.141 + case 'commit':
1.142 + $result = @mysqli_commit($this->db_connect_id);
1.143 + @mysqli_autocommit($this->db_connect_id, true);
1.144 + return $result;
1.145 + break;
1.146 +
1.147 + case 'rollback':
1.148 + $result = @mysqli_rollback($this->db_connect_id);
1.149 + @mysqli_autocommit($this->db_connect_id, true);
1.150 + return $result;
1.151 + break;
1.152 + }
1.153 +
1.154 + return true;
1.155 + }
1.156 +
1.157 + /**
1.158 + * Base query method
1.159 + *
1.160 + * @param string $query Contains the SQL query which shall be executed
1.161 + * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
1.162 + * @return mixed When casted to bool the returned value returns true on success and false on failure
1.163 + *
1.164 + * @access public
1.165 + */
1.166 + function sql_query($query = '', $cache_ttl = 0)
1.167 + {
1.168 + if ($query != '')
1.169 + {
1.170 + global $cache;
1.171 +
1.172 + // EXPLAIN only in extra debug mode
1.173 + if (defined('DEBUG_EXTRA'))
1.174 + {
1.175 + $this->sql_report('start', $query);
1.176 + }
1.177 +
1.178 + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
1.179 + $this->sql_add_num_queries($this->query_result);
1.180 +
1.181 + if ($this->query_result === false)
1.182 + {
1.183 + if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
1.184 + {
1.185 + $this->sql_error($query);
1.186 + }
1.187 +
1.188 + if (defined('DEBUG_EXTRA'))
1.189 + {
1.190 + $this->sql_report('stop', $query);
1.191 + }
1.192 +
1.193 + if ($cache_ttl && method_exists($cache, 'sql_save'))
1.194 + {
1.195 + $cache->sql_save($query, $this->query_result, $cache_ttl);
1.196 + }
1.197 + }
1.198 + else if (defined('DEBUG_EXTRA'))
1.199 + {
1.200 + $this->sql_report('fromcache', $query);
1.201 + }
1.202 + }
1.203 + else
1.204 + {
1.205 + return false;
1.206 + }
1.207 +
1.208 + return $this->query_result;
1.209 + }
1.210 +
1.211 + /**
1.212 + * Build LIMIT query
1.213 + */
1.214 + function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
1.215 + {
1.216 + $this->query_result = false;
1.217 +
1.218 + // if $total is set to 0 we do not want to limit the number of rows
1.219 + if ($total == 0)
1.220 + {
1.221 + // MySQL 4.1+ no longer supports -1 in limit queries
1.222 + $total = '18446744073709551615';
1.223 + }
1.224 +
1.225 + $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
1.226 +
1.227 + return $this->sql_query($query, $cache_ttl);
1.228 + }
1.229 +
1.230 + /**
1.231 + * Return number of affected rows
1.232 + */
1.233 + function sql_affectedrows()
1.234 + {
1.235 + return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
1.236 + }
1.237 +
1.238 + /**
1.239 + * Fetch current row
1.240 + */
1.241 + function sql_fetchrow($query_id = false)
1.242 + {
1.243 + global $cache;
1.244 +
1.245 + if ($query_id === false)
1.246 + {
1.247 + $query_id = $this->query_result;
1.248 + }
1.249 +
1.250 + if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
1.251 + {
1.252 + return $cache->sql_fetchrow($query_id);
1.253 + }
1.254 +
1.255 + if ($query_id !== false)
1.256 + {
1.257 + $result = @mysqli_fetch_assoc($query_id);
1.258 + return $result !== null ? $result : false;
1.259 + }
1.260 +
1.261 + return false;
1.262 + }
1.263 +
1.264 + /**
1.265 + * Seek to given row number
1.266 + * rownum is zero-based
1.267 + */
1.268 + function sql_rowseek($rownum, &$query_id)
1.269 + {
1.270 + global $cache;
1.271 +
1.272 + if ($query_id === false)
1.273 + {
1.274 + $query_id = $this->query_result;
1.275 + }
1.276 +
1.277 + if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
1.278 + {
1.279 + return $cache->sql_rowseek($rownum, $query_id);
1.280 + }
1.281 +
1.282 + return ($query_id !== false) ? @mysqli_data_seek($query_id, $rownum) : false;
1.283 + }
1.284 +
1.285 + /**
1.286 + * Get last inserted id after insert statement
1.287 + */
1.288 + function sql_nextid()
1.289 + {
1.290 + return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
1.291 + }
1.292 +
1.293 + /**
1.294 + * Free sql result
1.295 + */
1.296 + function sql_freeresult($query_id = false)
1.297 + {
1.298 + global $cache;
1.299 +
1.300 + if ($query_id === false)
1.301 + {
1.302 + $query_id = $this->query_result;
1.303 + }
1.304 +
1.305 + if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
1.306 + {
1.307 + return $cache->sql_freeresult($query_id);
1.308 + }
1.309 +
1.310 + return @mysqli_free_result($query_id);
1.311 + }
1.312 +
1.313 + /**
1.314 + * Escape string used in sql query
1.315 + */
1.316 + function sql_escape($msg)
1.317 + {
1.318 + return @mysqli_real_escape_string($this->db_connect_id, $msg);
1.319 + }
1.320 +
1.321 + /**
1.322 + * Build LIKE expression
1.323 + * @access private
1.324 + */
1.325 + function _sql_like_expression($expression)
1.326 + {
1.327 + return $expression;
1.328 + }
1.329 +
1.330 + /**
1.331 + * Build db-specific query data
1.332 + * @access private
1.333 + */
1.334 + function _sql_custom_build($stage, $data)
1.335 + {
1.336 + switch ($stage)
1.337 + {
1.338 + case 'FROM':
1.339 + $data = '(' . $data . ')';
1.340 + break;
1.341 + }
1.342 +
1.343 + return $data;
1.344 + }
1.345 +
1.346 + /**
1.347 + * return sql error array
1.348 + * @access private
1.349 + */
1.350 + function _sql_error()
1.351 + {
1.352 + if (!$this->db_connect_id)
1.353 + {
1.354 + return array(
1.355 + 'message' => @mysqli_connect_error(),
1.356 + 'code' => @mysqli_connect_errno()
1.357 + );
1.358 + }
1.359 +
1.360 + return array(
1.361 + 'message' => @mysqli_error($this->db_connect_id),
1.362 + 'code' => @mysqli_errno($this->db_connect_id)
1.363 + );
1.364 + }
1.365 +
1.366 + /**
1.367 + * Close sql connection
1.368 + * @access private
1.369 + */
1.370 + function _sql_close()
1.371 + {
1.372 + return @mysqli_close($this->db_connect_id);
1.373 + }
1.374 +
1.375 + /**
1.376 + * Build db-specific report
1.377 + * @access private
1.378 + */
1.379 + function _sql_report($mode, $query = '')
1.380 + {
1.381 + static $test_prof;
1.382 +
1.383 + // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
1.384 + if ($test_prof === null)
1.385 + {
1.386 + $test_prof = false;
1.387 + if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false)
1.388 + {
1.389 + $ver = mysqli_get_server_version($this->db_connect_id);
1.390 + if ($ver >= 50037 && $ver < 50100)
1.391 + {
1.392 + $test_prof = true;
1.393 + }
1.394 + }
1.395 + }
1.396 +
1.397 + switch ($mode)
1.398 + {
1.399 + case 'start':
1.400 +
1.401 + $explain_query = $query;
1.402 + if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
1.403 + {
1.404 + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
1.405 + }
1.406 + else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
1.407 + {
1.408 + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
1.409 + }
1.410 +
1.411 + if (preg_match('/^SELECT/', $explain_query))
1.412 + {
1.413 + $html_table = false;
1.414 +
1.415 + // begin profiling
1.416 + if ($test_prof)
1.417 + {
1.418 + @mysqli_query($this->db_connect_id, 'SET profiling = 1;');
1.419 + }
1.420 +
1.421 + if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
1.422 + {
1.423 + while ($row = @mysqli_fetch_assoc($result))
1.424 + {
1.425 + $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
1.426 + }
1.427 + }
1.428 + @mysqli_free_result($result);
1.429 +
1.430 + if ($html_table)
1.431 + {
1.432 + $this->html_hold .= '</table>';
1.433 + }
1.434 +
1.435 + if ($test_prof)
1.436 + {
1.437 + $html_table = false;
1.438 +
1.439 + // get the last profile
1.440 + if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
1.441 + {
1.442 + $this->html_hold .= '<br />';
1.443 + while ($row = @mysqli_fetch_assoc($result))
1.444 + {
1.445 + // make <unknown> HTML safe
1.446 + if (!empty($row['Source_function']))
1.447 + {
1.448 + $row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']);
1.449 + }
1.450 +
1.451 + // remove unsupported features
1.452 + foreach ($row as $key => $val)
1.453 + {
1.454 + if ($val === null)
1.455 + {
1.456 + unset($row[$key]);
1.457 + }
1.458 + }
1.459 + $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
1.460 + }
1.461 + }
1.462 + @mysqli_free_result($result);
1.463 +
1.464 + if ($html_table)
1.465 + {
1.466 + $this->html_hold .= '</table>';
1.467 + }
1.468 +
1.469 + @mysqli_query($this->db_connect_id, 'SET profiling = 0;');
1.470 + }
1.471 + }
1.472 +
1.473 + break;
1.474 +
1.475 + case 'fromcache':
1.476 + $endtime = explode(' ', microtime());
1.477 + $endtime = $endtime[0] + $endtime[1];
1.478 +
1.479 + $result = @mysqli_query($this->db_connect_id, $query);
1.480 + while ($void = @mysqli_fetch_assoc($result))
1.481 + {
1.482 + // Take the time spent on parsing rows into account
1.483 + }
1.484 + @mysqli_free_result($result);
1.485 +
1.486 + $splittime = explode(' ', microtime());
1.487 + $splittime = $splittime[0] + $splittime[1];
1.488 +
1.489 + $this->sql_report('record_fromcache', $query, $endtime, $splittime);
1.490 +
1.491 + break;
1.492 + }
1.493 + }
1.494 +}
1.495 +
1.496 +?>
1.497 \ No newline at end of file