Blame Identity/Webenv/phpBB/3.0.4/includes/db/mssql.php

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