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

ef5584
ef5584
/**
ef5584
*
ef5584
* @package dbal
ef5584
* @version $Id: firebird.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
* Firebird/Interbase Database Abstraction Layer
ef5584
* Minimum Requirement is Firebird 2.0
ef5584
* @package dbal
ef5584
*/
ef5584
class dbal_firebird extends dbal
ef5584
{
ef5584
	var $last_query_text = '';
ef5584
	var $service_handle = false;
ef5584
	var $affected_rows = 0;
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->server = $sqlserver . (($port) ? ':' . $port : '');
ef5584
		$this->dbname = str_replace('\\', '/', $database);
ef5584
ef5584
		// There are three possibilities to connect to an interbase db
ef5584
		if (!$this->server)
ef5584
		{
ef5584
			$use_database = $this->dbname;
ef5584
		}
ef5584
		else if (strpos($this->server, '//') === 0)
ef5584
		{
ef5584
			$use_database = $this->server . $this->dbname;
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$use_database = $this->server . ':' . $this->dbname;
ef5584
		}
ef5584
ef5584
		$this->db_connect_id = ($this->persistency) ? @ibase_pconnect($use_database, $this->user, $sqlpassword, false, false, 3) : @ibase_connect($use_database, $this->user, $sqlpassword, false, false, 3);
ef5584
ef5584
		$this->service_handle = (function_exists('ibase_service_attach') && $this->server) ? @ibase_service_attach($this->server, $this->user, $sqlpassword) : false;
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
		if ($this->service_handle !== false && function_exists('ibase_server_info'))
ef5584
		{
ef5584
			return @ibase_server_info($this->service_handle, IBASE_SVC_SERVER_VERSION);
ef5584
		}
ef5584
ef5584
		return ($raw) ? '2.0' : 'Firebird/Interbase';
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 true;
ef5584
			break;
ef5584
ef5584
			case 'commit':
ef5584
				return @ibase_commit();
ef5584
			break;
ef5584
ef5584
			case 'rollback':
ef5584
				return @ibase_rollback();
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
				$array = array();
ef5584
				// We overcome Firebird's 32767 char limit by binding vars
ef5584
				if (strlen($query) > 32767)
ef5584
				{
ef5584
					if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs))
ef5584
					{
ef5584
						if (strlen($regs[3]) > 32767)
ef5584
						{
ef5584
							preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
ef5584
ef5584
							$inserts = $vals[0];
ef5584
							unset($vals);
ef5584
ef5584
							foreach ($inserts as $key => $value)
ef5584
							{
ef5584
								if (!empty($value) && $value[0] === "'" && strlen($value) > 32769) // check to see if this thing is greater than the max + 'x2
ef5584
								{
ef5584
									$inserts[$key] = '?';
ef5584
									$array[] = str_replace("''", "'", substr($value, 1, -1));
ef5584
								}
ef5584
							}
ef5584
ef5584
							$query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
ef5584
						}
ef5584
					}
