2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : supportphpbb.com
9 * $Id: postgres7.php,v 1.19.2.3 2005/05/06 20:50:10 acydburn Exp $
11 ***************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 ***************************************************************************/
22 if(!defined("SQL_LAYER"))
25 define("SQL_LAYER","postgresql");
32 var $in_transaction = 0;
34 var $rowset = array();
35 var $rownum = array();
41 function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
43 $this->connect_string = "";
47 $this->connect_string .= "user=$sqluser ";
52 $this->connect_string .= "password=$sqlpassword ";
57 if( ereg(":", $sqlserver) )
59 list($sqlserver, $sqlport) = split(":", $sqlserver);
60 $this->connect_string .= "host=$sqlserver port=$sqlport ";
64 if( $sqlserver != "localhost" )
66 $this->connect_string .= "host=$sqlserver ";
73 $this->dbname = $database;
74 $this->connect_string .= "dbname=$database";
77 $this->persistency = $persistency;
79 $this->db_connect_id = ( $this->persistency ) ? pg_pconnect($this->connect_string) : pg_connect($this->connect_string);
81 return ( $this->db_connect_id ) ? $this->db_connect_id : false;
89 if( $this->db_connect_id )
92 // Commit any remaining transactions
94 if( $this->in_transaction )
96 @pg_exec($this->db_connect_id, "COMMIT");
99 if( $this->query_result )
101 @pg_freeresult($this->query_result);
104 return @pg_close($this->db_connect_id);
115 function sql_query($query = "", $transaction = false)
118 // Remove any pre-existing queries
120 unset($this->query_result);
123 $this->num_queries++;
125 $query = preg_replace("/LIMIT ([0-9]+),([ 0-9]+)/", "LIMIT \\2 OFFSET \\1", $query);
127 if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
129 $this->in_transaction = TRUE;
131 if( !@pg_exec($this->db_connect_id, "BEGIN") )
137 $this->query_result = @pg_exec($this->db_connect_id, $query);
138 if( $this->query_result )
140 if( $transaction == END_TRANSACTION )
142 $this->in_transaction = FALSE;
144 if( !@pg_exec($this->db_connect_id, "COMMIT") )
146 @pg_exec($this->db_connect_id, "ROLLBACK");
151 $this->last_query_text[$this->query_result] = $query;
152 $this->rownum[$this->query_result] = 0;
154 unset($this->row[$this->query_result]);
155 unset($this->rowset[$this->query_result]);
157 return $this->query_result;
161 if( $this->in_transaction )
163 @pg_exec($this->db_connect_id, "ROLLBACK");
165 $this->in_transaction = FALSE;
172 if( $transaction == END_TRANSACTION && $this->in_transaction )
174 $this->in_transaction = FALSE;
176 if( !@pg_exec($this->db_connect_id, "COMMIT") )
178 @pg_exec($this->db_connect_id, "ROLLBACK");
188 // Other query methods
190 function sql_numrows($query_id = 0)
194 $query_id = $this->query_result;
197 return ( $query_id ) ? @pg_numrows($query_id) : false;
200 function sql_numfields($query_id = 0)
204 $query_id = $this->query_result;
207 return ( $query_id ) ? @pg_numfields($query_id) : false;
210 function sql_fieldname($offset, $query_id = 0)
214 $query_id = $this->query_result;
217 return ( $query_id ) ? @pg_fieldname($query_id, $offset) : false;
220 function sql_fieldtype($offset, $query_id = 0)
224 $query_id = $this->query_result;
227 return ( $query_id ) ? @pg_fieldtype($query_id, $offset) : false;
230 function sql_fetchrow($query_id = 0)
234 $query_id = $this->query_result;
239 $this->row = @pg_fetch_array($query_id, $this->rownum[$query_id]);
243 $this->rownum[$query_id]++;
251 function sql_fetchrowset($query_id = 0)
255 $query_id = $this->query_result;
260 unset($this->rowset[$query_id]);
261 unset($this->row[$query_id]);
262 $this->rownum[$query_id] = 0;
264 while( $this->rowset = @pg_fetch_array($query_id, $this->rownum[$query_id], PGSQL_ASSOC) )
266 $result[] = $this->rowset;
267 $this->rownum[$query_id]++;
276 function sql_fetchfield($field, $row_offset=-1, $query_id = 0)
280 $query_id = $this->query_result;
285 if( $row_offset != -1 )
287 $this->row = @pg_fetch_array($query_id, $row_offset, PGSQL_ASSOC);
291 if( $this->rownum[$query_id] )
293 $this->row = @pg_fetch_array($query_id, $this->rownum[$query_id]-1, PGSQL_ASSOC);
297 $this->row = @pg_fetch_array($query_id, $this->rownum[$query_id], PGSQL_ASSOC);
301 $this->rownum[$query_id]++;
306 return $this->row[$field];
312 function sql_rowseek($offset, $query_id = 0)
317 $query_id = $this->query_result;
324 $this->rownum[$query_id] = $offset;
336 function sql_nextid()
338 $query_id = $this->query_result;
340 if($query_id && $this->last_query_text[$query_id] != "")
342 if( preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text[$query_id], $tablename) )
344 $query = "SELECT currval('" . $tablename[1] . "_id_seq') AS last_value";
345 $temp_q_id = @pg_exec($this->db_connect_id, $query);
351 $temp_result = @pg_fetch_array($temp_q_id, 0, PGSQL_ASSOC);
353 return ( $temp_result ) ? $temp_result['last_value'] : false;
360 function sql_affectedrows($query_id = 0)
364 $query_id = $this->query_result;
367 return ( $query_id ) ? @pg_cmdtuples($query_id) : false;
370 function sql_freeresult($query_id = 0)
374 $query_id = $this->query_result;
377 return ( $query_id ) ? @pg_freeresult($query_id) : false;
380 function sql_error($query_id = 0)
384 $query_id = $this->query_result;
387 $result['message'] = @pg_errormessage($this->db_connect_id);
388 $result['code'] = -1;
393 } // class ... db_sql