5 * @version $Id: oracle.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","oracle");
20 * @package dbal_oracle
21 * Oracle Database Abstraction Layer
28 var $in_transaction = 0;
30 var $rowset = array();
32 var $last_query_text = "";
37 function sql_db($sqlserver, $sqluser, $sqlpassword, $database="", $persistency = true)
39 $this->persistency = $persistency;
40 $this->user = $sqluser;
41 $this->password = $sqlpassword;
42 $this->server = $sqlserver;
43 $this->dbname = $database;
45 if($this->persistency)
47 $this->db_connect_id = @OCIPLogon($this->user, $this->password, $this->server);
51 $this->db_connect_id = @OCINLogon($this->user, $this->password, $this->server);
53 if($this->db_connect_id)
55 return $this->db_connect_id;
68 if($this->db_connect_id)
70 // Commit outstanding transactions
71 if($this->in_transaction)
73 OCICommit($this->db_connect_id);
76 if($this->query_result)
78 @OCIFreeStatement($this->query_result);
80 $result = @OCILogoff($this->db_connect_id);
92 function sql_query($query = "", $transaction = FALSE)
94 // Remove any pre-existing queries
95 unset($this->query_result);
97 // Put us in transaction mode because with Oracle as soon as you make a query you're in a transaction
98 $this->in_transaction = TRUE;
102 $this->last_query = $query;
103 $this->num_queries++;
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];
122 if(eregi("^(INSERT|UPDATE) ", $query))
124 $query = preg_replace("/\\\'/s", "''", $query);
127 $this->query_result = @OCIParse($this->db_connect_id, $query);
128 $success = @OCIExecute($this->query_result, OCI_DEFAULT);
132 if($transaction == END_TRANSACTION)
134 OCICommit($this->db_connect_id);
135 $this->in_transaction = FALSE;
138 unset($this->row[$this->query_result]);
139 unset($this->rowset[$this->query_result]);
140 $this->last_query_text[$this->query_result] = $query;
142 return $this->query_result;
146 if($this->in_transaction)
148 OCIRollback($this->db_connect_id);
155 // Other query methods
157 function sql_numrows($query_id = 0)
161 $query_id = $this->query_result;
165 $result = @OCIFetchStatement($query_id, $this->rowset);
166 // OCIFetchStatment kills our query result so we have to execute the statment again
167 // if we ever want to use the query_id again.
168 @OCIExecute($query_id, OCI_DEFAULT);
176 function sql_affectedrows($query_id = 0)
180 $query_id = $this->query_result;
184 $result = @OCIRowCount($query_id);
192 function sql_numfields($query_id = 0)
196 $query_id = $this->query_result;
200 $result = @OCINumCols($query_id);
208 function sql_fieldname($offset, $query_id = 0)
210 // OCIColumnName uses a 1 based array so we have to up the offset by 1 in here to maintain
211 // full abstraction compatibitly
215 $query_id = $this->query_result;
219 $result = strtolower(@OCIColumnName($query_id, $offset));
227 function sql_fieldtype($offset, $query_id = 0)
229 // This situation is the same as fieldname
233 $query_id = $this->query_result;
237 $result = @OCIColumnType($query_id, $offset);
245 function sql_fetchrow($query_id = 0, $debug = FALSE)
249 $query_id = $this->query_result;
254 $result = @OCIFetchInto($query_id, $result_row, OCI_ASSOC+OCI_RETURN_NULLS);
257 echo "Query was: ".$this->last_query . "<br>";
258 echo "Result: $result<br>";
259 echo "Query ID: $query_id<br>";
261 var_dump($result_row);
264 if($result_row == "")
269 for($i = 0; $i < count($result_row); $i++)
271 list($key, $val) = each($result_row);
272 $return_arr[strtolower($key)] = $val;
274 $this->row[$query_id] = $return_arr;
276 return $this->row[$query_id];
283 // This function probably isn't as efficant is it could be but any other way I do it
284 // I end up losing 1 row...
285 function sql_fetchrowset($query_id = 0)
289 $query_id = $this->query_result;
293 $rows = @OCIFetchStatement($query_id, $results);
294 @OCIExecute($query_id, OCI_DEFAULT);
295 for($i = 0; $i <= $rows; $i++)
297 @OCIFetchInto($query_id, $tmp_result, OCI_ASSOC+OCI_RETURN_NULLS);
299 for($j = 0; $j < count($tmp_result); $j++)
301 list($key, $val) = each($tmp_result);
302 $return_arr[strtolower($key)] = $val;
304 $result[] = $return_arr;
313 function sql_fetchfield($field, $rownum = -1, $query_id = 0)
317 $query_id = $this->query_result;
323 // Reset the internal rownum pointer.
324 @OCIExecute($query_id, OCI_DEFAULT);
325 for($i = 0; $i < $rownum; $i++)
327 // Move the interal pointer to the row we want
328 @OCIFetch($query_id);
330 // Get the field data.
331 $result = @OCIResult($query_id, strtoupper($field));
335 // The internal pointer should be where we want it
336 // so we just grab the field out of the current row.
337 $result = @OCIResult($query_id, strtoupper($field));
346 function sql_rowseek($rownum, $query_id = 0)
350 $query_id = $this->query_result;
354 @OCIExecute($query_id, OCI_DEFAULT);
355 for($i = 0; $i < $rownum; $i++)
357 @OCIFetch($query_id);
359 $result = @OCIFetch($query_id);
367 function sql_nextid($query_id = 0)
371 $query_id = $this->query_result;
373 if($query_id && $this->last_query_text[$query_id] != "")
375 if( eregi("^(INSERT{1}|^INSERT INTO{1})[[:space:]][\"]?([a-zA-Z0-9\_\-]+)[\"]?", $this->last_query_text[$query_id], $tablename))
377 $query = "SELECT ".$tablename[2]."_id_seq.currval FROM DUAL";
378 $stmt = @OCIParse($this->db_connect_id, $query);
379 @OCIExecute($stmt,OCI_DEFAULT );
380 $temp_result = @OCIFetchInto($stmt, $temp_result, OCI_ASSOC+OCI_RETURN_NULLS);
383 return $temp_result['CURRVAL'];
401 function sql_nextid($query_id = 0)
405 $query_id = $this->query_result;
407 if($query_id && $this->last_query_text[$query_id] != "")
409 if( eregi("^(INSERT{1}|^INSERT INTO{1})[[:space:]][\"]?([a-zA-Z0-9\_\-]+)[\"]?", $this->last_query_text[$query_id], $tablename))
411 $query = "SELECT ".$tablename[2]."_id_seq.CURRVAL FROM DUAL";
412 $temp_q_id = @OCIParse($this->db_connect_id, $query);
413 @OCIExecute($temp_q_id, OCI_DEFAULT);
414 @OCIFetchInto($temp_q_id, $temp_result, OCI_ASSOC+OCI_RETURN_NULLS);
418 return $temp_result['CURRVAL'];
438 function sql_freeresult($query_id = 0)
442 $query_id = $this->query_result;
446 $result = @OCIFreeStatement($query_id);
454 function sql_error($query_id = 0)
458 $query_id = $this->query_result;
460 $result = @OCIError($query_id);