5 * @version $Id: db2.php,v 1.2 2005/06/10 08:52:03 devalley Exp $
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if(!defined("SQL_LAYER"))
17 define("SQL_LAYER","db2");
21 * DB2 Database Abstraction Layer
32 var $rowset = array();
39 function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
41 $this->persistency = $persistency;
42 $this->user = $sqluser;
43 $this->password = $sqlpassword;
44 $this->dbname = $database;
46 $this->server = $sqlserver;
48 if($this->persistency)
50 $this->db_connect_id = odbc_pconnect($this->server, "", "");
54 $this->db_connect_id = odbc_connect($this->server, "", "");
57 if($this->db_connect_id)
59 @odbc_autocommit($this->db_connect_id, off);
61 return $this->db_connect_id;
73 if($this->db_connect_id)
75 if($this->query_result)
77 @odbc_free_result($this->query_result);
79 $result = @odbc_close($this->db_connect_id);
92 function sql_query($query = "", $transaction = FALSE)
95 // Remove any pre-existing queries
97 unset($this->query_result);
101 $this->num_queries++;
103 if(!eregi("^INSERT ",$query))
105 if(eregi("LIMIT", $query))
107 preg_match("/^(.*)LIMIT ([0-9]+)[, ]*([0-9]+)*/s", $query, $limits);
112 $row_offset = $limits[2];
113 $num_rows = $limits[3];
118 $num_rows = $limits[2];
121 $query .= " FETCH FIRST ".($row_offset+$num_rows)." ROWS ONLY OPTIMIZE FOR ".($row_offset+$num_rows)." ROWS";
123 $this->query_result = odbc_exec($this->db_connect_id, $query);
125 $query_limit_offset = $row_offset;
126 $this->result_numrows[$this->query_result] = $num_rows;
130 $this->query_result = odbc_exec($this->db_connect_id, $query);
133 $this->result_numrows[$this->query_result] = 5E6;
136 $result_id = $this->query_result;
137 if($this->query_result && eregi("^SELECT", $query))
140 for($i = 1; $i < odbc_num_fields($result_id)+1; $i++)
142 $this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);
145 $i = $row_offset + 1;
147 while(odbc_fetch_row($result_id, $i) && $k < $this->result_numrows[$result_id])
150 for($j = 1; $j < count($this->result_field_names[$result_id])+1; $j++)
152 $this->result_rowset[$result_id][$k][$this->result_field_names[$result_id][$j-1]] = odbc_result($result_id, $j);
158 $this->result_numrows[$result_id] = $k;
159 $this->row_index[$result_id] = 0;
163 $this->result_numrows[$result_id] = @odbc_num_rows($result_id);
164 $this->row_index[$result_id] = 0;
169 if(eregi("^(INSERT|UPDATE) ", $query))
171 $query = preg_replace("/\\\'/s", "''", $query);
174 $this->query_result = odbc_exec($this->db_connect_id, $query);
176 if($this->query_result)
178 $sql_id = "VALUES(IDENTITY_VAL_LOCAL())";
180 $id_result = odbc_exec($this->db_connect_id, $sql_id);
183 $row_result = odbc_fetch_row($id_result);
186 $this->next_id[$this->query_result] = odbc_result($id_result, 1);
191 odbc_commit($this->db_connect_id);
193 $this->query_limit_offset[$this->query_result] = 0;
194 $this->result_numrows[$this->query_result] = 0;
197 return $this->query_result;
206 // Other query methods
208 function sql_numrows($query_id = 0)
212 $query_id = $this->query_result;
216 return $this->result_numrows[$query_id];
223 function sql_affectedrows($query_id = 0)
227 $query_id = $this->query_result;
231 return $this->result_numrows[$query_id];
238 function sql_numfields($query_id = 0)
242 $query_id = $this->query_result;
246 $result = count($this->result_field_names[$query_id]);
254 function sql_fieldname($offset, $query_id = 0)
258 $query_id = $this->query_result;
262 $result = $this->result_field_names[$query_id][$offset];
270 function sql_fieldtype($offset, $query_id = 0)
274 $query_id = $this->query_result;
278 $result = @odbc_field_type($query_id, $offset);
286 function sql_fetchrow($query_id = 0)
290 $query_id = $this->query_result;
294 if($this->row_index[$query_id] < $this->result_numrows[$query_id])
296 $result = $this->result_rowset[$query_id][$this->row_index[$query_id]];
297 $this->row_index[$query_id]++;
310 function sql_fetchrowset($query_id = 0)
314 $query_id = $this->query_result;
318 $this->row_index[$query_id] = $this->result_numrows[$query_id];
319 return $this->result_rowset[$query_id];
326 function sql_fetchfield($field, $row = -1, $query_id = 0)
330 $query_id = $this->query_result;
334 if($row < $this->result_numrows[$query_id])
338 $getrow = $this->row_index[$query_id]-1;
345 return $this->result_rowset[$query_id][$getrow][$this->result_field_names[$query_id][$field]];
358 function sql_rowseek($offset, $query_id = 0)
362 $query_id = $this->query_result;
366 $this->row_index[$query_id] = 0;
374 function sql_nextid($query_id = 0)
378 $query_id = $this->query_result;
382 return $this->next_id[$query_id];
389 function sql_freeresult($query_id = 0)
393 $query_id = $this->query_result;
397 $result = @odbc_free_result($query_id);
405 function sql_error($query_id = 0)
407 // $result['code'] = @odbc_error($this->db_connect_id);
408 // $result['message'] = @odbc_errormsg($this->db_connect_id);