mirror of
https://github.com/phpbb/phpbb.git
synced 2025-05-09 00:55:23 +02:00
472 lines
12 KiB
PHP
472 lines
12 KiB
PHP
<?php
|
|
/**
|
|
*
|
|
* @package dbal
|
|
* @version $Id$
|
|
* @copyright (c) 2005 phpBB Group
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
if (!defined('IN_PHPBB'))
|
|
{
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* MySQLi Database Abstraction Layer
|
|
* Compatible with:
|
|
* MySQL 4.1+
|
|
* MySQL 5.0+
|
|
* @package dbal
|
|
*/
|
|
class phpbb_dbal_mysqli extends phpbb_dbal
|
|
{
|
|
/**
|
|
* @var string Database type. No distinction between versions or used extensions.
|
|
*/
|
|
public $dbms_type = 'mysql';
|
|
|
|
/**
|
|
* @var array Database type map, column layout information
|
|
*/
|
|
public $dbms_type_map = array(
|
|
'INT:' => 'int(%d)',
|
|
'BINT' => 'bigint(20)',
|
|
'UINT' => 'mediumint(8) UNSIGNED',
|
|
'UINT:' => 'int(%d) UNSIGNED',
|
|
'TINT:' => 'tinyint(%d)',
|
|
'USINT' => 'smallint(4) UNSIGNED',
|
|
'BOOL' => 'tinyint(1) UNSIGNED',
|
|
'VCHAR' => 'varchar(255)',
|
|
'VCHAR:' => 'varchar(%d)',
|
|
'CHAR:' => 'char(%d)',
|
|
'XSTEXT' => 'text',
|
|
'XSTEXT_UNI'=> 'varchar(100)',
|
|
'STEXT' => 'text',
|
|
'STEXT_UNI' => 'varchar(255)',
|
|
'TEXT' => 'text',
|
|
'TEXT_UNI' => 'text',
|
|
'MTEXT' => 'mediumtext',
|
|
'MTEXT_UNI' => 'mediumtext',
|
|
'TIMESTAMP' => 'int(11) UNSIGNED',
|
|
'DECIMAL' => 'decimal(5,2)',
|
|
'DECIMAL:' => 'decimal(%d,2)',
|
|
'PDECIMAL' => 'decimal(6,3)',
|
|
'PDECIMAL:' => 'decimal(%d,3)',
|
|
'VCHAR_UNI' => 'varchar(255)',
|
|
'VCHAR_UNI:'=> 'varchar(%d)',
|
|
'VARBINARY' => 'varbinary(255)',
|
|
);
|
|
|
|
/**
|
|
* Connect to server. See {@link phpbb_dbal::sql_connect() sql_connect()} for details.
|
|
*/
|
|
public function sql_connect($server, $user, $password, $database, $port = false, $persistency = false , $new_link = false)
|
|
{
|
|
$this->persistency = $persistency;
|
|
$this->user = $user;
|
|
$this->server = $server;
|
|
$this->dbname = $database;
|
|
$this->port = (!$port) ? NULL : $port;
|
|
|
|
// Persistant connections not supported by the mysqli extension?
|
|
$this->db_connect_id = @mysqli_connect($this->server, $this->user, $password, $this->dbname, $this->port);
|
|
|
|
if (!$this->db_connect_id || !$this->dbname)
|
|
{
|
|
return $this->sql_error('');
|
|
}
|
|
|
|
@mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
|
|
|
|
// enforce strict mode on databases that support it
|
|
if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
|
|
{
|
|
$result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
|
|
$row = @mysqli_fetch_assoc($result);
|
|
@mysqli_free_result($result);
|
|
|
|
$modes = array_map('trim', explode(',', $row['sql_mode']));
|
|
|
|
// TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
|
|
if (!in_array('TRADITIONAL', $modes))
|
|
{
|
|
if (!in_array('STRICT_ALL_TABLES', $modes))
|
|
{
|
|
$modes[] = 'STRICT_ALL_TABLES';
|
|
}
|
|
|
|
if (!in_array('STRICT_TRANS_TABLES', $modes))
|
|
{
|
|
$modes[] = 'STRICT_TRANS_TABLES';
|
|
}
|
|
}
|
|
|
|
$mode = implode(',', $modes);
|
|
@mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
|
|
}
|
|
|
|
return $this->db_connect_id;
|
|
}
|
|
|
|
/**
|
|
* Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details.
|
|
*/
|
|
public function sql_server_info($raw = 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);
|
|
@mysqli_free_result($result);
|
|
|
|
$this->sql_server_version = trim($row['version']);
|
|
|
|
if (phpbb::registered('acm'))
|
|
{
|
|
phpbb::$acm->put('#mysqli_version', $this->sql_server_version);
|
|
}
|
|
}
|
|
|
|
return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
|
|
}
|
|
|
|
/**
|
|
* DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
|
|
*/
|
|
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)
|
|
{
|
|
case 'begin':
|
|
return @mysqli_autocommit($this->db_connect_id, false);
|
|
break;
|
|
|
|
case 'commit':
|
|
$result = @mysqli_commit($this->db_connect_id);
|
|
@mysqli_autocommit($this->db_connect_id, true);
|
|
return $result;
|
|
break;
|
|
|
|
case 'rollback':
|
|
$result = @mysqli_rollback($this->db_connect_id);
|
|
@mysqli_autocommit($this->db_connect_id, true);
|
|
return $result;
|
|
break;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details.
|
|
*/
|
|
public function sql_affectedrows()
|
|
{
|
|
return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
|
|
}
|
|
|
|
/**
|
|
* Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details.
|
|
*/
|
|
public function sql_nextid()
|
|
{
|
|
return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
|
|
}
|
|
|
|
/**
|
|
* Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
|
|
*/
|
|
protected function _sql_fetchrow($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);
|
|
}
|
|
|
|
/**
|
|
* Correctly adjust LIKE expression for special characters. See {@link phpbb_dbal::_sql_like_expression() _sql_like_expression()} for details.
|
|
*/
|
|
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. See {@link phpbb_dbal::sql_function() sql_function()} for details.
|
|
*/
|
|
public function sql_function($type, $col)
|
|
{
|
|
switch ($type)
|
|
{
|
|
case 'length_varchar':
|
|
case 'length_text':
|
|
return 'LENGTH(' . $col . ')';
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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')
|
|
{
|
|
$stmt = mysqli_prepare($this->db_connect_id, "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES (" . substr(str_repeat('?, ', sizeof($data)) ,0, -1) . ')');
|
|
}
|
|
else
|
|
{
|
|
$query = "UPDATE $table SET ";
|
|
|
|
$set = array();
|
|
foreach (array_keys($data) as $key)
|
|
{
|
|
$set[] = "$key = ?";
|
|
}
|
|
$query .= implode(', ', $set);
|
|
|
|
if ($where !== '')
|
|
{
|
|
$query .= ' WHERE ' . $where;
|
|
}
|
|
|
|
$stmt = mysqli_prepare($this->db_connect_id, $query);
|
|
}
|
|
|
|
// get the stmt onto the top of the function arguments
|
|
array_unshift($data, $stmt);
|
|
|
|
call_user_func_array('mysqli_stmt_bind_param', $data);
|
|
mysqli_stmt_execute($stmt);
|
|
|
|
mysqli_stmt_close($stmt);
|
|
}
|
|
*/
|
|
|
|
/**
|
|
* Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
|
|
*/
|
|
protected function _sql_custom_build($stage, $data)
|
|
{
|
|
switch ($stage)
|
|
{
|
|
case 'FROM':
|
|
$data = '(' . $data . ')';
|
|
break;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
|
|
*/
|
|
protected function _sql_error()
|
|
{
|
|
if (!$this->db_connect_id)
|
|
{
|
|
return array(
|
|
'message' => @mysqli_connect_error(),
|
|
'code' => @mysqli_connect_errno()
|
|
);
|
|
}
|
|
|
|
return array(
|
|
'message' => @mysqli_error($this->db_connect_id),
|
|
'code' => @mysqli_errno($this->db_connect_id)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
protected function _sql_report($mode, $query = '')
|
|
{
|
|
static $test_prof;
|
|
static $test_extend;
|
|
|
|
// current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
|
|
if ($test_prof === null)
|
|
{
|
|
$test_prof = $test_extend = false;
|
|
if (version_compare($this->sql_server_info(true), '5.0.37', '>=') && version_compare($this->sql_server_info(true), '5.1', '<'))
|
|
{
|
|
$test_prof = true;
|
|
}
|
|
|
|
if (version_compare($this->sql_server_info(true), '4.1.1', '>='))
|
|
{
|
|
$test_extend = true;
|
|
}
|
|
}
|
|
|
|
switch ($mode)
|
|
{
|
|
case 'start':
|
|
|
|
$explain_query = $query;
|
|
if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
|
|
{
|
|
$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
|
|
}
|
|
else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
|
|
{
|
|
$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
|
|
}
|
|
|
|
if (preg_match('/^SELECT/', $explain_query))
|
|
{
|
|
$html_table = false;
|
|
|
|
// begin profiling
|
|
if ($test_prof)
|
|
{
|
|
@mysqli_query($this->db_connect_id, 'SET profiling = 1;');
|
|
}
|
|
|
|
if ($result = @mysqli_query($this->db_connect_id, 'EXPLAIN ' . (($test_extend) ? 'EXTENDED ' : '') . "$explain_query"))
|
|
{
|
|
while ($row = @mysqli_fetch_assoc($result))
|
|
{
|
|
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
|
}
|
|
}
|
|
@mysqli_free_result($result);
|
|
|
|
if ($html_table)
|
|
{
|
|
$this->html_hold .= '</table>';
|
|
}
|
|
|
|
if ($test_extend)
|
|
{
|
|
$html_table = false;
|
|
|
|
if ($result = @mysqli_query($this->db_connect_id, 'SHOW WARNINGS'))
|
|
{
|
|
$this->html_hold .= '<br />';
|
|
while ($row = @mysqli_fetch_assoc($result))
|
|
{
|
|
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
|
}
|
|
}
|
|
@mysqli_free_result($result);
|
|
|
|
if ($html_table)
|
|
{
|
|
$this->html_hold .= '</table>';
|
|
}
|
|
}
|
|
|
|
if ($test_prof)
|
|
{
|
|
$html_table = false;
|
|
|
|
// get the last profile
|
|
if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
|
|
{
|
|
$this->html_hold .= '<br />';
|
|
while ($row = @mysqli_fetch_assoc($result))
|
|
{
|
|
// make <unknown> HTML safe
|
|
if (!empty($row['Source_function']))
|
|
{
|
|
$row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']);
|
|
}
|
|
|
|
// remove unsupported features
|
|
foreach ($row as $key => $val)
|
|
{
|
|
if ($val === null)
|
|
{
|
|
unset($row[$key]);
|
|
}
|
|
}
|
|
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
|
}
|
|
}
|
|
@mysqli_free_result($result);
|
|
|
|
if ($html_table)
|
|
{
|
|
$this->html_hold .= '</table>';
|
|
}
|
|
|
|
@mysqli_query($this->db_connect_id, 'SET profiling = 0;');
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case 'fromcache':
|
|
$endtime = explode(' ', microtime());
|
|
$endtime = $endtime[0] + $endtime[1];
|
|
|
|
$result = @mysqli_query($this->db_connect_id, $query);
|
|
while ($void = @mysqli_fetch_assoc($result))
|
|
{
|
|
// Take the time spent on parsing rows into account
|
|
}
|
|
@mysqli_free_result($result);
|
|
|
|
$splittime = explode(' ', microtime());
|
|
$splittime = $splittime[0] + $splittime[1];
|
|
|
|
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|