/*
* $Id: dbi_oracle.php,v 1.7 2001/04/03 14:48:31 cfsl Exp $
*
* This class is based on the Perl DBI.
* New functionality should be added accordingly.
* Please refer to the DBI documentation (perldoc DBI).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
class DBI {
var $session;
var $mode = OCI_COMMIT_ON_SUCCESS;
var $debug = 1;
var $Driver_name = "oracle";
function DBI ($db, $user, $password, $debug = 0) {
$this->session = OCIPLogon($user, $password, $db);
$this->debug = $debug;
}
function prepare ($query) {
return new STH($this, $this->session, $query, $this->mode, $this->debug);
}
function autocommit ($value) {
if ($value) {
$this->mode = OCI_COMMIT_ON_SUCCESS;
} else {
$this->mode = OCI_DEFAULT;
}
}
function commit () {
return OCICommit($this->session);
}
function rollback () {
return OCIRollback($this->session);
}
function quote ($str) {
if (get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return "'".str_replace ("'", "''", $str)."'";
}
function insert_id ($sequence) {
$sth = new STH($this, $this->session, "SELECT $sequence.CURRVAL FROM DUAL", $this->mode, $this->debug);
$sth->execute();
list($res) = $sth->fetchrow_array();
return $res;
}
}
class STH {
var $query;
var $statement;
var $mode;
var $debug;
var $dbi;
var $placeholders;
function STH (&$dbi, &$session, $query, $mode, $debug) {
$this->dbi = &$dbi;
$this->query = $query;
$this->mode = $mode;
$this->debug = $debug;
$this->session = &$session;
// Scan for placeholders
$this->placeholders = array();
$quote = '';
for ($i = 0; $i < strlen($query); ++$i) {
if ($query[$i] == "'") {
if (empty($quote)) {
$quote = "'";
} elseif ($quote == "'") {
$quote = '';
}
} elseif ($query[$i] == '"') {
if (empty($quote)) {
$quote = '"';
} elseif ($quote == '"') {
$quote = '';
}
} elseif ($query[$i] == '?') {
if (empty($quote)) {
array_push($this->placeholders, $i);
}
}
}
}
function execute () {
global $SERVER_NAME;
$numargs = func_num_args();
$arg_list = func_get_args();
$parms = array();
for ($i = 0; $i < $numargs; $i++) {
if (is_array($arg_list[$i])) {
while (list($dummy,$parm) = each ($arg_list[$i])) {
array_push($parms, $parm);
}
} else {
array_push($parms,$arg_list[$i]);
}
}
if (sizeof($parms) != sizeof($this->placeholders)) {
print "
SQL Query ($this->query) contains ".sizeof($this->placeholders)." placeholders but ".sizeof($parms)." was passed
";
exit;
}
if (sizeof($parms) > 0) {
$query = substr($this->query, 0, $this->placeholders[0]);
for ($i = 0; $i < sizeof($parms) - 1; ++$i) {
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1, $this->placeholders[$i + 1] - $this->placeholders[$i] - 1);
}
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1);
} else {
$query = $this->query;
}
if ($this->debug) { // Log the query
$fd = fopen("/tmp/dbi.$SERVER_NAME.log", "a") or die ("Couldn't append to file");
fputs($fd, date("M d H:i:s",time()).": ".$query."\n==================\n");
fclose($fd);
}
$this->statement = OCIParse($this->session, $query);
if (!$this->statement) {
print "
Could not parse SQL query: \"$query\"
";
exit;
}
if (!OCIExecute($this->statement, $this->mode)) {
print "
Could not execute SQL query: \"".$query."\"
";
exit;
}
}
function fetchrow_array () {
$res = array();
OCIFetchInto($this->statement, $res);
return $res;
}
function fetchrow_hash () {
$res = array();
OCIFetchInto($this->statement, $res, OCI_ASSOC);
return $res;
}
function rows () {
return OCIRowCount ($this->statement);
}
function finish () {
OCIFreeStatement($this->statement);
}
}
?>