2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: db2.php,v 1.2 2002/01/28 17:24:45 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","db2");
36 var $rowset = array();
43 function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
45 $this->persistency = $persistency;
46 $this->user = $sqluser;
47 $this->password = $sqlpassword;
48 $this->dbname = $database;
50 $this->server = $sqlserver;
52 if($this->persistency)
54 $this->db_connect_id = odbc_pconnect($this->server, "", "");
58 $this->db_connect_id = odbc_connect($this->server, "", "");
61 if($this->db_connect_id)
63 @odbc_autocommit($this->db_connect_id, off);
65 return $this->db_connect_id;
77 if($this->db_connect_id)
79 if($this->query_result)
81 @odbc_free_result($this->query_result);
83 $result = @odbc_close($this->db_connect_id);
96 function sql_query($query = "", $transaction = FALSE)
99 // Remove any pre-existing queries
101 unset($this->query_result);
105 $this->num_queries++;
107 if(!eregi("^INSERT ",$query))
109 if(eregi("LIMIT", $query))
111 preg_match("/^(.*)LIMIT ([0-9]+)[, ]*([0-9]+)*/s", $query, $limits);
116 $row_offset = $limits[2];
117 $num_rows = $limits[3];
122 $num_rows = $limits[2];
125 $query .= " FETCH FIRST ".($row_offset+$num_rows)." ROWS ONLY OPTIMIZE FOR ".($row_offset+$num_rows)." ROWS";
127 $this->query_result = odbc_exec($this->db_connect_id, $query);
129 $query_limit_offset = $row_offset;
130 $this->result_numrows[$this->query_result] = $num_rows;
134 $this->query_result = odbc_exec($this->db_connect_id, $query);
137 $this->result_numrows[$this->query_result] = 5E6;
140 $result_id = $this->query_result;
141 if($this->query_result && eregi("^SELECT", $query))
144 for($i = 1; $i < odbc_num_fields($result_id)+1; $i++)
146 $this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);
149 $i = $row_offset + 1;
151 while(odbc_fetch_row($result_id, $i) && $k < $this->result_numrows[$result_id])
154 for($j = 1; $j < count($this->result_field_names[$result_id])+1; $j++)
156 $this->result_rowset[$result_id][$k][$this->result_field_names[$result_id][$j-1]] = odbc_result($result_id, $j);
162 $this->result_numrows[$result_id] = $k;
163 $this->row_index[$result_id] = 0;
167 $this->result_numrows[$result_id] = @odbc_num_rows($result_id);
168 $this->row_index[$result_id] = 0;
173 if(eregi("^(INSERT|UPDATE) ", $query))
175 $query = preg_replace("/\\\'/s", "''", $query);
178 $this->query_result = odbc_exec($this->db_connect_id, $query);
180 if($this->query_result)
182 $sql_id = "VALUES(IDENTITY_VAL_LOCAL())";
184 $id_result = odbc_exec($this->db_connect_id, $sql_id);
187 $row_result = odbc_fetch_row($id_result);
190 $this->next_id[$this->query_result] = odbc_result($id_result, 1);
195 odbc_commit($this->db_connect_id);
197 $this->query_limit_offset[$this->query_result] = 0;
198 $this->result_numrows[$this->query_result] = 0;
201 return $this->query_result;
210 // Other query methods
212 function sql_numrows($query_id = 0)
216 $query_id = $this->query_result;
220 return $this->result_numrows[$query_id];
227 function sql_affectedrows($query_id = 0)
231 $query_id = $this->query_result;
235 return $this->result_numrows[$query_id];
242 function sql_numfields($query_id = 0)
246 $query_id = $this->query_result;
250 $result = count($this->result_field_names[$query_id]);
258 function sql_fieldname($offset, $query_id = 0)
262 $query_id = $this->query_result;
266 $result = $this->result_field_names[$query_id][$offset];
274 function sql_fieldtype($offset, $query_id = 0)
278 $query_id = $this->query_result;
282 $result = @odbc_field_type($query_id, $offset);
290 function sql_fetchrow($query_id = 0)
294 $query_id = $this->query_result;
298 if($this->row_index[$query_id] < $this->result_numrows[$query_id])
300 $result = $this->result_rowset[$query_id][$this->row_index[$query_id]];
301 $this->row_index[$query_id]++;
314 function sql_fetchrowset($query_id = 0)
318 $query_id = $this->query_result;
322 $this->row_index[$query_id] = $this->result_numrows[$query_id];
323 return $this->result_rowset[$query_id];
330 function sql_fetchfield($field, $row = -1, $query_id = 0)
334 $query_id = $this->query_result;
338 if($row < $this->result_numrows[$query_id])
342 $getrow = $this->row_index[$query_id]-1;
349 return $this->result_rowset[$query_id][$getrow][$this->result_field_names[$query_id][$field]];
362 function sql_rowseek($offset, $query_id = 0)
366 $query_id = $this->query_result;
370 $this->row_index[$query_id] = 0;
378 function sql_nextid($query_id = 0)
382 $query_id = $this->query_result;
386 return $this->next_id[$query_id];
393 function sql_freeresult($query_id = 0)
397 $query_id = $this->query_result;
401 $result = @odbc_free_result($query_id);
409 function sql_error($query_id = 0)
411 // $result['code'] = @odbc_error($this->db_connect_id);
412 // $result['message'] = @odbc_errormsg($this->db_connect_id);