From 7082d1953b42d902506c422e137c4d854920b33e Mon Sep 17 00:00:00 2001 From: acydburn Date: Wed, 21 Jan 2009 17:05:38 +0000 Subject: [PATCH] MSSQL (MSSQL 2005 extension, odbc and plain) support git-svn-id: http://code.phpbb.com/svn/phpbb/trunk@9287 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/db/mssql.php | 324 +++++++---------- phpBB/includes/db/mssql_2005.php | 728 +++++++++++++++++++-------------------- phpBB/includes/db/mssql_odbc.php | 276 ++++++--------- 3 files changed, 588 insertions(+), 740 deletions(-) rewrite phpBB/includes/db/mssql_2005.php (65%) diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index 32ab1597c..d0dcb590f 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -16,57 +16,88 @@ if (!defined('IN_PHPBB')) exit; } -include_once(PHPBB_ROOT_PATH . 'includes/db/dbal.' . PHP_EXT); - /** * MSSQL Database Abstraction Layer * Minimum Requirement is MSSQL 2000+ * @package dbal */ -class dbal_mssql extends dbal +class phpbb_dbal_mssql extends phpbb_dbal { - var $dbms_type = 'mssql'; + /** + * @var string Database type. No distinction between versions or used extensions. + */ + public $dbms_type = 'mssql'; /** - * 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:' => '[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 = $sqluser; + $this->user = $user; $this->dbname = $database; + $this->port = $port; $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':'; - $this->server = $sqlserver . (($port) ? $port_delimiter . $port : ''); + $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, $sqlpassword, $new_link) : @mssql_connect($this->server, $this->user, $sqlpassword, $new_link); + $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 != '') + if (!$this->db_connect_id || !$this->dbname) { - if (!@mssql_select_db($this->dbname, $this->db_connect_id)) - { - @mssql_close($this->db_connect_id); - return false; - } + return $this->sql_error(phpbb::$last_notice['message']); } - return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); + if (!@mssql_select_db($this->dbname, $this->db_connect_id)) + { + return $this->sql_error(''); + } + + return $this->db_connect_id; } /** - * Version information about used database - * @param bool $raw if true, only return the fetched sql_server_version - * @return string sql server version + * Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details. */ - function sql_server_info($raw = false) + public function sql_server_info($raw = false) { - global $cache; - - if (empty($cache) || ($this->sql_server_version = $cache->get('mssql_version')) === 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); @@ -79,9 +110,9 @@ class dbal_mssql extends dbal $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0; - if (!empty($cache)) + if (phpbb::registered('acm')) { - $cache->put('mssql_version', $this->sql_server_version); + phpbb::$acm->put('#mssql_version', $this->sql_server_version); } } @@ -94,95 +125,18 @@ class dbal_mssql extends dbal } /** - * SQL Transaction - * @access private - */ - function _sql_transaction($status = 'begin') - { - 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; - } - - /** - * 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); - } - - $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) - { - if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === 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; + return @mssql_query($query, $this->db_connect_id); } /** - * 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) { - $this->query_result = false; - // 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) { @@ -209,55 +163,51 @@ class dbal_mssql extends dbal } /** - * 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) ? @mssql_rows_affected($this->db_connect_id) : false; + return @mssql_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 @mssql_query('BEGIN TRANSACTION', $this->db_connect_id); + break; - if (isset($cache->sql_rowset[$query_id])) - { - return $cache->sql_fetchrow($query_id); - } + case 'commit': + return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id); + break; - if ($query_id === false) - { - return false; + case 'rollback': + return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id); + break; } - $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 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) ? @mssql_rows_affected($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() { $result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id); + if ($result_id) { if ($row = @mssql_fetch_assoc($result_id)) @@ -272,43 +222,52 @@ class dbal_mssql 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; + $row = @mssql_fetch_assoc($query_id); - if ($query_id === false) + // I hope i am able to remove this later... hopefully only a PHP or MSSQL bug + if ($row) { - $query_id = $this->query_result; + foreach ($row as $key => $value) + { + $row[$key] = ($value === ' ' || $value === NULL) ? '' : $value; + } } - if (isset($cache->sql_rowset[$query_id])) - { - return $cache->sql_freeresult($query_id); - } + return $row; + } - if (isset($this->open_queries[$query_id])) - { - unset($this->open_queries[$query_id]); - return @mssql_free_result($query_id); - } + /** + * 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); + } - 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(array("'", "\0"), array("''", ''), $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) { @@ -319,35 +278,25 @@ class dbal_mssql extends dbal } } - 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 === 'UPDATE') - { - $this->sql_query('INSERT INTO ' . $table . ' ' . - $this->sql_build_array('INSERT', $data)); - } - else - { - $this->sql_query('UPDATE ' . $table . ' - SET ' . $db->sql_build_array('UPDATE', $data) . - $where); - } } + */ /** - * 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() { $error = array( 'message' => @mssql_get_last_message(), @@ -368,7 +317,7 @@ class dbal_mssql extends dbal FROM master.dbo.sysmessages WHERE error = ' . $error['code']; $result_id = @mssql_query($sql); - + if ($result_id) { $row = @mssql_fetch_assoc($result_id); @@ -383,28 +332,9 @@ class dbal_mssql extends dbal } /** - * Build db-specific query data - * @access private - */ - function _sql_custom_build($stage, $data) - { - return $data; - } - - /** - * Close sql connection - * @access private - */ - function _sql_close() - { - return @mssql_close($this->db_connect_id); - } - - /** - * 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/db/mssql_2005.php b/phpBB/includes/db/mssql_2005.php dissimilarity index 65% index 730013c6f..4206e1f7e 100644 --- a/phpBB/includes/db/mssql_2005.php +++ b/phpBB/includes/db/mssql_2005.php @@ -1,378 +1,350 @@ -persistency = $persistency; - $this->user = $sqluser; - $this->server = $sqlserver . (($port) ? ':' . $port : ''); - $this->dbname = $database; - - $this->db_connect_id = sqlsrv_connect($this->server, array('UID' => $this->user, 'PWD' => $sqlpassword)); - - if ($this->db_connect_id && $this->dbname != '') - { - if (!sqlsrv_conn_execute($this->db_connect_id, 'USE ' . $this->dbname)) - { - @sqlsrv_conn_close($this->db_connect_id); - return false; - } - } - - return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); - } - - /** - * Version information about used database - */ - function sql_server_info() - { - $server_info = sqlsrv_conn_server_info($this->db_connect_id); - - return 'MSSQL (2005)
' . $server_info['SQL Server Version']; - } - - /** - * SQL Transaction - * @access private - */ - function _sql_transaction($status = 'begin') - { - switch ($status) - { - case 'begin': - return sqlsrv_conn_execute($this->db_connect_id, 'BEGIN TRANSACTION'); - break; - - case 'commit': - return sqlsrv_conn_execute($this->db_connect_id, 'COMMIT TRANSACTION'); - break; - - case 'rollback': - return sqlsrv_conn_execute($this->db_connect_id, 'ROLLBACK TRANSACTION'); - break; - } - - return true; - } - - /** - * 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 - */ - function sql_query($query = '', $cache_ttl = 0) - { - if ($query != '') - { - global $cache; - - if (strpos($query, 'BEGIN') === 0 || strpos($query, 'COMMIT') === 0) - { - return true; - } - - // 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) - { - if (($this->query_result = @sqlsrv_conn_execute($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) - { - $this->query_result = false; - - // 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; - } - - /** - * Return number of affected rows - */ - function sql_affectedrows() - { - return ($this->db_connect_id) ? sqlsrv_stmt_rows_affected($this->db_connect_id) : false; - } - - /** - * Fetch current row - */ - function sql_fetchrow($query_id = false) - { - global $cache; - - if ($query_id === false) - { - $query_id = $this->query_result; - } - - if (isset($cache->sql_rowset[$query_id])) - { - return $cache->sql_fetchrow($query_id); - } - - if ($query_id === false) - { - return false; - } - - $row = @sqlsrv_stmt_fetch_array($query_id, SQLSRV_FETCH_TYPE_ARRAY); - - // 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; - } - - /** - * Get last inserted id after insert statement - */ - function sql_nextid() - { - $result_id = @sqlsrv_conn_execute($this->db_connect_id, 'SELECT SCOPE_IDENTITY()'); - if ($result_id) - { - if ($row = @sqlsrv_stmt_fetch_array($result_id, SQLSRV_FETCH_TYPE_ARRAY)) - { - @sqlsrv_stmt_close($result_id); - return $row['computed']; - } - @sqlsrv_stmt_close($result_id); - } - - return false; - } - - /** - * Free sql result - */ - function sql_freeresult($query_id = false) - { - global $cache; - - if ($query_id === false) - { - $query_id = $this->query_result; - } - - if (isset($cache->sql_rowset[$query_id])) - { - return $cache->sql_freeresult($query_id); - } - - if (isset($this->open_queries[$query_id])) - { - unset($this->open_queries[$query_id]); - return @sqlsrv_stmt_close($query_id); - } - - return false; - } - - /** - * Escape string used in sql query - */ - function sql_escape($msg) - { - return str_replace("'", "''", $msg); - } - - /** - * Expose a DBMS specific function - */ - function sql_function($type, $col) - { - switch ($type) - { - case 'length_varchar': - case 'length_text': - return 'DATALENGTH(' . $col . ')'; - break; - } - } - - /** - * Build LIKE expression - * @access private - */ - function _sql_like_expression($expression) - { - return $expression . " ESCAPE '\\'"; - } - - /** - * return sql error array - * @access private - */ - function _sql_error() - { - $error = array( - 'message' => '', - 'code' => '' - ); - - foreach (sqlsrv_errors() as $error_array) - { - $error['message'] .= $error_array['message'] . "
"; - $error['code'] .= $error_array['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 @sqlsrv_conn_close($this->db_connect_id); - } - - /** - * Build db-specific report - * @access private - */ - function _sql_report($mode, $query = '') - { - switch ($mode) - { - case 'fromcache': - $endtime = explode(' ', microtime()); - $endtime = $endtime[0] + $endtime[1]; - - $result = @sqlsrv_conn_execute($this->db_connect_id, $query); - while ($void = @sqlsrv_stmt_fetch_array($result)) - { - // Take the time spent on parsing rows into account - } - @sqlsrv_stmt_close($result); - - $splittime = explode(' ', microtime()); - $splittime = $splittime[0] + $splittime[1]; - - $this->sql_report('record_fromcache', $query, $endtime, $splittime); - - break; - } - } -} - -?> \ No newline at end of file + '[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('
', $message), + 'code' => implode('
', $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; + } + } +} + +?> \ No newline at end of file diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php index e2416ee95..3fd563515 100644 --- a/phpBB/includes/db/mssql_odbc.php +++ b/phpBB/includes/db/mssql_odbc.php @@ -29,21 +29,57 @@ include_once(PHPBB_ROOT_PATH . 'includes/db/dbal.' . PHP_EXT); * * @package dbal */ -class dbal_mssql_odbc extends dbal +class phpbb_dbal_mssql_odbc extends phpbb_dbal { - var $dbms_type = 'mssql'; + /** + * @var string Database type. No distinction between versions or used extensions. + */ + public $dbms_type = 'mssql'; /** - * 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:' => '[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 = $sqluser; + $this->user = $user; $this->dbname = $database; + $this->port = $port; $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':'; - $this->server = $sqlserver . (($port) ? $port_delimiter . $port : ''); + $this->server = $server . (($port) ? $port_delimiter . $port : ''); $max_size = @ini_get('odbc.defaultlrl'); if (!empty($max_size)) @@ -68,21 +104,17 @@ class dbal_mssql_odbc extends dbal @ini_set('odbc.defaultlrl', $max_size); } - $this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword); + $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 - * @param bool $raw if true, only return the fetched sql_server_version - * @return string sql server version + * Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details. */ - function sql_server_info($raw = false) + public function sql_server_info($raw = false) { - global $cache; - - if (empty($cache) || ($this->sql_server_version = $cache->get('mssqlodbc_version')) === 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')"); @@ -95,9 +127,9 @@ class dbal_mssql_odbc extends dbal $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0; - if (!empty($cache)) + if (phpbb::registered('acm')) { - $cache->put('mssqlodbc_version', $this->sql_server_version); + phpbb::$acm->put('#mssqlodbc_version', $this->sql_server_version); } } @@ -110,95 +142,18 @@ class dbal_mssql_odbc extends dbal } /** - * SQL Transaction - * @access private - */ - function _sql_transaction($status = 'begin') - { - 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; - } - - /** - * 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); - } - - $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) - { - if (($this->query_result = @odbc_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; + return @odbc_exec($this->db_connect_id, $query); } /** - * 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) { - $this->query_result = false; - // 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) { @@ -232,38 +187,48 @@ class dbal_mssql_odbc extends dbal } /** - * 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) ? @odbc_num_rows($this->query_result) : false; + return @odbc_close($this->db_connect_id); } /** - * Fetch current row - * @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. + * SQL Transaction. See {@link phpbb_dbal::_sql_transaction() _sql_transaction()} for details. */ - function sql_fetchrow($query_id = false, $debug = false) + protected function _sql_transaction($status) { - global $cache; - - if ($query_id === false) + switch ($status) { - $query_id = $this->query_result; - } + case 'begin': + return @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION'); + break; - if (isset($cache->sql_rowset[$query_id])) - { - return $cache->sql_fetchrow($query_id); + 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 ($query_id !== false) ? @odbc_fetch_array($query_id) : false; + return true; } /** - * Get last inserted id after insert statement + * Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details. */ - function sql_nextid() + 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'); @@ -282,43 +247,42 @@ class dbal_mssql_odbc extends dbal } /** - * Free sql result + * 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. */ - 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 @odbc_fetch_array($query_id); + } - if (isset($this->open_queries[(int) $query_id])) - { - unset($this->open_queries[(int) $query_id]); - return @odbc_free_result($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); + } - 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(array("'", "\0"), array("''", ''), $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) { @@ -329,7 +293,9 @@ class dbal_mssql_odbc extends dbal } } - 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') { @@ -359,30 +325,20 @@ class dbal_mssql_odbc extends dbal call_user_func_array('odbc_execute', $data); } - - /** - * Build LIKE expression - * @access private */ - function _sql_like_expression($expression) - { - return $expression . " ESCAPE '\\'"; - } /** - * Build db-specific query data - * @access private + * Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details. */ - function _sql_custom_build($stage, $data) + protected function _sql_custom_build($stage, $data) { 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() { return array( 'message' => @odbc_errormsg(), @@ -391,19 +347,9 @@ class dbal_mssql_odbc extends dbal } /** - * Close sql connection - * @access private - */ - function _sql_close() - { - return @odbc_close($this->db_connect_id); - } - - /** - * 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) { -- 2.11.4.GIT