|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
*
|
|
|
f2e824 |
* @package dbal
|
|
|
f2e824 |
* @version $Id: mssql.php 8967 2008-10-02 12:04:12Z acydburn $
|
|
|
f2e824 |
* @copyright (c) 2005 phpBB Group
|
|
|
f2e824 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
f2e824 |
*
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* @ignore
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
if (!defined('IN_PHPBB'))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
exit;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* MSSQL Database Abstraction Layer
|
|
|
f2e824 |
* Minimum Requirement is MSSQL 2000+
|
|
|
f2e824 |
* @package dbal
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
class dbal_mssql extends dbal
|
|
|
f2e824 |
{
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Connect to server
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->persistency = $persistency;
|
|
|
f2e824 |
$this->user = $sqluser;
|
|
|
f2e824 |
$this->dbname = $database;
|
|
|
f2e824 |
|
|
|
f2e824 |
$port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
|
|
|
f2e824 |
$this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
|
|
|
f2e824 |
|
|
|
f2e824 |
@ini_set('mssql.charset', 'UTF-8');
|
|
|
f2e824 |
@ini_set('mssql.textlimit', 2147483647);
|
|
|
f2e824 |
@ini_set('mssql.textsize', 2147483647);
|
|
|
f2e824 |
|
|
|
f2e824 |
if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.1', '>=')))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mssql_connect($this->server, $this->user, $sqlpassword, $new_link);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($this->db_connect_id && $this->dbname != '')
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if (!@mssql_select_db($this->dbname, $this->db_connect_id))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
@mssql_close($this->db_connect_id);
|
|
|
f2e824 |
return false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Version information about used database
|
|
|
f2e824 |
* @param bool $raw if true, only return the fetched sql_server_version
|
|
|
f2e824 |
* @return string sql server version
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function sql_server_info($raw = false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
global $cache;
|
|
|
f2e824 |
|
|
|
f2e824 |
if (empty($cache) || ($this->sql_server_version = $cache->get('mssql_version')) === false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
|
|
|
f2e824 |
|
|
|
f2e824 |
$row = false;
|
|
|
f2e824 |
if ($result_id)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$row = @mssql_fetch_assoc($result_id);
|
|
|
f2e824 |
@mssql_free_result($result_id);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
|
|
|
f2e824 |
|
|
|
f2e824 |
if (!empty($cache))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$cache->put('mssql_version', $this->sql_server_version);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($raw)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return $this->sql_server_version;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return ($this->sql_server_version) ? 'MSSQL ' . $this->sql_server_version : 'MSSQL';
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* SQL Transaction
|
|
|
f2e824 |
* @access private
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function _sql_transaction($status = 'begin')
|
|
|
f2e824 |
{
|
|
|
f2e824 |
switch ($status)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
case 'begin':
|
|
|
f2e824 |
return @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
|
|
|
f2e824 |
break;
|
|
|
f2e824 |
|
|
|
f2e824 |
case 'commit':
|
|
|
f2e824 |
return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
|
|
|
f2e824 |
break;
|
|
|
f2e824 |
|
|
|
f2e824 |
case 'rollback':
|
|
|
f2e824 |
return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
|
|
|
f2e824 |
break;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return true;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Base query method
|
|
|
f2e824 |
*
|
|
|
f2e824 |
* @param string $query Contains the SQL query which shall be executed
|
|
|
f2e824 |
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
|
|
|
f2e824 |
* @return mixed When casted to bool the returned value returns true on success and false on failure
|
|
|
f2e824 |
*
|
|
|
f2e824 |
* @access public
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function sql_query($query = '', $cache_ttl = 0)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if ($query != '')
|
|
|
f2e824 |
{
|
|
|
f2e824 |
global $cache;
|
|
|
f2e824 |
|
|
|
f2e824 |
// EXPLAIN only in extra debug mode
|
|
|
f2e824 |
if (defined('DEBUG_EXTRA'))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->sql_report('start', $query);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
|
|
|
f2e824 |
$this->sql_add_num_queries($this->query_result);
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($this->query_result === false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->sql_error($query);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (defined('DEBUG_EXTRA'))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->sql_report('stop', $query);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($cache_ttl && method_exists($cache, 'sql_save'))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
|
|
f2e824 |
$cache->sql_save($query, $this->query_result, $cache_ttl);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else if (defined('DEBUG_EXTRA'))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->sql_report('fromcache', $query);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return $this->query_result;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Build LIMIT query
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->query_result = false;
|
|
|
f2e824 |
|
|
|
f2e824 |
// Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
|
|
|
f2e824 |
if ($total)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// We need to grab the total number of rows + the offset number of rows to get the correct result
|
|
|
f2e824 |
if (strpos($query, 'SELECT DISTINCT') === 0)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
else
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$result = $this->sql_query($query, $cache_ttl);
|
|
|
f2e824 |
|
|
|
f2e824 |
// Seek by $offset rows
|
|
|
f2e824 |
if ($offset)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->sql_rowseek($offset, $result);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return $result;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Return number of affected rows
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function sql_affectedrows()
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Fetch current row
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function sql_fetchrow($query_id = false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
global $cache;
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($query_id === false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$query_id = $this->query_result;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return $cache->sql_fetchrow($query_id);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($query_id === false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
$row = @mssql_fetch_assoc($query_id);
|
|
|
f2e824 |
|
|
|
f2e824 |
// I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
|
|
|
f2e824 |
if ($row)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
foreach ($row as $key => $value)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return $row;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Seek to given row number
|
|
|
f2e824 |
* rownum is zero-based
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function sql_rowseek($rownum, &$query_id)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
global $cache;
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($query_id === false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$query_id = $this->query_result;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return $cache->sql_rowseek($rownum, $query_id);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return ($query_id !== false) ? @mssql_data_seek($query_id, $rownum) : false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Get last inserted id after insert statement
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function sql_nextid()
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
|
|
|
f2e824 |
if ($result_id)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
if ($row = @mssql_fetch_assoc($result_id))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
@mssql_free_result($result_id);
|
|
|
f2e824 |
return $row['computed'];
|
|
|
f2e824 |
}
|
|
|
f2e824 |
@mssql_free_result($result_id);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Free sql result
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function sql_freeresult($query_id = false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
global $cache;
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($query_id === false)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$query_id = $this->query_result;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return $cache->sql_freeresult($query_id);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
if (isset($this->open_queries[$query_id]))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
unset($this->open_queries[$query_id]);
|
|
|
f2e824 |
return @mssql_free_result($query_id);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return false;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Escape string used in sql query
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function sql_escape($msg)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return str_replace(array("'", "\0"), array("''", ''), $msg);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Build LIKE expression
|
|
|
f2e824 |
* @access private
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function _sql_like_expression($expression)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return $expression . " ESCAPE '\\'";
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* return sql error array
|
|
|
f2e824 |
* @access private
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function _sql_error()
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$error = array(
|
|
|
f2e824 |
'message' => @mssql_get_last_message(),
|
|
|
f2e824 |
'code' => ''
|
|
|
f2e824 |
);
|
|
|
f2e824 |
|
|
|
f2e824 |
// Get error code number
|
|
|
f2e824 |
$result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
|
|
|
f2e824 |
if ($result_id)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$row = @mssql_fetch_assoc($result_id);
|
|
|
f2e824 |
$error['code'] = $row['code'];
|
|
|
f2e824 |
@mssql_free_result($result_id);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
// Get full error message if possible
|
|
|
f2e824 |
$sql = 'SELECT CAST(description as varchar(255)) as message
|
|
|
f2e824 |
FROM master.dbo.sysmessages
|
|
|
f2e824 |
WHERE error = ' . $error['code'];
|
|
|
f2e824 |
$result_id = @mssql_query($sql);
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($result_id)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$row = @mssql_fetch_assoc($result_id);
|
|
|
f2e824 |
if (!empty($row['message']))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$error['message'] .= ' ' . $row['message'];
|
|
|
f2e824 |
}
|
|
|
f2e824 |
@mssql_free_result($result_id);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
return $error;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Build db-specific query data
|
|
|
f2e824 |
* @access private
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function _sql_custom_build($stage, $data)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return $data;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Close sql connection
|
|
|
f2e824 |
* @access private
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function _sql_close()
|
|
|
f2e824 |
{
|
|
|
f2e824 |
return @mssql_close($this->db_connect_id);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
/**
|
|
|
f2e824 |
* Build db-specific report
|
|
|
f2e824 |
* @access private
|
|
|
f2e824 |
*/
|
|
|
f2e824 |
function _sql_report($mode, $query = '')
|
|
|
f2e824 |
{
|
|
|
f2e824 |
switch ($mode)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
case 'start':
|
|
|
f2e824 |
$html_table = false;
|
|
|
f2e824 |
@mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
|
|
|
f2e824 |
if ($result = @mssql_query($query, $this->db_connect_id))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
@mssql_next_result($result);
|
|
|
f2e824 |
while ($row = @mssql_fetch_row($result))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
@mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
|
|
|
f2e824 |
@mssql_free_result($result);
|
|
|
f2e824 |
|
|
|
f2e824 |
if ($html_table)
|
|
|
f2e824 |
{
|
|
|
f2e824 |
$this->html_hold .= '';
|
|
|
f2e824 |
}
|
|
|
f2e824 |
break;
|
|
|
f2e824 |
|
|
|
f2e824 |
case 'fromcache':
|
|
|
f2e824 |
$endtime = explode(' ', microtime());
|
|
|
f2e824 |
$endtime = $endtime[0] + $endtime[1];
|
|
|
f2e824 |
|
|
|
f2e824 |
$result = @mssql_query($query, $this->db_connect_id);
|
|
|
f2e824 |
while ($void = @mssql_fetch_assoc($result))
|
|
|
f2e824 |
{
|
|
|
f2e824 |
// Take the time spent on parsing rows into account
|
|
|
f2e824 |
}
|
|
|
f2e824 |
@mssql_free_result($result);
|
|
|
f2e824 |
|
|
|
f2e824 |
$splittime = explode(' ', microtime());
|
|
|
f2e824 |
$splittime = $splittime[0] + $splittime[1];
|
|
|
f2e824 |
|
|
|
f2e824 |
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
|
|
f2e824 |
|
|
|
f2e824 |
break;
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
}
|
|
|
f2e824 |
|
|
|
f2e824 |
?>
|