aaaand DB2 support. :)

git-svn-id: file:///svn/phpbb/trunk@9294 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2009-01-22 12:54:26 +00:00
parent e9f2f841f6
commit 5f265f47d9
3 changed files with 265 additions and 278 deletions

View file

@ -16,51 +16,218 @@ if (!defined('IN_PHPBB'))
exit;
}
include_once(PHPBB_ROOT_PATH . 'includes/db/dbal.' . PHP_EXT);
/**
* MSSQL Database Abstraction Layer
* Minimum Requirement is DB2 8.2.2
* Minimum Requirement: DB2 8.2.2+
* Minimum extension version: PECL ibm_db2 1.6.0+
* @package dbal
*/
class dbal_db2 extends dbal
class phpbb_dbal_db2 extends phpbb_dbal
{
var $multi_insert = true;
// can't truncate a table
var $truncate = false;
var $dbms_type = 'db2';
/**
* @var string Database type. No distinction between versions or used extensions.
*/
public $dbms_type = 'db2';
/**
* Connect to server
* @var array Database type map, column layout information
*/
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
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 = $sqluser;
$this->server = $sqlserver . (($port) ? ':' . $port : '');
$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, $sqlpassword, array('autocommit' => DB2_AUTOCOMMIT_ON, 'DB2_ATTR_CASE' => DB2_CASE_LOWER)) : @db2_connect($this->dbname, $this->user, $sqlpassword, array('autocommit' => DB2_AUTOCOMMIT_ON, 'DB2_ATTR_CASE' => DB2_CASE_LOWER));
$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
* Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details.
*/
function sql_server_info()
public function sql_server_info($raw = false)
{
$info = db2_server_info($this->db_connect_id);
return $info->DBMS_VER;
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;
}
/**
* SQL Transaction
* @access private
* DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
*/
function _sql_transaction($status = 'begin')
protected function _sql_query($query)
{
$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)
{
@ -85,208 +252,25 @@ class dbal_db2 extends dbal
}
/**
* Base query method
*
* @param string $query Contains the SQL query which shall be executed
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
* @return mixed When casted to bool the returned value returns true on success and false on failure
*
* @access public
* Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details.
*/
function sql_query($query = '', $cache_ttl = 0)
{
if ($query != '')
{
global $cache;
// EXPLAIN only in extra debug mode
if (defined('DEBUG_EXTRA'))
{
$this->sql_report('start', $query);
}
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
$this->sql_add_num_queries($this->query_result);
if ($this->query_result === false)
{
$array = array();
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)
{
if (!empty($value) && $value[0] === "'" && strlen($value) > 32742) // check to see if this thing is greater than the max + 'x2
{
$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)
{
if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 32742) // check to see if this thing is greater than the max + 'x2
{
$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))
{
if (($this->query_result = @db2_prepare($this->db_connect_id, $query)) === false)
{
$this->sql_error($query);
}
if (!@db2_execute($this->query_result, $array))
{
$this->sql_error($query);
}
}
else
{
if (($this->query_result = @db2_exec($this->db_connect_id, $query)) === false)
{
$this->sql_error($query);
}
}
if (defined('DEBUG_EXTRA'))
{
$this->sql_report('stop', $query);
}
if ($cache_ttl && method_exists($cache, 'sql_save'))
{
$this->open_queries[(int) $this->query_result] = $this->query_result;
$cache->sql_save($query, $this->query_result, $cache_ttl);
}
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
{
$this->open_queries[(int) $this->query_result] = $this->query_result;
}
}
else if (defined('DEBUG_EXTRA'))
{
$this->sql_report('fromcache', $query);
}
}
else
{
return false;
}
return ($this->query_result) ? $this->query_result : false;
}
/**
* Build LIMIT query
*/
function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
{
if ($query != '')
{
$this->query_result = false;
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);
}
}
else
{
return false;
}
}
/**
* Return number of affected rows
*/
function sql_affectedrows()
public function sql_affectedrows()
{
return ($this->db_connect_id) ? @db2_num_rows($this->db_connect_id) : false;
}
/**
* Fetch current row
* Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details.
*/
function sql_fetchrow($query_id = false)
public function sql_nextid()
{
global $cache;
if ($query_id === false)
if (function_exists('db2_last_insert_id')
{
$query_id = $this->query_result;
return @db2_last_insert_id($this->db_connect_id);
}
if (isset($cache->sql_rowset[$query_id]))
{
return $cache->sql_fetchrow($query_id);
}
if ($query_id === false)
{
return false;
}
$row = @db2_fetch_assoc($query_id);
return $row;
}
/**
* Get last inserted id after insert statement
*/
function sql_nextid()
{
$result_id = @db2_exec($this->db_connect_id, 'VALUES IDENTITY_VAL_LOCAL()');
if ($result_id)
{
if ($row = @db2_fetch_assoc($result_id))
@ -301,57 +285,54 @@ class dbal_db2 extends dbal
}
/**
* Free sql result
* Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
*/
function sql_freeresult($query_id = false)
protected function _sql_fetchrow($query_id)
{
global $cache;
if ($query_id === false)
{
$query_id = $this->query_result;
return @db2_fetch_assoc($query_id);
}
if (isset($cache->sql_rowset[$query_id]))
/**
* Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
*/
protected function _sql_freeresult($query_id)
{
return $cache->sql_freeresult($query_id);
}
if (isset($this->open_queries[$query_id]))
{
unset($this->open_queries[$query_id]);
return @db2_free_result($query_id);
}
return false;
}
/**
* Escape string used in sql query
* Correctly adjust LIKE expression for special characters. See {@link phpbb_dbal::_sql_like_expression() _sql_like_expression()} for details.
*/
function sql_escape($msg)
protected function _sql_like_expression($expression)
{
return str_replace("'", "''", $msg);
return $expression . " ESCAPE '\\'";
}
/**
* Expose a DBMS specific function
* Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
*/
function sql_function($type, $col)
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':
return 'LENGTH(' . $col . ')';
break;
case 'length_text':
return 'LENGTH(' . $col . ')';
break;
}
}
function sql_handle_data($type, $table, $data, $where = '')
/**
* Handle data by using prepared statements. See {@link phpbb_dbal::sql_handle_data() sql_handle_data()} for details.
public function sql_handle_data($type, $table, $data, $where = '')
{
if ($type == 'INSERT')
{
@ -381,52 +362,42 @@ class dbal_db2 extends dbal
call_user_func_array('db2_execute', $data);
}
*/
/**
* Build LIKE expression
* @access private
* Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
*/
function _sql_like_expression($expression)
{
return $expression . " ESCAPE '\\'";
}
/**
* return sql error array
* @access private
*/
function _sql_error()
{
$error = array(
'message' => @db2_stmt_errormsg(),
'code' => @db2_stmt_error()
);
return $error;
}
/**
* Build db-specific query data
* @access private
*/
function _sql_custom_build($stage, $data)
protected function _sql_custom_build($stage, $data)
{
return $data;
}
/**
* Close sql connection
* @access private
* return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
*/
function _sql_close()
protected function _sql_error()
{
return @db2_close($this->db_connect_id);
$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;
}
/**
* Build db-specific report
* @access private
* Run DB-specific code to build SQL Report to explain queries, show statistics and runtime information. See {@link phpbb_dbal::_sql_report() _sql_report()} for details.
*/
function _sql_report($mode, $query = '')
protected function _sql_report($mode, $query = '')
{
switch ($mode)
{

View file

@ -505,6 +505,19 @@ function connect_check_db($dbms_details, $table_prefix, $dbhost, $dbuser, $dbpas
* @todo check odbc.defaultlrl (min 128K) and odbc.defaultbinmode (1)
*/
break;
case 'db2':
if (version_compare($db->sql_server_info(true), '8.2.2', '<'))
{
$error[] = phpbb::$user->lang['INST_ERR_DB_DB2_VERSION'];
}
// Now check the extension version
if (!function_exists('db2_escape_string'))
{
$error[] = phpbb::$user->lang['INST_ERR_DB_DB2_EXT_VERSION'];
}
break;
}
if (sizeof($error))

View file

@ -235,7 +235,10 @@ $lang = array_merge($lang, array(
'INST_ERR_DB_SQLITE_VERSION' => 'The version of the SQLite extension you have installed is too old, it must be upgraded to at least 2.8.2.',
'INST_ERR_DB_ORACLE_VERSION' => 'The version of Oracle installed on the target server must be at least 9.2, phpBB 3.1 cannot be installed on an older version.',
'INST_ERR_DB_FIREBIRD_VERSION' => 'The version of Firebird installed on this machine is older than 2.0, please upgrade to a newer version.',
'INST_ERR_DB_POSTGRES_VERSION' => 'The version of PostgreSQL installed on this machine is older than 8.2, please upgrade to a newer version.',
'INST_ERR_DB_POSTGRES_VERSION' => 'The version of PostgreSQL installed on the target server is older than 8.2, please upgrade to a newer version.',
'INST_ERR_DB_DB2_VERSION' => 'The version of DB2 installed on the target server is older than 8.2.2, please upgrade to a newer version.',
'INST_ERR_DB_DB2_EXT_VERSION' => 'The version of the IBM_DB2 extension installed on this machine is older than 1.6.0, please install a newer version.',
'INST_ERR_DB_NO_ORACLE_NLS' => 'You must configure Oracle so that the <var>NLS_CHARACTERSET</var> parameter is set to <var>ALU32UTF8</var>.',
'INST_ERR_DB_NO_FIREBIRD_PS' => 'The database you selected for Firebird has a page size less than 8192, it must be at least 8192.',
'INST_ERR_DB_NO_POSTGRES_UTF8' => 'The database you have selected was not created in <var>UNICODE</var> or <var>UTF8</var> encoding. Try installing with a database in <var>UNICODE</var> or <var>UTF8</var> encoding.',