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

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