dwh/class/ORAConnectionClass.php

508 lines
14 KiB
PHP
Raw Normal View History

2023-10-02 10:26:17 +00:00
<?php
class ORAConnectionClass {
public $conn = NULL;
private $user = '';
private $password = '';
private $tns = '';
public $conn_status = '';
public $debug = 0;
public $has_error= '';
public $last_error_msg = '';
public $last_exec_stmt = '';
public $execution_mode = OCI_COMMIT_ON_SUCCESS;
private $record_sep;
private $field_sep;
public $nrows = 0;
public function __construct ($file) {
if(file_exists($file)) {
require $file;
$this->tns = $ORA_TNS;
$this->user = $ORA_User;
$this->password = $ORA_Password;
} else {
$this->has_error = 1;
$this->last_error_msg = "The file $file do not exists.";
exit();
}
$this->execution_mode = $execution_mode;
$this->record_sep = chr(31);
$this->field_sep = chr(30);
# $this->record_sep = '#';
# $this->field_sep = '^';
}
public function set_debugger($mode) {
oci_internal_debug($mode);
}
public function set_auto_commit() {
$this->execution_mode = OCI_COMMIT_ON_SUCCESS;
}
public function set_no_auto_commit() {
$this->execution_mode = OCI_DEFAULT;
}
public function ORA_Connect () {
$result = false;
$this->conn = oci_connect($this->user, $this->password, $this->tns);
# $this->conn = oci_connect($this->user, $this->password, '//localhost/XE');
if (empty($this->conn)) {
$e = oci_error();
$this->last_error_msg = $e['message'];
$result = false;
} else {
$result = true;
}
$this->conn_status = $result;
return $result;
}
public function ORA_PConnect () {
$result = false;
$this->conn = oci_connect($this->user, $this->password, $this->tns);
if (empty($this->conn)) {
$e = oci_error();
$this->last_error_msg = $e['message'];
$result = false;
} else {
$result = true;
}
$this->conn_status = $result;
return $result;
}
public function ORA_Close () {
$status = false;
if ($this->conn) {
$status = oci_close($this->conn);
}
return $status;
} // end of private function DB2Close
function ORA_Commit()
{
$status = oci_commit($this->conn);
return $status;
}
function ORA_Rollback()
{
$status = oci_rollback($this->conn);
return $status;
}
function ORA_InsertData($stmt=null,$table_name=null)
{
if (is_null($stmt) && !is_null($table_name)) {
foreach ($_REQUEST as $key => $val) {
//str_replace(array('\'','\&#039;'),'&#039;',$val)
//$stmt_string_array[] = "{$key}".$this->field_sep."{$val}";
//$val = str_replace(array('\'','\&#039;'),'&#039;',$val);
$val = str_replace(array('\\\'','\&#039;'),'`',$val);
$val = str_replace(array('\\"'),'"',$val);
$stmt_string_array[] = "{$key}".$this->field_sep."{$val}";
}
if (!is_null($stmt_string_array)) {
$stmt_string=implode($this->record_sep,$stmt_string_array);
$stmt_string .= $this->record_sep;
$stmt_res = oci_parse($this->conn,"call sys_dml.Insert_Table('$table_name','$stmt_string')");
$result = oci_execute($stmt_res,$this->execution_mode);
if (!$result) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
$this->last_exec_stmt = $stmt_string;
#var_dump($e);
}
}
} elseif (!is_null($stmt)) {
$stmt_res = oci_parse($this->conn, $stmt);
$result = oci_execute($stmt_res, $this->execution_mode);
if (!$result) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
#var_dump($e);
}
} else {
$this->last_error_msg ="Both argument can not be null.";
$result = false;
}
if (!is_null($stmt_res)):oci_free_statement($stmt_res);endif;
return $result;
}
function ORA_DeleteData($stmt=null,$table_name=null)
{
if (is_null($stmt) && !is_null($table_name)) {
foreach ($_REQUEST as $key => $val) {
$stmt_string_array[] = "{$key}".$this->field_sep."{$val}";
}
if (!is_null($stmt_string_array)) {
$stmt_string=implode($this->record_sep,$stmt_string_array);
$stmt_string .= $this->record_sep;
$stmt_res = oci_parse($this->conn,"call sys_dml.Delete_Table('$table_name','$stmt_string')");
$result = oci_execute($stmt_res,$this->execution_mode);
if (!$result) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
#var_dump($e);
}
}
} elseif (!is_null($stmt)) {
$stmt = oci_parse($this->conn, $stmt);
$result = oci_execute($stmt, $this->execution_mode);
if (!$result) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
#var_dump($e);
}
} else {
$this->last_error_msg ="Both argument can not be null.";
$result = false;
}
if (!is_null($stmt_res)):oci_free_statement($stmt_res);endif;
return $result;
}
function ORA_UpdateData($stmt=null,$table_name=null)
{
if (is_null($stmt) && !is_null($table_name)) {
foreach ($_REQUEST as $key => $val) {
//$stmt_string_array[] = "{$key}".$this->field_sep."{$val}";
//$val = str_replace(array('\'','\&#039;'),'&#039;',$val);
$val = str_replace(array('\\\'','\&#039;'),'`',$val);
$val = str_replace(array('\\"'),'"',$val);
$stmt_string_array[] = "{$key}".$this->field_sep."{$val}";
}
if (!is_null($stmt_string_array)) {
$stmt_string=implode($this->record_sep,$stmt_string_array);
$stmt_string .= $this->record_sep;
$stmt_res = oci_parse($this->conn,"call sys_dml.Update_Table('$table_name','$stmt_string')");
$result = oci_execute($stmt_res,$this->execution_mode);
if (!$result) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
$this->last_exec_stmt = $stmt_string;
#var_dump($e);
}
}
} elseif (!is_null($stmt)) {
$stmt = oci_parse($this->conn, $stmt);
$result = oci_execute($stmt, $this->execution_mode);
if (!$result) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
#var_dump($e);
}
} else {
$this->last_error_msg ="Both argument can not be null.";
$result = false;
}
if (!is_null($stmt_res)):oci_free_statement($stmt_res);endif;
return $result;
}
function ORA_SelectData($stmt=null,$table_name=null,$where_clause=null,$select_method=OCI_FETCHSTATEMENT_BY_COLUMN)
{
if (!is_null($stmt)) {
$stmt_res = oci_parse($this->conn, $stmt);
if (!oci_execute($stmt_res, OCI_DEFAULT)) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
#var_dump($e);
} else {
$this->nrows = oci_fetch_all($stmt_res,$result,null,null,$select_method);
}
} elseif (!is_null($table_name)) {
$stmt = "select * from {$table_name} {$where_clause}";
$stmt_res = oci_parse($this->conn, $stmt);
if (!oci_execute($stmt_res, OCI_DEFAULT)) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
} else {
$this->nrows = oci_fetch_all($stmt,$result,null,null,$select_method);
}
} else {
$this->last_error_msg ="Either one of the argument can not be null.";
$result =null;
}
if (!is_null($stmt_res)):oci_free_statement($stmt_res);endif;
return $result;
}
function ORA_UpdateDataWithBind($table_name=null)
{
if (!is_null($table_name)) {
foreach ($_REQUEST as $key => $val) {
$stmt_string_array[] = "{$key}".$this->field_sep."{$val}";
}
if (!is_null($stmt_string_array)) {
$stmt_string=implode($this->record_sep,$stmt_string_array);
$stmt_string .= $this->record_sep;
$stmt = "begin sys_dml.Update_Table ('$table_name',:stmt_string); end;";
$stmt_res = oci_parse($this->conn,$stmt);
oci_bind_by_name($stmt_res, ':stmt_string',$stmt_string);
$result = oci_execute($stmt_res, $this->execution_mode);
if (!$result) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
$this->last_exec_stmt = $stmt_string;
#var_dump($e);
}
} else {
$this->last_error_msg ="There is nothing to update.";
$result = false;
}
} else {
$this->last_error_msg ="Table name can not be null";
$result = false;
}
if (!is_null($stmt_res)):oci_free_statement($stmt_res);endif;
return $result;
}
function ORA_InsertDataWithBind($table_name=null)
{
if (!is_null($table_name)) {
foreach ($_REQUEST as $key => $val) {
$stmt_string_array[] = "{$key}".$this->field_sep."{$val}";
}
if (!is_null($stmt_string_array)) {
$stmt_string=implode($this->record_sep,$stmt_string_array);
$stmt_string .= $this->record_sep;
$stmt = "begin sys_dml.Insert_Table ('$table_name',:stmt_string); end;";
$stmt_res = oci_parse($this->conn,$stmt);
oci_bind_by_name($stmt_res, ':stmt_string',$stmt_string);
$result = oci_execute($stmt_res, $this->execution_mode);
if (!$result) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
$this->last_exec_stmt = $stmt_string;
}
} else {
$this->last_error_msg ="There is nothing to add.";
$result = false;
}
} else {
$this->last_error_msg ="Table name can not be null.";
$result = false;
}
if (!is_null($stmt_res)):oci_free_statement($stmt_res);endif;
return $result;
}
function ORA_ProcedureCall($function_name,$num_ret_val,$str_ret_val)
{
$ret_var_string = '';
if (!is_null($function_name)) {
for ($i=0;$i<$num_ret_val;$i++) {
$ret_var_string_array[] = ":n{$i}";
}
if ($str_ret_val > 0) {
for ($i=0;$i<$str_ret_val;$i++) {
$ret_var_string_array[] = ":s{$i}";
}
}
if (!is_null($ret_var_string_array)){
$ret_var_string = ',';
$ret_var_string .= implode(',',$ret_var_string_array);
}
foreach ($_REQUEST as $key => $val) {
$stmt_string_array[] = "{$key}".$this->field_sep."{$val}";
}
if (!is_null($stmt_string_array)) {
$stmt_string=implode($this->record_sep,$stmt_string_array);
$stmt_string .= $this->record_sep;
}
$stmt = "begin {$function_name}('$stmt_string' $ret_var_string); end;";
$stmt_res = oci_parse($this->conn,$stmt);
for ($i=0;$i<$num_ret_val;$i++) {
oci_bind_by_name($stmt_res, ":n{$i}",$bind_result[$i], -1, SQLT_INT);
}
if ($str_ret_val > 0) {
$j=$i;
for ($i=0;$i<$str_ret_val;$i++) {
oci_bind_by_name($stmt_res, ":s{$i}",$bind_result[$j], 4000, SQLT_CHR);
$j++;
}
}
if (!oci_execute($stmt_res, $this->execution_mode)) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
}
}
if (!is_null($stmt_res)):oci_free_statement($stmt_res);endif;
return $bind_result;
}
function SetServerOutput($p) {
if ($p)
$s = "BEGIN DBMS_OUTPUT.ENABLE(NULL); END;";
else
$s = "BEGIN DBMS_OUTPUT.DISABLE(); END;";
$s = oci_parse($this->conn, $s);
$r = oci_execute($s);
if (!is_null($s)):oci_free_statement($s);endif;
// oci_free_statement($s);
return $r;
}
function SetDbmsOutput() {
$res = false;
$s = oci_parse($this->conn, "BEGIN DBMS_OUTPUT.GET_LINE(:LN, :ST); END;");
if (oci_bind_by_name($s, ":LN", $ln, 255) && oci_bind_by_name($s, ":ST", $st)) {
$res = array();
while (($succ = oci_execute($s)) && !$st)
$res[] = $ln;
if (!$succ)
$res = false;
}
if (!is_null($s)):oci_free_statement($s);endif;
// oci_free_statement($s);
return $res;
}
function GetDbmsOutput() {
$output = $this->SetDbmsOutput();
foreach ($output as $line)
echo "$line<br>";
}
function ORA_FunctionCall($function_name) {
if (!is_null($function_name)) {
$stmt = "begin :ret := {$function_name}; end;";
$stmt_res = oci_parse($this->conn, $stmt);
oci_bind_by_name($stmt_res, ":ret", $result, 32000);
if (!oci_execute($stmt_res, $this->execution_mode)) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
}
}
if (!is_null($stmt_res)):oci_free_statement($stmt_res);endif;
return $result;
}
function ORA_InsertBlob($tblName,$arrayCol=null,$arrayColNumber=null,$arrayBlob){
$totalCol = count($arrayCol);
$totalLob = count($arrayBlob);
$lobCounter = 1;
if (!is_null($arrayCol)) {
foreach ($arrayCol as $tkey => $tval) {
$col_line_array[] = $tkey;
$values_line_array[] = $tval;
$colBindNameAray[] = ":$tkey";
}
}
if (!is_null($arrayColNumber)) {
foreach ($arrayColNumber as $tkey => $tval) {
$col_line_array[] = $tkey;
$values_line_array[] = $tval;
$colBindNameAray[] = ":$tkey";
}
}
foreach ($arrayBlob as $lkey => $lval) {
$col_line_array[] = $lkey;
$blob_values_line_array[] = "EMPTY_BLOB()";
$ret_blob_line_array[] = $lkey;
$into_blob_line_array[] = ":$lkey";
${"lob_".$lobCounter} = oci_new_descriptor($this->conn,OCI_D_LOB);
$bindingLob[$lkey] = ${"lob_".$lobCounter};
$lobCounter++;
}
$values_line_array = array_merge($colBindNameAray,$blob_values_line_array);
$col_line = implode(',',$col_line_array);
$values_line = implode(',',$values_line_array);
$ret_blob_line = implode(',',$ret_blob_line_array);
$into_blob_line = implode(',',$into_blob_line_array);
$stmt = "INSERT INTO $tblName ($col_line) VALUES ($values_line) RETURNING $ret_blob_line INTO $into_blob_line";
$stmt_res = oci_parse($this->conn, $stmt);
//echo "$stmt<br>";
if (!is_null($arrayCol)) {
foreach ($arrayCol as $tkey => $tval) {
oci_bind_by_name($stmt_res,":$tkey",$tval,strlen($tval));
//echo "t = :$tkey,$tval,". strlen($tval) . "<br>";
}
}
if (!is_null($arrayColNumber)) {
foreach ($arrayColNumber as $nkey => $nval) {
oci_bind_by_name($stmt_res,":$nkey",$nval);
//echo "n = :$nkey,$nval<br>";
}
}
foreach ($arrayBlob as $lkey => $lval) {
oci_bind_by_name($stmt_res,":$lkey",$bindingLob[$lkey],-1,OCI_B_BLOB);
}
$result = oci_execute($stmt_res, $this->execution_mode);
if (!$result) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
} else {
$lobCounter = 1;
foreach ($arrayBlob as $lkey => $lval) {
${"lob_".$lobCounter}->save($lval);
$lobCounter++;
}
}
if (!is_null($stmt_res)):oci_free_statement($stmt_res);endif;
return $result;
}
function checkProcedure ($pkgName, $procName, $owner) {
$stmt = <<< sql
SELECT 1 as NUM_PROC
FROM all_procedures
WHERE owner = '{$owner}'
AND object_name = '{$pkgName}'
AND procedure_name = '{$procName}'
sql;
$numProc = 0;
$stmt_res = oci_parse($this->conn, $stmt);
if (!oci_execute($stmt_res, OCI_DEFAULT)) {
$e = oci_error($stmt_res);
$this->last_error_msg = split("\n",$e['message']);
} else {
$this->nrows = oci_fetch_all($stmt_res,$result,null,null,$select_method);
if ($this->nrows >0) {
$numProc = 1;
}
}
return $numProc;
}
}
?>