2 /***************************************************************************
5 * begin : Thrusday Feb 15, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: oracle.php,v 1.18.2.1 2002/11/26 11:42:12 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","oracle");
32 var $in_transaction = 0;
34 var $rowset = array();
36 var $last_query_text = "";
41 function sql_db($sqlserver, $sqluser, $sqlpassword, $database="", $persistency = true)
43 $this->persistency = $persistency;
44 $this->user = $sqluser;
45 $this->password = $sqlpassword;
46 $this->server = $sqlserver;
47 $this->dbname = $database;
49 if($this->persistency)
51 $this->db_connect_id = @OCIPLogon($this->user, $this->password, $this->server);
55 $this->db_connect_id = @OCINLogon($this->user, $this->password, $this->server);
57 if($this->db_connect_id)
59 return $this->db_connect_id;
72 if($this->db_connect_id)
74 // Commit outstanding transactions
75 if($this->in_transaction)
77 OCICommit($this->db_connect_id);
80 if($this->query_result)
82 @OCIFreeStatement($this->query_result);
84 $result = @OCILogoff($this->db_connect_id);
96 function sql_query($query = "", $transaction = FALSE)
98 // Remove any pre-existing queries
99 unset($this->query_result);
101 // Put us in transaction mode because with Oracle as soon as you make a query you're in a transaction
102 $this->in_transaction = TRUE;
106 $this->last_query = $query;
107 $this->num_queries++;
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];
126 if(eregi("^(INSERT|UPDATE) ", $query))
128 $query = preg_replace("/\\\'/s", "''", $query);
131 $this->query_result = @OCIParse($this->db_connect_id, $query);
132 $success = @OCIExecute($this->query_result, OCI_DEFAULT);
136 if($transaction == END_TRANSACTION)
138 OCICommit($this->db_connect_id);
139 $this->in_transaction = FALSE;
142 unset($this->row[$this->query_result]);
143 unset($this->rowset[$this->query_result]);
144 $this->last_query_text[$this->query_result] = $query;
146 return $this->query_result;
150 if($this->in_transaction)
152 OCIRollback($this->db_connect_id);
159 // Other query methods
161 function sql_numrows($query_id = 0)
165 $query_id = $this->query_result;
169 $result = @OCIFetchStatement($query_id, $this->rowset);
170 // OCIFetchStatment kills our query result so we have to execute the statment again
171 // if we ever want to use the query_id again.
172 @OCIExecute($query_id, OCI_DEFAULT);
180 function sql_affectedrows($query_id = 0)
184 $query_id = $this->query_result;
188 $result = @OCIRowCount($query_id);
196 function sql_numfields($query_id = 0)
200 $query_id = $this->query_result;
204 $result = @OCINumCols($query_id);
212 function sql_fieldname($offset, $query_id = 0)
214 // OCIColumnName uses a 1 based array so we have to up the offset by 1 in here to maintain
215 // full abstraction compatibitly
219 $query_id = $this->query_result;
223 $result = strtolower(@OCIColumnName($query_id, $offset));
231 function sql_fieldtype($offset, $query_id = 0)
233 // This situation is the same as fieldname
237 $query_id = $this->query_result;
241 $result = @OCIColumnType($query_id, $offset);
249 function sql_fetchrow($query_id = 0, $debug = FALSE)
253 $query_id = $this->query_result;
258 $result = @OCIFetchInto($query_id, $result_row, OCI_ASSOC+OCI_RETURN_NULLS);
261 echo "Query was: ".$this->last_query . "<br>";
262 echo "Result: $result<br>";
263 echo "Query ID: $query_id<br>";
265 var_dump($result_row);
268 if($result_row == "")
273 for($i = 0; $i < count($result_row); $i++)
275 list($key, $val) = each($result_row);
276 $return_arr[strtolower($key)] = $val;
278 $this->row[$query_id] = $return_arr;
280 return $this->row[$query_id];
287 // This function probably isn't as efficant is it could be but any other way I do it
288 // I end up losing 1 row...
289 function sql_fetchrowset($query_id = 0)
293 $query_id = $this->query_result;
297 $rows = @OCIFetchStatement($query_id, $results);
298 @OCIExecute($query_id, OCI_DEFAULT);
299 for($i = 0; $i < $rows; $i++)
301 @OCIFetchInto($query_id, $tmp_result, OCI_ASSOC+OCI_RETURN_NULLS);
303 for($j = 0; $j < count($tmp_result); $j++)
305 list($key, $val) = each($tmp_result);
306 $return_arr[strtolower($key)] = $val;
308 $result[] = $return_arr;
317 function sql_fetchfield($field, $rownum = -1, $query_id = 0)
321 $query_id = $this->query_result;
327 // Reset the internal rownum pointer.
328 @OCIExecute($query_id, OCI_DEFAULT);
329 for($i = 0; $i < $rownum; $i++)
331 // Move the interal pointer to the row we want
332 @OCIFetch($query_id);
334 // Get the field data.
335 $result = @OCIResult($query_id, strtoupper($field));
339 // The internal pointer should be where we want it
340 // so we just grab the field out of the current row.
341 $result = @OCIResult($query_id, strtoupper($field));
350 function sql_rowseek($rownum, $query_id = 0)
354 $query_id = $this->query_result;
358 @OCIExecute($query_id, OCI_DEFAULT);
359 for($i = 0; $i < $rownum; $i++)
361 @OCIFetch($query_id);
363 $result = @OCIFetch($query_id);
371 function sql_nextid($query_id = 0)
375 $query_id = $this->query_result;
377 if($query_id && $this->last_query_text[$query_id] != "")
379 if( eregi("^(INSERT{1}|^INSERT INTO{1})[[:space:]][\"]?([a-zA-Z0-9\_\-]+)[\"]?", $this->last_query_text[$query_id], $tablename))
381 $query = "SELECT ".$tablename[2]."_id_seq.currval FROM DUAL";
382 $stmt = @OCIParse($this->db_connect_id, $query);
383 @OCIExecute($stmt,OCI_DEFAULT );
384 $temp_result = @OCIFetchInto($stmt, $temp_result, OCI_ASSOC+OCI_RETURN_NULLS);
387 return $temp_result['CURRVAL'];
405 function sql_nextid($query_id = 0)
409 $query_id = $this->query_result;
411 if($query_id && $this->last_query_text[$query_id] != "")
413 if( eregi("^(INSERT{1}|^INSERT INTO{1})[[:space:]][\"]?([a-zA-Z0-9\_\-]+)[\"]?", $this->last_query_text[$query_id], $tablename))
415 $query = "SELECT ".$tablename[2]."_id_seq.CURRVAL FROM DUAL";
416 $temp_q_id = @OCIParse($this->db_connect_id, $query);
417 @OCIExecute($temp_q_id, OCI_DEFAULT);
418 @OCIFetchInto($temp_q_id, $temp_result, OCI_ASSOC+OCI_RETURN_NULLS);
422 return $temp_result['CURRVAL'];
442 function sql_freeresult($query_id = 0)
446 $query_id = $this->query_result;
450 $result = @OCIFreeStatement($query_id);
458 function sql_error($query_id = 0)
462 $query_id = $this->query_result;
464 $result = @OCIError($query_id);