Updated DBAL

git-svn-id: file:///svn/phpbb/trunk@9244 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2008-12-30 17:19:17 +00:00
parent 4cd13cf424
commit 96721d4bf1
2 changed files with 1023 additions and 439 deletions

File diff suppressed because it is too large Load diff

View file

@ -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;
return @mysqli_fetch_assoc($query_id);
}
if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
/**
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
*/
protected function _sql_freeresult($query_id)
{
return $cache->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;