|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package dbal
|
|
|
4c79b5 |
* @version $Id: postgres.php 8814 2008-09-04 12:01:47Z acydburn $
|
|
|
4c79b5 |
* @copyright (c) 2005 phpBB Group
|
|
|
4c79b5 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @ignore
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
if (!defined('IN_PHPBB'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
exit;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* PostgreSQL Database Abstraction Layer
|
|
|
4c79b5 |
* Minimum Requirement is Version 7.3+
|
|
|
4c79b5 |
* @package dbal
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class dbal_postgres extends dbal
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
var $last_query_text = '';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Connect to server
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$connect_string = '';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($sqluser)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$connect_string .= "user=$sqluser ";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($sqlpassword)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$connect_string .= "password=$sqlpassword ";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($sqlserver)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (strpos($sqlserver, ':') !== false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
list($sqlserver, $port) = explode(':', $sqlserver);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($sqlserver !== 'localhost')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$connect_string .= "host=$sqlserver ";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($port)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$connect_string .= "port=$port ";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$schema = '';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($database)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->dbname = $database;
|
|
|
4c79b5 |
if (strpos($database, '.') !== false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
list($database, $schema) = explode('.', $database);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$connect_string .= "dbname=$database";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->persistency = $persistency;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->db_connect_id = ($this->persistency) ? @pg_pconnect($connect_string, $new_link) : @pg_connect($connect_string, $new_link);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->db_connect_id)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (version_compare($this->sql_server_info(true), '8.2', '>='))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->multi_insert = true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($schema !== '')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
@pg_query($this->db_connect_id, 'SET search_path TO ' . $schema);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
return $this->db_connect_id;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->sql_error('');
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Version information about used database
|
|
|
4c79b5 |
* @param bool $raw if true, only return the fetched sql_server_version
|
|
|
4c79b5 |
* @return string sql server version
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_server_info($raw = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (empty($cache) || ($this->sql_server_version = $cache->get('pgsql_version')) === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = @pg_query($this->db_connect_id, 'SELECT VERSION() AS version');
|
|
|
4c79b5 |
$row = @pg_fetch_assoc($query_id, null);
|
|
|
4c79b5 |
@pg_free_result($query_id);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->sql_server_version = (!empty($row['version'])) ? trim(substr($row['version'], 10)) : 0;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!empty($cache))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$cache->put('pgsql_version', $this->sql_server_version);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* SQL Transaction
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_transaction($status = 'begin')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($status)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'begin':
|
|
|
4c79b5 |
return @pg_query($this->db_connect_id, 'BEGIN');
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'commit':
|
|
|
4c79b5 |
return @pg_query($this->db_connect_id, 'COMMIT');
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'rollback':
|
|
|
4c79b5 |
return @pg_query($this->db_connect_id, 'ROLLBACK');
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Base query method
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @param string $query Contains the SQL query which shall be executed
|
|
|
4c79b5 |
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
|
|
|
4c79b5 |
* @return mixed When casted to bool the returned value returns true on success and false on failure
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access public
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_query($query = '', $cache_ttl = 0)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($query != '')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// EXPLAIN only in extra debug mode
|
|
|
4c79b5 |
if (defined('DEBUG_EXTRA'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_report('start', $query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->last_query_text = $query;
|
|
|
4c79b5 |
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
|
|
|
4c79b5 |
$this->sql_add_num_queries($this->query_result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->query_result === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (($this->query_result = @pg_query($this->db_connect_id, $query)) === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_error($query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (defined('DEBUG_EXTRA'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_report('stop', $query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($cache_ttl && method_exists($cache, 'sql_save'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
|
|
4c79b5 |
$cache->sql_save($query, $this->query_result, $cache_ttl);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if (defined('DEBUG_EXTRA'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_report('fromcache', $query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build db-specific query data
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_custom_build($stage, $data)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $data;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build LIMIT query
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->query_result = false;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// if $total is set to 0 we do not want to limit the number of rows
|
|
|
4c79b5 |
if ($total == 0)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$total = -1;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$query .= "\n LIMIT $total OFFSET $offset";
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->sql_query($query, $cache_ttl);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Return number of affected rows
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_affectedrows()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Fetch current row
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_fetchrow($query_id = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $cache->sql_fetchrow($query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return ($query_id !== false) ? @pg_fetch_assoc($query_id, null) : false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Seek to given row number
|
|
|
4c79b5 |
* rownum is zero-based
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_rowseek($rownum, &$query_id)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $cache->sql_rowseek($rownum, $query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return ($query_id !== false) ? @pg_result_seek($query_id, $rownum) : false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Get last inserted id after insert statement
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_nextid()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id !== false && $this->last_query_text != '')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text, $tablename))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query = "SELECT currval('" . $tablename[1] . "_seq') AS last_value";
|
|
|
4c79b5 |
$temp_q_id = @pg_query($this->db_connect_id, $query);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$temp_q_id)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$temp_result = @pg_fetch_assoc($temp_q_id, NULL);
|
|
|
4c79b5 |
@pg_free_result($query_id);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return ($temp_result) ? $temp_result['last_value'] : false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Free sql result
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_freeresult($query_id = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $cache->sql_freeresult($query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($this->open_queries[(int) $query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
unset($this->open_queries[(int) $query_id]);
|
|
|
4c79b5 |
return @pg_free_result($query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Escape string used in sql query
|
|
|
4c79b5 |
* Note: Do not use for bytea values if we may use them at a later stage
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_escape($msg)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return @pg_escape_string($msg);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build LIKE expression
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_like_expression($expression)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $expression;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* return sql error array
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_error()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'message' => (!$this->db_connect_id) ? @pg_last_error() : @pg_last_error($this->db_connect_id),
|
|
|
4c79b5 |
'code' => ''
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Close sql connection
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_close()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return @pg_close($this->db_connect_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build db-specific report
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_report($mode, $query = '')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($mode)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'start':
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$explain_query = $query;
|
|
|
4c79b5 |
if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (preg_match('/^SELECT/', $explain_query))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$html_table = false;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($result = @pg_query($this->db_connect_id, "EXPLAIN $explain_query"))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
while ($row = @pg_fetch_assoc($result, NULL))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
@pg_free_result($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($html_table)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->html_hold .= '';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'fromcache':
|
|
|
4c79b5 |
$endtime = explode(' ', microtime());
|
|
|
4c79b5 |
$endtime = $endtime[0] + $endtime[1];
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$result = @pg_query($this->db_connect_id, $query);
|
|
|
4c79b5 |
while ($void = @pg_fetch_assoc($result, NULL))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// Take the time spent on parsing rows into account
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
@pg_free_result($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$splittime = explode(' ', microtime());
|
|
|
4c79b5 |
$splittime = $splittime[0] + $splittime[1];
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
?>
|