1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/db/dbal.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,915 @@
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 +/**
1.23 +* Database Abstraction Layer
1.24 +* @package dbal
1.25 +*/
1.26 +class dbal
1.27 +{
1.28 + var $db_connect_id;
1.29 + var $query_result;
1.30 + var $return_on_error = false;
1.31 + var $transaction = false;
1.32 + var $sql_time = 0;
1.33 + var $num_queries = array();
1.34 + var $open_queries = array();
1.35 +
1.36 + var $curtime = 0;
1.37 + var $query_hold = '';
1.38 + var $html_hold = '';
1.39 + var $sql_report = '';
1.40 +
1.41 + var $persistency = false;
1.42 + var $user = '';
1.43 + var $server = '';
1.44 + var $dbname = '';
1.45 +
1.46 + // Set to true if error triggered
1.47 + var $sql_error_triggered = false;
1.48 +
1.49 + // Holding the last sql query on sql error
1.50 + var $sql_error_sql = '';
1.51 + // Holding the error information - only populated if sql_error_triggered is set
1.52 + var $sql_error_returned = array();
1.53 +
1.54 + // Holding transaction count
1.55 + var $transactions = 0;
1.56 +
1.57 + // Supports multi inserts?
1.58 + var $multi_insert = false;
1.59 +
1.60 + /**
1.61 + * Current sql layer
1.62 + */
1.63 + var $sql_layer = '';
1.64 +
1.65 + /**
1.66 + * Wildcards for matching any (%) or exactly one (_) character within LIKE expressions
1.67 + */
1.68 + var $any_char;
1.69 + var $one_char;
1.70 +
1.71 + /**
1.72 + * Exact version of the DBAL, directly queried
1.73 + */
1.74 + var $sql_server_version = false;
1.75 +
1.76 + /**
1.77 + * Constructor
1.78 + */
1.79 + function dbal()
1.80 + {
1.81 + $this->num_queries = array(
1.82 + 'cached' => 0,
1.83 + 'normal' => 0,
1.84 + 'total' => 0,
1.85 + );
1.86 +
1.87 + // Fill default sql layer based on the class being called.
1.88 + // This can be changed by the specified layer itself later if needed.
1.89 + $this->sql_layer = substr(get_class($this), 5);
1.90 +
1.91 + // Do not change this please! This variable is used to easy the use of it - and is hardcoded.
1.92 + $this->any_char = chr(0) . '%';
1.93 + $this->one_char = chr(0) . '_';
1.94 + }
1.95 +
1.96 + /**
1.97 + * return on error or display error message
1.98 + */
1.99 + function sql_return_on_error($fail = false)
1.100 + {
1.101 + $this->sql_error_triggered = false;
1.102 + $this->sql_error_sql = '';
1.103 +
1.104 + $this->return_on_error = $fail;
1.105 + }
1.106 +
1.107 + /**
1.108 + * Return number of sql queries and cached sql queries used
1.109 + */
1.110 + function sql_num_queries($cached = false)
1.111 + {
1.112 + return ($cached) ? $this->num_queries['cached'] : $this->num_queries['normal'];
1.113 + }
1.114 +
1.115 + /**
1.116 + * Add to query count
1.117 + */
1.118 + function sql_add_num_queries($cached = false)
1.119 + {
1.120 + $this->num_queries['cached'] += ($cached !== false) ? 1 : 0;
1.121 + $this->num_queries['normal'] += ($cached !== false) ? 0 : 1;
1.122 + $this->num_queries['total'] += 1;
1.123 + }
1.124 +
1.125 + /**
1.126 + * DBAL garbage collection, close sql connection
1.127 + */
1.128 + function sql_close()
1.129 + {
1.130 + if (!$this->db_connect_id)
1.131 + {
1.132 + return false;
1.133 + }
1.134 +
1.135 + if ($this->transaction)
1.136 + {
1.137 + do
1.138 + {
1.139 + $this->sql_transaction('commit');
1.140 + }
1.141 + while ($this->transaction);
1.142 + }
1.143 +
1.144 + foreach ($this->open_queries as $query_id)
1.145 + {
1.146 + $this->sql_freeresult($query_id);
1.147 + }
1.148 +
1.149 + // Connection closed correctly. Set db_connect_id to false to prevent errors
1.150 + if ($result = $this->_sql_close())
1.151 + {
1.152 + $this->db_connect_id = false;
1.153 + }
1.154 +
1.155 + return $result;
1.156 + }
1.157 +
1.158 + /**
1.159 + * Build LIMIT query
1.160 + * Doing some validation here.
1.161 + */
1.162 + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
1.163 + {
1.164 + if (empty($query))
1.165 + {
1.166 + return false;
1.167 + }
1.168 +
1.169 + // Never use a negative total or offset
1.170 + $total = ($total < 0) ? 0 : $total;
1.171 + $offset = ($offset < 0) ? 0 : $offset;
1.172 +
1.173 + return $this->_sql_query_limit($query, $total, $offset, $cache_ttl);
1.174 + }
1.175 +
1.176 + /**
1.177 + * Fetch all rows
1.178 + */
1.179 + function sql_fetchrowset($query_id = false)
1.180 + {
1.181 + if ($query_id === false)
1.182 + {
1.183 + $query_id = $this->query_result;
1.184 + }
1.185 +
1.186 + if ($query_id !== false)
1.187 + {
1.188 + $result = array();
1.189 + while ($row = $this->sql_fetchrow($query_id))
1.190 + {
1.191 + $result[] = $row;
1.192 + }
1.193 +
1.194 + return $result;
1.195 + }
1.196 +
1.197 + return false;
1.198 + }
1.199 +
1.200 + /**
1.201 + * Fetch field
1.202 + * if rownum is false, the current row is used, else it is pointing to the row (zero-based)
1.203 + */
1.204 + function sql_fetchfield($field, $rownum = false, $query_id = false)
1.205 + {
1.206 + global $cache;
1.207 +
1.208 + if ($query_id === false)
1.209 + {
1.210 + $query_id = $this->query_result;
1.211 + }
1.212 +
1.213 + if ($query_id !== false)
1.214 + {
1.215 + if ($rownum !== false)
1.216 + {
1.217 + $this->sql_rowseek($rownum, $query_id);
1.218 + }
1.219 +
1.220 + if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
1.221 + {
1.222 + return $cache->sql_fetchfield($query_id, $field);
1.223 + }
1.224 +
1.225 + $row = $this->sql_fetchrow($query_id);
1.226 + return (isset($row[$field])) ? $row[$field] : false;
1.227 + }
1.228 +
1.229 + return false;
1.230 + }
1.231 +
1.232 + /**
1.233 + * Correctly adjust LIKE expression for special characters
1.234 + * Some DBMS are handling them in a different way
1.235 + *
1.236 + * @param string $expression The expression to use. Every wildcard is escaped, except $this->any_char and $this->one_char
1.237 + * @return string LIKE expression including the keyword!
1.238 + */
1.239 + function sql_like_expression($expression)
1.240 + {
1.241 + $expression = utf8_str_replace(array('_', '%'), array("\_", "\%"), $expression);
1.242 + $expression = utf8_str_replace(array(chr(0) . "\_", chr(0) . "\%"), array('_', '%'), $expression);
1.243 +
1.244 + return $this->_sql_like_expression('LIKE \'' . $this->sql_escape($expression) . '\'');
1.245 + }
1.246 +
1.247 + /**
1.248 + * Returns whether results of a query need to be buffered to run a transaction while iterating over them.
1.249 + *
1.250 + * @return bool Whether buffering is required.
1.251 + */
1.252 + function sql_buffer_nested_transactions()
1.253 + {
1.254 + return false;
1.255 + }
1.256 +
1.257 + /**
1.258 + * SQL Transaction
1.259 + * @access private
1.260 + */
1.261 + function sql_transaction($status = 'begin')
1.262 + {
1.263 + switch ($status)
1.264 + {
1.265 + case 'begin':
1.266 + // If we are within a transaction we will not open another one, but enclose the current one to not loose data (prevening auto commit)
1.267 + if ($this->transaction)
1.268 + {
1.269 + $this->transactions++;
1.270 + return true;
1.271 + }
1.272 +
1.273 + $result = $this->_sql_transaction('begin');
1.274 +
1.275 + if (!$result)
1.276 + {
1.277 + $this->sql_error();
1.278 + }
1.279 +
1.280 + $this->transaction = true;
1.281 + break;
1.282 +
1.283 + case 'commit':
1.284 + // If there was a previously opened transaction we do not commit yet... but count back the number of inner transactions
1.285 + if ($this->transaction && $this->transactions)
1.286 + {
1.287 + $this->transactions--;
1.288 + return true;
1.289 + }
1.290 +
1.291 + // Check if there is a transaction (no transaction can happen if there was an error, with a combined rollback and error returning enabled)
1.292 + // This implies we have transaction always set for autocommit db's
1.293 + if (!$this->transaction)
1.294 + {
1.295 + return false;
1.296 + }
1.297 +
1.298 + $result = $this->_sql_transaction('commit');
1.299 +
1.300 + if (!$result)
1.301 + {
1.302 + $this->sql_error();
1.303 + }
1.304 +
1.305 + $this->transaction = false;
1.306 + $this->transactions = 0;
1.307 + break;
1.308 +
1.309 + case 'rollback':
1.310 + $result = $this->_sql_transaction('rollback');
1.311 + $this->transaction = false;
1.312 + $this->transactions = 0;
1.313 + break;
1.314 +
1.315 + default:
1.316 + $result = $this->_sql_transaction($status);
1.317 + break;
1.318 + }
1.319 +
1.320 + return $result;
1.321 + }
1.322 +
1.323 + /**
1.324 + * Build sql statement from array for insert/update/select statements
1.325 + *
1.326 + * Idea for this from Ikonboard
1.327 + * Possible query values: INSERT, INSERT_SELECT, UPDATE, SELECT
1.328 + *
1.329 + */
1.330 + function sql_build_array($query, $assoc_ary = false)
1.331 + {
1.332 + if (!is_array($assoc_ary))
1.333 + {
1.334 + return false;
1.335 + }
1.336 +
1.337 + $fields = $values = array();
1.338 +
1.339 + if ($query == 'INSERT' || $query == 'INSERT_SELECT')
1.340 + {
1.341 + foreach ($assoc_ary as $key => $var)
1.342 + {
1.343 + $fields[] = $key;
1.344 +
1.345 + if (is_array($var) && is_string($var[0]))
1.346 + {
1.347 + // This is used for INSERT_SELECT(s)
1.348 + $values[] = $var[0];
1.349 + }
1.350 + else
1.351 + {
1.352 + $values[] = $this->_sql_validate_value($var);
1.353 + }
1.354 + }
1.355 +
1.356 + $query = ($query == 'INSERT') ? ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')' : ' (' . implode(', ', $fields) . ') SELECT ' . implode(', ', $values) . ' ';
1.357 + }
1.358 + else if ($query == 'MULTI_INSERT')
1.359 + {
1.360 + trigger_error('The MULTI_INSERT query value is no longer supported. Please use sql_multi_insert() instead.', E_USER_ERROR);
1.361 + }
1.362 + else if ($query == 'UPDATE' || $query == 'SELECT')
1.363 + {
1.364 + $values = array();
1.365 + foreach ($assoc_ary as $key => $var)
1.366 + {
1.367 + $values[] = "$key = " . $this->_sql_validate_value($var);
1.368 + }
1.369 + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values);
1.370 + }
1.371 +
1.372 + return $query;
1.373 + }
1.374 +
1.375 + /**
1.376 + * Build IN or NOT IN sql comparison string, uses <> or = on single element
1.377 + * arrays to improve comparison speed
1.378 + *
1.379 + * @access public
1.380 + * @param string $field name of the sql column that shall be compared
1.381 + * @param array $array array of values that are allowed (IN) or not allowed (NOT IN)
1.382 + * @param bool $negate true for NOT IN (), false for IN () (default)
1.383 + * @param bool $allow_empty_set If true, allow $array to be empty, this function will return 1=1 or 1=0 then. Default to false.
1.384 + */
1.385 + function sql_in_set($field, $array, $negate = false, $allow_empty_set = false)
1.386 + {
1.387 + if (!sizeof($array))
1.388 + {
1.389 + if (!$allow_empty_set)
1.390 + {
1.391 + // Print the backtrace to help identifying the location of the problematic code
1.392 + $this->sql_error('No values specified for SQL IN comparison');
1.393 + }
1.394 + else
1.395 + {
1.396 + // NOT IN () actually means everything so use a tautology
1.397 + if ($negate)
1.398 + {
1.399 + return '1=1';
1.400 + }
1.401 + // IN () actually means nothing so use a contradiction
1.402 + else
1.403 + {
1.404 + return '1=0';
1.405 + }
1.406 + }
1.407 + }
1.408 +
1.409 + if (!is_array($array))
1.410 + {
1.411 + $array = array($array);
1.412 + }
1.413 +
1.414 + if (sizeof($array) == 1)
1.415 + {
1.416 + @reset($array);
1.417 + $var = current($array);
1.418 +
1.419 + return $field . ($negate ? ' <> ' : ' = ') . $this->_sql_validate_value($var);
1.420 + }
1.421 + else
1.422 + {
1.423 + return $field . ($negate ? ' NOT IN ' : ' IN ') . '(' . implode(', ', array_map(array($this, '_sql_validate_value'), $array)) . ')';
1.424 + }
1.425 + }
1.426 +
1.427 + /**
1.428 + * Run binary AND operator on DB column.
1.429 + * Results in sql statement: "{$column_name} & (1 << {$bit}) {$compare}"
1.430 + *
1.431 + * @param string $column_name The column name to use
1.432 + * @param int $bit The value to use for the AND operator, will be converted to (1 << $bit). Is used by options, using the number schema... 0, 1, 2...29
1.433 + * @param string $compare Any custom SQL code after the check (for example "= 0")
1.434 + */
1.435 + function sql_bit_and($column_name, $bit, $compare = '')
1.436 + {
1.437 + if (method_exists($this, '_sql_bit_and'))
1.438 + {
1.439 + return $this->_sql_bit_and($column_name, $bit, $compare);
1.440 + }
1.441 +
1.442 + return $column_name . ' & ' . (1 << $bit) . (($compare) ? ' ' . $compare : '');
1.443 + }
1.444 +
1.445 + /**
1.446 + * Run binary OR operator on DB column.
1.447 + * Results in sql statement: "{$column_name} | (1 << {$bit}) {$compare}"
1.448 + *
1.449 + * @param string $column_name The column name to use
1.450 + * @param int $bit The value to use for the OR operator, will be converted to (1 << $bit). Is used by options, using the number schema... 0, 1, 2...29
1.451 + * @param string $compare Any custom SQL code after the check (for example "= 0")
1.452 + */
1.453 + function sql_bit_or($column_name, $bit, $compare = '')
1.454 + {
1.455 + if (method_exists($this, '_sql_bit_or'))
1.456 + {
1.457 + return $this->_sql_bit_or($column_name, $bit, $compare);
1.458 + }
1.459 +
1.460 + return $column_name . ' | ' . (1 << $bit) . (($compare) ? ' ' . $compare : '');
1.461 + }
1.462 +
1.463 + /**
1.464 + * Run more than one insert statement.
1.465 + *
1.466 + * @param string $table table name to run the statements on
1.467 + * @param array &$sql_ary multi-dimensional array holding the statement data.
1.468 + *
1.469 + * @return bool false if no statements were executed.
1.470 + * @access public
1.471 + */
1.472 + function sql_multi_insert($table, &$sql_ary)
1.473 + {
1.474 + if (!sizeof($sql_ary))
1.475 + {
1.476 + return false;
1.477 + }
1.478 +
1.479 + if ($this->multi_insert)
1.480 + {
1.481 + $ary = array();
1.482 + foreach ($sql_ary as $id => $_sql_ary)
1.483 + {
1.484 + // If by accident the sql array is only one-dimensional we build a normal insert statement
1.485 + if (!is_array($_sql_ary))
1.486 + {
1.487 + return $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $sql_ary));
1.488 + }
1.489 +
1.490 + $values = array();
1.491 + foreach ($_sql_ary as $key => $var)
1.492 + {
1.493 + $values[] = $this->_sql_validate_value($var);
1.494 + }
1.495 + $ary[] = '(' . implode(', ', $values) . ')';
1.496 + }
1.497 +
1.498 + return $this->sql_query('INSERT INTO ' . $table . ' ' . ' (' . implode(', ', array_keys($sql_ary[0])) . ') VALUES ' . implode(', ', $ary));
1.499 + }
1.500 + else
1.501 + {
1.502 + foreach ($sql_ary as $ary)
1.503 + {
1.504 + if (!is_array($ary))
1.505 + {
1.506 + return false;
1.507 + }
1.508 +
1.509 + $result = $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $ary));
1.510 +
1.511 + if (!$result)
1.512 + {
1.513 + return false;
1.514 + }
1.515 + }
1.516 + }
1.517 +
1.518 + return true;
1.519 + }
1.520 +
1.521 + /**
1.522 + * Function for validating values
1.523 + * @access private
1.524 + */
1.525 + function _sql_validate_value($var)
1.526 + {
1.527 + if (is_null($var))
1.528 + {
1.529 + return 'NULL';
1.530 + }
1.531 + else if (is_string($var))
1.532 + {
1.533 + return "'" . $this->sql_escape($var) . "'";
1.534 + }
1.535 + else
1.536 + {
1.537 + return (is_bool($var)) ? intval($var) : $var;
1.538 + }
1.539 + }
1.540 +
1.541 + /**
1.542 + * Build sql statement from array for select and select distinct statements
1.543 + *
1.544 + * Possible query values: SELECT, SELECT_DISTINCT
1.545 + */
1.546 + function sql_build_query($query, $array)
1.547 + {
1.548 + $sql = '';
1.549 + switch ($query)
1.550 + {
1.551 + case 'SELECT':
1.552 + case 'SELECT_DISTINCT';
1.553 +
1.554 + $sql = str_replace('_', ' ', $query) . ' ' . $array['SELECT'] . ' FROM ';
1.555 +
1.556 + // Build table array. We also build an alias array for later checks.
1.557 + $table_array = $aliases = array();
1.558 + $used_multi_alias = false;
1.559 +
1.560 + foreach ($array['FROM'] as $table_name => $alias)
1.561 + {
1.562 + if (is_array($alias))
1.563 + {
1.564 + $used_multi_alias = true;
1.565 +
1.566 + foreach ($alias as $multi_alias)
1.567 + {
1.568 + $table_array[] = $table_name . ' ' . $multi_alias;
1.569 + $aliases[] = $multi_alias;
1.570 + }
1.571 + }
1.572 + else
1.573 + {
1.574 + $table_array[] = $table_name . ' ' . $alias;
1.575 + $aliases[] = $alias;
1.576 + }
1.577 + }
1.578 +
1.579 + // We run the following code to determine if we need to re-order the table array. ;)
1.580 + // The reason for this is that for multi-aliased tables (two equal tables) in the FROM statement the last table need to match the first comparison.
1.581 + // DBMS who rely on this: Oracle, PostgreSQL and MSSQL. For all other DBMS it makes absolutely no difference in which order the table is.
1.582 + if (!empty($array['LEFT_JOIN']) && sizeof($array['FROM']) > 1 && $used_multi_alias !== false)
1.583 + {
1.584 + // Take first LEFT JOIN
1.585 + $join = current($array['LEFT_JOIN']);
1.586 +
1.587 + // Determine the table used there (even if there are more than one used, we only want to have one
1.588 + preg_match('/(' . implode('|', $aliases) . ')\.[^\s]+/U', str_replace(array('(', ')', 'AND', 'OR', ' '), '', $join['ON']), $matches);
1.589 +
1.590 + // If there is a first join match, we need to make sure the table order is correct
1.591 + if (!empty($matches[1]))
1.592 + {
1.593 + $first_join_match = trim($matches[1]);
1.594 + $table_array = $last = array();
1.595 +
1.596 + foreach ($array['FROM'] as $table_name => $alias)
1.597 + {
1.598 + if (is_array($alias))
1.599 + {
1.600 + foreach ($alias as $multi_alias)
1.601 + {
1.602 + ($multi_alias === $first_join_match) ? $last[] = $table_name . ' ' . $multi_alias : $table_array[] = $table_name . ' ' . $multi_alias;
1.603 + }
1.604 + }
1.605 + else
1.606 + {
1.607 + ($alias === $first_join_match) ? $last[] = $table_name . ' ' . $alias : $table_array[] = $table_name . ' ' . $alias;
1.608 + }
1.609 + }
1.610 +
1.611 + $table_array = array_merge($table_array, $last);
1.612 + }
1.613 + }
1.614 +
1.615 + $sql .= $this->_sql_custom_build('FROM', implode(' CROSS JOIN ', $table_array));
1.616 +
1.617 + if (!empty($array['LEFT_JOIN']))
1.618 + {
1.619 + foreach ($array['LEFT_JOIN'] as $join)
1.620 + {
1.621 + $sql .= ' LEFT JOIN ' . key($join['FROM']) . ' ' . current($join['FROM']) . ' ON (' . $join['ON'] . ')';
1.622 + }
1.623 + }
1.624 +
1.625 + if (!empty($array['WHERE']))
1.626 + {
1.627 + $sql .= ' WHERE ' . $this->_sql_custom_build('WHERE', $array['WHERE']);
1.628 + }
1.629 +
1.630 + if (!empty($array['GROUP_BY']))
1.631 + {
1.632 + $sql .= ' GROUP BY ' . $array['GROUP_BY'];
1.633 + }
1.634 +
1.635 + if (!empty($array['ORDER_BY']))
1.636 + {
1.637 + $sql .= ' ORDER BY ' . $array['ORDER_BY'];
1.638 + }
1.639 +
1.640 + break;
1.641 + }
1.642 +
1.643 + return $sql;
1.644 + }
1.645 +
1.646 + /**
1.647 + * display sql error page
1.648 + */
1.649 + function sql_error($sql = '')
1.650 + {
1.651 + global $auth, $user, $config;
1.652 +
1.653 + // Set var to retrieve errored status
1.654 + $this->sql_error_triggered = true;
1.655 + $this->sql_error_sql = $sql;
1.656 +
1.657 + $this->sql_error_returned = $this->_sql_error();
1.658 +
1.659 + if (!$this->return_on_error)
1.660 + {
1.661 + $message = 'SQL ERROR [ ' . $this->sql_layer . ' ]<br /><br />' . $this->sql_error_returned['message'] . ' [' . $this->sql_error_returned['code'] . ']';
1.662 +
1.663 + // Show complete SQL error and path to administrators only
1.664 + // Additionally show complete error on installation or if extended debug mode is enabled
1.665 + // The DEBUG_EXTRA constant is for development only!
1.666 + if ((isset($auth) && $auth->acl_get('a_')) || defined('IN_INSTALL') || defined('DEBUG_EXTRA'))
1.667 + {
1.668 + // Print out a nice backtrace...
1.669 + $backtrace = get_backtrace();
1.670 +
1.671 + $message .= ($sql) ? '<br /><br />SQL<br /><br />' . htmlspecialchars($sql) : '';
1.672 + $message .= ($backtrace) ? '<br /><br />BACKTRACE<br />' . $backtrace : '';
1.673 + $message .= '<br />';
1.674 + }
1.675 + else
1.676 + {
1.677 + // If error occurs in initiating the session we need to use a pre-defined language string
1.678 + // This could happen if the connection could not be established for example (then we are not able to grab the default language)
1.679 + if (!isset($user->lang['SQL_ERROR_OCCURRED']))
1.680 + {
1.681 + $message .= '<br /><br />An sql error occurred while fetching this page. Please contact an administrator if this problem persists.';
1.682 + }
1.683 + else
1.684 + {
1.685 + if (!empty($config['board_contact']))
1.686 + {
1.687 + $message .= '<br /><br />' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>');
1.688 + }
1.689 + else
1.690 + {
1.691 + $message .= '<br /><br />' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '', '');
1.692 + }
1.693 + }
1.694 + }
1.695 +
1.696 + if ($this->transaction)
1.697 + {
1.698 + $this->sql_transaction('rollback');
1.699 + }
1.700 +
1.701 + if (strlen($message) > 1024)
1.702 + {
1.703 + // We need to define $msg_long_text here to circumvent text stripping.
1.704 + global $msg_long_text;
1.705 + $msg_long_text = $message;
1.706 +
1.707 + trigger_error(false, E_USER_ERROR);
1.708 + }
1.709 +
1.710 + trigger_error($message, E_USER_ERROR);
1.711 + }
1.712 +
1.713 + if ($this->transaction)
1.714 + {
1.715 + $this->sql_transaction('rollback');
1.716 + }
1.717 +
1.718 + return $this->sql_error_returned;
1.719 + }
1.720 +
1.721 + /**
1.722 + * Explain queries
1.723 + */
1.724 + function sql_report($mode, $query = '')
1.725 + {
1.726 + global $cache, $starttime, $phpbb_root_path, $user;
1.727 +
1.728 + if (empty($_REQUEST['explain']))
1.729 + {
1.730 + return false;
1.731 + }
1.732 +
1.733 + if (!$query && $this->query_hold != '')
1.734 + {
1.735 + $query = $this->query_hold;
1.736 + }
1.737 +
1.738 + switch ($mode)
1.739 + {
1.740 + case 'display':
1.741 + if (!empty($cache))
1.742 + {
1.743 + $cache->unload();
1.744 + }
1.745 + $this->sql_close();
1.746 +
1.747 + $mtime = explode(' ', microtime());
1.748 + $totaltime = $mtime[0] + $mtime[1] - $starttime;
1.749 +
1.750 + echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1.751 + <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
1.752 + <head>
1.753 + <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
1.754 + <meta http-equiv="Content-Style-Type" content="text/css" />
1.755 + <meta http-equiv="imagetoolbar" content="no" />
1.756 + <title>SQL Report</title>
1.757 + <link href="' . $phpbb_root_path . 'adm/style/admin.css" rel="stylesheet" type="text/css" media="screen" />
1.758 + </head>
1.759 + <body id="errorpage">
1.760 + <div id="wrap">
1.761 + <div id="page-header">
1.762 + <a href="' . build_url('explain') . '">Return to previous page</a>
1.763 + </div>
1.764 + <div id="page-body">
1.765 + <div id="acp">
1.766 + <div class="panel">
1.767 + <span class="corners-top"><span></span></span>
1.768 + <div id="content">
1.769 + <h1>SQL Report</h1>
1.770 + <br />
1.771 + <p><b>Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries['normal']} queries" . (($this->num_queries['cached']) ? " + {$this->num_queries['cached']} " . (($this->num_queries['cached'] == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '</b></p>
1.772 +
1.773 + <p>Time spent on ' . $this->sql_layer . ' queries: <b>' . round($this->sql_time, 5) . 's</b> | Time spent on PHP: <b>' . round($totaltime - $this->sql_time, 5) . 's</b></p>
1.774 +
1.775 + <br /><br />
1.776 + ' . $this->sql_report . '
1.777 + </div>
1.778 + <span class="corners-bottom"><span></span></span>
1.779 + </div>
1.780 + </div>
1.781 + </div>
1.782 + <div id="page-footer">
1.783 + Powered by <a href="http://www.phpbb.com/">phpBB</a>® Forum Software © phpBB Group
1.784 + </div>
1.785 + </div>
1.786 + </body>
1.787 + </html>';
1.788 +
1.789 + exit_handler();
1.790 +
1.791 + break;
1.792 +
1.793 + case 'stop':
1.794 + $endtime = explode(' ', microtime());
1.795 + $endtime = $endtime[0] + $endtime[1];
1.796 +
1.797 + $this->sql_report .= '
1.798 +
1.799 + <table cellspacing="1">
1.800 + <thead>
1.801 + <tr>
1.802 + <th>Query #' . $this->num_queries['total'] . '</th>
1.803 + </tr>
1.804 + </thead>
1.805 + <tbody>
1.806 + <tr>
1.807 + <td class="row3"><textarea style="font-family:\'Courier New\',monospace;width:99%" rows="5" cols="10">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td>
1.808 + </tr>
1.809 + </tbody>
1.810 + </table>
1.811 +
1.812 + ' . $this->html_hold . '
1.813 +
1.814 + <p style="text-align: center;">
1.815 + ';
1.816 +
1.817 + if ($this->query_result)
1.818 + {
1.819 + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query))
1.820 + {
1.821 + $this->sql_report .= 'Affected rows: <b>' . $this->sql_affectedrows($this->query_result) . '</b> | ';
1.822 + }
1.823 + $this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: <b>' . sprintf('%.5f', $endtime - $this->curtime) . 's</b>';
1.824 + }
1.825 + else
1.826 + {
1.827 + $error = $this->sql_error();
1.828 + $this->sql_report .= '<b style="color: red">FAILED</b> - ' . $this->sql_layer . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']);
1.829 + }
1.830 +
1.831 + $this->sql_report .= '</p><br /><br />';
1.832 +
1.833 + $this->sql_time += $endtime - $this->curtime;
1.834 + break;
1.835 +
1.836 + case 'start':
1.837 + $this->query_hold = $query;
1.838 + $this->html_hold = '';
1.839 +
1.840 + $this->_sql_report($mode, $query);
1.841 +
1.842 + $this->curtime = explode(' ', microtime());
1.843 + $this->curtime = $this->curtime[0] + $this->curtime[1];
1.844 +
1.845 + break;
1.846 +
1.847 + case 'add_select_row':
1.848 +
1.849 + $html_table = func_get_arg(2);
1.850 + $row = func_get_arg(3);
1.851 +
1.852 + if (!$html_table && sizeof($row))
1.853 + {
1.854 + $html_table = true;
1.855 + $this->html_hold .= '<table cellspacing="1"><tr>';
1.856 +
1.857 + foreach (array_keys($row) as $val)
1.858 + {
1.859 + $this->html_hold .= '<th>' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '</th>';
1.860 + }
1.861 + $this->html_hold .= '</tr>';
1.862 + }
1.863 + $this->html_hold .= '<tr>';
1.864 +
1.865 + $class = 'row1';
1.866 + foreach (array_values($row) as $val)
1.867 + {
1.868 + $class = ($class == 'row1') ? 'row2' : 'row1';
1.869 + $this->html_hold .= '<td class="' . $class . '">' . (($val) ? $val : ' ') . '</td>';
1.870 + }
1.871 + $this->html_hold .= '</tr>';
1.872 +
1.873 + return $html_table;
1.874 +
1.875 + break;
1.876 +
1.877 + case 'fromcache':
1.878 +
1.879 + $this->_sql_report($mode, $query);
1.880 +
1.881 + break;
1.882 +
1.883 + case 'record_fromcache':
1.884 +
1.885 + $endtime = func_get_arg(2);
1.886 + $splittime = func_get_arg(3);
1.887 +
1.888 + $time_cache = $endtime - $this->curtime;
1.889 + $time_db = $splittime - $endtime;
1.890 + $color = ($time_db > $time_cache) ? 'green' : 'red';
1.891 +
1.892 + $this->sql_report .= '<table cellspacing="1"><thead><tr><th>Query results obtained from the cache</th></tr></thead><tbody><tr>';
1.893 + $this->sql_report .= '<td class="row3"><textarea style="font-family:\'Courier New\',monospace;width:99%" rows="5" cols="10">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></tbody></table>';
1.894 + $this->sql_report .= '<p style="text-align: center;">';
1.895 + $this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p><br /><br />';
1.896 +
1.897 + // Pad the start time to not interfere with page timing
1.898 + $starttime += $time_db;
1.899 +
1.900 + break;
1.901 +
1.902 + default:
1.903 +
1.904 + $this->_sql_report($mode, $query);
1.905 +
1.906 + break;
1.907 + }
1.908 +
1.909 + return true;
1.910 + }
1.911 +}
1.912 +
1.913 +/**
1.914 +* This variable holds the class name to use later
1.915 +*/
1.916 +$sql_db = (!empty($dbms)) ? 'dbal_' . basename($dbms) : 'dbal';
1.917 +
1.918 +?>
1.919 \ No newline at end of file