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