|
Listing 1: Complete Wrapper Library
<?php
// DB_Oracle.inc
class DB_Oracle {
var $user;
var $pass;
var $tnsname;
var $dbh;
function DB_Oracle($user, $pass, $tnsname)
{
$this->user = $user;
$this->pass = $pass;
$this->tnsname = $tnsname;
}
function connect($dedicated=false) {
if($dedicated) {
$this->dbh = OCINLogin($this->user, $this->pass, $this->tnsname);
} else {
$this->dbh = OCIPLogin($this->user, $this->pass, $this->tnsname);
}
if(!is_resource($this->dbh)) {
$this->error();
return false;
}
return true;
}
function execute($query) {
if(!$this->dbh) {
if(!$this->connect()) {
return false;
}
}
$stmt = OCIParse($query);
if($stmt) {
$this->error("Parse failed");
return false;
}
$os = new DB_OracleStatement($stmt, $query);
if(!$os->execute()) {
return false;
}
return $os;
}
function prepare($query) {
if(!$this->dbh) {
if(!$this->connect()) {
return false;
}
}
$stmt = OCIParse($query);
if($stmt) {
$this->error("Parse failed");
return false;
}
return new DB_OracleStatement($stmt, $query);
}
function commit()
{
OCICommit($this->dbh);
}
function rollback()
{
OCIRollback($this->dbh);
}
function error($text)
{
$oci_error = OCIError();
$message = "$text ($oci_error) at: ".
print_r(debug_backtrace(), true);
trigger_error($message, E_USER_ERROR);
}
}
class DB_OracleStatement {
var $result;
var $binds;
var $query;
var $dbh;
var $preFetch = 1000;
function DB_OracleStatement($stmt, $query) {
$this->query = $query;
$this->stmt = $stmt;
if(!is_resource($stmt)) {
$this->error("DB_OracleStatement requires a valid statement handle");
return false;
}
}
function bindParam($ph, $pv) {
OCIBindByName($this->stmt, $ph, $pv);
}
function execute() {
$binds = func_get_args();
foreach($binds as $ph => $pv) {
$this->bindParam($ph, $pv);
}
OCISetPreFetch($this->stmt, $this->preFetch);
if(!OCIExecute($this->stmt)) {
$this->error("Execute failed");
return false;
}
}
function fetch() {
if($this->executed) {
$this->error("Attempted to fetch unexecuted cursor");[
return false;
}
$mode = OCI_ASSOC + OCI_RETURN_NULLS + OCI_RETURN_LOBS;
ocifetchinto($this->stmt, $row, $mode);
return $row;
}
function fetchall() {
$mode = OCI_FETCHSTATEMENT_BY_COLUMN + OCI_ASSOC;
OCIFetchStatement($this->stmt, $results);
return $results;
}
function finish() {
OCIFreeStatement($this->stmt);
$this->executed = 0;
$this->stmt = false;
}
function error($text)
{
$oci_error = OCIError();
$message = "$text ($oci_error) at: ".
print_r(debug_backtrace(), true);
trigger_error($message, E_USER_ERROR);
}
}
?>
|