2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: msaccess.php,v 1.8.2.2 2002/09/28 12:50:59 psotfx 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","msaccess");
31 var $result_ids = array();
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();
47 function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
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;
64 if($this->db_connect_id)
66 if( $this->in_transaction )
68 @odbc_commit($this->db_connect_id);
71 if( count($this->result_rowset) )
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);
80 return @odbc_close($this->db_connect_id);
91 function sql_query($query = "", $transaction = FALSE)
97 if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
99 if( !odbc_autocommit($this->db_connect_id, false) )
103 $this->in_transaction = TRUE;
106 $query = str_replace("LOWER(", "LCASE(", $query);
108 if( preg_match("/^SELECT(.*?)(LIMIT ([0-9]+)[, ]*([0-9]+)*)?$/s", $query, $limits) )
112 if( !empty($limits[2]) )
114 $row_offset = ( $limits[4] ) ? $limits[3] : "";
115 $num_rows = ( $limits[4] ) ? $limits[4] : $limits[3];
117 $query = "TOP " . ( $row_offset + $num_rows ) . $query;
120 $this->result = odbc_exec($this->db_connect_id, "SELECT $query");
124 if( empty($this->field_names[$this->result]) )
126 for($i = 1; $i < odbc_num_fields($this->result) + 1; $i++)
128 $this->field_names[$this->result][] = odbc_field_name($this->result, $i);
129 $this->field_types[$this->result][] = odbc_field_type($this->result, $i);
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;
140 while( odbc_fetch_row($this->result, $row_outer) && $row_outer < $row_outer_max )
142 for($j = 0; $j < count($this->field_names[$this->result]); $j++)
144 $this->result_rowset[$this->result][$row_inner][$this->field_names[$this->result][$j]] = stripslashes(odbc_result($this->result, $j + 1));
151 $this->num_rows[$this->result] = count($this->result_rowset[$this->result]);
153 odbc_free_result($this->result);
157 else if( eregi("^INSERT ", $query) )
159 $this->result = odbc_exec($this->db_connect_id, $query);
163 $result_id = odbc_exec($this->db_connect_id, "SELECT @@IDENTITY");
166 if( odbc_fetch_row($result_id) )
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);
176 $this->result = odbc_exec($this->db_connect_id, $query);
180 $this->affected_rows[$this->db_connect_id] = odbc_num_rows($this->result);
186 if( $this->in_transaction )
188 odbc_rollback($this->db_connect_id);
189 odbc_autocommit($this->db_connect_id, true);
190 $this->in_transaction = FALSE;
196 if( $transaction == END_TRANSACTION && $this->in_transaction )
198 $this->in_transaction = FALSE;
200 if ( !@odbc_commit($this->db_connect_id) )
202 odbc_rollback($this->db_connect_id);
203 odbc_autocommit($this->db_connect_id, true);
206 odbc_autocommit($this->db_connect_id, true);
209 return $this->result;
213 if( $transaction == END_TRANSACTION && $this->in_transaction )
215 $this->in_transaction = FALSE;
217 if ( !@odbc_commit($this->db_connect_id) )
219 odbc_rollback($this->db_connect_id);
220 odbc_autocommit($this->db_connect_id, true);
223 odbc_autocommit($this->db_connect_id, true);
231 // Other query methods
233 function sql_numrows($query_id = 0)
237 $query_id = $this->result;
240 return ( $query_id ) ? $this->num_rows[$query_id] : false;
243 function sql_numfields($query_id = 0)
247 $query_id = $this->result;
250 return ( $query_id ) ? count($this->field_names[$query_id]) : false;
253 function sql_fieldname($offset, $query_id = 0)
257 $query_id = $this->result;
260 return ( $query_id ) ? $this->field_names[$query_id][$offset] : false;
263 function sql_fieldtype($offset, $query_id = 0)
267 $query_id = $this->result;
270 return ( $query_id ) ? $this->field_types[$query_id][$offset] : false;
273 function sql_fetchrow($query_id = 0)
277 $query_id = $this->result;
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;
290 function sql_fetchrowset($query_id = 0)
294 $query_id = $this->result;
299 return ( $this->num_rows[$query_id] ) ? $this->result_rowset[$query_id] : false;
307 function sql_fetchfield($field, $row = -1, $query_id = 0)
311 $query_id = $this->result;
316 if( $row < $this->num_rows[$query_id] )
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]];
333 function sql_rowseek($offset, $query_id = 0)
337 $query_id = $this->result;
342 $this->current_row[$query_id] = $offset - 1;
351 function sql_nextid()
353 return ( $this->next_id[$this->db_connect_id] ) ? $this->next_id[$this->db_connect_id] : false;
356 function sql_affectedrows()
358 return ( $this->affected_rows[$this->db_connect_id] ) ? $this->affected_rows[$this->db_connect_id] : false;
361 function sql_freeresult($query_id = 0)
365 $query_id = $this->result;
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]);
379 $error['code'] = "";//odbc_error($this->db_connect_id);
380 $error['message'] = "Error";//odbc_errormsg($this->db_connect_id);