ef5584
					else if (preg_match('/^(UPDATE ([\\w_]++)\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|\\d+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data))
ef5584
					{
ef5584
						if (strlen($data[3]) > 32767)
ef5584
						{
ef5584
							$update = $data[1];
ef5584
							$where = $data[4];
ef5584
							preg_match_all('/(\\w++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[3], $temp, PREG_SET_ORDER);
ef5584
							unset($data);
ef5584
ef5584
							$cols = array();
ef5584
							foreach ($temp as $value)
ef5584
							{
ef5584
								if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 32769) // check to see if this thing is greater than the max + 'x2
ef5584
								{
ef5584
									$array[] = str_replace("''", "'", substr($value[2], 1, -1));
ef5584
									$cols[] = $value[1] . '=?';
ef5584
								}
ef5584
								else
ef5584
								{
ef5584
									$cols[] = $value[1] . '=' . $value[2];
ef5584
								}
ef5584
							}
ef5584
ef5584
							$query = $update . implode(', ', $cols) . ' ' . $where;
ef5584
							unset($cols);
ef5584
						}
ef5584
					}
ef5584
				}
ef5584
ef5584
				if (!function_exists('ibase_affected_rows') && (preg_match('/^UPDATE ([\w_]++)\s+SET [\w_]++\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+)(?:,\s*[\w_]++\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+\s+(WHERE.*)?$/s', $query, $regs) || preg_match('/^DELETE FROM ([\w_]++)\s*(WHERE\s*.*)?$/s', $query, $regs)))
ef5584
				{
ef5584
					$affected_sql = 'SELECT COUNT(*) as num_rows_affected FROM ' . $regs[1];
ef5584
					if (!empty($regs[2]))
ef5584
					{
ef5584
						$affected_sql .= ' ' . $regs[2];
ef5584
					}
ef5584
ef5584
					if (!($temp_q_id = @ibase_query($this->db_connect_id, $affected_sql)))
ef5584
					{
ef5584
						return false;
ef5584
					}
ef5584
ef5584
					$temp_result = @ibase_fetch_assoc($temp_q_id);
ef5584
					@ibase_free_result($temp_q_id);
ef5584
ef5584
					$this->affected_rows = ($temp_result) ? $temp_result['NUM_ROWS_AFFECTED'] : false;
ef5584
				}
ef5584
ef5584
				if (sizeof($array))
ef5584
				{
ef5584
					$p_query = @ibase_prepare($this->db_connect_id, $query);
ef5584
					array_unshift($array, $p_query);
ef5584
					$this->query_result = call_user_func_array('ibase_execute', $array);
ef5584
					unset($array);
ef5584
ef5584
					if ($this->query_result === false)
ef5584
					{
ef5584
						$this->sql_error($query);
ef5584
					}
ef5584
				}
ef5584
				else if (($this->query_result = @ibase_query($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 (!$this->transaction)
ef5584
				{
ef5584
					if (function_exists('ibase_commit_ret'))
ef5584
					{
ef5584
						@ibase_commit_ret();
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						// way cooler than ibase_commit_ret :D
ef5584
						@ibase_query('COMMIT RETAIN;');
ef5584
					}
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
		$query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6);
ef5584
ef5584
		return $this->sql_query($query, $cache_ttl);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Return number of affected rows
ef5584
	*/
ef5584
	function sql_affectedrows()
ef5584
	{
ef5584
		// PHP 5+ function
ef5584
		if (function_exists('ibase_affected_rows'))
ef5584
		{
ef5584
			return ($this->db_connect_id) ? @ibase_affected_rows($this->db_connect_id) : false;
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			return $this->affected_rows;
ef5584
		}
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 = array();
ef5584
		$cur_row = @ibase_fetch_object($query_id, IBASE_TEXT);
ef5584
ef5584
		if (!$cur_row)
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		foreach (get_object_vars($cur_row) as $key => $value)
ef5584
		{
ef5584
			$row[strtolower($key)] = (is_string($value)) ? trim(str_replace(array("\\0", "\\n"), array("\0", "\n"), $value)) : $value;
ef5584
		}
ef5584
ef5584
		return (sizeof($row)) ? $row : 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;
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
		$query_id = $this->query_result;
ef5584
ef5584
		if ($query_id !== false && $this->last_query_text != '')
ef5584
		{
ef5584
			if ($this->query_result && preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#i', $this->last_query_text, $tablename))
ef5584
			{
ef5584
				$sql = 'SELECT GEN_ID(' . $tablename[1] . '_gen, 0) AS new_id FROM RDB$DATABASE';
ef5584
ef5584
				if (!($temp_q_id = @ibase_query($this->db_connect_id, $sql)))
ef5584
				{
ef5584
					return false;
ef5584
				}
ef5584
ef5584
				$temp_result = @ibase_fetch_assoc($temp_q_id);
ef5584
				@ibase_free_result($temp_q_id);
ef5584
ef5584
				return ($temp_result) ? $temp_result['NEW_ID'] : false;
ef5584
			}
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 @ibase_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'	=> @ibase_errmsg(),
ef5584
			'code'		=> (@function_exists('ibase_errcode') ? @ibase_errcode() : '')
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Close sql connection
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_close()
ef5584
	{
ef5584
		if ($this->service_handle !== false)
ef5584
		{
ef5584
			@ibase_service_detach($this->service_handle);
ef5584
		}
ef5584
ef5584
		return @ibase_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 = @ibase_query($this->db_connect_id, $query);
ef5584
				while ($void = @ibase_fetch_object($result, IBASE_TEXT))
ef5584
				{
ef5584
					// Take the time spent on parsing rows into account
ef5584
				}
ef5584
				@ibase_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
?>