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);
}
}
?>