mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-30 21:40:43 +02:00
Move trunk/phpBB to old_trunk/phpBB
git-svn-id: file:///svn/phpbb/trunk@10210 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
@@ -1,455 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package dbal
|
||||
* @version $Id$
|
||||
* @copyright (c) 2009 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
if (!defined('IN_PHPBB'))
|
||||
{
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* MSSQL Database Abstraction Layer
|
||||
* Minimum Requirement: DB2 8.2.2+
|
||||
* Minimum extension version: PECL ibm_db2 1.6.0+
|
||||
* @package dbal
|
||||
*/
|
||||
class phpbb_dbal_db2 extends phpbb_dbal
|
||||
{
|
||||
/**
|
||||
* @var string Database type. No distinction between versions or used extensions.
|
||||
*/
|
||||
public $dbms_type = 'db2';
|
||||
|
||||
/**
|
||||
* @var array Database type map, column layout information
|
||||
*/
|
||||
public $dbms_type_map = array(
|
||||
'INT:' => 'integer',
|
||||
'BINT' => 'float',
|
||||
'UINT' => 'integer',
|
||||
'UINT:' => 'integer',
|
||||
'TINT:' => 'smallint',
|
||||
'USINT' => 'smallint',
|
||||
'BOOL' => 'smallint',
|
||||
'VCHAR' => 'varchar(255)',
|
||||
'VCHAR:' => 'varchar(%d)',
|
||||
'CHAR:' => 'char(%d)',
|
||||
'XSTEXT' => 'clob(65K)',
|
||||
'STEXT' => 'varchar(3000)',
|
||||
'TEXT' => 'clob(65K)',
|
||||
'MTEXT' => 'clob(16M)',
|
||||
'XSTEXT_UNI'=> 'varchar(100)',
|
||||
'STEXT_UNI' => 'varchar(255)',
|
||||
'TEXT_UNI' => 'clob(65K)',
|
||||
'MTEXT_UNI' => 'clob(16M)',
|
||||
'TIMESTAMP' => 'integer',
|
||||
'DECIMAL' => 'float',
|
||||
'VCHAR_UNI' => 'varchar(255)',
|
||||
'VCHAR_UNI:'=> 'varchar(%d)',
|
||||
'VARBINARY' => 'varchar(255)',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var array Database features
|
||||
*/
|
||||
public $features = array(
|
||||
'multi_insert' => true,
|
||||
'count_distinct' => true,
|
||||
'multi_table_deletion' => true,
|
||||
'truncate' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* 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 . (($port) ? ':' . $port : '');
|
||||
$this->dbname = $database;
|
||||
$this->port = $port;
|
||||
|
||||
$this->db_connect_id = ($this->persistency) ? @db2_pconnect($this->dbname, $this->user, $password, array('autocommit' => DB2_AUTOCOMMIT_ON, 'DB2_ATTR_CASE' => DB2_CASE_LOWER)) : @db2_connect($this->dbname, $this->user, $password, array('autocommit' => DB2_AUTOCOMMIT_ON, 'DB2_ATTR_CASE' => DB2_CASE_LOWER));
|
||||
|
||||
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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('#db2_version')) === false)
|
||||
{
|
||||
$info = @db2_server_info($this->db_connect_id);
|
||||
|
||||
$this->sql_server_info = is_object($info) ? $info->DBMS_VER : 0;
|
||||
|
||||
if (phpbb::registered('acm'))
|
||||
{
|
||||
phpbb::$acm->put('#db2_version', $this->sql_server_version);
|
||||
}
|
||||
}
|
||||
|
||||
return ($raw) ? $this->sql_server_version : 'IBM DB2 ' . $this->sql_server_version;
|
||||
}
|
||||
|
||||
/**
|
||||
* DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
|
||||
*/
|
||||
protected function _sql_query($query)
|
||||
{
|
||||
$array = array();
|
||||
|
||||
// Cope with queries larger than 32K
|
||||
if (strlen($query) > 32740)
|
||||
{
|
||||
if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs))
|
||||
{
|
||||
if (strlen($regs[3]) > 32740)
|
||||
{
|
||||
preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
|
||||
|
||||
$inserts = $vals[0];
|
||||
unset($vals);
|
||||
|
||||
foreach ($inserts as $key => $value)
|
||||
{
|
||||
// check to see if this thing is greater than the max + 'x2
|
||||
if (!empty($value) && $value[0] === "'" && strlen($value) > 32742)
|
||||
{
|
||||
$inserts[$key] = '?';
|
||||
$array[] = str_replace("''", "'", substr($value, 1, -1));
|
||||
}
|
||||
}
|
||||
|
||||
$query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
|
||||
}
|
||||
}
|
||||
else if (preg_match_all('/^(UPDATE ([\\w_]++)\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data, PREG_SET_ORDER))
|
||||
{
|
||||
if (strlen($data[0][3]) > 32740)
|
||||
{
|
||||
$update = $data[0][1];
|
||||
$where = $data[0][4];
|
||||
preg_match_all('/(\\w++) = (\'(?:[^\']++|\'\')*+\'|\\d++)/', $data[0][3], $temp, PREG_SET_ORDER);
|
||||
unset($data);
|
||||
|
||||
$cols = array();
|
||||
foreach ($temp as $value)
|
||||
{
|
||||
// check to see if this thing is greater than the max + 'x2
|
||||
if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 32742)
|
||||
{
|
||||
$array[] = str_replace("''", "'", substr($value[2], 1, -1));
|
||||
$cols[] = $value[1] . '=?';
|
||||
}
|
||||
else
|
||||
{
|
||||
$cols[] = $value[1] . '=' . $value[2];
|
||||
}
|
||||
}
|
||||
|
||||
$query = $update . implode(', ', $cols) . ' ' . $where;
|
||||
unset($cols);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sizeof($array))
|
||||
{
|
||||
$result = @db2_prepare($this->db_connect_id, $query);
|
||||
|
||||
if (!$result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!@db2_execute($result, $array))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = @db2_exec($this->db_connect_id, $query);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 && $offset == 0)
|
||||
{
|
||||
return $this->sql_query($query . ' fetch first ' . $total . ' rows only', $cache_ttl);
|
||||
}
|
||||
|
||||
// Seek by $offset rows
|
||||
if ($offset)
|
||||
{
|
||||
$limit_sql = 'SELECT a2.*
|
||||
FROM (
|
||||
SELECT ROW_NUMBER() OVER() AS rownum, a1.*
|
||||
FROM (
|
||||
' . $query . '
|
||||
) a1
|
||||
) a2
|
||||
WHERE a2.rownum BETWEEN ' . ($offset + 1) . ' AND ' . ($offset + $total);
|
||||
|
||||
return $this->sql_query($limit_sql, $cache_ttl);
|
||||
}
|
||||
|
||||
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 @db2_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 @db2_autocommit($this->db_connect_id, DB2_AUTOCOMMIT_OFF);
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
$result = @db2_commit($this->db_connect_id);
|
||||
@db2_autocommit($this->db_connect_id, DB2_AUTOCOMMIT_ON);
|
||||
return $result;
|
||||
break;
|
||||
|
||||
case 'rollback':
|
||||
$result = @db2_rollback($this->db_connect_id);
|
||||
@db2_autocommit($this->db_connect_id, DB2_AUTOCOMMIT_ON);
|
||||
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) ? @db2_num_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()
|
||||
{
|
||||
if (function_exists('db2_last_insert_id'))
|
||||
{
|
||||
return @db2_last_insert_id($this->db_connect_id);
|
||||
}
|
||||
|
||||
$result_id = @db2_exec($this->db_connect_id, 'VALUES IDENTITY_VAL_LOCAL()');
|
||||
|
||||
if ($result_id)
|
||||
{
|
||||
if ($row = @db2_fetch_assoc($result_id))
|
||||
{
|
||||
@db2_free_result($result_id);
|
||||
return (int) $row[1];
|
||||
}
|
||||
@db2_free_result($result_id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
|
||||
*/
|
||||
protected function _sql_fetchrow($query_id)
|
||||
{
|
||||
return @db2_fetch_assoc($query_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
|
||||
*/
|
||||
protected function _sql_freeresult($query_id)
|
||||
{
|
||||
return @db2_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 '\\'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
|
||||
*/
|
||||
public function sql_escape($msg)
|
||||
{
|
||||
return @db2_escape_string($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 = db2_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;
|
||||
}
|
||||
|
||||
$stmt = db2_prepare($this->db_connect_id, $query);
|
||||
}
|
||||
|
||||
// get the stmt onto the top of the function arguments
|
||||
array_unshift($data, $stmt);
|
||||
|
||||
call_user_func_array('db2_execute', $data);
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
|
||||
*/
|
||||
protected function _sql_custom_build($stage, $data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
|
||||
*/
|
||||
protected function _sql_error()
|
||||
{
|
||||
$message = @db2_stmt_errormsg();
|
||||
$code = @db2_stmt_error();
|
||||
|
||||
if (!$message && !$code)
|
||||
{
|
||||
$message = @db2_conn_errormsg();
|
||||
$code = @db2_conn_error();
|
||||
}
|
||||
|
||||
$error = array(
|
||||
'message' => $message,
|
||||
'code' => $code,
|
||||
);
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
switch ($mode)
|
||||
{
|
||||
case 'start':
|
||||
|
||||
$html_table = false;
|
||||
@db2_exec($this->db_connect_id, 'DELETE FROM EXPLAIN_INSTANCE');
|
||||
@db2_exec($this->db_connect_id, 'EXPLAIN PLAN FOR ' . $query);
|
||||
|
||||
// Get the data from the plan
|
||||
$sql = "SELECT O.Operator_ID, S2.Target_ID, O.Operator_Type, S.Object_Name, CAST(O.Total_Cost AS INTEGER) Cost
|
||||
FROM EXPLAIN_OPERATOR O
|
||||
LEFT OUTER JOIN EXPLAIN_STREAM S2 ON O.Operator_ID = S2.Source_ID
|
||||
LEFT OUTER JOIN EXPLAIN_STREAM S ON O.Operator_ID = S.Target_ID AND O.Explain_Time = S.Explain_Time AND S.Object_Name IS NOT NULL
|
||||
ORDER BY O.Explain_Time ASC, Operator_ID ASC";
|
||||
$query_id = @db2_exec($this->db_connect_id, $sql);
|
||||
|
||||
if ($query_id)
|
||||
{
|
||||
while ($row = @db2_fetch_assoc($query_id))
|
||||
{
|
||||
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
||||
}
|
||||
|
||||
@db2_free_result($query_id);
|
||||
}
|
||||
|
||||
if ($html_table)
|
||||
{
|
||||
$this->html_hold .= '</table>';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'fromcache':
|
||||
$endtime = explode(' ', microtime());
|
||||
$endtime = $endtime[0] + $endtime[1];
|
||||
|
||||
$result = @db2_exec($this->db_connect_id, $query);
|
||||
while ($void = @db2_fetch_assoc($result, IBASE_TEXT))
|
||||
{
|
||||
// Take the time spent on parsing rows into account
|
||||
}
|
||||
@db2_free_result($result);
|
||||
|
||||
$splittime = explode(' ', microtime());
|
||||
$splittime = $splittime[0] + $splittime[1];
|
||||
|
||||
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,453 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Firebird/Interbase Database Abstraction Layer
|
||||
* Minimum Requirement: 2.0+
|
||||
* @package dbal
|
||||
*/
|
||||
class phpbb_dbal_firebird extends phpbb_dbal
|
||||
{
|
||||
/**
|
||||
* @var string Database type. No distinction between versions or used extensions.
|
||||
*/
|
||||
public $dbms_type = 'firebird';
|
||||
|
||||
/**
|
||||
* @var array Database type map, column layout information
|
||||
*/
|
||||
public $dbms_type_map = array(
|
||||
'INT:' => 'INTEGER',
|
||||
'BINT' => 'DOUBLE PRECISION',
|
||||
'UINT' => 'INTEGER',
|
||||
'UINT:' => 'INTEGER',
|
||||
'TINT:' => 'INTEGER',
|
||||
'USINT' => 'INTEGER',
|
||||
'BOOL' => 'INTEGER',
|
||||
'VCHAR' => 'VARCHAR(255) CHARACTER SET NONE',
|
||||
'VCHAR:' => 'VARCHAR(%d) CHARACTER SET NONE',
|
||||
'CHAR:' => 'CHAR(%d) CHARACTER SET NONE',
|
||||
'XSTEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
||||
'STEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
||||
'TEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
||||
'MTEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
||||
'XSTEXT_UNI'=> 'VARCHAR(100) CHARACTER SET UTF8',
|
||||
'STEXT_UNI' => 'VARCHAR(255) CHARACTER SET UTF8',
|
||||
'TEXT_UNI' => 'BLOB SUB_TYPE TEXT CHARACTER SET UTF8',
|
||||
'MTEXT_UNI' => 'BLOB SUB_TYPE TEXT CHARACTER SET UTF8',
|
||||
'TIMESTAMP' => 'INTEGER',
|
||||
'DECIMAL' => 'DOUBLE PRECISION',
|
||||
'DECIMAL:' => 'DOUBLE PRECISION',
|
||||
'PDECIMAL' => 'DOUBLE PRECISION',
|
||||
'PDECIMAL:' => 'DOUBLE PRECISION',
|
||||
'VCHAR_UNI' => 'VARCHAR(255) CHARACTER SET UTF8',
|
||||
'VCHAR_UNI:'=> 'VARCHAR(%d) CHARACTER SET UTF8',
|
||||
'VARBINARY' => 'CHAR(255) CHARACTER SET NONE',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var string Last query executed. We need this for sql_nextid()
|
||||
*/
|
||||
var $last_query_text = '';
|
||||
|
||||
/**
|
||||
* @var resource Attached service handle.
|
||||
*/
|
||||
var $service_handle = false;
|
||||
|
||||
/**
|
||||
* @var array Database features
|
||||
*/
|
||||
public $features = array(
|
||||
'multi_insert' => false,
|
||||
'count_distinct' => true,
|
||||
'multi_table_deletion' => true,
|
||||
'truncate' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* 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 . (($port) ? ':' . $port : '');
|
||||
$this->dbname = str_replace('\\', '/', $database);
|
||||
|
||||
// There are three possibilities to connect to an interbase db
|
||||
if (!$this->server)
|
||||
{
|
||||
$use_database = $this->dbname;
|
||||
}
|
||||
else if (strpos($this->server, '//') === 0)
|
||||
{
|
||||
$use_database = $this->server . $this->dbname;
|
||||
}
|
||||
else
|
||||
{
|
||||
$use_database = $this->server . ':' . $this->dbname;
|
||||
}
|
||||
|
||||
$this->db_connect_id = ($this->persistency) ? @ibase_pconnect($use_database, $this->user, $password, false, false, 3) : @ibase_connect($use_database, $this->user, $password, false, false, 3);
|
||||
|
||||
if (!$this->db_connect_id)
|
||||
{
|
||||
return $this->sql_error('');
|
||||
}
|
||||
|
||||
$this->service_handle = ($this->server) ? @ibase_service_attach($this->server, $this->user, $password) : false;
|
||||
|
||||
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('#firebird_version')) === false)
|
||||
{
|
||||
$version = false;
|
||||
|
||||
if ($this->service_handle !== false)
|
||||
{
|
||||
$val = @ibase_server_info($this->service_handle, IBASE_SVC_SERVER_VERSION);
|
||||
preg_match('#V([\d.]+)#', $val, $version);
|
||||
$version = (!empty($version[1])) ? $version[1] : false;
|
||||
}
|
||||
|
||||
$this->sql_server_version = (!$version) ? '2.0' : $version;
|
||||
|
||||
if (phpbb::registered('acm'))
|
||||
{
|
||||
phpbb::$acm->put('#firebird_version', $this->sql_server_version);
|
||||
}
|
||||
}
|
||||
|
||||
return ($raw) ? $this->sql_server_version : 'Firebird/Interbase ' . $this->sql_server_version;
|
||||
}
|
||||
|
||||
/**
|
||||
* DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
|
||||
*/
|
||||
protected function _sql_query($query)
|
||||
{
|
||||
$this->last_query_text = $query;
|
||||
|
||||
$array = array();
|
||||
|
||||
// We overcome Firebird's 32767 char limit by binding vars
|
||||
if (strlen($query) > 32767)
|
||||
{
|
||||
if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs))
|
||||
{
|
||||
if (strlen($regs[3]) > 32767)
|
||||
{
|
||||
preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
|
||||
|
||||
$inserts = $vals[0];
|
||||
unset($vals);
|
||||
|
||||
foreach ($inserts as $key => $value)
|
||||
{
|
||||
// check to see if this thing is greater than the max + 'x2
|
||||
if (!empty($value) && $value[0] === "'" && strlen($value) > 32769)
|
||||
{
|
||||
$inserts[$key] = '?';
|
||||
$array[] = str_replace("''", "'", substr($value, 1, -1));
|
||||
}
|
||||
}
|
||||
|
||||
$query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
|
||||
}
|
||||
}
|
||||
else if (preg_match('/^(UPDATE ([\\w_]++)\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|\\d+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data))
|
||||
{
|
||||
if (strlen($data[3]) > 32767)
|
||||
{
|
||||
$update = $data[1];
|
||||
$where = $data[4];
|
||||
preg_match_all('/(\\w++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[3], $temp, PREG_SET_ORDER);
|
||||
unset($data);
|
||||
|
||||
$cols = array();
|
||||
foreach ($temp as $value)
|
||||
{
|
||||
// check to see if this thing is greater than the max + 'x2
|
||||
if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 32769)
|
||||
{
|
||||
$array[] = str_replace("''", "'", substr($value[2], 1, -1));
|
||||
$cols[] = $value[1] . '=?';
|
||||
}
|
||||
else
|
||||
{
|
||||
$cols[] = $value[1] . '=' . $value[2];
|
||||
}
|
||||
}
|
||||
|
||||
$query = $update . implode(', ', $cols) . ' ' . $where;
|
||||
unset($cols);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sizeof($array))
|
||||
{
|
||||
$p_query = @ibase_prepare($this->db_connect_id, $query);
|
||||
array_unshift($array, $p_query);
|
||||
$result = call_user_func_array('ibase_execute', $array);
|
||||
unset($array);
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = @ibase_query($this->db_connect_id, $query);
|
||||
|
||||
if ($result && !$this->transaction)
|
||||
{
|
||||
@ibase_commit_ret();
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6);
|
||||
return $this->sql_query($query, $cache_ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details.
|
||||
*/
|
||||
protected function _sql_close()
|
||||
{
|
||||
if ($this->service_handle !== false)
|
||||
{
|
||||
@ibase_service_detach($this->service_handle);
|
||||
}
|
||||
|
||||
return @ibase_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 true;
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
return @ibase_commit();
|
||||
break;
|
||||
|
||||
case 'rollback':
|
||||
return @ibase_rollback();
|
||||
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) ? @ibase_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()
|
||||
{
|
||||
if (!$this->query_result || !$this->last_query_text)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#i', $this->last_query_text, $tablename))
|
||||
{
|
||||
$sql = 'SELECT GEN_ID(' . $tablename[1] . '_gen, 0) AS new_id FROM RDB$DATABASE';
|
||||
|
||||
if (!($temp_q_id = @ibase_query($this->db_connect_id, $sql)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$temp_result = @ibase_fetch_assoc($temp_q_id);
|
||||
@ibase_free_result($temp_q_id);
|
||||
|
||||
return ($temp_result) ? $temp_result['NEW_ID'] : false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
|
||||
*/
|
||||
protected function _sql_fetchrow($query_id)
|
||||
{
|
||||
$cur_row = @ibase_fetch_object($query_id, IBASE_TEXT);
|
||||
|
||||
if (!$cur_row)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (get_object_vars($cur_row) as $key => $value)
|
||||
{
|
||||
$row[strtolower($key)] = (is_string($value)) ? trim(str_replace(array("\\0", "\\n"), array("\0", "\n"), $value)) : $value;
|
||||
}
|
||||
|
||||
return (sizeof($row)) ? $row : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
|
||||
*/
|
||||
protected function _sql_freeresult($query_id)
|
||||
{
|
||||
return @ibase_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 '\\'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
|
||||
*/
|
||||
public function sql_escape($msg)
|
||||
{
|
||||
return str_replace(array("'", "\0"), array("''", ''), $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 'OCTET_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 = ibase_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;
|
||||
}
|
||||
|
||||
$stmt = ibase_prepare($this->db_connect_id, $query);
|
||||
}
|
||||
|
||||
// get the stmt onto the top of the function arguments
|
||||
array_unshift($data, $stmt);
|
||||
|
||||
call_user_func_array('ibase_execute', $data);
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
|
||||
*/
|
||||
protected function _sql_custom_build($stage, $data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
|
||||
*/
|
||||
protected function _sql_error()
|
||||
{
|
||||
return array(
|
||||
'message' => @ibase_errmsg(),
|
||||
'code' => @ibase_errcode()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
switch ($mode)
|
||||
{
|
||||
case 'start':
|
||||
break;
|
||||
|
||||
case 'fromcache':
|
||||
$endtime = explode(' ', microtime());
|
||||
$endtime = $endtime[0] + $endtime[1];
|
||||
|
||||
$result = @ibase_query($this->db_connect_id, $query);
|
||||
while ($void = @ibase_fetch_object($result, IBASE_TEXT))
|
||||
{
|
||||
// Take the time spent on parsing rows into account
|
||||
}
|
||||
@ibase_free_result($result);
|
||||
|
||||
$splittime = explode(' ', microtime());
|
||||
$splittime = $splittime[0] + $splittime[1];
|
||||
|
||||
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -1,382 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* MSSQL Database Abstraction Layer
|
||||
* Minimum Requirement is MSSQL 2000+
|
||||
* @package dbal
|
||||
*/
|
||||
class phpbb_dbal_mssql extends phpbb_dbal
|
||||
{
|
||||
/**
|
||||
* @var string Database type. No distinction between versions or used extensions.
|
||||
*/
|
||||
public $dbms_type = 'mssql';
|
||||
|
||||
/**
|
||||
* @var array Database type map, column layout information
|
||||
*/
|
||||
public $dbms_type_map = array(
|
||||
'INT:' => '[int]',
|
||||
'BINT' => '[float]',
|
||||
'UINT' => '[int]',
|
||||
'UINT:' => '[int]',
|
||||
'TINT:' => '[int]',
|
||||
'USINT' => '[int]',
|
||||
'BOOL' => '[int]',
|
||||
'VCHAR' => '[varchar] (255)',
|
||||
'VCHAR:' => '[varchar] (%d)',
|
||||
'CHAR:' => '[char] (%d)',
|
||||
'XSTEXT' => '[varchar] (1000)',
|
||||
'STEXT' => '[varchar] (3000)',
|
||||
'TEXT' => '[varchar] (8000)',
|
||||
'MTEXT' => '[text]',
|
||||
'XSTEXT_UNI'=> '[varchar] (100)',
|
||||
'STEXT_UNI' => '[varchar] (255)',
|
||||
'TEXT_UNI' => '[varchar] (4000)',
|
||||
'MTEXT_UNI' => '[text]',
|
||||
'TIMESTAMP' => '[int]',
|
||||
'DECIMAL' => '[float]',
|
||||
'DECIMAL:' => '[float]',
|
||||
'PDECIMAL' => '[float]',
|
||||
'PDECIMAL:' => '[float]',
|
||||
'VCHAR_UNI' => '[varchar] (255)',
|
||||
'VCHAR_UNI:'=> '[varchar] (%d)',
|
||||
'VARBINARY' => '[varchar] (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->dbname = $database;
|
||||
$this->port = $port;
|
||||
|
||||
$port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
|
||||
$this->server = $server . (($this->port) ? $port_delimiter . $this->port : '');
|
||||
|
||||
@ini_set('mssql.charset', 'UTF-8');
|
||||
@ini_set('mssql.textlimit', 2147483647);
|
||||
@ini_set('mssql.textsize', 2147483647);
|
||||
|
||||
$this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $password, $new_link) : @mssql_connect($this->server, $this->user, $password, $new_link);
|
||||
|
||||
if (!$this->db_connect_id || !$this->dbname)
|
||||
{
|
||||
return $this->sql_error(phpbb::$last_notice['message']);
|
||||
}
|
||||
|
||||
if (!@mssql_select_db($this->dbname, $this->db_connect_id))
|
||||
{
|
||||
return $this->sql_error('');
|
||||
}
|
||||
|
||||
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('#mssql_version')) === false)
|
||||
{
|
||||
$result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
|
||||
|
||||
$row = false;
|
||||
if ($result_id)
|
||||
{
|
||||
$row = @mssql_fetch_assoc($result_id);
|
||||
@mssql_free_result($result_id);
|
||||
}
|
||||
|
||||
$this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
|
||||
|
||||
if (phpbb::registered('acm'))
|
||||
{
|
||||
phpbb::$acm->put('#mssql_version', $this->sql_server_version);
|
||||
}
|
||||
}
|
||||
|
||||
if ($raw)
|
||||
{
|
||||
return $this->sql_server_version;
|
||||
}
|
||||
|
||||
return ($this->sql_server_version) ? 'MSSQL<br />' . $this->sql_server_version : 'MSSQL';
|
||||
}
|
||||
|
||||
/**
|
||||
* DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
|
||||
*/
|
||||
protected function _sql_query($query)
|
||||
{
|
||||
return @mssql_query($query, $this->db_connect_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
// Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
|
||||
if ($total)
|
||||
{
|
||||
// We need to grab the total number of rows + the offset number of rows to get the correct result
|
||||
if (strpos($query, 'SELECT DISTINCT') === 0)
|
||||
{
|
||||
$query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->sql_query($query, $cache_ttl);
|
||||
|
||||
// Seek by $offset rows
|
||||
if ($offset)
|
||||
{
|
||||
@mssql_data_seek($result, $offset);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details.
|
||||
*/
|
||||
protected function _sql_close()
|
||||
{
|
||||
return @mssql_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 @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
|
||||
break;
|
||||
|
||||
case 'rollback':
|
||||
return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
|
||||
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) ? @mssql_rows_affected($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()
|
||||
{
|
||||
$result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
|
||||
|
||||
if ($result_id)
|
||||
{
|
||||
if ($row = @mssql_fetch_assoc($result_id))
|
||||
{
|
||||
@mssql_free_result($result_id);
|
||||
return $row['computed'];
|
||||
}
|
||||
@mssql_free_result($result_id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
|
||||
*/
|
||||
protected function _sql_fetchrow($query_id)
|
||||
{
|
||||
$row = @mssql_fetch_assoc($query_id);
|
||||
|
||||
// I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
|
||||
if ($row)
|
||||
{
|
||||
foreach ($row as $key => $value)
|
||||
{
|
||||
$row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
|
||||
*/
|
||||
protected function _sql_freeresult($query_id)
|
||||
{
|
||||
return @mssql_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 '\\'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
|
||||
*/
|
||||
public function sql_escape($msg)
|
||||
{
|
||||
return str_replace(array("'", "\0"), array("''", ''), $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 'DATALENGTH(' . $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 = '')
|
||||
{
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
|
||||
*/
|
||||
protected function _sql_custom_build($stage, $data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
|
||||
*/
|
||||
protected function _sql_error()
|
||||
{
|
||||
$error = array(
|
||||
'message' => @mssql_get_last_message(),
|
||||
'code' => ''
|
||||
);
|
||||
|
||||
// Get error code number
|
||||
$result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
|
||||
if ($result_id)
|
||||
{
|
||||
$row = @mssql_fetch_assoc($result_id);
|
||||
$error['code'] = $row['code'];
|
||||
@mssql_free_result($result_id);
|
||||
}
|
||||
|
||||
// Get full error message if possible
|
||||
$sql = 'SELECT CAST(description as varchar(255)) as message
|
||||
FROM master.dbo.sysmessages
|
||||
WHERE error = ' . $error['code'];
|
||||
$result_id = @mssql_query($sql);
|
||||
|
||||
if ($result_id)
|
||||
{
|
||||
$row = @mssql_fetch_assoc($result_id);
|
||||
if (!empty($row['message']))
|
||||
{
|
||||
$error['message'] .= '<br />' . $row['message'];
|
||||
}
|
||||
@mssql_free_result($result_id);
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
switch ($mode)
|
||||
{
|
||||
case 'start':
|
||||
$html_table = false;
|
||||
@mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
|
||||
if ($result = @mssql_query($query, $this->db_connect_id))
|
||||
{
|
||||
@mssql_next_result($result);
|
||||
while ($row = @mssql_fetch_row($result))
|
||||
{
|
||||
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
||||
}
|
||||
}
|
||||
@mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
|
||||
@mssql_free_result($result);
|
||||
|
||||
if ($html_table)
|
||||
{
|
||||
$this->html_hold .= '</table>';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'fromcache':
|
||||
$endtime = explode(' ', microtime());
|
||||
$endtime = $endtime[0] + $endtime[1];
|
||||
|
||||
$result = @mssql_query($query, $this->db_connect_id);
|
||||
while ($void = @mssql_fetch_assoc($result))
|
||||
{
|
||||
// Take the time spent on parsing rows into account
|
||||
}
|
||||
@mssql_free_result($result);
|
||||
|
||||
$splittime = explode(' ', microtime());
|
||||
$splittime = $splittime[0] + $splittime[1];
|
||||
|
||||
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -1,350 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
include_once(PHPBB_ROOT_PATH . 'includes/db/dbal.' . PHP_EXT);
|
||||
|
||||
/**
|
||||
* MSSQL Database Abstraction Layer
|
||||
* Minimum Requirement is MSSQL 2005+
|
||||
* @package dbal
|
||||
*/
|
||||
class phpbb_dbal_mssql_2005 extends phpbb_dbal
|
||||
{
|
||||
/**
|
||||
* @var string Database type. No distinction between versions or used extensions.
|
||||
*/
|
||||
public $dbms_type = 'mssql';
|
||||
|
||||
/**
|
||||
* @var array Database type map, column layout information
|
||||
*/
|
||||
public $dbms_type_map = array(
|
||||
'INT:' => '[int]',
|
||||
'BINT' => '[float]',
|
||||
'UINT' => '[int]',
|
||||
'UINT:' => '[int]',
|
||||
'TINT:' => '[int]',
|
||||
'USINT' => '[int]',
|
||||
'BOOL' => '[int]',
|
||||
'VCHAR' => '[varchar] (255)',
|
||||
'VCHAR:' => '[varchar] (%d)',
|
||||
'CHAR:' => '[char] (%d)',
|
||||
'XSTEXT' => '[varchar] (1000)',
|
||||
'STEXT' => '[varchar] (3000)',
|
||||
'TEXT' => '[varchar] (8000)',
|
||||
'MTEXT' => '[text]',
|
||||
'XSTEXT_UNI'=> '[varchar] (100)',
|
||||
'STEXT_UNI' => '[varchar] (255)',
|
||||
'TEXT_UNI' => '[varchar] (4000)',
|
||||
'MTEXT_UNI' => '[text]',
|
||||
'TIMESTAMP' => '[int]',
|
||||
'DECIMAL' => '[float]',
|
||||
'DECIMAL:' => '[float]',
|
||||
'PDECIMAL' => '[float]',
|
||||
'PDECIMAL:' => '[float]',
|
||||
'VCHAR_UNI' => '[varchar] (255)',
|
||||
'VCHAR_UNI:'=> '[varchar] (%d)',
|
||||
'VARBINARY' => '[varchar] (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 . (($port) ? ':' . $port : '');
|
||||
$this->dbname = $database;
|
||||
$this->port = $port;
|
||||
|
||||
$conn_info = array();
|
||||
if ($this->user)
|
||||
{
|
||||
$conn_info['UID'] = $this->user;
|
||||
}
|
||||
|
||||
if ($password)
|
||||
{
|
||||
$conn_info['PWD'] = $password;
|
||||
}
|
||||
|
||||
$this->db_connect_id = @sqlsrv_connect($this->server, $conn_info);
|
||||
|
||||
if (!$this->db_connect_id || !$this->dbname)
|
||||
{
|
||||
return $this->sql_error('');
|
||||
}
|
||||
|
||||
if (!@sqlsrv_query($this->db_connect_id, 'USE ' . $this->dbname))
|
||||
{
|
||||
return $this->sql_error('');
|
||||
}
|
||||
|
||||
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('#mssql2005_version')) === false)
|
||||
{
|
||||
$server_info = @sqlsrv_server_info($this->db_connect_id);
|
||||
|
||||
$this->sql_server_version = (!empty($server_info['SQLServerVersion'])) ? $server_info['SQLServerVersion'] : 0;
|
||||
|
||||
if (phpbb::registered('acm'))
|
||||
{
|
||||
phpbb::$acm->put('#mssql2005_version', $this->sql_server_version);
|
||||
}
|
||||
}
|
||||
|
||||
return ($raw) ? $this->sql_server_version : 'MSSQL ' . $this->sql_server_version;
|
||||
}
|
||||
|
||||
/**
|
||||
* DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
|
||||
*/
|
||||
protected function _sql_query($query)
|
||||
{
|
||||
if (strpos($query, 'BEGIN') === 0 || strpos($query, 'COMMIT') === 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return @sqlsrv_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)
|
||||
{
|
||||
// Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
|
||||
if ($total)
|
||||
{
|
||||
// We need to grab the total number of rows + the offset number of rows to get the correct result
|
||||
if (strpos($query, 'SELECT DISTINCT') === 0)
|
||||
{
|
||||
$query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->sql_query($query, $cache_ttl);
|
||||
|
||||
// Seek by $offset rows
|
||||
if ($offset)
|
||||
{
|
||||
// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
|
||||
for ($i = 0; $i < $offset; $i++)
|
||||
{
|
||||
if (!$this->sql_fetchrow($result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details.
|
||||
*/
|
||||
protected function _sql_close()
|
||||
{
|
||||
return @sqlsrv_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 @sqlsrv_query($this->db_connect_id, 'BEGIN TRANSACTION');
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
return @sqlsrv_query($this->db_connect_id, 'COMMIT TRANSACTION');
|
||||
break;
|
||||
|
||||
case 'rollback':
|
||||
return @sqlsrv_query($this->db_connect_id, 'ROLLBACK TRANSACTION');
|
||||
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) ? @sqlsrv_rows_affected($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()
|
||||
{
|
||||
$result_id = @sqlsrv_query($this->db_connect_id, 'SELECT SCOPE_IDENTITY()');
|
||||
if ($result_id)
|
||||
{
|
||||
if ($row = @sqlsrv_fetch_array($result_id, SQLSRV_FETCH_ASSOC))
|
||||
{
|
||||
@sqlsrv_free_stmt($result_id);
|
||||
return $row['computed'];
|
||||
}
|
||||
@sqlsrv_free_stmt($result_id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
|
||||
*/
|
||||
protected function _sql_fetchrow($query_id)
|
||||
{
|
||||
$row = @sqlsrv_fetch_array($query_id, SQLSRV_FETCH_ASSOC);
|
||||
|
||||
// I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
|
||||
if ($row)
|
||||
{
|
||||
foreach ($row as $key => $value)
|
||||
{
|
||||
$row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
|
||||
*/
|
||||
protected function _sql_freeresult($query_id)
|
||||
{
|
||||
return @sqlsrv_free_stmt($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 '\\'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
|
||||
*/
|
||||
public function sql_escape($msg)
|
||||
{
|
||||
return str_replace("'", "''", $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 'DATALENGTH(' . $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 = '')
|
||||
{
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
|
||||
*/
|
||||
protected function _sql_custom_build($stage, $data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
|
||||
*/
|
||||
protected function _sql_error()
|
||||
{
|
||||
$message = $code = array();
|
||||
foreach (@sqlsrv_errors() as $error_array)
|
||||
{
|
||||
$message[] = $error_array['message'];
|
||||
$code[] = $error_array['code'];
|
||||
}
|
||||
|
||||
$error = array(
|
||||
'message' => implode('<br />', $message),
|
||||
'code' => implode('<br />', $code),
|
||||
);
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
switch ($mode)
|
||||
{
|
||||
case 'fromcache':
|
||||
$endtime = explode(' ', microtime());
|
||||
$endtime = $endtime[0] + $endtime[1];
|
||||
|
||||
$result = @sqlsrv_query($this->db_connect_id, $query);
|
||||
while ($void = @sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
|
||||
{
|
||||
// Take the time spent on parsing rows into account
|
||||
}
|
||||
@sqlsrv_free_stmt($result);
|
||||
|
||||
$splittime = explode(' ', microtime());
|
||||
$splittime = $splittime[0] + $splittime[1];
|
||||
|
||||
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -1,380 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
include_once(PHPBB_ROOT_PATH . 'includes/db/dbal.' . PHP_EXT);
|
||||
|
||||
/**
|
||||
* Unified ODBC functions
|
||||
* Unified ODBC functions support any database having ODBC driver, for example Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere...
|
||||
* Here we only support MSSQL Server 2000+ because of the provided schema
|
||||
*
|
||||
* @note number of bytes returned for returning data depends on odbc.defaultlrl php.ini setting.
|
||||
* If it is limited to 4K for example only 4K of data is returned max, resulting in incomplete theme data for example.
|
||||
* @note odbc.defaultbinmode may affect UTF8 characters
|
||||
*
|
||||
* @package dbal
|
||||
*/
|
||||
class phpbb_dbal_mssql_odbc extends phpbb_dbal
|
||||
{
|
||||
/**
|
||||
* @var string Database type. No distinction between versions or used extensions.
|
||||
*/
|
||||
public $dbms_type = 'mssql';
|
||||
|
||||
/**
|
||||
* @var array Database type map, column layout information
|
||||
*/
|
||||
public $dbms_type_map = array(
|
||||
'INT:' => '[int]',
|
||||
'BINT' => '[float]',
|
||||
'UINT' => '[int]',
|
||||
'UINT:' => '[int]',
|
||||
'TINT:' => '[int]',
|
||||
'USINT' => '[int]',
|
||||
'BOOL' => '[int]',
|
||||
'VCHAR' => '[varchar] (255)',
|
||||
'VCHAR:' => '[varchar] (%d)',
|
||||
'CHAR:' => '[char] (%d)',
|
||||
'XSTEXT' => '[varchar] (1000)',
|
||||
'STEXT' => '[varchar] (3000)',
|
||||
'TEXT' => '[varchar] (8000)',
|
||||
'MTEXT' => '[text]',
|
||||
'XSTEXT_UNI'=> '[varchar] (100)',
|
||||
'STEXT_UNI' => '[varchar] (255)',
|
||||
'TEXT_UNI' => '[varchar] (4000)',
|
||||
'MTEXT_UNI' => '[text]',
|
||||
'TIMESTAMP' => '[int]',
|
||||
'DECIMAL' => '[float]',
|
||||
'DECIMAL:' => '[float]',
|
||||
'PDECIMAL' => '[float]',
|
||||
'PDECIMAL:' => '[float]',
|
||||
'VCHAR_UNI' => '[varchar] (255)',
|
||||
'VCHAR_UNI:'=> '[varchar] (%d)',
|
||||
'VARBINARY' => '[varchar] (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->dbname = $database;
|
||||
$this->port = $port;
|
||||
|
||||
$port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
|
||||
$this->server = $server . (($port) ? $port_delimiter . $port : '');
|
||||
|
||||
$max_size = @ini_get('odbc.defaultlrl');
|
||||
if (!empty($max_size))
|
||||
{
|
||||
$unit = strtolower(substr($max_size, -1, 1));
|
||||
$max_size = (int) $max_size;
|
||||
|
||||
if ($unit == 'k')
|
||||
{
|
||||
$max_size = floor($max_size / 1024);
|
||||
}
|
||||
else if ($unit == 'g')
|
||||
{
|
||||
$max_size *= 1024;
|
||||
}
|
||||
else if (is_numeric($unit))
|
||||
{
|
||||
$max_size = floor((int) ($max_size . $unit) / 1048576);
|
||||
}
|
||||
$max_size = max(8, $max_size) . 'M';
|
||||
|
||||
@ini_set('odbc.defaultlrl', $max_size);
|
||||
}
|
||||
|
||||
$this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $password) : @odbc_connect($this->server, $this->user, $password);
|
||||
|
||||
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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('#mssqlodbc_version')) === false)
|
||||
{
|
||||
$result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
|
||||
|
||||
$row = false;
|
||||
if ($result_id)
|
||||
{
|
||||
$row = @odbc_fetch_array($result_id);
|
||||
@odbc_free_result($result_id);
|
||||
}
|
||||
|
||||
$this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
|
||||
|
||||
if (phpbb::registered('acm'))
|
||||
{
|
||||
phpbb::$acm->put('#mssqlodbc_version', $this->sql_server_version);
|
||||
}
|
||||
}
|
||||
|
||||
if ($raw)
|
||||
{
|
||||
return $this->sql_server_version;
|
||||
}
|
||||
|
||||
return ($this->sql_server_version) ? 'MSSQL (ODBC)<br />' . $this->sql_server_version : 'MSSQL (ODBC)';
|
||||
}
|
||||
|
||||
/**
|
||||
* DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
|
||||
*/
|
||||
protected function _sql_query($query)
|
||||
{
|
||||
return @odbc_exec($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)
|
||||
{
|
||||
// Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
|
||||
if ($total)
|
||||
{
|
||||
// We need to grab the total number of rows + the offset number of rows to get the correct result
|
||||
if (strpos($query, 'SELECT DISTINCT') === 0)
|
||||
{
|
||||
$query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->sql_query($query, $cache_ttl);
|
||||
|
||||
// Seek by $offset rows
|
||||
if ($offset)
|
||||
{
|
||||
// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
|
||||
for ($i = 0; $i < $offset; $i++)
|
||||
{
|
||||
if (!$this->sql_fetchrow($result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details.
|
||||
*/
|
||||
protected function _sql_close()
|
||||
{
|
||||
return @odbc_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 @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
|
||||
break;
|
||||
|
||||
case 'rollback':
|
||||
return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
|
||||
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) ? @odbc_num_rows($this->query_result) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details.
|
||||
*/
|
||||
public function sql_nextid()
|
||||
{
|
||||
$result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
|
||||
|
||||
if ($result_id)
|
||||
{
|
||||
if (@odbc_fetch_array($result_id))
|
||||
{
|
||||
$id = @odbc_result($result_id, 1);
|
||||
@odbc_free_result($result_id);
|
||||
return $id;
|
||||
}
|
||||
@odbc_free_result($result_id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
|
||||
* @note number of bytes returned depends on odbc.defaultlrl php.ini setting. If it is limited to 4K for example only 4K of data is returned max.
|
||||
*/
|
||||
protected function _sql_fetchrow($query_id)
|
||||
{
|
||||
return @odbc_fetch_array($query_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
|
||||
*/
|
||||
protected function _sql_freeresult($query_id)
|
||||
{
|
||||
return @odbc_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 '\\'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
|
||||
*/
|
||||
public function sql_escape($msg)
|
||||
{
|
||||
return str_replace(array("'", "\0"), array("''", ''), $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 'DATALENGTH(' . $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 = odbc_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;
|
||||
}
|
||||
|
||||
$stmt = odbc_prepare($this->db_connect_id, $query);
|
||||
}
|
||||
|
||||
// get the stmt onto the top of the function arguments
|
||||
array_unshift($data, $stmt);
|
||||
|
||||
call_user_func_array('odbc_execute', $data);
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
|
||||
*/
|
||||
protected function _sql_custom_build($stage, $data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
|
||||
*/
|
||||
protected function _sql_error()
|
||||
{
|
||||
return array(
|
||||
'message' => @odbc_errormsg(),
|
||||
'code' => @odbc_error()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
switch ($mode)
|
||||
{
|
||||
case 'start':
|
||||
break;
|
||||
|
||||
case 'fromcache':
|
||||
$endtime = explode(' ', microtime());
|
||||
$endtime = $endtime[0] + $endtime[1];
|
||||
|
||||
$result = @odbc_exec($this->db_connect_id, $query);
|
||||
while ($void = @odbc_fetch_array($result))
|
||||
{
|
||||
// Take the time spent on parsing rows into account
|
||||
}
|
||||
@odbc_free_result($result);
|
||||
|
||||
$splittime = explode(' ', microtime());
|
||||
$splittime = $splittime[0] + $splittime[1];
|
||||
|
||||
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -1,447 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* MySQL Database Abstraction Layer
|
||||
* Compatible with:
|
||||
* MySQL 4.1+
|
||||
* MySQL 5.0+
|
||||
* @package dbal
|
||||
*/
|
||||
class phpbb_dbal_mysql 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 . (($port) ? ':' . $port : '');
|
||||
$this->dbname = $database;
|
||||
$this->port = $port;
|
||||
|
||||
$this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $password, $new_link) : @mysql_connect($this->server, $this->user, $password, $new_link);
|
||||
|
||||
if (!$this->db_connect_id || !$this->dbname)
|
||||
{
|
||||
return $this->sql_error('');
|
||||
}
|
||||
|
||||
if (!@mysql_select_db($this->dbname, $this->db_connect_id))
|
||||
{
|
||||
return $this->sql_error('');
|
||||
}
|
||||
|
||||
@mysql_query("SET NAMES 'utf8'", $this->db_connect_id);
|
||||
|
||||
// enforce strict mode on databases that support it
|
||||
if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
|
||||
{
|
||||
$result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id);
|
||||
$row = @mysql_fetch_assoc($result);
|
||||
@mysql_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);
|
||||
@mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id);
|
||||
}
|
||||
|
||||
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('#mysql_version')) === false)
|
||||
{
|
||||
$result = @mysql_query('SELECT VERSION() AS version', $this->db_connect_id);
|
||||
$row = @mysql_fetch_assoc($result);
|
||||
@mysql_free_result($result);
|
||||
|
||||
$this->sql_server_version = trim($row['version']);
|
||||
|
||||
if (phpbb::registered('acm'))
|
||||
{
|
||||
phpbb::$acm->put('#mysql_version', $this->sql_server_version);
|
||||
}
|
||||
}
|
||||
|
||||
return ($raw) ? $this->sql_server_version : 'MySQL ' . $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 @mysql_query($query, $this->db_connect_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 @mysql_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 @mysql_query('BEGIN', $this->db_connect_id);
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
return @mysql_query('COMMIT', $this->db_connect_id);
|
||||
break;
|
||||
|
||||
case 'rollback':
|
||||
return @mysql_query('ROLLBACK', $this->db_connect_id);
|
||||
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) ? @mysql_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) ? @mysql_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 @mysql_fetch_assoc($query_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
|
||||
*/
|
||||
protected function _sql_freeresult($query_id)
|
||||
{
|
||||
return @mysql_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)
|
||||
{
|
||||
if (!$this->db_connect_id)
|
||||
{
|
||||
return @mysql_real_escape_string($msg);
|
||||
}
|
||||
|
||||
return @mysql_real_escape_string($msg, $this->db_connect_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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' => @mysql_error(),
|
||||
'code' => @mysql_errno()
|
||||
);
|
||||
}
|
||||
|
||||
return array(
|
||||
'message' => @mysql_error($this->db_connect_id),
|
||||
'code' => @mysql_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)
|
||||
{
|
||||
@mysql_query('SET profiling = 1;', $this->db_connect_id);
|
||||
}
|
||||
|
||||
if ($result = @mysql_query('EXPLAIN ' . (($test_extend) ? 'EXTENDED ' : '') . "$explain_query", $this->db_connect_id))
|
||||
{
|
||||
while ($row = @mysql_fetch_assoc($result))
|
||||
{
|
||||
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
||||
}
|
||||
}
|
||||
@mysql_free_result($result);
|
||||
|
||||
if ($html_table)
|
||||
{
|
||||
$this->html_hold .= '</table>';
|
||||
}
|
||||
|
||||
if ($test_extend)
|
||||
{
|
||||
$html_table = false;
|
||||
|
||||
if ($result = @mysql_query('SHOW WARNINGS', $this->db_connect_id))
|
||||
{
|
||||
$this->html_hold .= '<br />';
|
||||
while ($row = @mysql_fetch_assoc($result))
|
||||
{
|
||||
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
||||
}
|
||||
}
|
||||
@mysql_free_result($result);
|
||||
|
||||
if ($html_table)
|
||||
{
|
||||
$this->html_hold .= '</table>';
|
||||
}
|
||||
}
|
||||
|
||||
if ($test_prof)
|
||||
{
|
||||
$html_table = false;
|
||||
|
||||
// get the last profile
|
||||
if ($result = @mysql_query('SHOW PROFILE ALL;', $this->db_connect_id))
|
||||
{
|
||||
$this->html_hold .= '<br />';
|
||||
while ($row = @mysql_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);
|
||||
}
|
||||
}
|
||||
@mysql_free_result($result);
|
||||
|
||||
if ($html_table)
|
||||
{
|
||||
$this->html_hold .= '</table>';
|
||||
}
|
||||
|
||||
@mysql_query('SET profiling = 0;', $this->db_connect_id);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'fromcache':
|
||||
$endtime = explode(' ', microtime());
|
||||
$endtime = $endtime[0] + $endtime[1];
|
||||
|
||||
$result = @mysql_query($query, $this->db_connect_id);
|
||||
while ($void = @mysql_fetch_assoc($result))
|
||||
{
|
||||
// Take the time spent on parsing rows into account
|
||||
}
|
||||
@mysql_free_result($result);
|
||||
|
||||
$splittime = explode(' ', microtime());
|
||||
$splittime = $splittime[0] + $splittime[1];
|
||||
|
||||
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -1,472 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -1,664 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Oracle Database Abstraction Layer
|
||||
* Minimum Requirement: 9.2+
|
||||
* @package dbal
|
||||
*/
|
||||
class phpbb_dbal_oracle extends phpbb_dbal
|
||||
{
|
||||
/**
|
||||
* @var string Database type. No distinction between versions or used extensions.
|
||||
*/
|
||||
public $dbms_type = 'oracle';
|
||||
|
||||
/**
|
||||
* @var array Database type map, column layout information
|
||||
*/
|
||||
public $dbms_type_map = array(
|
||||
'INT:' => 'number(%d)',
|
||||
'BINT' => 'number(20)',
|
||||
'UINT' => 'number(8)',
|
||||
'UINT:' => 'number(%d)',
|
||||
'TINT:' => 'number(%d)',
|
||||
'USINT' => 'number(4)',
|
||||
'BOOL' => 'number(1)',
|
||||
'VCHAR' => 'varchar2(255)',
|
||||
'VCHAR:' => 'varchar2(%d)',
|
||||
'CHAR:' => 'char(%d)',
|
||||
'XSTEXT' => 'varchar2(1000)',
|
||||
'STEXT' => 'varchar2(3000)',
|
||||
'TEXT' => 'clob',
|
||||
'MTEXT' => 'clob',
|
||||
'XSTEXT_UNI'=> 'varchar2(300)',
|
||||
'STEXT_UNI' => 'varchar2(765)',
|
||||
'TEXT_UNI' => 'clob',
|
||||
'MTEXT_UNI' => 'clob',
|
||||
'TIMESTAMP' => 'number(11)',
|
||||
'DECIMAL' => 'number(5, 2)',
|
||||
'DECIMAL:' => 'number(%d, 2)',
|
||||
'PDECIMAL' => 'number(6, 3)',
|
||||
'PDECIMAL:' => 'number(%d, 3)',
|
||||
'VCHAR_UNI' => 'varchar2(255)',
|
||||
'VCHAR_UNI:'=> array('varchar2(%d)', 'limit' => array('mult', 3, 765, 'clob')),
|
||||
'VARBINARY' => 'raw(255)',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var string Last query executed. We need this for sql_nextid()
|
||||
*/
|
||||
var $last_query_text = '';
|
||||
|
||||
/**
|
||||
* 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 . (($port) ? ':' . $port : '');
|
||||
$this->dbname = $database;
|
||||
$this->port = $port;
|
||||
|
||||
$connect = $database;
|
||||
|
||||
// support for "easy connect naming"
|
||||
if ($server !== '' && $server !== '/')
|
||||
{
|
||||
if (substr($server, -1, 1) == '/')
|
||||
{
|
||||
$server == substr($sqlserver, 0, -1);
|
||||
}
|
||||
$connect = $server . (($port) ? ':' . $port : '') . '/' . $database;
|
||||
}
|
||||
|
||||
$this->db_connect_id = ($new_link) ? @oci_new_connect($this->user, $password, $connect, 'AL32UTF8') : (($this->persistency) ? @oci_pconnect($this->user, $password, $connect, 'AL32UTF8') : @oci_connect($this->user, $password, $connect, 'AL32UTF8'));
|
||||
|
||||
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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('#oracle_version')) === false)
|
||||
{
|
||||
$sql = "SELECT value
|
||||
FROM NLS_DATABASE_PARAMETERS
|
||||
WHERE PARAMETER = 'NLS_RDBMS_VERSION'";
|
||||
$result = @ociparse($this->db_connect_id, $sql);
|
||||
@ociexecute($result, OCI_DEFAULT);
|
||||
@ocicommit($this->db_connect_id);
|
||||
|
||||
$row = array();
|
||||
@ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS);
|
||||
@ocifreestatement($result);
|
||||
|
||||
$this->sql_server_version = (isset($row['VALUE'])) ? trim($row['VALUE']) : 0;
|
||||
|
||||
if (phpbb::registered('acm'))
|
||||
{
|
||||
phpbb::$acm->put('#oracle_version', $this->sql_server_version);
|
||||
}
|
||||
}
|
||||
|
||||
return ($raw) ? $this->sql_server_version : @oci_server_version($this->db_connect_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
|
||||
*/
|
||||
protected function _sql_query($query)
|
||||
{
|
||||
$this->last_query_text = $query;
|
||||
|
||||
$in_transaction = false;
|
||||
if (!$this->transaction)
|
||||
{
|
||||
$this->sql_transaction('begin');
|
||||
}
|
||||
else
|
||||
{
|
||||
$in_transaction = true;
|
||||
}
|
||||
|
||||
$array = array();
|
||||
|
||||
// We overcome Oracle's 4000 char limit by binding vars
|
||||
if (strlen($query) > 4000)
|
||||
{
|
||||
if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs))
|
||||
{
|
||||
if (strlen($regs[3]) > 4000)
|
||||
{
|
||||
$cols = explode(', ', $regs[2]);
|
||||
preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
|
||||
|
||||
$inserts = $vals[0];
|
||||
unset($vals);
|
||||
|
||||
foreach ($inserts as $key => $value)
|
||||
{
|
||||
// check to see if this thing is greater than the max + 'x2
|
||||
if (!empty($value) && $value[0] === "'" && strlen($value) > 4002)
|
||||
{
|
||||
$inserts[$key] = ':' . strtoupper($cols[$key]);
|
||||
$array[$inserts[$key]] = str_replace("''", "'", substr($value, 1, -1));
|
||||
}
|
||||
}
|
||||
|
||||
$query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
|
||||
}
|
||||
}
|
||||
else if (preg_match_all('/^(UPDATE [\\w_]++\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data, PREG_SET_ORDER))
|
||||
{
|
||||
if (strlen($data[0][2]) > 4000)
|
||||
{
|
||||
$update = $data[0][1];
|
||||
$where = $data[0][3];
|
||||
preg_match_all('/([\\w_]++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[0][2], $temp, PREG_SET_ORDER);
|
||||
unset($data);
|
||||
|
||||
$cols = array();
|
||||
foreach ($temp as $value)
|
||||
{
|
||||
// check to see if this thing is greater than the max + 'x2
|
||||
if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 4002)
|
||||
{
|
||||
$cols[] = $value[1] . '=:' . strtoupper($value[1]);
|
||||
$array[$value[1]] = str_replace("''", "'", substr($value[2], 1, -1));
|
||||
}
|
||||
else
|
||||
{
|
||||
$cols[] = $value[1] . '=' . $value[2];
|
||||
}
|
||||
}
|
||||
|
||||
$query = $update . implode(', ', $cols) . ' ' . $where;
|
||||
unset($cols);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (substr($query, 0, 6))
|
||||
{
|
||||
case 'DELETE':
|
||||
if (preg_match('/^(DELETE FROM [\w_]++ WHERE)((?:\s*(?:AND|OR)?\s*[\w_]+\s*(?:(?:=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]+)|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))*+)$/', $query, $regs))
|
||||
{
|
||||
$query = $regs[1] . $this->_rewrite_where($regs[2]);
|
||||
unset($regs);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'UPDATE':
|
||||
if (preg_match('/^(UPDATE [\\w_]++\\s+SET [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++)(?:, [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++))*+\\s+WHERE)(.*)$/s', $query, $regs))
|
||||
{
|
||||
$query = $regs[1] . $this->_rewrite_where($regs[2]);
|
||||
unset($regs);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'SELECT':
|
||||
$query = preg_replace_callback('/([\w_.]++)\s*(?:(=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]++|([\w_.]++))|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]++,? ?)*+\))/', array($this, '_rewrite_col_compare'), $query);
|
||||
break;
|
||||
}
|
||||
|
||||
$result = @oci_parse($this->db_connect_id, $query);
|
||||
|
||||
if (!$result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($array as $key => $value)
|
||||
{
|
||||
@oci_bind_by_name($result, $key, $array[$key], -1);
|
||||
}
|
||||
|
||||
$success = @oci_execute($result, OCI_DEFAULT);
|
||||
|
||||
if (!$success)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$in_transaction)
|
||||
{
|
||||
$this->sql_transaction('commit');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$query = 'SELECT * FROM (SELECT /*+ FIRST_ROWS */ rownum AS xrownum, a.* FROM (' . $query . ') a WHERE rownum <= ' . ($offset + $total) . ') WHERE xrownum >= ' . $offset;
|
||||
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 @oci_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 true;
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
return @oci_commit($this->db_connect_id);
|
||||
break;
|
||||
|
||||
case 'rollback':
|
||||
return @oci_rollback($this->db_connect_id);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details.
|
||||
*/
|
||||
public function sql_affectedrows()
|
||||
{
|
||||
return ($this->query_result) ? @oci_num_rows($this->query_result) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details.
|
||||
*/
|
||||
public function sql_nextid()
|
||||
{
|
||||
if (!$this->query_result || !$this->last_query_text)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text, $tablename))
|
||||
{
|
||||
$query = 'SELECT ' . $tablename[1] . '_seq.currval FROM DUAL';
|
||||
$stmt = @oci_parse($this->db_connect_id, $query);
|
||||
@oci_execute($stmt, OCI_DEFAULT);
|
||||
|
||||
$temp_array = @oci_fetch_array($stmt, OCI_ASSOC + OCI_RETURN_NULLS);
|
||||
@oci_free_statement($stmt);
|
||||
|
||||
return (isset($temp_array['CURRVAL'])) ? $temp_array['CURRVAL'] : false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
|
||||
*/
|
||||
protected function _sql_fetchrow($query_id)
|
||||
{
|
||||
$row = @oci_fetch_array($query_id, OCI_ASSOC + OCI_RETURN_NULLS);
|
||||
|
||||
if (!$row)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$result_row = array();
|
||||
foreach ($row as $key => $value)
|
||||
{
|
||||
// Oracle treats empty strings as null
|
||||
if (is_null($value))
|
||||
{
|
||||
$value = '';
|
||||
}
|
||||
|
||||
// OCI->CLOB?
|
||||
if (is_object($value))
|
||||
{
|
||||
$value = $value->load();
|
||||
}
|
||||
|
||||
$result_row[strtolower($key)] = $value;
|
||||
}
|
||||
|
||||
return $result_row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
|
||||
*/
|
||||
protected function _sql_freeresult($query_id)
|
||||
{
|
||||
return @oci_free_statement($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 '\\'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
|
||||
*/
|
||||
public function sql_escape($msg)
|
||||
{
|
||||
return str_replace(array("'", "\0"), array("''", ''), $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':
|
||||
return 'LENGTH(' . $col . ')';
|
||||
break;
|
||||
|
||||
case 'length_text':
|
||||
return 'dbms_lob.getlength(' . $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 = oci_parse($this->db_connect_id, "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES (:" . implode(', :', array_keys($data)) . ')');
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = "UPDATE $table SET ";
|
||||
|
||||
$set = array();
|
||||
foreach (array_keys($data) as $key)
|
||||
{
|
||||
$set[] = "$key = :$key";
|
||||
}
|
||||
$query .= implode(', ', $set);
|
||||
|
||||
if ($where !== '')
|
||||
{
|
||||
$query .= $where;
|
||||
}
|
||||
|
||||
$stmt = oci_parse($this->db_connect_id, $query);
|
||||
}
|
||||
|
||||
foreach ($data as $column => $value)
|
||||
{
|
||||
oci_bind_by_name($stmt, ":$column", $data[$column], -1);
|
||||
}
|
||||
|
||||
oci_execute($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)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
|
||||
*/
|
||||
protected function _sql_error()
|
||||
{
|
||||
$error = @oci_error();
|
||||
$error = (empty($error)) ? @oci_error($this->query_result) : $error;
|
||||
$error = (empty($error)) ? @oci_error($this->db_connect_id) : $error;
|
||||
|
||||
if (empty($error))
|
||||
{
|
||||
$error = array(
|
||||
'message' => '',
|
||||
'code' => '',
|
||||
);
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
switch ($mode)
|
||||
{
|
||||
case 'start':
|
||||
$html_table = false;
|
||||
|
||||
// Grab a plan table, any will do
|
||||
$sql = "SELECT table_name
|
||||
FROM USER_TABLES
|
||||
WHERE table_name LIKE '%PLAN_TABLE%'";
|
||||
$stmt = @oci_parse($this->db_connect_id, $sql);
|
||||
@oci_execute($stmt);
|
||||
$result = array();
|
||||
|
||||
if ($result = @oci_fetch_array($stmt, OCI_ASSOC + OCI_RETURN_NULLS))
|
||||
{
|
||||
$table = $result['TABLE_NAME'];
|
||||
|
||||
// This is the statement_id that will allow us to track the plan
|
||||
$statement_id = substr(md5($query), 0, 30);
|
||||
|
||||
// Remove any stale plans
|
||||
$stmt2 = @oci_parse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
|
||||
@oci_execute($stmt2);
|
||||
@oci_free_statement($stmt2);
|
||||
|
||||
// Explain the plan
|
||||
$sql = "EXPLAIN PLAN
|
||||
SET STATEMENT_ID = '$statement_id'
|
||||
FOR $query";
|
||||
$stmt2 = @ociparse($this->db_connect_id, $sql);
|
||||
@oci_execute($stmt2);
|
||||
@oci_free_statement($stmt2);
|
||||
|
||||
// Get the data from the plan
|
||||
$sql = "SELECT operation, options, object_name, object_type, cardinality, cost
|
||||
FROM plan_table
|
||||
START WITH id = 0 AND statement_id = '$statement_id'
|
||||
CONNECT BY PRIOR id = parent_id
|
||||
AND statement_id = '$statement_id'";
|
||||
$stmt2 = @oci_parse($this->db_connect_id, $sql);
|
||||
@oci_execute($stmt2);
|
||||
|
||||
$row = array();
|
||||
while ($row = @oci_fetch_array($stmt2, OCI_ASSOC + OCI_RETURN_NULLS))
|
||||
{
|
||||
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
||||
}
|
||||
@oci_free_statement($stmt2);
|
||||
|
||||
// Remove the plan we just made, we delete them on request anyway
|
||||
$stmt2 = @oci_parse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
|
||||
@oci_execute($stmt2);
|
||||
@oci_free_statement($stmt2);
|
||||
}
|
||||
|
||||
@oci_free_statement($stmt);
|
||||
|
||||
if ($html_table)
|
||||
{
|
||||
$this->html_hold .= '</table>';
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'fromcache':
|
||||
$endtime = explode(' ', microtime());
|
||||
$endtime = $endtime[0] + $endtime[1];
|
||||
|
||||
$result = @oci_parse($this->db_connect_id, $query);
|
||||
$success = @oci_execute($result, OCI_DEFAULT);
|
||||
$row = array();
|
||||
|
||||
while ($void = @oci_fetch_array($result, OCI_ASSOC + OCI_RETURN_NULLS))
|
||||
{
|
||||
// Take the time spent on parsing rows into account
|
||||
}
|
||||
@oci_free_statement($result);
|
||||
|
||||
$splittime = explode(' ', microtime());
|
||||
$splittime = $splittime[0] + $splittime[1];
|
||||
|
||||
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Oracle specific code to handle the fact that it does not compare columns properly
|
||||
* @access private
|
||||
*/
|
||||
private function _rewrite_col_compare($args)
|
||||
{
|
||||
if (sizeof($args) == 4)
|
||||
{
|
||||
if ($args[2] == '=')
|
||||
{
|
||||
return '(' . $args[0] . ' OR (' . $args[1] . ' is NULL AND ' . $args[3] . ' is NULL))';
|
||||
}
|
||||
else if ($args[2] == '<>')
|
||||
{
|
||||
// really just a fancy way of saying foo <> bar or (foo is NULL XOR bar is NULL) but SQL has no XOR :P
|
||||
return '(' . $args[0] . ' OR ((' . $args[1] . ' is NULL AND ' . $args[3] . ' is NOT NULL) OR (' . $args[1] . ' is NOT NULL AND ' . $args[3] . ' is NULL)))';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->_rewrite_where($args[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Oracle specific code to handle it's lack of sanity
|
||||
* @access private
|
||||
*/
|
||||
private function _rewrite_where($where_clause)
|
||||
{
|
||||
preg_match_all('/\s*(AND|OR)?\s*([\w_.]++)\s*(?:(=|<[=>]?|>=?)\s*((?>\'(?>[^\']++|\'\')*+\'|[\d-.]+))|((NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))/', $where_clause, $result, PREG_SET_ORDER);
|
||||
$out = '';
|
||||
|
||||
foreach ($result as $val)
|
||||
{
|
||||
if (!isset($val[5]))
|
||||
{
|
||||
if ($val[4] !== "''")
|
||||
{
|
||||
$out .= $val[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
$out .= ' ' . $val[1] . ' ' . $val[2];
|
||||
if ($val[3] == '=')
|
||||
{
|
||||
$out .= ' is NULL';
|
||||
}
|
||||
else if ($val[3] == '<>')
|
||||
{
|
||||
$out .= ' is NOT NULL';
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$in_clause = array();
|
||||
$sub_exp = substr($val[5], strpos($val[5], '(') + 1, -1);
|
||||
$extra = false;
|
||||
preg_match_all('/\'(?>[^\']++|\'\')*+\'|[\d-.]++/', $sub_exp, $sub_vals, PREG_PATTERN_ORDER);
|
||||
$i = 0;
|
||||
|
||||
foreach ($sub_vals[0] as $sub_val)
|
||||
{
|
||||
// two things:
|
||||
// 1) This determines if an empty string was in the IN clausing, making us turn it into a NULL comparison
|
||||
// 2) This fixes the 1000 list limit that Oracle has (ORA-01795)
|
||||
if ($sub_val !== "''")
|
||||
{
|
||||
$in_clause[(int) $i++/1000][] = $sub_val;
|
||||
}
|
||||
else
|
||||
{
|
||||
$extra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$extra && $i < 1000)
|
||||
{
|
||||
$out .= $val[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
$out .= ' ' . $val[1] . '(';
|
||||
$in_array = array();
|
||||
|
||||
// constuct each IN() clause
|
||||
foreach ($in_clause as $in_values)
|
||||
{
|
||||
$in_array[] = $val[2] . ' ' . (isset($val[6]) ? $val[6] : '') . 'IN(' . implode(', ', $in_values) . ')';
|
||||
}
|
||||
|
||||
// Join the IN() clauses against a few ORs (IN is just a nicer OR anyway)
|
||||
$out .= implode(' OR ', $in_array);
|
||||
|
||||
// handle the empty string case
|
||||
if ($extra)
|
||||
{
|
||||
$out .= ' OR ' . $val[2] . ' is ' . (isset($val[6]) ? $val[6] : '') . 'NULL';
|
||||
}
|
||||
$out .= ')';
|
||||
|
||||
unset($in_array, $in_clause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -1,409 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* PostgreSQL Database Abstraction Layer
|
||||
* Minimum Requirement: 8.2+
|
||||
* @package dbal
|
||||
*/
|
||||
class phpbb_dbal_postgres extends phpbb_dbal
|
||||
{
|
||||
/**
|
||||
* @var string Database type. No distinction between versions or used extensions.
|
||||
*/
|
||||
public $dbms_type = 'postgres';
|
||||
|
||||
/**
|
||||
* @var array Database type map, column layout information
|
||||
*/
|
||||
public $dbms_type_map = array(
|
||||
'INT:' => 'INT4',
|
||||
'BINT' => 'INT8',
|
||||
'UINT' => 'INT4', // unsigned
|
||||
'UINT:' => 'INT4', // unsigned
|
||||
'USINT' => 'INT2', // unsigned
|
||||
'BOOL' => 'INT2', // unsigned
|
||||
'TINT:' => 'INT2',
|
||||
'VCHAR' => 'varchar(255)',
|
||||
'VCHAR:' => 'varchar(%d)',
|
||||
'CHAR:' => 'char(%d)',
|
||||
'XSTEXT' => 'varchar(1000)',
|
||||
'STEXT' => 'varchar(3000)',
|
||||
'TEXT' => 'varchar(8000)',
|
||||
'MTEXT' => 'TEXT',
|
||||
'XSTEXT_UNI'=> 'varchar(100)',
|
||||
'STEXT_UNI' => 'varchar(255)',
|
||||
'TEXT_UNI' => 'varchar(4000)',
|
||||
'MTEXT_UNI' => 'TEXT',
|
||||
'TIMESTAMP' => 'INT4', // 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' => 'bytea',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var string PostgreSQL schema (if supplied with $database -> database.schema)
|
||||
*/
|
||||
public $schema = '';
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
$connect_string = '';
|
||||
|
||||
if ($this->user)
|
||||
{
|
||||
$connect_string .= 'user=' . $this->user . ' ';
|
||||
}
|
||||
|
||||
if ($password)
|
||||
{
|
||||
$connect_string .= 'password=' . $password . ' ';
|
||||
}
|
||||
|
||||
if ($this->server)
|
||||
{
|
||||
if (strpos($this->server, ':') !== false)
|
||||
{
|
||||
list($this->server, $this->port) = explode(':', $this->server, 2);
|
||||
}
|
||||
|
||||
if ($this->server !== 'localhost')
|
||||
{
|
||||
$connect_string .= 'host=' . $this->server . ' ';
|
||||
}
|
||||
|
||||
if ($this->port)
|
||||
{
|
||||
$connect_string .= 'port=' . $this->port . ' ';
|
||||
}
|
||||
}
|
||||
|
||||
$this->schema = '';
|
||||
|
||||
if ($this->dbname)
|
||||
{
|
||||
if (strpos($this->dbname, '.') !== false)
|
||||
{
|
||||
list($this->dbname, $this->schema) = explode('.', $this->dbname, 2);
|
||||
}
|
||||
$connect_string .= 'dbname=' . $this->dbname;
|
||||
}
|
||||
|
||||
$this->db_connect_id = ($this->persistency) ? @pg_pconnect($connect_string, ($new_link) ? PGSQL_CONNECT_FORCE_NEW : false) : @pg_connect($connect_string, ($new_link) ? PGSQL_CONNECT_FORCE_NEW : false);
|
||||
|
||||
if (!$this->db_connect_id)
|
||||
{
|
||||
return $this->sql_error(htmlspecialchars_decode(phpbb::$last_notice['message']));
|
||||
}
|
||||
|
||||
if ($this->schema)
|
||||
{
|
||||
@pg_query($this->db_connect_id, 'SET search_path TO ' . $this->schema);
|
||||
}
|
||||
|
||||
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('#pgsql_version')) === false)
|
||||
{
|
||||
$query_id = @pg_query($this->db_connect_id, 'SELECT VERSION() AS version');
|
||||
$row = @pg_fetch_assoc($query_id, null);
|
||||
@pg_free_result($query_id);
|
||||
|
||||
$this->sql_server_version = (!empty($row['version'])) ? trim(substr($row['version'], 10)) : 0;
|
||||
|
||||
if (phpbb::registered('acm'))
|
||||
{
|
||||
phpbb::$acm->put('#pgsql_version', $this->sql_server_version);
|
||||
}
|
||||
}
|
||||
|
||||
return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $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 @pg_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)
|
||||
{
|
||||
$total = 'ALL';
|
||||
}
|
||||
|
||||
$query .= "\n LIMIT $total OFFSET $offset";
|
||||
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 @pg_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 @pg_query($this->db_connect_id, 'BEGIN');
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
return @pg_query($this->db_connect_id, 'COMMIT');
|
||||
break;
|
||||
|
||||
case 'rollback':
|
||||
return @pg_query($this->db_connect_id, 'ROLLBACK');
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details.
|
||||
*/
|
||||
public function sql_affectedrows()
|
||||
{
|
||||
return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details.
|
||||
*/
|
||||
public function sql_nextid()
|
||||
{
|
||||
if (!$this->db_connect_id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = "SELECT lastval() AS last_value";
|
||||
$temp_q_id = @pg_query($this->db_connect_id, $query);
|
||||
|
||||
if (!$temp_q_id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$temp_result = @pg_fetch_assoc($temp_q_id, NULL);
|
||||
@pg_free_result($query_id);
|
||||
|
||||
return ($temp_result) ? $temp_result['last_value'] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
|
||||
*/
|
||||
protected function _sql_fetchrow($query_id)
|
||||
{
|
||||
return @pg_fetch_assoc($query_id, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
|
||||
*/
|
||||
protected function _sql_freeresult($query_id)
|
||||
{
|
||||
return @pg_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.
|
||||
* Note: Do not use for bytea values if we may use them at a later stage
|
||||
*/
|
||||
public function sql_escape($msg)
|
||||
{
|
||||
return @pg_escape_string($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 = '')
|
||||
{
|
||||
// for now, stmtname is an empty string, it might change to something more unique in the future
|
||||
if ($type === 'INSERT')
|
||||
{
|
||||
$stmt = pg_prepare($this->dbms_type, '', "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES ($" . implode(', $', range(1, sizeof($data))) . ')');
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = "UPDATE $table SET ";
|
||||
|
||||
$set = array();
|
||||
foreach (array_keys($data) as $key_id => $key)
|
||||
{
|
||||
$set[] = $key . ' = $' . $key_id;
|
||||
}
|
||||
$query .= implode(', ', $set);
|
||||
|
||||
if ($where !== '')
|
||||
{
|
||||
$query .= $where;
|
||||
}
|
||||
|
||||
$stmt = pg_prepare($this->db_connect_id, '', $query);
|
||||
}
|
||||
|
||||
// add the stmtname to the top
|
||||
array_unshift($data, '');
|
||||
|
||||
// add the connection resource
|
||||
array_unshift($data, $this->db_connect_id);
|
||||
|
||||
call_user_func_array('pg_execute', $data);
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
|
||||
*/
|
||||
protected function _sql_custom_build($stage, $data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
|
||||
*/
|
||||
protected function _sql_error()
|
||||
{
|
||||
return array(
|
||||
'message' => (!$this->db_connect_id) ? @pg_last_error() : @pg_last_error($this->db_connect_id),
|
||||
'code' => ''
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = '')
|
||||
{
|
||||
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;
|
||||
|
||||
if ($result = @pg_query($this->db_connect_id, "EXPLAIN $explain_query"))
|
||||
{
|
||||
while ($row = @pg_fetch_assoc($result, NULL))
|
||||
{
|
||||
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
||||
}
|
||||
}
|
||||
@pg_free_result($result);
|
||||
|
||||
if ($html_table)
|
||||
{
|
||||
$this->html_hold .= '</table>';
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'fromcache':
|
||||
$endtime = explode(' ', microtime());
|
||||
$endtime = $endtime[0] + $endtime[1];
|
||||
|
||||
$result = @pg_query($this->db_connect_id, $query);
|
||||
while ($void = @pg_fetch_assoc($result, NULL))
|
||||
{
|
||||
// Take the time spent on parsing rows into account
|
||||
}
|
||||
@pg_free_result($result);
|
||||
|
||||
$splittime = explode(' ', microtime());
|
||||
$splittime = $splittime[0] + $splittime[1];
|
||||
|
||||
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -1,307 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sqlite Database Abstraction Layer
|
||||
* Minimum Requirement: 2.8.2+
|
||||
* @package dbal
|
||||
*/
|
||||
class phpbb_dbal_sqlite extends phpbb_dbal
|
||||
{
|
||||
/**
|
||||
* @var string Database type. No distinction between versions or used extensions.
|
||||
*/
|
||||
public $dbms_type = 'sqlite';
|
||||
|
||||
/**
|
||||
* Database features
|
||||
*
|
||||
* <ul>
|
||||
* <li>multi_insert: Supports multi inserts</li>
|
||||
* <li>count_distinct: Supports COUNT(DISTINGT ...)</li>
|
||||
* <li>multi_table_deletion: Supports multiple table deletion</li>
|
||||
* <li>truncate: Supports table truncation</li>
|
||||
* </ul>
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $features = array(
|
||||
'multi_insert' => true,
|
||||
// like MS ACCESS, SQLite does not support COUNT(DISTINCT ...)
|
||||
'count_distinct' => false,
|
||||
'multi_table_deletion' => true,
|
||||
// can't truncate a table
|
||||
'truncate' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* @var array Database type map, column layout information
|
||||
*/
|
||||
public $dbms_type_map = array(
|
||||
'INT:' => 'int(%d)',
|
||||
'BINT' => 'bigint(20)',
|
||||
'UINT' => 'INTEGER UNSIGNED', //'mediumint(8) UNSIGNED',
|
||||
'UINT:' => 'INTEGER UNSIGNED', // 'int(%d) UNSIGNED',
|
||||
'TINT:' => 'tinyint(%d)',
|
||||
'USINT' => 'INTEGER UNSIGNED', //'mediumint(4) UNSIGNED',
|
||||
'BOOL' => 'INTEGER UNSIGNED', //'tinyint(1) UNSIGNED',
|
||||
'VCHAR' => 'varchar(255)',
|
||||
'VCHAR:' => 'varchar(%d)',
|
||||
'CHAR:' => 'char(%d)',
|
||||
'XSTEXT' => 'text(65535)',
|
||||
'STEXT' => 'text(65535)',
|
||||
'TEXT' => 'text(65535)',
|
||||
'MTEXT' => 'mediumtext(16777215)',
|
||||
'XSTEXT_UNI'=> 'text(65535)',
|
||||
'STEXT_UNI' => 'text(65535)',
|
||||
'TEXT_UNI' => 'text(65535)',
|
||||
'MTEXT_UNI' => 'mediumtext(16777215)',
|
||||
'TIMESTAMP' => 'INTEGER UNSIGNED', //'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' => 'blob',
|
||||
);
|
||||
|
||||
/**
|
||||
* 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 . (($port) ? ':' . $port : '');
|
||||
$this->dbname = $database;
|
||||
|
||||
$error = '';
|
||||
$this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0666, $error) : @sqlite_open($this->server, 0666, $error);
|
||||
|
||||
if ($this->db_connect_id)
|
||||
{
|
||||
@sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id);
|
||||
@sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id);
|
||||
}
|
||||
|
||||
return ($this->db_connect_id) ? true : array('message' => $error);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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('#sqlite_version')) === false)
|
||||
{
|
||||
$result = @sqlite_query('SELECT sqlite_version() AS version', $this->db_connect_id);
|
||||
$row = @sqlite_fetch_array($result, SQLITE_ASSOC);
|
||||
|
||||
$this->sql_server_version = (!empty($row['version'])) ? $row['version'] : 0;
|
||||
|
||||
if (phpbb::registered('acm'))
|
||||
{
|
||||
phpbb::$acm->put('#sqlite_version', $this->sql_server_version);
|
||||
}
|
||||
}
|
||||
|
||||
return ($raw) ? $this->sql_server_version : 'SQLite ' . $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 @sqlite_query($query, $this->db_connect_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$total = -1;
|
||||
}
|
||||
|
||||
$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 @sqlite_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 @sqlite_query('BEGIN', $this->db_connect_id);
|
||||
break;
|
||||
|
||||
case 'commit':
|
||||
return @sqlite_query('COMMIT', $this->db_connect_id);
|
||||
break;
|
||||
|
||||
case 'rollback':
|
||||
return @sqlite_query('ROLLBACK', $this->db_connect_id);
|
||||
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) ? @sqlite_changes($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) ? @sqlite_last_insert_rowid($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 @sqlite_fetch_array($query_id, SQLITE_ASSOC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
|
||||
*/
|
||||
protected function _sql_freeresult($query_id)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
// Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it!
|
||||
// We only catch * and ? here, not the character map possible for file globbing.
|
||||
$expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
|
||||
|
||||
$expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
|
||||
$expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
|
||||
|
||||
return 'GLOB \'' . $this->sql_escape($expression) . '\'';
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
|
||||
*/
|
||||
public function sql_escape($msg)
|
||||
{
|
||||
return @sqlite_escape_string($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 = '')
|
||||
{
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
|
||||
*/
|
||||
protected function _sql_custom_build($stage, $data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
|
||||
*/
|
||||
protected function _sql_error()
|
||||
{
|
||||
return array(
|
||||
'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
|
||||
'code' => @sqlite_last_error($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 = '')
|
||||
{
|
||||
switch ($mode)
|
||||
{
|
||||
case 'start':
|
||||
break;
|
||||
|
||||
case 'fromcache':
|
||||
$endtime = explode(' ', microtime());
|
||||
$endtime = $endtime[0] + $endtime[1];
|
||||
|
||||
$result = @sqlite_query($query, $this->db_connect_id);
|
||||
while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC))
|
||||
{
|
||||
// Take the time spent on parsing rows into account
|
||||
}
|
||||
|
||||
$splittime = explode(' ', microtime());
|
||||
$splittime = $splittime[0] + $splittime[1];
|
||||
|
||||
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user