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

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