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

f2e824
f2e824
/**
f2e824
*
f2e824
* @package dbal
f2e824
* @version $Id: postgres.php 8814 2008-09-04 12:01:47Z 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
* PostgreSQL Database Abstraction Layer
f2e824
* Minimum Requirement is Version 7.3+
f2e824
* @package dbal
f2e824
*/
f2e824
class dbal_postgres extends dbal
f2e824
{
f2e824
	var $last_query_text = '';
f2e824
	
f2e824
	/**
f2e824
	* Connect to server
f2e824
	*/
f2e824
	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
f2e824
	{
f2e824
		$connect_string = '';
f2e824
f2e824
		if ($sqluser)
f2e824
		{
f2e824
			$connect_string .= "user=$sqluser ";
f2e824
		}
f2e824
f2e824
		if ($sqlpassword)
f2e824
		{
f2e824
			$connect_string .= "password=$sqlpassword ";
f2e824
		}
f2e824
f2e824
		if ($sqlserver)
f2e824
		{
f2e824
			if (strpos($sqlserver, ':') !== false)
f2e824
			{
f2e824
				list($sqlserver, $port) = explode(':', $sqlserver);
f2e824
			}
f2e824
f2e824
			if ($sqlserver !== 'localhost')
f2e824
			{
f2e824
				$connect_string .= "host=$sqlserver ";
f2e824
			}
f2e824
		
f2e824
			if ($port)
f2e824
			{
f2e824
				$connect_string .= "port=$port ";
f2e824
			}
f2e824
		}
f2e824
f2e824
		$schema = '';
f2e824
f2e824
		if ($database)
f2e824
		{
f2e824
			$this->dbname = $database;
f2e824
			if (strpos($database, '.') !== false)
f2e824
			{
f2e824
				list($database, $schema) = explode('.', $database);
f2e824
			}
f2e824
			$connect_string .= "dbname=$database";
f2e824
		}
f2e824
f2e824
		$this->persistency = $persistency;
f2e824
f2e824
		$this->db_connect_id = ($this->persistency) ? @pg_pconnect($connect_string, $new_link) : @pg_connect($connect_string, $new_link);
f2e824
f2e824
		if ($this->db_connect_id)
f2e824
		{
f2e824
			if (version_compare($this->sql_server_info(true), '8.2', '>='))
f2e824
			{
f2e824
				$this->multi_insert = true;
f2e824
			}
f2e824
f2e824
			if ($schema !== '')
f2e824
			{
f2e824
				@pg_query($this->db_connect_id, 'SET search_path TO ' . $schema);
f2e824
			}
f2e824
			return $this->db_connect_id;
f2e824
		}
f2e824
f2e824
		return $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('pgsql_version')) === false)
f2e824
		{
f2e824
			$query_id = @pg_query($this->db_connect_id, 'SELECT VERSION() AS version');
f2e824
			$row = @pg_fetch_assoc($query_id, null);
f2e824
			@pg_free_result($query_id);
f2e824
f2e824
			$this->sql_server_version = (!empty($row['version'])) ? trim(substr($row['version'], 10)) : 0;
f2e824
f2e824
			if (!empty($cache))
f2e824
			{
f2e824
				$cache->put('pgsql_version', $this->sql_server_version);
f2e824
			}
f2e824
		}
f2e824
f2e824
		return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version;
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 @pg_query($this->db_connect_id, 'BEGIN');
f2e824
			break;
f2e824
f2e824
			case 'commit':
f2e824
				return @pg_query($this->db_connect_id, 'COMMIT');
f2e824
			break;
f2e824
f2e824
			case 'rollback':
f2e824
				return @pg_query($this->db_connect_id, '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
				if (($this->query_result = @pg_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 ($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 db-specific query data
f2e824
	* @access private
f2e824
	*/
f2e824
	function _sql_custom_build($stage, $data)
f2e824
	{
f2e824
		return $data;
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
		// if $total is set to 0 we do not want to limit the number of rows
f2e824
		if ($total == 0)
f2e824
		{
f2e824
			$total = -1;
f2e824
		}
f2e824
f2e824
		$query .= "\n LIMIT $total OFFSET $offset";
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
		return ($this->query_result) ? @pg_affected_rows($this->query_result) : 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
		return ($query_id !== false) ? @pg_fetch_assoc($query_id, null) : 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
		return ($query_id !== false) ? @pg_result_seek($query_id, $rownum) : false;
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 (preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text, $tablename))
f2e824
			{
f2e824
				$query = "SELECT currval('" . $tablename[1] . "_seq') AS last_value";
f2e824
				$temp_q_id = @pg_query($this->db_connect_id, $query);
f2e824
f2e824
				if (!$temp_q_id)
f2e824
				{
f2e824
					return false;
f2e824
				}
f2e824
f2e824
				$temp_result = @pg_fetch_assoc($temp_q_id, NULL);
f2e824
				@pg_free_result($query_id);
f2e824
f2e824
				return ($temp_result) ? $temp_result['last_value'] : 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 @pg_free_result($query_id);
f2e824
		}
f2e824
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Escape string used in sql query
f2e824
	* Note: Do not use for bytea values if we may use them at a later stage
f2e824
	*/
f2e824
	function sql_escape($msg)
f2e824
	{
f2e824
		return @pg_escape_string($msg);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Build LIKE expression
f2e824
	* @access private
f2e824
	*/
f2e824
	function _sql_like_expression($expression)
f2e824
	{
f2e824
		return $expression;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* return sql error array
f2e824
	* @access private
f2e824
	*/
f2e824
	function _sql_error()
f2e824
	{
f2e824
		return array(
f2e824
			'message'	=> (!$this->db_connect_id) ? @pg_last_error() : @pg_last_error($this->db_connect_id),
f2e824
			'code'		=> ''
f2e824
		);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Close sql connection
f2e824
	* @access private
f2e824
	*/
f2e824
	function _sql_close()
f2e824
	{
f2e824
		return @pg_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
f2e824
				$explain_query = $query;
f2e824
				if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
f2e824
				{
f2e824
					$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
f2e824
				}
f2e824
				else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
f2e824
				{
f2e824
					$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
f2e824
				}
f2e824
f2e824
				if (preg_match('/^SELECT/', $explain_query))
f2e824
				{
f2e824
					$html_table = false;
f2e824
f2e824
					if ($result = @pg_query($this->db_connect_id, "EXPLAIN $explain_query"))
f2e824
					{
f2e824
						while ($row = @pg_fetch_assoc($result, NULL))
f2e824
						{
f2e824
							$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
f2e824
						}
f2e824
					}
f2e824
					@pg_free_result($result);
f2e824
f2e824
					if ($html_table)
f2e824
					{
f2e824
						$this->html_hold .= '';
f2e824
					}
f2e824
				}
f2e824
f2e824
			break;
f2e824
f2e824
			case 'fromcache':
f2e824
				$endtime = explode(' ', microtime());
f2e824
				$endtime = $endtime[0] + $endtime[1];
f2e824
f2e824
				$result = @pg_query($this->db_connect_id, $query);
f2e824
				while ($void = @pg_fetch_assoc($result, NULL))
f2e824
				{
f2e824
					// Take the time spent on parsing rows into account
f2e824
				}
f2e824
				@pg_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
?>