]> git.vanrenterghem.biz Git - www.vanrenterghem.biz.git/blob - phpBB2/db/postgres7.php
Update section descriptions.
[www.vanrenterghem.biz.git] / phpBB2 / db / postgres7.php
1 <?php
2   /***************************************************************************
3    *                               postgres7.php
4    *                            -------------------
5    *   begin                : Saturday, Feb 13, 2001
6    *   copyright            : (C) 2001 The phpBB Group
7    *   email                : supportphpbb.com
8    *
9    *   $Id: postgres7.php,v 1.19.2.3 2005/05/06 20:50:10 acydburn Exp $
10    *
11    ***************************************************************************/
13 /***************************************************************************
14  *
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.
19  *
20  ***************************************************************************/
22 if(!defined("SQL_LAYER"))
23 {
25 define("SQL_LAYER","postgresql");
27 class sql_db
28 {
30         var $db_connect_id;
31         var $query_result;
32         var $in_transaction = 0;
33         var $row = array();
34         var $rowset = array();
35         var $rownum = array();
36         var $num_queries = 0;
38         //
39         // Constructor
40         //
41         function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
42         {
43                 $this->connect_string = "";
45                 if( $sqluser )
46                 {
47                         $this->connect_string .= "user=$sqluser ";
48                 }
50                 if( $sqlpassword )
51                 {
52                         $this->connect_string .= "password=$sqlpassword ";
53                 }
55                 if( $sqlserver )
56                 {
57                         if( ereg(":", $sqlserver) )
58                         {
59                                 list($sqlserver, $sqlport) = split(":", $sqlserver);
60                                 $this->connect_string .= "host=$sqlserver port=$sqlport ";
61                         }
62                         else
63                         {
64                                 if( $sqlserver != "localhost" )
65                                 {
66                                         $this->connect_string .= "host=$sqlserver ";
67                                 }
68                         }
69                 }
71                 if( $database )
72                 {
73                         $this->dbname = $database;
74                         $this->connect_string .= "dbname=$database";
75                 }
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;
82         }
84         //
85         // Other base methods
86         //
87         function sql_close()
88         {
89                 if( $this->db_connect_id )
90                 {
91                         //
92                         // Commit any remaining transactions
93                         //
94                         if( $this->in_transaction )
95                         {
96                                 @pg_exec($this->db_connect_id, "COMMIT");
97                         }
99                         if( $this->query_result )
100                         {
101                                 @pg_freeresult($this->query_result);
102                         }
104                         return @pg_close($this->db_connect_id);
105                 }
106                 else
107                 {
108                         return false;
109                 }
110         }
112         //
113         // Query method
114         //
115         function sql_query($query = "", $transaction = false)
116         {
117                 //
118                 // Remove any pre-existing queries
119                 //
120                 unset($this->query_result);
121                 if( $query != "" )
122                 {
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 )
128                         {
129                                 $this->in_transaction = TRUE;
131                                 if( !@pg_exec($this->db_connect_id, "BEGIN") )
132                                 {
133                                         return false;
134                                 }
135                         }
137                         $this->query_result = @pg_exec($this->db_connect_id, $query);
138                         if( $this->query_result )
139                         {
140                                 if( $transaction == END_TRANSACTION )
141                                 {
142                                         $this->in_transaction = FALSE;
144                                         if( !@pg_exec($this->db_connect_id, "COMMIT") )
145                                         {
146                                                 @pg_exec($this->db_connect_id, "ROLLBACK");
147                                                 return false;
148                                         }
149                                 }
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;
158                         }
159                         else
160                         {
161                                 if( $this->in_transaction )
162                                 {
163                                         @pg_exec($this->db_connect_id, "ROLLBACK");
164                                 }
165                                 $this->in_transaction = FALSE;
167                                 return false;
168                         }
169                 }
170                 else
171                 {
172                         if( $transaction == END_TRANSACTION && $this->in_transaction )
173                         {
174                                 $this->in_transaction = FALSE;
176                                 if( !@pg_exec($this->db_connect_id, "COMMIT") )
177                                 {
178                                         @pg_exec($this->db_connect_id, "ROLLBACK");
179                                         return false;
180                                 }
181                         }
183                         return true;
184                 }
185         }
187         //
188         // Other query methods
189         //
190         function sql_numrows($query_id = 0)
191         {
192                 if( !$query_id )
193                 {
194                         $query_id = $this->query_result;
195                 }
197                 return ( $query_id ) ? @pg_numrows($query_id) : false;
198         }
200         function sql_numfields($query_id = 0)
201         {
202                 if( !$query_id )
203                 {
204                         $query_id = $this->query_result;
205                 }
207                 return ( $query_id ) ? @pg_numfields($query_id) : false;
208         }
210         function sql_fieldname($offset, $query_id = 0)
211         {
212                 if( !$query_id )
213                 {
214                         $query_id = $this->query_result;
215                 }
217                 return ( $query_id ) ? @pg_fieldname($query_id, $offset) : false;
218         }
220         function sql_fieldtype($offset, $query_id = 0)
221         {
222                 if( !$query_id )
223                 {
224                         $query_id = $this->query_result;
225                 }
227                 return ( $query_id ) ? @pg_fieldtype($query_id, $offset) : false;
228         }
230         function sql_fetchrow($query_id = 0)
231         {
232                 if( !$query_id )
233                 {
234                         $query_id = $this->query_result;
235                 }
237                 if($query_id)
238                 {
239                         $this->row = @pg_fetch_array($query_id, $this->rownum[$query_id]);
241                         if( $this->row )
242                         {
243                                 $this->rownum[$query_id]++;
244                                 return $this->row;
245                         }
246                 }
248                 return false;
249         }
251         function sql_fetchrowset($query_id = 0)
252         {
253                 if( !$query_id )
254                 {
255                         $query_id = $this->query_result;
256                 }
258                 if( $query_id )
259                 {
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) )
265                         {
266                                 $result[] = $this->rowset;
267                                 $this->rownum[$query_id]++;
268                         }
270                         return $result;
271                 }
273                 return false;
274         }
276         function sql_fetchfield($field, $row_offset=-1, $query_id = 0)
277         {
278                 if( !$query_id )
279                 {
280                         $query_id = $this->query_result;
281                 }
283                 if( $query_id )
284                 {
285                         if( $row_offset != -1 )
286                         {
287                                 $this->row = @pg_fetch_array($query_id, $row_offset, PGSQL_ASSOC);
288                         }
289                         else
290                         {
291                                 if( $this->rownum[$query_id] )
292                                 {
293                                         $this->row = @pg_fetch_array($query_id, $this->rownum[$query_id]-1, PGSQL_ASSOC);
294                                 }
295                                 else
296                                 {
297                                         $this->row = @pg_fetch_array($query_id, $this->rownum[$query_id], PGSQL_ASSOC);
299                                         if( $this->row )
300                                         {
301                                                 $this->rownum[$query_id]++;
302                                         }
303                                 }
304                         }
306                         return $this->row[$field];
307                 }
309                 return false;
310         }
312         function sql_rowseek($offset, $query_id = 0)
313         {
315                 if(!$query_id)
316                 {
317                         $query_id = $this->query_result;
318                 }
320                 if( $query_id )
321                 {
322                         if( $offset > -1 )
323                         {
324                                 $this->rownum[$query_id] = $offset;
325                                 return true;
326                         }
327                         else
328                         {
329                                 return false;
330                         }
331                 }
333                 return false;
334         }
336         function sql_nextid()
337         {
338                 $query_id = $this->query_result;
340                 if($query_id && $this->last_query_text[$query_id] != "")
341                 {
342                         if( preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text[$query_id], $tablename) )
343                         {
344                                 $query = "SELECT currval('" . $tablename[1] . "_id_seq') AS last_value";
345                                 $temp_q_id =  @pg_exec($this->db_connect_id, $query);
346                                 if( !$temp_q_id )
347                                 {
348                                         return false;
349                                 }
351                                 $temp_result = @pg_fetch_array($temp_q_id, 0, PGSQL_ASSOC);
353                                 return ( $temp_result ) ? $temp_result['last_value'] : false;
354                         }
355                 }
357                 return false;
358         }
360         function sql_affectedrows($query_id = 0)
361         {
362                 if( !$query_id )
363                 {
364                         $query_id = $this->query_result;
365                 }
367                 return ( $query_id ) ? @pg_cmdtuples($query_id) : false;
368         }
370         function sql_freeresult($query_id = 0)
371         {
372                 if( !$query_id )
373                 {
374                         $query_id = $this->query_result;
375                 }
377                 return ( $query_id ) ? @pg_freeresult($query_id) : false;
378         }
380         function sql_error($query_id = 0)
381         {
382                 if( !$query_id )
383                 {
384                         $query_id = $this->query_result;
385                 }
387                 $result['message'] = @pg_errormessage($this->db_connect_id);
388                 $result['code'] = -1;
390                 return $result;
391         }
393 } // class ... db_sql
395 } // if ... defined
397 ?>