aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/db/postgres.php
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/includes/db/postgres.php')
-rw-r--r--phpBB/includes/db/postgres.php248
1 files changed, 90 insertions, 158 deletions
diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php
index f16ecff2c6..72590d6330 100644
--- a/phpBB/includes/db/postgres.php
+++ b/phpBB/includes/db/postgres.php
@@ -24,7 +24,11 @@ if (!defined('SQL_LAYER'))
*/
class dbal_postgres extends dbal
{
-
+ var $last_query_text = '';
+
+ /**
+ * Connect to server
+ */
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
{
$this->connect_string = '';
@@ -73,24 +77,9 @@ class dbal_postgres extends dbal
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
}
- //
- // Other base methods
- //
- function sql_close()
- {
- if (!$this->db_connect_id)
- {
- return false;
- }
-
- if ($this->transaction)
- {
- @pg_exec($this->db_connect_id, 'COMMIT');
- }
-
- return @pg_close($this->db_connect_id);
- }
-
+ /**
+ * sql transaction
+ */
function sql_transaction($status = 'begin')
{
switch ($status)
@@ -122,7 +111,9 @@ class dbal_postgres extends dbal
return $result;
}
- // Base query method
+ /**
+ * Base query method
+ */
function sql_query($query = '', $cache_ttl = 0)
{
if ($query != '')
@@ -135,12 +126,12 @@ class dbal_postgres extends dbal
$this->sql_report('start', $query);
}
+ $this->last_query_text = $query;
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
if (!$this->query_result)
{
$this->num_queries++;
- $this->last_query_text = $query;
if (($this->query_result = @pg_exec($this->db_connect_id, $query)) === false)
{
@@ -154,8 +145,13 @@ class dbal_postgres extends dbal
if ($cache_ttl && method_exists($cache, 'sql_save'))
{
+ $this->open_queries[(int) $this->query_result] = $this->query_result;
$cache->sql_save($query, $this->query_result, $cache_ttl);
}
+ else if (strpos($query, 'SELECT') !== false && $this->query_result)
+ {
+ $this->open_queries[(int) $this->query_result] = $this->query_result;
+ }
}
else if (defined('DEBUG_EXTRA'))
{
@@ -170,6 +166,9 @@ class dbal_postgres extends dbal
return ($this->query_result) ? $this->query_result : false;
}
+ /**
+ * Build LIMIT query
+ */
function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
{
if ($query != '')
@@ -192,10 +191,10 @@ class dbal_postgres extends dbal
}
}
- // Other query methods
- //
- // NOTE :: Want to remove _ALL_ reliance on sql_numrows from core code ...
- // don't want this here by a middle Milestone
+ /**
+ * Return number of rows
+ * Not used within core code
+ */
function sql_numrows($query_id = false)
{
if (!$query_id)
@@ -206,16 +205,17 @@ class dbal_postgres extends dbal
return ($query_id) ? @pg_numrows($query_id) : false;
}
- function sql_affectedrows($query_id = false)
+ /**
+ * Return number of affected rows
+ */
+ function sql_affectedrows()
{
- if (!$query_id)
- {
- $query_id = $this->query_result;
- }
-
- return ($query_id) ? @pg_cmdtuples($query_id) : false;
+ return ($this->query_result) ? @pg_cmdtuples($this->query_result) : false;
}
+ /**
+ * Fetch current row
+ */
function sql_fetchrow($query_id = false)
{
global $cache;
@@ -225,134 +225,56 @@ class dbal_postgres extends dbal
$query_id = $this->query_result;
}
- if (!isset($this->rownum[$query_id]))
- {
- $this->rownum[$query_id] = 0;
- }
-
if (isset($cache->sql_rowset[$query_id]))
{
return $cache->sql_fetchrow($query_id);
}
- $result = @pg_fetch_array($query_id, NULL, PGSQL_ASSOC);
-
- if ($result)
- {
- $this->rownum[$query_id]++;
- }
-
- return $result;
+ return ($query_id) ? @pg_fetch_array($query_id, NULL, PGSQL_ASSOC) : false;
}
- function sql_fetchrowset($query_id = false)
+ /**
+ * 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)
{
if (!$query_id)
{
$query_id = $this->query_result;
}
- $result = array();
-
if ($query_id)
{
- unset($this->rowset[$query_id]);
- unset($this->row[$query_id]);
-
- $result = array();
- while ($this->rowset[$query_id] = $this->sql_fetchrow($query_id))
+ if ($rownum !== false)
{
- $result[] = $this->rowset[$query_id];
+ $this->sql_rowseek($rownum, $query_id);
}
- return $result;
+
+ $row = $this->sql_fetchrow($query_id);
+ return isset($row[$field]) ? $row[$field] : false;
}
return false;
}
- function sql_fetchfield($field, $rownum = -1, $query_id = false)
+ /**
+ * Seek to given row number
+ * rownum is zero-based
+ */
+ function sql_rowseek($rownum, $query_id = false)
{
if (!$query_id)
{
$query_id = $this->query_result;
}
- if ($query_id)
- {
- if ($rownum > -1)
- {
- if (@function_exists('pg_result_seek'))
- {
- @pg_result_seek($query_id, $rownum);
- $row = @pg_fetch_assoc($query_id);
- $result = isset($row[$field]) ? $row[$field] : false;
- }
- else
- {
- $this->sql_rowseek($offset, $query_id);
- $row = $this->sql_fetchrow($query_id);
- $result = isset($row[$field]) ? $row[$field] : false;
- }
- }
- else
- {
- if (empty($this->row[$query_id]) && empty($this->rowset[$query_id]))
- {
- if ($this->row[$query_id] = $this->sql_fetchrow($query_id))
- {
- $result = $this->row[$query_id][$field];
- }
- }
- else
- {
- if ($this->rowset[$query_id])
- {
- $result = $this->rowset[$query_id][$field];
- }
- elseif ($this->row[$query_id])
- {
- $result = $this->row[$query_id][$field];
- }
- }
- }
- return $result;
- }
- return false;
- }
-
- function sql_rowseek($offset, $query_id = false)
- {
- if (!$query_id)
- {
- $query_id = $this->query_result;
- }
-
- if ($query_id)
- {
- if ($offset > -1)
- {
- if (@function_exists('pg_result_seek'))
- {
- @pg_result_seek($query_id, $rownum);
- }
- else
- {
- for ($i = $this->rownum[$query_id]; $i < $offset; $i++)
- {
- $this->sql_fetchrow($query_id);
- }
- }
- return true;
- }
- else
- {
- return false;
- }
- }
-
- return false;
+ return ($query_id) ? @pg_result_seek($query_id, $rownum) : false;
}
+ /**
+ * Get last inserted id after insert statement
+ */
function sql_nextid()
{
$query_id = $this->query_result;
@@ -369,6 +291,7 @@ class dbal_postgres extends dbal
}
$temp_result = @pg_fetch_array($temp_q_id, NULL, PGSQL_ASSOC);
+ @pg_freeresult($query_id);
return ($temp_result) ? $temp_result['last_value'] : false;
}
@@ -377,22 +300,37 @@ class dbal_postgres extends dbal
return false;
}
+ /**
+ * Free sql result
+ */
function sql_freeresult($query_id = false)
{
if (!$query_id)
{
$query_id = $this->query_result;
}
-
- return (is_resource($query_id)) ? @pg_freeresult($query_id) : false;
+
+ if (isset($this->open_queries[(int) $query_id]))
+ {
+ unset($this->open_queries[(int) $query_id]);
+ return @pg_freeresult($query_id);
+ }
}
+ /**
+ * Escape string used in sql query
+ */
function sql_escape($msg)
{
- return str_replace("'", "''", str_replace('\\', '\\\\', $msg));
+ // Do not use for bytea values
+ return pg_escape_string($msg);
}
- function db_sql_error()
+ /**
+ * return sql error array
+ * @private
+ */
+ function _sql_error()
{
return array(
'message' => @pg_errormessage(),
@@ -400,22 +338,25 @@ class dbal_postgres extends dbal
);
}
- function _sql_report($mode, $query = '')
+ /**
+ * Close sql connection
+ * @private
+ */
+ function _sql_close()
{
- global $cache, $starttime, $phpbb_root_path;
- static $curtime, $query_hold, $html_hold;
- static $sql_report = '';
- static $cache_num_queries = 0;
+ return @pg_close($this->db_connect_id);
+ }
+ /**
+ * Build db-specific report
+ * @private
+ */
+ function _sql_report($mode, $query = '')
+ {
switch ($mode)
{
case 'start':
- $query_hold = $query;
- $html_hold = '';
-
- $curtime = explode(' ', microtime());
- $curtime = $curtime[0] + $curtime[1];
- break;
+ break;
case 'fromcache':
$endtime = explode(' ', microtime());
@@ -426,23 +367,14 @@ class dbal_postgres extends dbal
{
// Take the time spent on parsing rows into account
}
+ @pg_freeresult($result);
+
$splittime = explode(' ', microtime());
$splittime = $splittime[0] + $splittime[1];
- $time_cache = $endtime - $curtime;
- $time_db = $splittime - $endtime;
- $color = ($time_db > $time_cache) ? 'green' : 'red';
+ $this->sql_report('record_fromcache', $query, $endtime, $splittime);
- $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query results obtained from the cache</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table><p align="center">';
-
- $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p>';
-
- // Pad the start time to not interfere with page timing
- $starttime += $time_db;
-
- @pg_freeresult($result);
- $cache_num_queries++;
- break;
+ break;
}
}