includes/db/postgres.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/db/postgres.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,485 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package dbal
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
    1.23 +
    1.24 +if (!class_exists('phpbb_error_collector'))
    1.25 +{
    1.26 +	include($phpbb_root_path . 'includes/error_collector.' . $phpEx);
    1.27 +}
    1.28 +
    1.29 +/**
    1.30 +* PostgreSQL Database Abstraction Layer
    1.31 +* Minimum Requirement is Version 7.3+
    1.32 +* @package dbal
    1.33 +*/
    1.34 +class dbal_postgres extends dbal
    1.35 +{
    1.36 +	var $last_query_text = '';
    1.37 +	var $connect_error = '';
    1.38 +
    1.39 +	/**
    1.40 +	* Connect to server
    1.41 +	*/
    1.42 +	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
    1.43 +	{
    1.44 +		$connect_string = '';
    1.45 +
    1.46 +		if ($sqluser)
    1.47 +		{
    1.48 +			$connect_string .= "user=$sqluser ";
    1.49 +		}
    1.50 +
    1.51 +		if ($sqlpassword)
    1.52 +		{
    1.53 +			$connect_string .= "password=$sqlpassword ";
    1.54 +		}
    1.55 +
    1.56 +		if ($sqlserver)
    1.57 +		{
    1.58 +			// $sqlserver can carry a port separated by : for compatibility reasons
    1.59 +			// If $sqlserver has more than one : it's probably an IPv6 address.
    1.60 +			// In this case we only allow passing a port via the $port variable.
    1.61 +			if (substr_count($sqlserver, ':') === 1)
    1.62 +			{
    1.63 +				list($sqlserver, $port) = explode(':', $sqlserver);
    1.64 +			}
    1.65 +
    1.66 +			if ($sqlserver !== 'localhost')
    1.67 +			{
    1.68 +				$connect_string .= "host=$sqlserver ";
    1.69 +			}
    1.70 +
    1.71 +			if ($port)
    1.72 +			{
    1.73 +				$connect_string .= "port=$port ";
    1.74 +			}
    1.75 +		}
    1.76 +
    1.77 +		$schema = '';
    1.78 +
    1.79 +		if ($database)
    1.80 +		{
    1.81 +			$this->dbname = $database;
    1.82 +			if (strpos($database, '.') !== false)
    1.83 +			{
    1.84 +				list($database, $schema) = explode('.', $database);
    1.85 +			}
    1.86 +			$connect_string .= "dbname=$database";
    1.87 +		}
    1.88 +
    1.89 +		$this->persistency = $persistency;
    1.90 +
    1.91 +		if ($this->persistency)
    1.92 +		{
    1.93 +			if (!function_exists('pg_pconnect'))
    1.94 +			{
    1.95 +				$this->connect_error = 'pg_pconnect function does not exist, is pgsql extension installed?';
    1.96 +				return $this->sql_error('');
    1.97 +			}
    1.98 +			$collector = new phpbb_error_collector;
    1.99 +			$collector->install();
   1.100 +			$this->db_connect_id = (!$new_link) ? @pg_pconnect($connect_string) : @pg_pconnect($connect_string, PGSQL_CONNECT_FORCE_NEW);
   1.101 +		}
   1.102 +		else
   1.103 +		{
   1.104 +			if (!function_exists('pg_connect'))
   1.105 +			{
   1.106 +				$this->connect_error = 'pg_connect function does not exist, is pgsql extension installed?';
   1.107 +				return $this->sql_error('');
   1.108 +			}
   1.109 +			$collector = new phpbb_error_collector;
   1.110 +			$collector->install();
   1.111 +			$this->db_connect_id = (!$new_link) ? @pg_connect($connect_string) : @pg_connect($connect_string, PGSQL_CONNECT_FORCE_NEW);
   1.112 +		}
   1.113 +
   1.114 +		$collector->uninstall();
   1.115 +
   1.116 +		if ($this->db_connect_id)
   1.117 +		{
   1.118 +			if (version_compare($this->sql_server_info(true), '8.2', '>='))
   1.119 +			{
   1.120 +				$this->multi_insert = true;
   1.121 +			}
   1.122 +
   1.123 +			if ($schema !== '')
   1.124 +			{
   1.125 +				@pg_query($this->db_connect_id, 'SET search_path TO ' . $schema);
   1.126 +			}
   1.127 +			return $this->db_connect_id;
   1.128 +		}
   1.129 +
   1.130 +		$this->connect_error = $collector->format_errors();
   1.131 +		return $this->sql_error('');
   1.132 +	}
   1.133 +
   1.134 +	/**
   1.135 +	* Version information about used database
   1.136 +	* @param bool $raw if true, only return the fetched sql_server_version
   1.137 +	* @param bool $use_cache If true, it is safe to retrieve the value from the cache
   1.138 +	* @return string sql server version
   1.139 +	*/
   1.140 +	function sql_server_info($raw = false, $use_cache = true)
   1.141 +	{
   1.142 +		global $cache;
   1.143 +
   1.144 +		if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('pgsql_version')) === false)
   1.145 +		{
   1.146 +			$query_id = @pg_query($this->db_connect_id, 'SELECT VERSION() AS version');
   1.147 +			$row = @pg_fetch_assoc($query_id, null);
   1.148 +			@pg_free_result($query_id);
   1.149 +
   1.150 +			$this->sql_server_version = (!empty($row['version'])) ? trim(substr($row['version'], 10)) : 0;
   1.151 +
   1.152 +			if (!empty($cache) && $use_cache)
   1.153 +			{
   1.154 +				$cache->put('pgsql_version', $this->sql_server_version);
   1.155 +			}
   1.156 +		}
   1.157 +
   1.158 +		return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version;
   1.159 +	}
   1.160 +
   1.161 +	/**
   1.162 +	* SQL Transaction
   1.163 +	* @access private
   1.164 +	*/
   1.165 +	function _sql_transaction($status = 'begin')
   1.166 +	{
   1.167 +		switch ($status)
   1.168 +		{
   1.169 +			case 'begin':
   1.170 +				return @pg_query($this->db_connect_id, 'BEGIN');
   1.171 +			break;
   1.172 +
   1.173 +			case 'commit':
   1.174 +				return @pg_query($this->db_connect_id, 'COMMIT');
   1.175 +			break;
   1.176 +
   1.177 +			case 'rollback':
   1.178 +				return @pg_query($this->db_connect_id, 'ROLLBACK');
   1.179 +			break;
   1.180 +		}
   1.181 +
   1.182 +		return true;
   1.183 +	}
   1.184 +
   1.185 +	/**
   1.186 +	* Base query method
   1.187 +	*
   1.188 +	* @param	string	$query		Contains the SQL query which shall be executed
   1.189 +	* @param	int		$cache_ttl	Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
   1.190 +	* @return	mixed				When casted to bool the returned value returns true on success and false on failure
   1.191 +	*
   1.192 +	* @access	public
   1.193 +	*/
   1.194 +	function sql_query($query = '', $cache_ttl = 0)
   1.195 +	{
   1.196 +		if ($query != '')
   1.197 +		{
   1.198 +			global $cache;
   1.199 +
   1.200 +			// EXPLAIN only in extra debug mode
   1.201 +			if (defined('DEBUG_EXTRA'))
   1.202 +			{
   1.203 +				$this->sql_report('start', $query);
   1.204 +			}
   1.205 +
   1.206 +			$this->last_query_text = $query;
   1.207 +			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
   1.208 +			$this->sql_add_num_queries($this->query_result);
   1.209 +
   1.210 +			if ($this->query_result === false)
   1.211 +			{
   1.212 +				if (($this->query_result = @pg_query($this->db_connect_id, $query)) === false)
   1.213 +				{
   1.214 +					$this->sql_error($query);
   1.215 +				}
   1.216 +
   1.217 +				if (defined('DEBUG_EXTRA'))
   1.218 +				{
   1.219 +					$this->sql_report('stop', $query);
   1.220 +				}
   1.221 +
   1.222 +				if ($cache_ttl && method_exists($cache, 'sql_save'))
   1.223 +				{
   1.224 +					$this->open_queries[(int) $this->query_result] = $this->query_result;
   1.225 +					$cache->sql_save($query, $this->query_result, $cache_ttl);
   1.226 +				}
   1.227 +				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
   1.228 +				{
   1.229 +					$this->open_queries[(int) $this->query_result] = $this->query_result;
   1.230 +				}
   1.231 +			}
   1.232 +			else if (defined('DEBUG_EXTRA'))
   1.233 +			{
   1.234 +				$this->sql_report('fromcache', $query);
   1.235 +			}
   1.236 +		}
   1.237 +		else
   1.238 +		{
   1.239 +			return false;
   1.240 +		}
   1.241 +
   1.242 +		return $this->query_result;
   1.243 +	}
   1.244 +
   1.245 +	/**
   1.246 +	* Build db-specific query data
   1.247 +	* @access private
   1.248 +	*/
   1.249 +	function _sql_custom_build($stage, $data)
   1.250 +	{
   1.251 +		return $data;
   1.252 +	}
   1.253 +
   1.254 +	/**
   1.255 +	* Build LIMIT query
   1.256 +	*/
   1.257 +	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
   1.258 +	{
   1.259 +		$this->query_result = false;
   1.260 +
   1.261 +		// if $total is set to 0 we do not want to limit the number of rows
   1.262 +		if ($total == 0)
   1.263 +		{
   1.264 +			$total = 'ALL';
   1.265 +		}
   1.266 +
   1.267 +		$query .= "\n LIMIT $total OFFSET $offset";
   1.268 +
   1.269 +		return $this->sql_query($query, $cache_ttl);
   1.270 +	}
   1.271 +
   1.272 +	/**
   1.273 +	* Return number of affected rows
   1.274 +	*/
   1.275 +	function sql_affectedrows()
   1.276 +	{
   1.277 +		return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
   1.278 +	}
   1.279 +
   1.280 +	/**
   1.281 +	* Fetch current row
   1.282 +	*/
   1.283 +	function sql_fetchrow($query_id = false)
   1.284 +	{
   1.285 +		global $cache;
   1.286 +
   1.287 +		if ($query_id === false)
   1.288 +		{
   1.289 +			$query_id = $this->query_result;
   1.290 +		}
   1.291 +
   1.292 +		if (isset($cache->sql_rowset[$query_id]))
   1.293 +		{
   1.294 +			return $cache->sql_fetchrow($query_id);
   1.295 +		}
   1.296 +
   1.297 +		return ($query_id !== false) ? @pg_fetch_assoc($query_id, null) : false;
   1.298 +	}
   1.299 +
   1.300 +	/**
   1.301 +	* Seek to given row number
   1.302 +	* rownum is zero-based
   1.303 +	*/
   1.304 +	function sql_rowseek($rownum, &$query_id)
   1.305 +	{
   1.306 +		global $cache;
   1.307 +
   1.308 +		if ($query_id === false)
   1.309 +		{
   1.310 +			$query_id = $this->query_result;
   1.311 +		}
   1.312 +
   1.313 +		if (isset($cache->sql_rowset[$query_id]))
   1.314 +		{
   1.315 +			return $cache->sql_rowseek($rownum, $query_id);
   1.316 +		}
   1.317 +
   1.318 +		return ($query_id !== false) ? @pg_result_seek($query_id, $rownum) : false;
   1.319 +	}
   1.320 +
   1.321 +	/**
   1.322 +	* Get last inserted id after insert statement
   1.323 +	*/
   1.324 +	function sql_nextid()
   1.325 +	{
   1.326 +		$query_id = $this->query_result;
   1.327 +
   1.328 +		if ($query_id !== false && $this->last_query_text != '')
   1.329 +		{
   1.330 +			if (preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text, $tablename))
   1.331 +			{
   1.332 +				$query = "SELECT currval('" . $tablename[1] . "_seq') AS last_value";
   1.333 +				$temp_q_id = @pg_query($this->db_connect_id, $query);
   1.334 +
   1.335 +				if (!$temp_q_id)
   1.336 +				{
   1.337 +					return false;
   1.338 +				}
   1.339 +
   1.340 +				$temp_result = @pg_fetch_assoc($temp_q_id, NULL);
   1.341 +				@pg_free_result($query_id);
   1.342 +
   1.343 +				return ($temp_result) ? $temp_result['last_value'] : false;
   1.344 +			}
   1.345 +		}
   1.346 +
   1.347 +		return false;
   1.348 +	}
   1.349 +
   1.350 +	/**
   1.351 +	* Free sql result
   1.352 +	*/
   1.353 +	function sql_freeresult($query_id = false)
   1.354 +	{
   1.355 +		global $cache;
   1.356 +
   1.357 +		if ($query_id === false)
   1.358 +		{
   1.359 +			$query_id = $this->query_result;
   1.360 +		}
   1.361 +
   1.362 +		if (isset($cache->sql_rowset[$query_id]))
   1.363 +		{
   1.364 +			return $cache->sql_freeresult($query_id);
   1.365 +		}
   1.366 +
   1.367 +		if (isset($this->open_queries[(int) $query_id]))
   1.368 +		{
   1.369 +			unset($this->open_queries[(int) $query_id]);
   1.370 +			return @pg_free_result($query_id);
   1.371 +		}
   1.372 +
   1.373 +		return false;
   1.374 +	}
   1.375 +
   1.376 +	/**
   1.377 +	* Escape string used in sql query
   1.378 +	* Note: Do not use for bytea values if we may use them at a later stage
   1.379 +	*/
   1.380 +	function sql_escape($msg)
   1.381 +	{
   1.382 +		return @pg_escape_string($msg);
   1.383 +	}
   1.384 +
   1.385 +	/**
   1.386 +	* Build LIKE expression
   1.387 +	* @access private
   1.388 +	*/
   1.389 +	function _sql_like_expression($expression)
   1.390 +	{
   1.391 +		return $expression;
   1.392 +	}
   1.393 +
   1.394 +	/**
   1.395 +	* return sql error array
   1.396 +	* @access private
   1.397 +	*/
   1.398 +	function _sql_error()
   1.399 +	{
   1.400 +		// pg_last_error only works when there is an established connection.
   1.401 +		// Connection errors have to be tracked by us manually.
   1.402 +		if ($this->db_connect_id)
   1.403 +		{
   1.404 +			$message = @pg_last_error($this->db_connect_id);
   1.405 +		}
   1.406 +		else
   1.407 +		{
   1.408 +			$message = $this->connect_error;
   1.409 +		}
   1.410 +
   1.411 +		return array(
   1.412 +			'message'	=> $message,
   1.413 +			'code'		=> ''
   1.414 +		);
   1.415 +	}
   1.416 +
   1.417 +	/**
   1.418 +	* Close sql connection
   1.419 +	* @access private
   1.420 +	*/
   1.421 +	function _sql_close()
   1.422 +	{
   1.423 +		return @pg_close($this->db_connect_id);
   1.424 +	}
   1.425 +
   1.426 +	/**
   1.427 +	* Build db-specific report
   1.428 +	* @access private
   1.429 +	*/
   1.430 +	function _sql_report($mode, $query = '')
   1.431 +	{
   1.432 +		switch ($mode)
   1.433 +		{
   1.434 +			case 'start':
   1.435 +
   1.436 +				$explain_query = $query;
   1.437 +				if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
   1.438 +				{
   1.439 +					$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
   1.440 +				}
   1.441 +				else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
   1.442 +				{
   1.443 +					$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
   1.444 +				}
   1.445 +
   1.446 +				if (preg_match('/^SELECT/', $explain_query))
   1.447 +				{
   1.448 +					$html_table = false;
   1.449 +
   1.450 +					if ($result = @pg_query($this->db_connect_id, "EXPLAIN $explain_query"))
   1.451 +					{
   1.452 +						while ($row = @pg_fetch_assoc($result, NULL))
   1.453 +						{
   1.454 +							$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
   1.455 +						}
   1.456 +					}
   1.457 +					@pg_free_result($result);
   1.458 +
   1.459 +					if ($html_table)
   1.460 +					{
   1.461 +						$this->html_hold .= '</table>';
   1.462 +					}
   1.463 +				}
   1.464 +
   1.465 +			break;
   1.466 +
   1.467 +			case 'fromcache':
   1.468 +				$endtime = explode(' ', microtime());
   1.469 +				$endtime = $endtime[0] + $endtime[1];
   1.470 +
   1.471 +				$result = @pg_query($this->db_connect_id, $query);
   1.472 +				while ($void = @pg_fetch_assoc($result, NULL))
   1.473 +				{
   1.474 +					// Take the time spent on parsing rows into account
   1.475 +				}
   1.476 +				@pg_free_result($result);
   1.477 +
   1.478 +				$splittime = explode(' ', microtime());
   1.479 +				$splittime = $splittime[0] + $splittime[1];
   1.480 +
   1.481 +				$this->sql_report('record_fromcache', $query, $endtime, $splittime);
   1.482 +
   1.483 +			break;
   1.484 +		}
   1.485 +	}
   1.486 +}
   1.487 +
   1.488 +?>
   1.489 \ No newline at end of file