2005-04-22 17:44:36 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
2005-08-17 15:57:50 +00:00
|
|
|
* @package dbal
|
2005-04-22 17:44:36 +00:00
|
|
|
* @version $Id$
|
|
|
|
* @copyright (c) 2005 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-03-19 14:23:21 +00:00
|
|
|
/**
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2005-04-22 17:44:36 +00:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
if (!defined('SQL_LAYER'))
|
|
|
|
{
|
|
|
|
|
2005-08-17 15:57:50 +00:00
|
|
|
define('SQL_LAYER', 'mysqli');
|
2006-08-01 16:14:14 +00:00
|
|
|
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
|
2005-04-22 17:44:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MySQLi Database Abstraction Layer
|
2005-11-28 18:38:49 +00:00
|
|
|
* mysqli-extension has to be compiled with:
|
|
|
|
* MySQL 4.1+ or MySQL 5.0+
|
2006-06-13 21:06:29 +00:00
|
|
|
* @package dbal
|
2005-04-22 17:44:36 +00:00
|
|
|
*/
|
2005-08-17 15:57:50 +00:00
|
|
|
class dbal_mysqli extends dbal
|
2005-04-22 17:44:36 +00:00
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Connect to server
|
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
|
|
|
|
{
|
|
|
|
$this->persistency = $persistency;
|
|
|
|
$this->user = $sqluser;
|
2006-04-08 17:34:04 +00:00
|
|
|
$this->server = $sqlserver;
|
2005-04-22 17:44:36 +00:00
|
|
|
$this->dbname = $database;
|
2006-04-08 17:34:04 +00:00
|
|
|
$port = (!$port) ? NULL : $port;
|
2005-04-22 17:44:36 +00:00
|
|
|
|
2006-04-08 17:34:04 +00:00
|
|
|
// Persistant connections not supported by the mysqli extension?
|
|
|
|
$this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port);
|
2005-04-22 17:44:36 +00:00
|
|
|
|
|
|
|
if ($this->db_connect_id && $this->dbname != '')
|
|
|
|
{
|
|
|
|
if (@mysqli_select_db($this->db_connect_id, $this->dbname))
|
|
|
|
{
|
|
|
|
return $this->db_connect_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->sql_error('');
|
|
|
|
}
|
|
|
|
|
2006-07-09 16:23:57 +00:00
|
|
|
/**
|
|
|
|
* Version information about used database
|
|
|
|
*/
|
|
|
|
function sql_server_info()
|
|
|
|
{
|
|
|
|
return 'MySQL(i) ' . @mysqli_get_server_info($this->db_connect_id);
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
2006-06-06 20:53:46 +00:00
|
|
|
* SQL Transaction
|
2006-06-13 21:06:29 +00:00
|
|
|
* @access: private
|
2005-10-07 23:00:41 +00:00
|
|
|
*/
|
2006-06-06 20:53:46 +00:00
|
|
|
function _sql_transaction($status = 'begin')
|
2005-04-22 17:44:36 +00:00
|
|
|
{
|
|
|
|
switch ($status)
|
|
|
|
{
|
|
|
|
case 'begin':
|
2006-06-06 20:53:46 +00:00
|
|
|
return @mysqli_autocommit($this->db_connect_id, false);
|
2006-06-02 13:26:27 +00:00
|
|
|
break;
|
2005-04-22 17:44:36 +00:00
|
|
|
|
|
|
|
case 'commit':
|
|
|
|
$result = @mysqli_commit($this->db_connect_id);
|
|
|
|
@mysqli_autocommit($this->db_connect_id, true);
|
2006-06-06 20:53:46 +00:00
|
|
|
return $result;
|
2006-06-02 13:26:27 +00:00
|
|
|
break;
|
2005-04-22 17:44:36 +00:00
|
|
|
|
|
|
|
case 'rollback':
|
|
|
|
$result = @mysqli_rollback($this->db_connect_id);
|
|
|
|
@mysqli_autocommit($this->db_connect_id, true);
|
2006-06-06 20:53:46 +00:00
|
|
|
return $result;
|
2006-06-02 13:26:27 +00:00
|
|
|
break;
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 20:53:46 +00:00
|
|
|
return true;
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Base query method
|
2006-08-01 16:14:14 +00:00
|
|
|
*
|
|
|
|
* @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
|
2005-10-07 23:00:41 +00:00
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
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;
|
2006-05-20 13:20:38 +00:00
|
|
|
$this->sql_add_num_queries($this->query_result);
|
|
|
|
|
2005-04-22 17:44:36 +00:00
|
|
|
if (!$this->query_result)
|
|
|
|
{
|
|
|
|
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) ? $this->query_result : false;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Build LIMIT query
|
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
|
2006-06-02 13:26:27 +00:00
|
|
|
{
|
|
|
|
if ($query != '')
|
2005-04-22 17:44:36 +00:00
|
|
|
{
|
2006-06-02 13:26:27 +00:00
|
|
|
$this->query_result = false;
|
2005-04-22 17:44:36 +00:00
|
|
|
|
|
|
|
// if $total is set to 0 we do not want to limit the number of rows
|
|
|
|
if ($total == 0)
|
|
|
|
{
|
2005-11-21 09:24:58 +00:00
|
|
|
// MySQL 4.1+ no longer supports -1 in limit queries
|
2006-01-01 21:23:33 +00:00
|
|
|
$total = '18446744073709551615';
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
|
|
|
|
|
2006-06-02 13:26:27 +00:00
|
|
|
return $this->sql_query($query, $cache_ttl);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Return number of rows
|
|
|
|
* Not used within core code
|
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
function sql_numrows($query_id = false)
|
|
|
|
{
|
2006-08-01 16:14:14 +00:00
|
|
|
global $cache;
|
|
|
|
|
2005-04-22 17:44:36 +00:00
|
|
|
if (!$query_id)
|
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
}
|
|
|
|
|
2006-08-02 09:13:39 +00:00
|
|
|
if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
|
2006-08-01 16:14:14 +00:00
|
|
|
{
|
|
|
|
return $cache->sql_numrows($query_id);
|
|
|
|
}
|
|
|
|
|
2005-04-22 17:44:36 +00:00
|
|
|
return ($query_id) ? @mysqli_num_rows($query_id) : false;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Return number of affected rows
|
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
function sql_affectedrows()
|
|
|
|
{
|
|
|
|
return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Fetch current row
|
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
function sql_fetchrow($query_id = false)
|
|
|
|
{
|
|
|
|
global $cache;
|
|
|
|
|
|
|
|
if (!$query_id)
|
|
|
|
{
|
|
|
|
$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) ? @mysqli_fetch_assoc($query_id) : false;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Fetch field
|
|
|
|
* if rownum is false, the current row is used, else it is pointing to the row (zero-based)
|
|
|
|
*/
|
|
|
|
function sql_fetchfield($field, $rownum = false, $query_id = false)
|
2005-04-22 17:44:36 +00:00
|
|
|
{
|
2006-08-01 16:14:14 +00:00
|
|
|
global $cache;
|
|
|
|
|
2005-04-22 17:44:36 +00:00
|
|
|
if (!$query_id)
|
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($query_id)
|
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
if ($rownum !== false)
|
2005-04-22 17:44:36 +00:00
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
$this->sql_rowseek($rownum, $query_id);
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
2006-06-02 13:26:27 +00:00
|
|
|
|
2006-08-02 09:13:39 +00:00
|
|
|
if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
|
2006-08-01 16:14:14 +00:00
|
|
|
{
|
|
|
|
return $cache->sql_fetchfield($query_id, $field);
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
$row = $this->sql_fetchrow($query_id);
|
|
|
|
return isset($row[$field]) ? $row[$field] : false;
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Seek to given row number
|
|
|
|
* rownum is zero-based
|
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
function sql_rowseek($rownum, $query_id = false)
|
|
|
|
{
|
2006-08-01 16:14:14 +00:00
|
|
|
global $cache;
|
|
|
|
|
2005-04-22 17:44:36 +00:00
|
|
|
if (!$query_id)
|
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
}
|
|
|
|
|
2006-08-02 09:13:39 +00:00
|
|
|
if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
|
2006-08-01 16:14:14 +00:00
|
|
|
{
|
|
|
|
return $cache->sql_rowseek($query_id, $rownum);
|
|
|
|
}
|
|
|
|
|
2005-04-22 17:44:36 +00:00
|
|
|
return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Get last inserted id after insert statement
|
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
function sql_nextid()
|
|
|
|
{
|
|
|
|
return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Free sql result
|
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
function sql_freeresult($query_id = false)
|
|
|
|
{
|
2006-08-01 16:14:14 +00:00
|
|
|
global $cache;
|
|
|
|
|
2005-04-22 17:44:36 +00:00
|
|
|
if (!$query_id)
|
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
}
|
|
|
|
|
2006-08-02 09:13:39 +00:00
|
|
|
if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
|
2005-04-22 17:44:36 +00:00
|
|
|
{
|
2006-08-01 16:14:14 +00:00
|
|
|
return $cache->sql_freeresult($query_id);
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
2005-10-07 23:00:41 +00:00
|
|
|
|
2006-08-01 16:14:14 +00:00
|
|
|
return @mysqli_free_result($query_id);
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Escape string used in sql query
|
|
|
|
*/
|
2005-04-22 17:44:36 +00:00
|
|
|
function sql_escape($msg)
|
|
|
|
{
|
|
|
|
return @mysqli_real_escape_string($this->db_connect_id, $msg);
|
|
|
|
}
|
2006-05-05 22:06:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build db-specific query data
|
2006-06-13 21:06:29 +00:00
|
|
|
* @access: private
|
2006-05-05 22:06:17 +00:00
|
|
|
*/
|
|
|
|
function _sql_custom_build($stage, $data)
|
|
|
|
{
|
|
|
|
switch ($stage)
|
|
|
|
{
|
|
|
|
case 'FROM':
|
|
|
|
$data = '(' . $data . ')';
|
|
|
|
break;
|
|
|
|
}
|
2006-06-02 13:26:27 +00:00
|
|
|
|
2006-05-05 22:06:17 +00:00
|
|
|
return $data;
|
|
|
|
}
|
2006-06-02 13:26:27 +00:00
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* return sql error array
|
2006-06-13 21:06:29 +00:00
|
|
|
* @access: private
|
2005-10-07 23:00:41 +00:00
|
|
|
*/
|
|
|
|
function _sql_error()
|
2005-04-22 17:44:36 +00:00
|
|
|
{
|
2006-04-08 17:34:04 +00:00
|
|
|
if (!$this->db_connect_id)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'message' => @mysqli_connect_error(),
|
|
|
|
'code' => @mysqli_connect_errno()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-08-17 15:57:50 +00:00
|
|
|
return array(
|
2005-04-22 17:44:36 +00:00
|
|
|
'message' => @mysqli_error($this->db_connect_id),
|
|
|
|
'code' => @mysqli_errno($this->db_connect_id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Close sql connection
|
2006-06-13 21:06:29 +00:00
|
|
|
* @access: private
|
2005-10-07 23:00:41 +00:00
|
|
|
*/
|
|
|
|
function _sql_close()
|
2005-04-22 17:44:36 +00:00
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
return @mysqli_close($this->db_connect_id);
|
|
|
|
}
|
2005-04-22 17:44:36 +00:00
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Build db-specific report
|
2006-06-13 21:06:29 +00:00
|
|
|
* @access: private
|
2005-10-07 23:00:41 +00:00
|
|
|
*/
|
|
|
|
function _sql_report($mode, $query = '')
|
|
|
|
{
|
2005-04-22 17:44:36 +00:00
|
|
|
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];
|
|
|
|
}
|
2005-10-07 23:00:41 +00:00
|
|
|
else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
|
2005-04-22 17:44:36 +00:00
|
|
|
{
|
|
|
|
$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/^SELECT/', $explain_query))
|
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
$html_table = false;
|
2005-04-22 17:44:36 +00:00
|
|
|
|
|
|
|
if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
|
|
|
|
{
|
|
|
|
while ($row = @mysqli_fetch_assoc($result))
|
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-07 23:00:41 +00:00
|
|
|
@mysqli_free_result($result);
|
2005-04-22 17:44:36 +00:00
|
|
|
|
|
|
|
if ($html_table)
|
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
$this->html_hold .= '</table>';
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
break;
|
2005-04-22 17:44:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2005-10-07 23:00:41 +00:00
|
|
|
@mysqli_free_result($result);
|
|
|
|
|
2005-04-22 17:44:36 +00:00
|
|
|
$splittime = explode(' ', microtime());
|
|
|
|
$splittime = $splittime[0] + $splittime[1];
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
2005-04-22 17:44:36 +00:00
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
break;
|
2005-04-22 17:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
2005-08-17 15:57:50 +00:00
|
|
|
}
|
2005-04-22 17:44:36 +00:00
|
|
|
|
|
|
|
} // if ... define
|
|
|
|
|
|
|
|
?>
|