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

ef5584
ef5584
/**
ef5584
*
ef5584
* @package dbal
ef5584
* @version $Id: mssql_odbc.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
* Unified ODBC functions
ef5584
* Unified ODBC functions support any database having ODBC driver, for example Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere...
ef5584
* Here we only support MSSQL Server 2000+ because of the provided schema
ef5584
*
ef5584
* @note number of bytes returned for returning data depends on odbc.defaultlrl php.ini setting.
ef5584
* If it is limited to 4K for example only 4K of data is returned max, resulting in incomplete theme data for example.
ef5584
* @note odbc.defaultbinmode may affect UTF8 characters
ef5584
*
ef5584
* @package dbal
ef5584
*/
ef5584
class dbal_mssql_odbc extends dbal
ef5584
{
ef5584
	var $last_query_text = '';
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
		$max_size = @ini_get('odbc.defaultlrl');
ef5584
		if (!empty($max_size))
ef5584
		{
ef5584
			$unit = strtolower(substr($max_size, -1, 1));
ef5584
			$max_size = (int) $max_size;
ef5584
ef5584
			if ($unit == 'k')
ef5584
			{
ef5584
				$max_size = floor($max_size / 1024);
ef5584
			}
ef5584
			else if ($unit == 'g')
ef5584
			{
ef5584
				$max_size *= 1024;
ef5584
			}
ef5584
			else if (is_numeric($unit))
ef5584
			{
ef5584
				$max_size = floor((int) ($max_size . $unit) / 1048576);
ef5584
			}
ef5584
			$max_size = max(8, $max_size) . 'M';
ef5584
ef5584
			@ini_set('odbc.defaultlrl', $max_size);
ef5584
		}
ef5584
ef5584
		$this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword);
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('mssqlodbc_version')) === false)
ef5584
		{
ef5584
			$result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
ef5584
ef5584
			$row = false;
ef5584
			if ($result_id)
ef5584
			{
ef5584
				$row = @odbc_fetch_array($result_id);
ef5584
				@odbc_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('mssqlodbc_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 (ODBC)
' . $this->sql_server_version : 'MSSQL (ODBC)';
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 @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
ef5584
			break;
ef5584
ef5584
			case 'commit':
ef5584
				return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
ef5584
			break;
ef5584
ef5584
			case 'rollback':
ef5584
				return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
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->last_query_text = $query;
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 = @odbc_exec($this->db_connect_id, $query)) === 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) ? @odbc_num_rows($this->query_result) : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Fetch current row
ef5584
	* @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.
ef5584
	*/
ef5584
	function sql_fetchrow($query_id = false, $debug = 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
		return ($query_id !== false) ? @odbc_fetch_array($query_id) : false;
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
		if ($query_id === false)
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$this->sql_freeresult($query_id);
ef5584
		$query_id = $this->sql_query($this->last_query_text);
ef5584
ef5584
		if ($query_id === false)
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
ef5584
		for ($i = 0; $i < $rownum; $i++)
ef5584
		{
ef5584
			if (!$this->sql_fetchrow($query_id))
ef5584
			{
ef5584
				return false;
ef5584
			}
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get last inserted id after insert statement
ef5584
	*/
ef5584
	function sql_nextid()
ef5584
	{
ef5584
		$result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
ef5584
ef5584
		if ($result_id)
ef5584
		{
ef5584
			if (@odbc_fetch_array($result_id))
ef5584
			{
ef5584
				$id = @odbc_result($result_id, 1);
ef5584
				@odbc_free_result($result_id);
ef5584
				return $id;
ef5584
			}
ef5584
			@odbc_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[(int) $query_id]))
ef5584
		{
ef5584
			unset($this->open_queries[(int) $query_id]);
ef5584
			return @odbc_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
	* Build db-specific query data
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_custom_build($stage, $data)
ef5584
	{
ef5584
		return $data;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* return sql error array
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_error()
ef5584
	{
ef5584
		return array(
ef5584
			'message'	=> @odbc_errormsg(),
ef5584
			'code'		=> @odbc_error()
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Close sql connection
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_close()
ef5584
	{
ef5584
		return @odbc_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
			break;
ef5584
ef5584
			case 'fromcache':
ef5584
				$endtime = explode(' ', microtime());
ef5584
				$endtime = $endtime[0] + $endtime[1];
ef5584
ef5584
				$result = @odbc_exec($this->db_connect_id, $query);
ef5584
				while ($void = @odbc_fetch_array($result))
ef5584
				{
ef5584
					// Take the time spent on parsing rows into account
ef5584
				}
ef5584
				@odbc_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
?>