@@ -860,11 +1334,205 @@ class dbal
return true;
}
+
+ /**
+ * Get stored data from SQL cache and fill the relevant cach rowset.
+ *
+ * @param string $query The query cached.
+ *
+ * @return bool True if the caching was successful, else false
+ * @access private
+ */
+ private function sql_get_cache($query)
+ {
+ if (!phpbb::registered('acm') || !phpbb::$acm->supported('sql'))
+ {
+ return false;
+ }
+
+ // Remove extra spaces and tabs
+ $var_name = preg_replace('/[\n\r\s\t]+/', ' ', $query);
+ $var_name = md5($this->sql_layer . '_' . $var_name);
+
+ $data = phpbb::$acm->get($var_name, 'sql');
+
+ if ($data !== false)
+ {
+ $this->query_result = ++$this->cache_index;
+ $this->cache_rowset[$this->query_result] = $data['rowset'];
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Put query to cache.
+ *
+ * @param string $query The query cached.
+ * @param int $cache_ttl Cache lifetime in seconds.
+ *
+ * @return bool True if the caching was successful, else false
+ * @access private
+ */
+ private function sql_put_cache($query, $cache_ttl)
+ {
+ if (!phpbb::registered('acm') || !phpbb::$acm->supported('sql'))
+ {
+ return false;
+ }
+
+ // Prepare the data
+ $var_name = preg_replace('/[\n\r\s\t]+/', ' ', $query);
+ $var_name = md5($this->sql_layer . '_' . $var_name);
+
+ $data = array(
+ 'query' => $query,
+ 'rowset' => array(),
+ );
+
+ while ($row = $this->sql_fetchrow($this->query_result))
+ {
+ $data['rowset'][] = $row;
+ }
+ $this->sql_freeresult($this->query_result);
+
+ phpbb::$acm->put($var_name, $data, $cache_ttl, 'sql');
+
+ $this->query_result = ++$this->cache_index;
+ $this->cache_rowset[$this->query_result] = $data['rowset'];
+ @reset($this->cache_rowset[$this->query_result]);
+
+ return true;
+ }
+
+ /**
+ * Check if an sql cache exist for a specific query id.
+ *
+ * @param int $query_id The query_id to check (int)
+ *
+ * @return bool True if an cache entry exists.
+ * @access private
+ */
+ private function sql_cache_exists($query_id)
+ {
+ return is_int($query_id) && isset($this->cache_rowset[$query_id]);
+ }
+
+ /**
+ * Fetch row from cache (database). Used in {@link phpbb_dbal::sql_fetchrow() sql_fetchrow()}.
+ *
+ * @param int $query_id The query_id to fetch from.
+ *
+ * @return array The result row
+ * @access private
+ */
+ private function sql_cache_fetchrow($query_id)
+ {
+ list(, $row) = each($this->cache_rowset[$query_id]);
+ return ($row !== NULL) ? $row : false;
+ }
+
+ /**
+ * Fetch a field from the current row of a cached database result (database). Used in {@link phpbb_dbal::sql_fetchfield() sql_fetchfield()}.
+ *
+ * @param int $query_id The query_id to fetch from.
+ * @param string $field The column name.
+ *
+ * @return array The field data
+ * @access private
+ */
+ private function sql_cache_fetchfield($query_id, $field)
+ {
+ $row = current($this->cache_rowset[$query_id]);
+ return ($row !== false && isset($row[$field])) ? $row[$field] : false;
+ }
+
+ /**
+ * Free memory used for a cached database result (database). Used in {@link phpbb_dbal::sql_freeresult() sql_freeresult()}.
+ *
+ * @param int $query_id The query_id.
+ *
+ * @return bool True on success
+ * @access private
+ */
+ private function sql_cache_freeresult($query_id)
+ {
+ if (!isset($this->cache_rowset[$query_id]))
+ {
+ return false;
+ }
+
+ if (($key = $this->sql_get_result_key($query_id)) !== false)
+ {
+ unset($this->open_queries[$key]);
+ }
+
+ unset($this->cache_rowset[$query_id]);
+ return true;
+ }
+
+ /**
+ * Function for validating SQL values
+ *
+ * @param mixed Value. Typecasted to it's type.
+ *
+ * @return mixed Typecasted value.
+ * @access private
+ */
+ private function _sql_validate_value($var)
+ {
+ if (is_null($var))
+ {
+ return 'NULL';
+ }
+ else if (is_string($var))
+ {
+ return "'" . $this->sql_escape($var) . "'";
+ }
+ else
+ {
+ return (is_bool($var)) ? intval($var) : $var;
+ }
+ }
+
+ /**
+ * Add "one" to query count.
+ *
+ * @param bool $cached If true, add one to cached query count. Otherwise to non-cached query count
+ * @access private
+ */
+ private function sql_add_num_queries($cached = false)
+ {
+ $this->num_queries['cached'] += ($cached !== false) ? 1 : 0;
+ $this->num_queries['normal'] += ($cached !== false) ? 0 : 1;
+ $this->num_queries['total'] += 1;
+ }
+
+ /**
+ * Get SQL result key for storing open connection
+ *
+ * @param string $query_result Query result id/resource/object
+ *
+ * @return mixed Key usable as array key. False if storing is not possible.
+ * @access private
+ */
+ private function sql_get_result_key($query_result)
+ {
+ $key = $query_result;
+
+ if (is_object($query_result))
+ {
+ $key = false;
+ }
+ else if (is_resource($query_result))
+ {
+ $key = (int) $query_result;
+ }
+
+ return $key;
+ }
}
-/**
-* This variable holds the class name to use later
-*/
-$sql_db = (!empty($dbms)) ? 'dbal_' . basename($dbms) : 'dbal';
-
?>
\ No newline at end of file
diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php
index 11030508bd..3c153e8dfe 100644
--- a/phpBB/includes/db/mysqli.php
+++ b/phpBB/includes/db/mysqli.php
@@ -16,8 +16,6 @@ if (!defined('IN_PHPBB'))
exit;
}
-include_once(PHPBB_ROOT_PATH . 'includes/db/dbal.' . PHP_EXT);
-
/**
* MySQLi Database Abstraction Layer
* Compatible with:
@@ -25,28 +23,26 @@ include_once(PHPBB_ROOT_PATH . 'includes/db/dbal.' . PHP_EXT);
* MySQL 5.0+
* @package dbal
*/
-class dbal_mysqli extends dbal
+class phpbb_dbal_mysqli extends phpbb_dbal
{
- var $multi_insert = true;
-
- // Supports multiple table deletion
- var $multi_table_deletion = true;
-
- var $dbms_type = 'mysql';
+ /**
+ * @var string Database type. No distinction between versions or used extensions.
+ */
+ public $dbms_type = 'mysql';
/**
- * Connect to server
+ * Connect to server. See {@link phpbb_dbal::sql_connect() sql_connect()} for details.
*/
- function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false , $new_link = false)
+ public function sql_connect($server, $user, $password, $database, $port = false, $persistency = false , $new_link = false)
{
$this->persistency = $persistency;
- $this->user = $sqluser;
- $this->server = $sqlserver;
+ $this->user = $user;
+ $this->server = $server;
$this->dbname = $database;
- $port = (!$port) ? NULL : $port;
+ $this->port = (!$port) ? NULL : $port;
// Persistant connections not supported by the mysqli extension?
- $this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port);
+ $this->db_connect_id = @mysqli_connect($this->server, $this->user, $password, $this->dbname, $this->port);
if ($this->db_connect_id && $this->dbname != '')
{
@@ -85,15 +81,11 @@ class dbal_mysqli extends dbal
}
/**
- * Version information about used database
- * @param bool $raw if true, only return the fetched sql_server_version
- * @return string sql server version
+ * Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details.
*/
- function sql_server_info($raw = false)
+ public function sql_server_info($raw = false)
{
- global $cache;
-
- if (empty($cache) || ($this->sql_server_version = $cache->get('mysqli_version')) === false)
+ if (!phpbb::registered('acm') || ($this->sql_server_version = phpbb::$acm->get('#mysqli_version')) === false)
{
$result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version');
$row = @mysqli_fetch_assoc($result);
@@ -101,9 +93,9 @@ class dbal_mysqli extends dbal
$this->sql_server_version = $row['version'];
- if (!empty($cache))
+ if (phpbb::registered('acm'))
{
- $cache->put('mysqli_version', $this->sql_server_version);
+ phpbb::$acm->put('#mysqli_version', $this->sql_server_version);
}
}
@@ -111,10 +103,41 @@ class dbal_mysqli extends dbal
}
/**
- * SQL Transaction
- * @access private
+ * DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
*/
- function _sql_transaction($status = 'begin')
+ protected function _sql_query($query)
+ {
+ return @mysqli_query($this->db_connect_id, $query);
+ }
+
+ /**
+ * Build LIMIT query and run it. See {@link phpbb_dbal::_sql_query_limit() _sql_query_limit()} for details.
+ */
+ protected function _sql_query_limit($query, $total, $offset, $cache_ttl)
+ {
+ // if $total is set to 0 we do not want to limit the number of rows
+ if ($total == 0)
+ {
+ // MySQL 4.1+ no longer supports -1 in limit queries
+ $total = '18446744073709551615';
+ }
+
+ $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
+ return $this->sql_query($query, $cache_ttl);
+ }
+
+ /**
+ * Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details.
+ */
+ protected function _sql_close()
+ {
+ return @mysqli_close($this->db_connect_id);
+ }
+
+ /**
+ * SQL Transaction. See {@link phpbb_dbal::_sql_transaction() _sql_transaction()} for details.
+ */
+ protected function _sql_transaction($status)
{
switch ($status)
{
@@ -139,146 +162,57 @@ class dbal_mysqli extends dbal
}
/**
- * Base query method
- *
- * @param string $query Contains the SQL query which shall be executed
- * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
- * @return mixed When casted to bool the returned value returns true on success and false on failure
- *
- * @access public
+ * Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details.
*/
- function sql_query($query = '', $cache_ttl = 0)
- {
- if ($query != '')
- {
- global $cache;
-
- // EXPLAIN only in extra debug mode
- if (defined('DEBUG_EXTRA'))
- {
- $this->sql_report('start', $query);
- }
-
- $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
- $this->sql_add_num_queries($this->query_result);
-
- if ($this->query_result === false)
- {
- if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
- {
- $this->sql_error($query);
- }
-
- if (defined('DEBUG_EXTRA'))
- {
- $this->sql_report('stop', $query);
- }
-
- if ($cache_ttl && method_exists($cache, 'sql_save'))
- {
- $cache->sql_save($query, $this->query_result, $cache_ttl);
- }
- }
- else if (defined('DEBUG_EXTRA'))
- {
- $this->sql_report('fromcache', $query);
- }
- }
- else
- {
- return false;
- }
-
- return $this->query_result;
- }
-
- /**
- * Build LIMIT query
- */
- function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
- {
- $this->query_result = false;
-
- // if $total is set to 0 we do not want to limit the number of rows
- if ($total == 0)
- {
- // MySQL 4.1+ no longer supports -1 in limit queries
- $total = '18446744073709551615';
- }
-
- $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
-
- return $this->sql_query($query, $cache_ttl);
- }
-
- /**
- * Return number of affected rows
- */
- function sql_affectedrows()
+ public function sql_affectedrows()
{
return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
}
/**
- * Fetch current row
+ * Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details.
*/
- function sql_fetchrow($query_id = false)
- {
- global $cache;
-
- if ($query_id === false)
- {
- $query_id = $this->query_result;
- }
-
- if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
- {
- return $cache->sql_fetchrow($query_id);
- }
-
- return ($query_id !== false) ? @mysqli_fetch_assoc($query_id) : false;
- }
-
- /**
- * Get last inserted id after insert statement
- */
- function sql_nextid()
+ public function sql_nextid()
{
return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
}
/**
- * Free sql result
+ * Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
*/
- function sql_freeresult($query_id = false)
+ protected function _sql_fetchrow($query_id)
{
- global $cache;
-
- if ($query_id === false)
- {
- $query_id = $this->query_result;
- }
-
- if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
- {
- return $cache->sql_freeresult($query_id);
- }
+ return @mysqli_fetch_assoc($query_id);
+ }
+ /**
+ * Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
+ */
+ protected function _sql_freeresult($query_id)
+ {
return @mysqli_free_result($query_id);
}
/**
- * Escape string used in sql query
+ * Correctly adjust LIKE expression for special characters. See {@link phpbb_dbal::_sql_like_expression() _sql_like_expression()} for details.
*/
- function sql_escape($msg)
+ protected function _sql_like_expression($expression)
+ {
+ return $expression;
+ }
+
+ /**
+ * Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
+ */
+ public function sql_escape($msg)
{
return @mysqli_real_escape_string($this->db_connect_id, $msg);
}
/**
- * Expose a DBMS specific function
+ * Expose a DBMS specific function. See {@link phpbb_dbal::sql_function() sql_function()} for details.
*/
- function sql_function($type, $col)
+ public function sql_function($type, $col)
{
switch ($type)
{
@@ -289,7 +223,10 @@ class dbal_mysqli extends dbal
}
}
- function sql_handle_data($type, $table, $data, $where = '')
+ /**
+ * Handle data by using prepared statements. See {@link phpbb_dbal::sql_handle_data() sql_handle_data()} for details.
+ */
+ public function sql_handle_data($type, $table, $data, $where = '')
{
if ($type === 'INSERT')
{
@@ -323,21 +260,10 @@ class dbal_mysqli extends dbal
mysqli_stmt_close($stmt);
}
-
/**
- * Build LIKE expression
- * @access private
+ * Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
*/
- function _sql_like_expression($expression)
- {
- return $expression;
- }
-
- /**
- * Build db-specific query data
- * @access private
- */
- function _sql_custom_build($stage, $data)
+ protected function _sql_custom_build($stage, $data)
{
switch ($stage)
{
@@ -350,10 +276,9 @@ class dbal_mysqli extends dbal
}
/**
- * return sql error array
- * @access private
+ * return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
*/
- function _sql_error()
+ protected function _sql_error()
{
if (!$this->db_connect_id)
{
@@ -369,20 +294,11 @@ class dbal_mysqli extends dbal
);
}
- /**
- * Close sql connection
- * @access private
- */
- function _sql_close()
- {
- return @mysqli_close($this->db_connect_id);
- }
/**
- * Build db-specific report
- * @access private
+ * Run DB-specific code to build SQL Report to explain queries, show statistics and runtime information. See {@link phpbb_dbal::_sql_report() _sql_report()} for details.
*/
- function _sql_report($mode, $query = '')
+ protected function _sql_report($mode, $query = '')
{
static $test_prof;