]> git.vanrenterghem.biz Git - www.vanrenterghem.biz.git/blob - phpBB2/db/msaccess.php
Baseline
[www.vanrenterghem.biz.git] / phpBB2 / db / msaccess.php
1 <?php
2 /***************************************************************************
3  *                               msaccess.php
4  *                            -------------------
5  *   begin                : Saturday, Feb 13, 2001
6  *   copyright            : (C) 2001 The phpBB Group
7  *   email                : support@phpbb.com
8  *
9  *   $Id: msaccess.php,v 1.8.2.2 2002/09/28 12:50:59 psotfx 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","msaccess");
27 class sql_db
28 {
30         var $db_connect_id;
31         var $result_ids = array();
32         var $result;
34         var $next_id;
36         var $num_rows = array();
37         var $current_row = array();
38         var $field_names = array();
39         var $field_types = array();
40         var $result_rowset = array();
42         var $num_queries = 0;
44         //
45         // Constructor
46         //
47         function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
48         {
49                 $this->persistency = $persistency;
50                 $this->server = $sqlserver;
51                 $this->user = $sqluser;
52                 $this->password = $sqlpassword;
53                 $this->dbname = $database;
55                 $this->db_connect_id = ($this->persistency) ? odbc_pconnect($this->server, $this->user, $this->password) : odbc_connect($this->server, $this->user, $this->password);
57                 return ( $this->db_connect_id ) ? $this->db_connect_id : false;
58         }
59         //
60         // Other base methods
61         //
62         function sql_close()
63         {
64                 if($this->db_connect_id)
65                 {
66                         if( $this->in_transaction )
67                         {
68                                 @odbc_commit($this->db_connect_id);
69                         }
71                         if( count($this->result_rowset) )
72                         {
73                                 unset($this->result_rowset);
74                                 unset($this->field_names);
75                                 unset($this->field_types);
76                                 unset($this->num_rows);
77                                 unset($this->current_row);
78                         }
80                         return @odbc_close($this->db_connect_id);
81                 }
82                 else
83                 {
84                         return false;
85                 }
86         }
88         //
89         // Query method
90         //
91         function sql_query($query = "", $transaction = FALSE)
92         {
93                 if( $query != "" )
94                 {
95                         $this->num_queries++;
97                         if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
98                         {
99                                 if( !odbc_autocommit($this->db_connect_id, false) )
100                                 {
101                                         return false;
102                                 }
103                                 $this->in_transaction = TRUE;
104                         }
106                         $query = str_replace("LOWER(", "LCASE(", $query);
108                         if( preg_match("/^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$/s", $query, $limits) )
109                         {
110                                 $query = $limits[1];
112                                 if( !empty($limits[2]) )
113                                 {
114                                         $row_offset = ( $limits[4] ) ? $limits[3] : "";
115                                         $num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
117                                         $query = "TOP " . ( $row_offset + $num_rows ) . $query;
118                                 }
120                                 $this->result = odbc_exec($this->db_connect_id, "SELECT $query");
122                                 if( $this->result )
123                                 {
124                                         if( empty($this->field_names[$this->result]) )
125                                         {
126                                                 for($i = 1; $i < odbc_num_fields($this->result) + 1; $i++)
127                                                 {
128                                                         $this->field_names[$this->result][] = odbc_field_name($this->result, $i);
129                                                         $this->field_types[$this->result][] = odbc_field_type($this->result, $i);
130                                                 }
131                                         }
133                                         $this->current_row[$this->result] = 0;
134                                         $this->result_rowset[$this->result] = array();
136                                         $row_outer = ( isset($row_offset) ) ? $row_offset + 1 : 1;
137                                         $row_outer_max = ( isset($num_rows) ) ? $row_offset + $num_rows + 1 : 1E9;
138                                         $row_inner = 0;
140                                         while( odbc_fetch_row($this->result, $row_outer) && $row_outer < $row_outer_max )
141                                         {
142                                                 for($j = 0; $j < count($this->field_names[$this->result]); $j++)
143                                                 {
144                                                         $this->result_rowset[$this->result][$row_inner][$this->field_names[$this->result][$j]] = stripslashes(odbc_result($this->result, $j + 1));
145                                                 }
147                                                 $row_outer++;
148                                                 $row_inner++;
149                                         }
151                                         $this->num_rows[$this->result] = count($this->result_rowset[$this->result]);
153                                         odbc_free_result($this->result);
154                                 }
156                         }
157                         else if( eregi("^INSERT ", $query) )
158                         {
159                                 $this->result = odbc_exec($this->db_connect_id, $query);
161                                 if( $this->result )
162                                 {
163                                         $result_id = odbc_exec($this->db_connect_id, "SELECT @@IDENTITY");
164                                         if( $result_id )
165                                         {
166                                                 if( odbc_fetch_row($result_id) )
167                                                 {
168                                                         $this->next_id[$this->db_connect_id] = odbc_result($result_id, 1);
169                                                         $this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
170                                                 }
171                                         }
172                                 }
173                         }
174                         else
175                         {
176                                 $this->result = odbc_exec($this->db_connect_id, $query);
178                                 if( $this->result )
179                                 {
180                                         $this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
181                                 }
182                         }
184                         if( !$this->result )
185                         {
186                                 if( $this->in_transaction )
187                                 {
188                                         odbc_rollback($this->db_connect_id);
189                                         odbc_autocommit($this->db_connect_id, true);
190                                         $this->in_transaction = FALSE;
191                                 }
193                                 return false;
194                         }
196                         if( $transaction == END_TRANSACTION && $this->in_transaction )
197                         {
198                                 $this->in_transaction = FALSE;
200                                 if ( !@odbc_commit($this->db_connect_id) )
201                                 {
202                                         odbc_rollback($this->db_connect_id);
203                                         odbc_autocommit($this->db_connect_id, true);
204                                         return false;
205                                 }
206                                 odbc_autocommit($this->db_connect_id, true);
207                         }
209                         return $this->result;
210                 }
211                 else
212                 {
213                         if( $transaction == END_TRANSACTION && $this->in_transaction )
214                         {
215                                 $this->in_transaction = FALSE;
217                                 if ( !@odbc_commit($this->db_connect_id) )
218                                 {
219                                         odbc_rollback($this->db_connect_id);
220                                         odbc_autocommit($this->db_connect_id, true);
221                                         return false;
222                                 }
223                                 odbc_autocommit($this->db_connect_id, true);
224                         }
226                         return true;
227                 }
228         }
230         //
231         // Other query methods
232         //
233         function sql_numrows($query_id = 0)
234         {
235                 if( !$query_id )
236                 {
237                         $query_id = $this->result;
238                 }
240                 return ( $query_id ) ? $this->num_rows[$query_id] : false;
241         }
243         function sql_numfields($query_id = 0)
244         {
245                 if( !$query_id )
246                 {
247                         $query_id = $this->result;
248                 }
250                 return ( $query_id ) ? count($this->field_names[$query_id]) : false;
251         }
253         function sql_fieldname($offset, $query_id = 0)
254         {
255                 if( !$query_id )
256                 {
257                         $query_id = $this->result;
258                 }
260                 return ( $query_id ) ? $this->field_names[$query_id][$offset] : false;
261         }
263         function sql_fieldtype($offset, $query_id = 0)
264         {
265                 if( !$query_id )
266                 {
267                         $query_id = $this->result;
268                 }
270                 return ( $query_id ) ? $this->field_types[$query_id][$offset] : false;
271         }
273         function sql_fetchrow($query_id = 0)
274         {
275                 if( !$query_id )
276                 {
277                         $query_id = $this->result;
278                 }
280                 if( $query_id )
281                 {
282                         return ( $this->num_rows[$query_id] && $this->current_row[$query_id] < $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id][$this->current_row[$query_id]++] : false;
283                 }
284                 else
285                 {
286                         return false;
287                 }
288         }
290         function sql_fetchrowset($query_id = 0)
291         {
292                 if( !$query_id )
293                 {
294                         $query_id = $this->result;
295                 }
297                 if( $query_id )
298                 {
299                         return ( $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id] : false;
300                 }
301                 else
302                 {
303                         return false;
304                 }
305         }
307         function sql_fetchfield($field, $row = -1, $query_id = 0)
308         {
309                 if( !$query_id )
310                 {
311                         $query_id = $this->result;
312                 }
314                 if( $query_id )
315                 {
316                         if( $row < $this->num_rows[$query_id] )
317                         {
318                                 $getrow = ($row == -1) ? $this->current_row[$query_id] - 1 : $row;
320                                 return $this->result_rowset[$query_id][$getrow][$this->field_names[$query_id][$field]];
321                         }
322                         else
323                         {
324                                 return false;
325                         }
326                 }
327                 else
328                 {
329                         return false;
330                 }
331         }
333         function sql_rowseek($offset, $query_id = 0)
334         {
335                 if( !$query_id )
336                 {
337                         $query_id = $this->result;
338                 }
340                 if( $query_id )
341                 {
342                         $this->current_row[$query_id] = $offset - 1;
343                         return true;
344                 }
345                 else
346                 {
347                         return false;
348                 }
349         }
351         function sql_nextid()
352         {
353                 return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
354         }
356         function sql_affectedrows()
357         {
358                 return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
359         }
361         function sql_freeresult($query_id = 0)
362         {
363                 if( !$query_id )
364                 {
365                         $query_id = $this->result;
366                 }
368                 unset($this->num_rows[$query_id]);
369                 unset($this->current_row[$query_id]);
370                 unset($this->result_rowset[$query_id]);
371                 unset($this->field_names[$query_id]);
372                 unset($this->field_types[$query_id]);
374                 return true;
375         }
377         function sql_error()
378         {
379                 $error['code'] = "";//odbc_error($this->db_connect_id);
380                 $error['message'] = "Error";//odbc_errormsg($this->db_connect_id);
382                 return $error;
383         }
385 } // class sql_db
387 } // if ... define
389 ?>