<? ################################
# Oracle Database Abastract Layer. v2.1
# Feranndo Labombarda - fllabombarda@hotmail.com
################################
# - EXAMPLE -
# $db = new database();
# $rs = $db->Select("SELECT COL FROM TB_SECURITY");
# while ($db->Fetch($rs)) {
# $value = $db->Result($rs,"COL");
# }
#
################################ class database {
var $DB_Name; // banco.
var $DB_User; // usuario.
var $DB_Pass; // senha.
var $DB_ID; // conection_id.
var $query; // Guarda query no m�todo prepare
var $result; // RESULT
var $dbh; // Handle do banco
var $erro;
function database() {
$this->dbh = $this->Connect();
}
/**
* @return void
* @desc Destructor, when finish shutdown connection
*/
function _database() {
$this->DBClose();
}
/**
* @return Connection
* @desc Start DB Connection
*/
function Connect() {
$this->DB_Name = "";
$this->DB_User = "portaleds";
$this->DB_Pass = "************";
$this->erro = "Error opening Database.";
if (!$this->dbh) {
return $this->dbh = @OCILogon( $this->DB_User, $this->DB_Pass, $this->DB_Name);
} else {
return $this->dbh;
}
}
/**
* @return null
* @param String $query
* @desc Log SQL commands into default error log file EX: Apache, "error_log"
*/
function logSQL($sql){
$fp = fopen("/tmp/sql.log","a+");
flock($fp, LOCK_EX);
fwrite($fp,$sql.";n");
fflush($fp);
fclose($fp);
}
/**
* @return resultset
* @param String $query, connection $dbh
* @desc Execute a SELECT querys
*/
function Select($query) {
$result = OCIParse($this->dbh, $query);
$this->logSQL($query);
if (!OCIExecute($result))
error_log($_SERVER["PHP_SELF"]." S -> ".$query."n",0);
return $result;
}
/**
* @return int rowcount
* @param String $query
* @desc Execute DML query into Database
*/
function Dml($query) {
$rs = @OCIParse($this->dbh, $query);
if (!OCIExecute($rs))
error_log($_SERVER["PHP_SELF"]." D -> ".$query,0);
@OCICommit($this->dbh);
$affected = @OCIRowcount($rs);
$this->freeStatement($rs);
return $affected;
}
/**
* @return boolean
* @paramr resultset $rs
* @desc Close cursor, free resources
*/
function freeStatement($rs) {
return ocifreestatement($rs);
}
/**
* @return row
* @param resultset $result
* @desc Fetch ResultSet
*/
function Fetch($result) {
$row=null;
OCIFetchinto($result, $row);
return $row;
}
function NumRow($result, $garbage) {
$this->number = OCIFetchStatement($result, $garbage);
return $this->number;
}
function Result($result, $CAMPO) {
$this->result = @OCIResult($result, $CAMPO);
return $this->result;
}
function NumCol($result) {
$this->number = @OCINumCols($result);
return $this->number;
}
/**
* @return true
* @desc Commit connection
*/
function Commit() {
@OCICommit($this->dbh);
return true;
}
/**
* @return true
* @desc Close DB Connection
*/
function DBClose() {
@OCILogoff ();
return true ;
}
function ServerVersion(){
return OCIServerVersion($this->dbh);
}
}
|
|