6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
19 include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
21 if (!class_exists('phpbb_error_collector'))
23 include($phpbb_root_path . 'includes/error_collector.' . $phpEx);
27 * PostgreSQL Database Abstraction Layer
28 * Minimum Requirement is Version 7.3+
31 class dbal_postgres extends dbal
33 var $last_query_text = '';
34 var $connect_error = '';
39 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
45 $connect_string .= "user=$sqluser ";
50 $connect_string .= "password=$sqlpassword ";
55 // $sqlserver can carry a port separated by : for compatibility reasons
56 // If $sqlserver has more than one : it's probably an IPv6 address.
57 // In this case we only allow passing a port via the $port variable.
58 if (substr_count($sqlserver, ':') === 1)
60 list($sqlserver, $port) = explode(':', $sqlserver);
63 if ($sqlserver !== 'localhost')
65 $connect_string .= "host=$sqlserver ";
70 $connect_string .= "port=$port ";
78 $this->dbname = $database;
79 if (strpos($database, '.') !== false)
81 list($database, $schema) = explode('.', $database);
83 $connect_string .= "dbname=$database";
86 $this->persistency = $persistency;
88 if ($this->persistency)
90 if (!function_exists('pg_pconnect'))
92 $this->connect_error = 'pg_pconnect function does not exist, is pgsql extension installed?';
93 return $this->sql_error('');
95 $collector = new phpbb_error_collector;
96 $collector->install();
97 $this->db_connect_id = (!$new_link) ? @pg_pconnect($connect_string) : @pg_pconnect($connect_string, PGSQL_CONNECT_FORCE_NEW);
101 if (!function_exists('pg_connect'))
103 $this->connect_error = 'pg_connect function does not exist, is pgsql extension installed?';
104 return $this->sql_error('');
106 $collector = new phpbb_error_collector;
107 $collector->install();
108 $this->db_connect_id = (!$new_link) ? @pg_connect($connect_string) : @pg_connect($connect_string, PGSQL_CONNECT_FORCE_NEW);
111 $collector->uninstall();
113 if ($this->db_connect_id)
115 if (version_compare($this->sql_server_info(true), '8.2', '>='))
117 $this->multi_insert = true;
122 @pg_query($this->db_connect_id, 'SET search_path TO ' . $schema);
124 return $this->db_connect_id;
127 $this->connect_error = $collector->format_errors();
128 return $this->sql_error('');
132 * Version information about used database
133 * @param bool $raw if true, only return the fetched sql_server_version
134 * @param bool $use_cache If true, it is safe to retrieve the value from the cache
135 * @return string sql server version
137 function sql_server_info($raw = false, $use_cache = true)
141 if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('pgsql_version')) === false)
143 $query_id = @pg_query($this->db_connect_id, 'SELECT VERSION() AS version');
144 $row = @pg_fetch_assoc($query_id, null);
145 @pg_free_result($query_id);
147 $this->sql_server_version = (!empty($row['version'])) ? trim(substr($row['version'], 10)) : 0;
149 if (!empty($cache) && $use_cache)
151 $cache->put('pgsql_version', $this->sql_server_version);
155 return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version;
162 function _sql_transaction($status = 'begin')
167 return @pg_query($this->db_connect_id, 'BEGIN');
171 return @pg_query($this->db_connect_id, 'COMMIT');
175 return @pg_query($this->db_connect_id, 'ROLLBACK');
185 * @param string $query Contains the SQL query which shall be executed
186 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
187 * @return mixed When casted to bool the returned value returns true on success and false on failure
191 function sql_query($query = '', $cache_ttl = 0)
197 // EXPLAIN only in extra debug mode
198 if (defined('DEBUG_EXTRA'))
200 $this->sql_report('start', $query);
203 $this->last_query_text = $query;
204 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
205 $this->sql_add_num_queries($this->query_result);
207 if ($this->query_result === false)
209 if (($this->query_result = @pg_query($this->db_connect_id, $query)) === false)
211 $this->sql_error($query);
214 if (defined('DEBUG_EXTRA'))
216 $this->sql_report('stop', $query);
219 if ($cache_ttl && method_exists($cache, 'sql_save'))
221 $this->open_queries[(int) $this->query_result] = $this->query_result;
222 $cache->sql_save($query, $this->query_result, $cache_ttl);
224 else if (strpos($query, 'SELECT') === 0 && $this->query_result)
226 $this->open_queries[(int) $this->query_result] = $this->query_result;
229 else if (defined('DEBUG_EXTRA'))
231 $this->sql_report('fromcache', $query);
239 return $this->query_result;
243 * Build db-specific query data
246 function _sql_custom_build($stage, $data)
254 function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
256 $this->query_result = false;
258 // if $total is set to 0 we do not want to limit the number of rows
264 $query .= "\n LIMIT $total OFFSET $offset";
266 return $this->sql_query($query, $cache_ttl);
270 * Return number of affected rows
272 function sql_affectedrows()
274 return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
280 function sql_fetchrow($query_id = false)
284 if ($query_id === false)
286 $query_id = $this->query_result;
289 if (isset($cache->sql_rowset[$query_id]))
291 return $cache->sql_fetchrow($query_id);
294 return ($query_id !== false) ? @pg_fetch_assoc($query_id, null) : false;
298 * Seek to given row number
299 * rownum is zero-based
301 function sql_rowseek($rownum, &$query_id)
305 if ($query_id === false)
307 $query_id = $this->query_result;
310 if (isset($cache->sql_rowset[$query_id]))
312 return $cache->sql_rowseek($rownum, $query_id);
315 return ($query_id !== false) ? @pg_result_seek($query_id, $rownum) : false;
319 * Get last inserted id after insert statement
321 function sql_nextid()
323 $query_id = $this->query_result;
325 if ($query_id !== false && $this->last_query_text != '')
327 if (preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text, $tablename))
329 $query = "SELECT currval('" . $tablename[1] . "_seq') AS last_value";
330 $temp_q_id = @pg_query($this->db_connect_id, $query);
337 $temp_result = @pg_fetch_assoc($temp_q_id, NULL);
338 @pg_free_result($query_id);
340 return ($temp_result) ? $temp_result['last_value'] : false;
350 function sql_freeresult($query_id = false)
354 if ($query_id === false)
356 $query_id = $this->query_result;
359 if (isset($cache->sql_rowset[$query_id]))
361 return $cache->sql_freeresult($query_id);
364 if (isset($this->open_queries[(int) $query_id]))
366 unset($this->open_queries[(int) $query_id]);
367 return @pg_free_result($query_id);
374 * Escape string used in sql query
375 * Note: Do not use for bytea values if we may use them at a later stage
377 function sql_escape($msg)
379 return @pg_escape_string($msg);
383 * Build LIKE expression
386 function _sql_like_expression($expression)
392 * return sql error array
395 function _sql_error()
397 // pg_last_error only works when there is an established connection.
398 // Connection errors have to be tracked by us manually.
399 if ($this->db_connect_id)
401 $message = @pg_last_error($this->db_connect_id);
405 $message = $this->connect_error;
409 'message' => $message,
415 * Close sql connection
418 function _sql_close()
420 return @pg_close($this->db_connect_id);
424 * Build db-specific report
427 function _sql_report($mode, $query = '')
433 $explain_query = $query;
434 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
436 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
438 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
440 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
443 if (preg_match('/^SELECT/', $explain_query))
447 if ($result = @pg_query($this->db_connect_id, "EXPLAIN $explain_query"))
449 while ($row = @pg_fetch_assoc($result, NULL))
451 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
454 @pg_free_result($result);
458 $this->html_hold .= '</table>';
465 $endtime = explode(' ', microtime());
466 $endtime = $endtime[0] + $endtime[1];
468 $result = @pg_query($this->db_connect_id, $query);
469 while ($void = @pg_fetch_assoc($result, NULL))
471 // Take the time spent on parsing rows into account
473 @pg_free_result($result);
475 $splittime = explode(' ', microtime());
476 $splittime = $splittime[0] + $splittime[1];
478 $this->sql_report('record_fromcache', $query, $endtime, $splittime);