2003-03-17 10:54:23 +00:00
|
|
|
<?php
|
2005-04-09 12:26:45 +00:00
|
|
|
/**
|
|
|
|
*
|
2005-08-17 15:57:50 +00:00
|
|
|
* @package dbal
|
2005-04-09 12:26:45 +00:00
|
|
|
* @version $Id$
|
|
|
|
* @copyright (c) 2005 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-03-19 14:23:21 +00:00
|
|
|
/**
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2005-04-09 12:26:45 +00:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
2005-04-20 20:47:03 +00:00
|
|
|
if (!defined('SQL_LAYER'))
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
|
2006-03-17 05:30:44 +00:00
|
|
|
define('SQL_LAYER', 'postgres');
|
2005-08-17 15:57:50 +00:00
|
|
|
include($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-04-09 12:26:45 +00:00
|
|
|
/**
|
2005-08-17 15:57:50 +00:00
|
|
|
* @package dbal
|
2005-04-09 12:26:45 +00:00
|
|
|
* PostgreSQL Database Abstraction Layer
|
|
|
|
* Minimum Requirement is Version 7.3+
|
|
|
|
*/
|
2005-08-17 15:57:50 +00:00
|
|
|
class dbal_postgres extends dbal
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
var $last_query_text = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to server
|
|
|
|
*/
|
2005-04-20 20:47:03 +00:00
|
|
|
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2003-08-27 16:31:54 +00:00
|
|
|
$this->connect_string = '';
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2003-04-26 01:17:40 +00:00
|
|
|
if ($sqluser)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$this->connect_string .= "user=$sqluser ";
|
|
|
|
}
|
|
|
|
|
2003-04-26 01:17:40 +00:00
|
|
|
if ($sqlpassword)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$this->connect_string .= "password=$sqlpassword ";
|
|
|
|
}
|
|
|
|
|
2003-04-26 01:17:40 +00:00
|
|
|
if ($sqlserver)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2006-03-22 06:23:37 +00:00
|
|
|
if (strpos($sqlserver, ':') !== false)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2006-03-22 06:23:37 +00:00
|
|
|
list($sqlserver, $sqlport) = explode(':', $sqlserver);
|
2003-03-17 10:54:23 +00:00
|
|
|
$this->connect_string .= "host=$sqlserver port=$sqlport ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-04-26 01:17:40 +00:00
|
|
|
if ($sqlserver != "localhost")
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$this->connect_string .= "host=$sqlserver ";
|
|
|
|
}
|
2005-04-20 20:47:03 +00:00
|
|
|
|
|
|
|
if ($port)
|
|
|
|
{
|
|
|
|
$this->connect_string .= "port=$port ";
|
|
|
|
}
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-26 01:17:40 +00:00
|
|
|
if ($database)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$this->dbname = $database;
|
|
|
|
$this->connect_string .= "dbname=$database";
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->persistency = $persistency;
|
|
|
|
|
2003-06-12 17:11:43 +00:00
|
|
|
$this->db_connect_id = ($this->persistency) ? @pg_pconnect($this->connect_string) : @pg_connect($this->connect_string);
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2003-06-12 17:11:43 +00:00
|
|
|
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* sql transaction
|
|
|
|
*/
|
2005-04-20 20:47:03 +00:00
|
|
|
function sql_transaction($status = 'begin')
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
switch ($status)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
case 'begin':
|
2006-03-18 06:39:47 +00:00
|
|
|
$result = @pg_query($this->db_connect_id, 'BEGIN');
|
2005-04-20 20:47:03 +00:00
|
|
|
$this->transaction = true;
|
|
|
|
break;
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
case 'commit':
|
2006-03-18 06:39:47 +00:00
|
|
|
$result = @pg_query($this->db_connect_id, 'COMMIT');
|
2005-04-20 20:47:03 +00:00
|
|
|
$this->transaction = false;
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
if (!$result)
|
|
|
|
{
|
2006-03-18 06:39:47 +00:00
|
|
|
@pg_query($this->db_connect_id, 'ROLLBACK');
|
2005-04-20 20:47:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'rollback':
|
2006-03-18 06:39:47 +00:00
|
|
|
$result = @pg_query($this->db_connect_id, 'ROLLBACK');
|
2005-04-20 20:47:03 +00:00
|
|
|
$this->transaction = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$result = true;
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
2005-04-20 20:47:03 +00:00
|
|
|
|
|
|
|
return $result;
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Base query method
|
|
|
|
*/
|
2005-04-20 20:47:03 +00:00
|
|
|
function sql_query($query = '', $cache_ttl = 0)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
if ($query != '')
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
global $cache;
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-11-28 18:38:49 +00:00
|
|
|
if (strpos($query, 'SELECT') === 0 && strpos($query, 'FROM (') !== false)
|
|
|
|
{
|
2006-03-17 05:30:44 +00:00
|
|
|
$query = preg_replace('#FROM \(([^)]+)\)\s#', 'FROM \1 ', $query);
|
2005-11-28 18:38:49 +00:00
|
|
|
}
|
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
// EXPLAIN only in extra debug mode
|
|
|
|
if (defined('DEBUG_EXTRA'))
|
|
|
|
{
|
|
|
|
$this->sql_report('start', $query);
|
|
|
|
}
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
$this->last_query_text = $query;
|
2005-04-20 20:47:03 +00:00
|
|
|
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
|
|
|
|
|
|
|
|
if (!$this->query_result)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
$this->num_queries++;
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
if (($this->query_result = @pg_exec($this->db_connect_id, $query)) === false)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
$this->sql_error($query);
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
if (defined('DEBUG_EXTRA'))
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
$this->sql_report('stop', $query);
|
|
|
|
}
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
if ($cache_ttl && method_exists($cache, 'sql_save'))
|
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
2005-04-20 20:47:03 +00:00
|
|
|
$cache->sql_save($query, $this->query_result, $cache_ttl);
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
2006-03-17 12:51:32 +00:00
|
|
|
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
|
2005-10-07 23:00:41 +00:00
|
|
|
{
|
|
|
|
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
|
|
|
}
|
2005-04-20 20:47:03 +00:00
|
|
|
}
|
|
|
|
else if (defined('DEBUG_EXTRA'))
|
|
|
|
{
|
|
|
|
$this->sql_report('fromcache', $query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
return ($this->query_result) ? $this->query_result : false;
|
|
|
|
}
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Build LIMIT query
|
|
|
|
*/
|
2005-04-20 20:47:03 +00:00
|
|
|
function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
|
|
|
|
{
|
|
|
|
if ($query != '')
|
|
|
|
{
|
|
|
|
$this->query_result = false;
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
// if $total is set to 0 we do not want to limit the number of rows
|
|
|
|
if ($total == 0)
|
|
|
|
{
|
|
|
|
$total = -1;
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
2005-04-20 20:47:03 +00:00
|
|
|
|
|
|
|
$query .= "\n LIMIT $total OFFSET $offset";
|
|
|
|
|
|
|
|
return $this->sql_query($query, $cache_ttl);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Return number of rows
|
|
|
|
* Not used within core code
|
|
|
|
*/
|
2005-04-20 20:47:03 +00:00
|
|
|
function sql_numrows($query_id = false)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2003-04-26 01:17:40 +00:00
|
|
|
if (!$query_id)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
}
|
|
|
|
|
2006-03-18 06:39:47 +00:00
|
|
|
return ($query_id) ? @pg_num_rows($query_id) : false;
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Return number of affected rows
|
|
|
|
*/
|
|
|
|
function sql_affectedrows()
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
return ($this->query_result) ? @pg_cmdtuples($this->query_result) : false;
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Fetch current row
|
|
|
|
*/
|
2005-04-20 20:47:03 +00:00
|
|
|
function sql_fetchrow($query_id = false)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
global $cache;
|
|
|
|
|
2003-04-26 01:17:40 +00:00
|
|
|
if (!$query_id)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
}
|
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
if (isset($cache->sql_rowset[$query_id]))
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
return $cache->sql_fetchrow($query_id);
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2006-02-16 10:26:52 +00:00
|
|
|
return ($query_id) ? @pg_fetch_assoc($query_id, NULL) : false;
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Fetch field
|
|
|
|
* if rownum is false, the current row is used, else it is pointing to the row (zero-based)
|
|
|
|
*/
|
|
|
|
function sql_fetchfield($field, $rownum = false, $query_id = false)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2003-04-26 01:17:40 +00:00
|
|
|
if (!$query_id)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
}
|
|
|
|
|
2003-04-26 01:17:40 +00:00
|
|
|
if ($query_id)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
if ($rownum !== false)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
$this->sql_rowseek($rownum, $query_id);
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
2005-10-07 23:00:41 +00:00
|
|
|
|
|
|
|
$row = $this->sql_fetchrow($query_id);
|
|
|
|
return isset($row[$field]) ? $row[$field] : false;
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Seek to given row number
|
|
|
|
* rownum is zero-based
|
|
|
|
*/
|
|
|
|
function sql_rowseek($rownum, $query_id = false)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2003-04-26 01:17:40 +00:00
|
|
|
if (!$query_id)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
return ($query_id) ? @pg_result_seek($query_id, $rownum) : false;
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Get last inserted id after insert statement
|
|
|
|
*/
|
2003-03-17 10:54:23 +00:00
|
|
|
function sql_nextid()
|
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
if ($query_id && $this->last_query_text != '')
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
if (preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text, $tablename))
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$query = "SELECT currval('" . $tablename[1] . "_id_seq') AS last_value";
|
2006-03-18 06:39:47 +00:00
|
|
|
$temp_q_id = @pg_query($this->db_connect_id, $query);
|
2003-04-26 01:17:40 +00:00
|
|
|
if (!$temp_q_id)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-16 10:26:52 +00:00
|
|
|
$temp_result = @pg_fetch_assoc($temp_q_id, NULL);
|
2006-03-18 06:39:47 +00:00
|
|
|
@pg_free_result($query_id);
|
2003-03-17 10:54:23 +00:00
|
|
|
|
2003-04-26 01:17:40 +00:00
|
|
|
return ($temp_result) ? $temp_result['last_value'] : false;
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Free sql result
|
|
|
|
*/
|
2005-04-20 20:47:03 +00:00
|
|
|
function sql_freeresult($query_id = false)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2003-04-26 01:17:40 +00:00
|
|
|
if (!$query_id)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
|
|
|
$query_id = $this->query_result;
|
|
|
|
}
|
2005-10-07 23:00:41 +00:00
|
|
|
|
|
|
|
if (isset($this->open_queries[(int) $query_id]))
|
|
|
|
{
|
|
|
|
unset($this->open_queries[(int) $query_id]);
|
2006-03-18 06:39:47 +00:00
|
|
|
return @pg_free_result($query_id);
|
2005-10-07 23:00:41 +00:00
|
|
|
}
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Escape string used in sql query
|
|
|
|
*/
|
2005-04-20 20:47:03 +00:00
|
|
|
function sql_escape($msg)
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
// Do not use for bytea values
|
2006-02-16 10:26:52 +00:00
|
|
|
return @pg_escape_string($msg);
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* return sql error array
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function _sql_error()
|
2003-03-17 10:54:23 +00:00
|
|
|
{
|
2005-08-17 15:57:50 +00:00
|
|
|
return array(
|
2006-02-16 10:26:52 +00:00
|
|
|
'message' => (!$this->db_connect_id) ? @pg_last_error() : @pg_last_error($this->db_connect_id),
|
2003-06-12 17:11:43 +00:00
|
|
|
'code' => ''
|
|
|
|
);
|
2003-03-17 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Close sql connection
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function _sql_close()
|
2005-04-20 20:47:03 +00:00
|
|
|
{
|
2005-10-07 23:00:41 +00:00
|
|
|
return @pg_close($this->db_connect_id);
|
|
|
|
}
|
2005-04-20 20:47:03 +00:00
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
/**
|
|
|
|
* Build db-specific report
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function _sql_report($mode, $query = '')
|
|
|
|
{
|
2005-04-20 20:47:03 +00:00
|
|
|
switch ($mode)
|
|
|
|
{
|
|
|
|
case 'start':
|
2005-10-07 23:00:41 +00:00
|
|
|
break;
|
2005-04-20 20:47:03 +00:00
|
|
|
|
|
|
|
case 'fromcache':
|
|
|
|
$endtime = explode(' ', microtime());
|
|
|
|
$endtime = $endtime[0] + $endtime[1];
|
|
|
|
|
2006-03-18 06:39:47 +00:00
|
|
|
$result = @pg_query($this->db_connect_id, $query);
|
2006-02-16 10:26:52 +00:00
|
|
|
while ($void = @pg_fetch_assoc($result, NULL))
|
2005-04-20 20:47:03 +00:00
|
|
|
{
|
|
|
|
// Take the time spent on parsing rows into account
|
|
|
|
}
|
2006-03-18 06:39:47 +00:00
|
|
|
@pg_free_result($result);
|
2005-10-07 23:00:41 +00:00
|
|
|
|
2005-04-20 20:47:03 +00:00
|
|
|
$splittime = explode(' ', microtime());
|
|
|
|
$splittime = $splittime[0] + $splittime[1];
|
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
2005-04-20 20:47:03 +00:00
|
|
|
|
2005-10-07 23:00:41 +00:00
|
|
|
break;
|
2005-04-20 20:47:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-17 15:57:50 +00:00
|
|
|
}
|
2003-03-17 10:54:23 +00:00
|
|
|
|
|
|
|
} // if ... defined
|
|
|
|
|
|
|
|
?>
|