Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / db / postgres.php
blobd74a8167e91a11b15dde02eabb2f05d5ff150c6f
1 <?php
2 /**
4 * @package dbal
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * PostgreSQL Database Abstraction Layer
21 * Minimum Requirement: 8.2+
22 * @package dbal
24 class phpbb_dbal_postgres extends phpbb_dbal
26 /**
27 * @var string Database type. No distinction between versions or used extensions.
29 public $dbms_type = 'postgres';
31 /**
32 * @var array Database type map, column layout information
34 public $dbms_type_map = array(
35 'INT:' => 'INT4',
36 'BINT' => 'INT8',
37 'UINT' => 'INT4', // unsigned
38 'UINT:' => 'INT4', // unsigned
39 'USINT' => 'INT2', // unsigned
40 'BOOL' => 'INT2', // unsigned
41 'TINT:' => 'INT2',
42 'VCHAR' => 'varchar(255)',
43 'VCHAR:' => 'varchar(%d)',
44 'CHAR:' => 'char(%d)',
45 'XSTEXT' => 'varchar(1000)',
46 'STEXT' => 'varchar(3000)',
47 'TEXT' => 'varchar(8000)',
48 'MTEXT' => 'TEXT',
49 'XSTEXT_UNI'=> 'varchar(100)',
50 'STEXT_UNI' => 'varchar(255)',
51 'TEXT_UNI' => 'varchar(4000)',
52 'MTEXT_UNI' => 'TEXT',
53 'TIMESTAMP' => 'INT4', // unsigned
54 'DECIMAL' => 'decimal(5,2)',
55 'DECIMAL:' => 'decimal(%d,2)',
56 'PDECIMAL' => 'decimal(6,3)',
57 'PDECIMAL:' => 'decimal(%d,3)',
58 'VCHAR_UNI' => 'varchar(255)',
59 'VCHAR_UNI:'=> 'varchar(%d)',
60 'VARBINARY' => 'bytea',
63 /**
64 * @var string PostgreSQL schema (if supplied with $database -> database.schema)
66 public $schema = '';
68 /**
69 * Connect to server. See {@link phpbb_dbal::sql_connect() sql_connect()} for details.
71 public function sql_connect($server, $user, $password, $database, $port = false, $persistency = false , $new_link = false)
73 $this->persistency = $persistency;
74 $this->user = $user;
75 $this->server = $server;
76 $this->dbname = $database;
77 $this->port = $port;
79 $connect_string = '';
81 if ($this->user)
83 $connect_string .= 'user=' . $this->user . ' ';
86 if ($password)
88 $connect_string .= 'password=' . $password . ' ';
91 if ($this->server)
93 if (strpos($this->server, ':') !== false)
95 list($this->server, $this->port) = explode(':', $this->server, 2);
98 if ($this->server !== 'localhost')
100 $connect_string .= 'host=' . $this->server . ' ';
103 if ($this->port)
105 $connect_string .= 'port=' . $this->port . ' ';
109 $this->schema = '';
111 if ($this->dbname)
113 if (strpos($this->dbname, '.') !== false)
115 list($this->dbname, $this->schema) = explode('.', $this->dbname, 2);
117 $connect_string .= 'dbname=' . $this->dbname;
120 $this->db_connect_id = ($this->persistency) ? @pg_pconnect($connect_string, ($new_link) ? PGSQL_CONNECT_FORCE_NEW : false) : @pg_connect($connect_string, ($new_link) ? PGSQL_CONNECT_FORCE_NEW : false);
122 if (!$this->db_connect_id)
124 return $this->sql_error(htmlspecialchars_decode(phpbb::$last_notice['message']));
127 if ($this->schema)
129 @pg_query($this->db_connect_id, 'SET search_path TO ' . $this->schema);
132 return $this->db_connect_id;
136 * Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details.
138 public function sql_server_info($raw = false)
140 if (!phpbb::registered('acm') || ($this->sql_server_version = phpbb::$acm->get('#pgsql_version')) === false)
142 $query_id = @pg_query($this->db_connect_id, 'SELECT VERSION() AS version');
143 $row = @pg_fetch_assoc($query_id, null);
144 @pg_free_result($query_id);
146 $this->sql_server_version = (!empty($row['version'])) ? trim(substr($row['version'], 10)) : 0;
148 if (phpbb::registered('acm'))
150 phpbb::$acm->put('#pgsql_version', $this->sql_server_version);
154 return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version;
158 * DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details.
160 protected function _sql_query($query)
162 return @pg_query($this->db_connect_id, $query);
166 * Build LIMIT query and run it. See {@link phpbb_dbal::_sql_query_limit() _sql_query_limit()} for details.
168 protected function _sql_query_limit($query, $total, $offset, $cache_ttl)
170 // if $total is set to 0 we do not want to limit the number of rows
171 if ($total == 0)
173 $total = 'ALL';
176 $query .= "\n LIMIT $total OFFSET $offset";
177 return $this->sql_query($query, $cache_ttl);
181 * Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details.
183 protected function _sql_close()
185 return @pg_close($this->db_connect_id);
189 * SQL Transaction. See {@link phpbb_dbal::_sql_transaction() _sql_transaction()} for details.
191 protected function _sql_transaction($status)
193 switch ($status)
195 case 'begin':
196 return @pg_query($this->db_connect_id, 'BEGIN');
197 break;
199 case 'commit':
200 return @pg_query($this->db_connect_id, 'COMMIT');
201 break;
203 case 'rollback':
204 return @pg_query($this->db_connect_id, 'ROLLBACK');
205 break;
208 return true;
212 * Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details.
214 public function sql_affectedrows()
216 return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
220 * Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details.
222 public function sql_nextid()
224 if (!$this->db_connect_id)
226 return false;
229 $query = "SELECT lastval() AS last_value";
230 $temp_q_id = @pg_query($this->db_connect_id, $query);
232 if (!$temp_q_id)
234 return false;
237 $temp_result = @pg_fetch_assoc($temp_q_id, NULL);
238 @pg_free_result($query_id);
240 return ($temp_result) ? $temp_result['last_value'] : false;
244 * Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details.
246 protected function _sql_fetchrow($query_id)
248 return @pg_fetch_assoc($query_id, null);
252 * Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details.
254 protected function _sql_freeresult($query_id)
256 return @pg_free_result($query_id);
260 * Correctly adjust LIKE expression for special characters. See {@link phpbb_dbal::_sql_like_expression() _sql_like_expression()} for details.
262 protected function _sql_like_expression($expression)
264 return $expression;
268 * Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details.
269 * Note: Do not use for bytea values if we may use them at a later stage
271 public function sql_escape($msg)
273 return @pg_escape_string($msg);
277 * Expose a DBMS specific function. See {@link phpbb_dbal::sql_function() sql_function()} for details.
279 public function sql_function($type, $col)
281 switch ($type)
283 case 'length_varchar':
284 case 'length_text':
285 return 'LENGTH(' . $col . ')';
286 break;
292 * Handle data by using prepared statements. See {@link phpbb_dbal::sql_handle_data() sql_handle_data()} for details.
293 public function sql_handle_data($type, $table, $data, $where = '')
295 // for now, stmtname is an empty string, it might change to something more unique in the future
296 if ($type === 'INSERT')
298 $stmt = pg_prepare($this->dbms_type, '', "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES ($" . implode(', $', range(1, sizeof($data))) . ')');
300 else
302 $query = "UPDATE $table SET ";
304 $set = array();
305 foreach (array_keys($data) as $key_id => $key)
307 $set[] = $key . ' = $' . $key_id;
309 $query .= implode(', ', $set);
311 if ($where !== '')
313 $query .= $where;
316 $stmt = pg_prepare($this->db_connect_id, '', $query);
319 // add the stmtname to the top
320 array_unshift($data, '');
322 // add the connection resource
323 array_unshift($data, $this->db_connect_id);
325 call_user_func_array('pg_execute', $data);
330 * Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details.
332 protected function _sql_custom_build($stage, $data)
334 return $data;
338 * return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details.
340 protected function _sql_error()
342 return array(
343 'message' => (!$this->db_connect_id) ? @pg_last_error() : @pg_last_error($this->db_connect_id),
344 'code' => ''
349 * Run DB-specific code to build SQL Report to explain queries, show statistics and runtime information. See {@link phpbb_dbal::_sql_report() _sql_report()} for details.
351 protected function _sql_report($mode, $query = '')
353 switch ($mode)
355 case 'start':
357 $explain_query = $query;
358 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
360 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
362 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
364 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
367 if (preg_match('/^SELECT/', $explain_query))
369 $html_table = false;
371 if ($result = @pg_query($this->db_connect_id, "EXPLAIN $explain_query"))
373 while ($row = @pg_fetch_assoc($result, NULL))
375 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
378 @pg_free_result($result);
380 if ($html_table)
382 $this->html_hold .= '</table>';
386 break;
388 case 'fromcache':
389 $endtime = explode(' ', microtime());
390 $endtime = $endtime[0] + $endtime[1];
392 $result = @pg_query($this->db_connect_id, $query);
393 while ($void = @pg_fetch_assoc($result, NULL))
395 // Take the time spent on parsing rows into account
397 @pg_free_result($result);
399 $splittime = explode(' ', microtime());
400 $splittime = $splittime[0] + $splittime[1];
402 $this->sql_report('record_fromcache', $query, $endtime, $splittime);
404 break;