diff options
-rw-r--r-- | phpBB/includes/db/db2.php | 461 | ||||
-rw-r--r-- | phpBB/includes/functions_install.php | 13 | ||||
-rw-r--r-- | phpBB/language/en/install.php | 5 |
3 files changed, 233 insertions, 246 deletions
diff --git a/phpBB/includes/db/db2.php b/phpBB/includes/db/db2.php index 8a5ce0103d..df03d047c3 100644 --- a/phpBB/includes/db/db2.php +++ b/phpBB/includes/db/db2.php @@ -1,10 +1,10 @@ <?php -/** +/** * * @package dbal * @version $Id$ -* @copyright (c) 2005 phpBB Group -* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ @@ -16,277 +16,261 @@ 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; + /** + * @var string Database type. No distinction between versions or used extensions. + */ + public $dbms_type = 'db2'; - // can't truncate a table - var $truncate = false; + /** + * @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 $dbms_type = 'db2'; + /** + * @var array Database features + */ + public $features = array( + 'multi_insert' => true, + 'count_distinct' => true, + 'multi_table_deletion' => true, + 'truncate' => false, + ); /** - * Connect to server + * Connect to server. See {@link phpbb_dbal::sql_connect() sql_connect()} for details. */ - function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false) + public function sql_connect($server, $user, $password, $database, $port = false, $persistency = false , $new_link = false) { $this->persistency = $persistency; - $this->user = $sqluser; - $this->server = $sqlserver . (($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; - } - - /** - * SQL Transaction - * @access private - */ - function _sql_transaction($status = 'begin') - { - switch ($status) + if (!phpbb::registered('acm') || ($this->sql_server_version = phpbb::$acm->get('#db2_version')) === false) { - case 'begin': - return @db2_autocommit($this->db_connect_id, DB2_AUTOCOMMIT_OFF); - break; + $info = @db2_server_info($this->db_connect_id); - case 'commit': - $result = @db2_commit($this->db_connect_id); - @db2_autocommit($this->db_connect_id, DB2_AUTOCOMMIT_ON); - return $result; - break; + $this->sql_server_info = is_object($info) ? $info->DBMS_VER : 0; - case 'rollback': - $result = @db2_rollback($this->db_connect_id); - @db2_autocommit($this->db_connect_id, DB2_AUTOCOMMIT_ON); - return $result; - break; + if (phpbb::registered('acm')) + { + phpbb::$acm->put('#db2_version', $this->sql_server_version); + } } - return true; + return ($raw) ? $this->sql_server_version : 'IBM DB2 ' . $this->sql_server_version; } /** - * 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 + * DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details. */ - function sql_query($query = '', $cache_ttl = 0) + protected function _sql_query($query) { - if ($query != '') - { - global $cache; - - // EXPLAIN only in extra debug mode - if (defined('DEBUG_EXTRA')) - { - $this->sql_report('start', $query); - } + $array = array(); - $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) + // Cope with queries larger than 32K + if (strlen($query) > 32740) + { + if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs)) { - $array = array(); - if (strlen($query) > 32740) + if (strlen($regs[3]) > 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); + preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER); - $inserts = $vals[0]; - unset($vals); + $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)) + foreach ($inserts as $key => $value) { - if (strlen($data[0][3]) > 32740) + // check to see if this thing is greater than the max + 'x2 + if (!empty($value) && $value[0] === "'" && strlen($value) > 32742) { - $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); + $inserts[$key] = '?'; + $array[] = str_replace("''", "'", substr($value, 1, -1)); } } - } - - 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); - } + $query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')'; } - else + } + 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) { - if (($this->query_result = @db2_exec($this->db_connect_id, $query)) === false) + $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) { - $this->sql_error($query); + // 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]; + } } - } - if (defined('DEBUG_EXTRA')) - { - $this->sql_report('stop', $query); + $query = $update . implode(', ', $cols) . ' ' . $where; + unset($cols); } + } + } - 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; - } + if (sizeof($array)) + { + $result = @db2_prepare($this->db_connect_id, $query); + + if (!$result) + { + return false; } - else if (defined('DEBUG_EXTRA')) + + if (!@db2_execute($result, $array)) { - $this->sql_report('fromcache', $query); + return false; } } else { - return false; + $result = @db2_exec($this->db_connect_id, $query); } - return ($this->query_result) ? $this->query_result : false; + return $result; } /** - * Build LIMIT query + * Build LIMIT query and run it. See {@link phpbb_dbal::_sql_query_limit() _sql_query_limit()} for details. */ - function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) + protected function _sql_query_limit($query, $total, $offset, $cache_ttl) { - if ($query != '') + if ($total && $offset == 0) { - $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); - } + return $this->sql_query($query . ' fetch first ' . $total . ' rows only', $cache_ttl); } - else + + // Seek by $offset rows + if ($offset) { - return false; + $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); } /** - * Return number of affected rows + * Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details. */ - function sql_affectedrows() + protected function _sql_close() { - return ($this->db_connect_id) ? @db2_num_rows($this->db_connect_id) : false; + return @db2_close($this->db_connect_id); } /** - * Fetch current row + * SQL Transaction. See {@link phpbb_dbal::_sql_transaction() _sql_transaction()} for details. */ - function sql_fetchrow($query_id = false) + protected function _sql_transaction($status) { - global $cache; - - if ($query_id === false) + switch ($status) { - $query_id = $this->query_result; - } + case 'begin': + return @db2_autocommit($this->db_connect_id, DB2_AUTOCOMMIT_OFF); + break; - if (isset($cache->sql_rowset[$query_id])) - { - return $cache->sql_fetchrow($query_id); - } + case 'commit': + $result = @db2_commit($this->db_connect_id); + @db2_autocommit($this->db_connect_id, DB2_AUTOCOMMIT_ON); + return $result; + break; - if ($query_id === false) - { - return false; + case 'rollback': + $result = @db2_rollback($this->db_connect_id); + @db2_autocommit($this->db_connect_id, DB2_AUTOCOMMIT_ON); + return $result; + break; } - $row = @db2_fetch_assoc($query_id); + return true; + } - return $row; + /** + * 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 + * Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details. */ - function sql_nextid() + 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)) @@ -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; - } - - if (isset($cache->sql_rowset[$query_id])) - { - return $cache->sql_freeresult($query_id); - } + return @db2_fetch_assoc($query_id); + } - if (isset($this->open_queries[$query_id])) - { - unset($this->open_queries[$query_id]); - return @db2_free_result($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); + } - return false; + /** + * 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 + * Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details. */ - function sql_escape($msg) + public function sql_escape($msg) { - return str_replace("'", "''", $msg); + return @db2_escape_string($msg); } /** - * Expose a DBMS specific function + * Expose a DBMS specific function. See {@link phpbb_dbal::sql_function() sql_function()} for details. */ - function sql_function($type, $col) + public function sql_function($type, $col) { switch ($type) { 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) + protected function _sql_custom_build($stage, $data) { - return $expression . " ESCAPE '\\'"; + return $data; } /** - * return sql error array - * @access private + * return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details. */ - function _sql_error() + protected function _sql_error() { + $message = @db2_stmt_errormsg(); + $code = @db2_stmt_error(); + + if (!$message && !$code) + { + $message = @db2_conn_errormsg(); + $code = @db2_conn_error(); + } + $error = array( - 'message' => @db2_stmt_errormsg(), - 'code' => @db2_stmt_error() + 'message' => $message, + 'code' => $code, ); - return $error; - } - - /** - * Build db-specific query data - * @access private - */ - function _sql_custom_build($stage, $data) - { - return $data; - } - /** - * Close sql connection - * @access private - */ - function _sql_close() - { - return @db2_close($this->db_connect_id); + 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) { diff --git a/phpBB/includes/functions_install.php b/phpBB/includes/functions_install.php index 53be92ac14..630bb78a71 100644 --- a/phpBB/includes/functions_install.php +++ b/phpBB/includes/functions_install.php @@ -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)) diff --git a/phpBB/language/en/install.php b/phpBB/language/en/install.php index 99b608e326..4181e081ca 100644 --- a/phpBB/language/en/install.php +++ b/phpBB/language/en/install.php @@ -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.', |