分享一个PHP ORACLE 类

2016-07-07 15:00 来源:www.chinab4c.com 作者:ecshop专家

<?php

if (!defined('IN_ECS'))
{
die('Hacking attempt');
}
class DB_OracleOCI
{
public $Debug = 0;
public $sqoe = 1;
public $DBDatabase =
"(DESCRIPTION=(ADDRESS=(PROTOCOL =TCP)(HOST=192.168.1.100)(PORT = 1521))(CONNECT_DATA =(SID=GZCMCC)))";
public $DBUser = "GMCCLFDH";
public $DBPassword = "GMCCLFDH";
public $Persistent = false;
public $Uppercase = false;
public $Record = array();
public $Row;
public $Binds = array();
public $Link_ID = 0;
public $Query_ID = 0;
public $Connected = false;

var $queryCount = 0;
var $queryTime= '';
var $queryLog= array();
public $Encoding = "UTF8";
public $Error = "";

function DB_Sql($query = "")
{
}
function try_connect()
{
$this->Query_ID = 0;
if ($this->Persistent)
$this->Link_ID = @OCIplogon("$this->DBUser", "$this->DBPassword", "$this->DBDatabase");
else
$this->Link_ID = @OCIlogon("$this->DBUser", "$this->DBPassword", "$this->DBDatabase");
$this->Connected = $this->Link_ID ? true : false;
return $this->Connected;
}
function connect()
{
if (!$this->Connected)
{
$this->Query_ID = 0;
if ($this->Debug)
{
printf("<br>Connecting to $this->DBDatabase...<br> ");
}
if ($this->Persistent)
$this->Link_ID = OCIplogon("$this->DBUser", "$this->DBPassword", "$this->DBDatabase",
$this->Encoding);
else
$this->Link_ID = OCIlogon("$this->DBUser", "$this->DBPassword", "$this->DBDatabase",
$this->Encoding);

if (!$this->Link_ID)
{
$this->Error = OCIError(!$this->Link_ID);
$this->Halt("Cannot connect to Database: " . $this->Error["message"]);
return 0;
}
if ($this->Debug)
{
printf("<br>Obtained the Link_ID: $this->Link_ID<br> ");
}
$this->Connected = true;
}
}
function bind($parameter_name, $parameter_value, $parameter_length = -1, $parameter_type =
0)
{
if ($parameter_length == -1 && $parameter_type == 0)
$parameter_length = strlen($parameter_value);
$this->Binds[$parameter_name] = array($parameter_value, $parameter_length, $parameter_type);
}
function query($Query_String)
{

if ($Query_String == "")
return 0;

$this->connect();
$this->Query_ID = OCIParse($this->Link_ID, $Query_String);
if (!$this->Query_ID)
{
$this->Error = OCIError($this->Query_ID);
}
else
{
if (sizeof($this->Binds) > 0)
{
foreach ($this->Binds as $parameter_name => $parameter_values)
{
if ($parameter_values[2] == OCI_B_CURSOR)
$this->Binds[$parameter_name][0] = OCINewCursor($this->Link_ID);
if ($parameter_values[2] == 0)
OCIBindByName($this->Query_ID, ":" . $parameter_name, $this->Binds[$parameter_name][0],
$parameter_values[1]);
else
OCIBindByName($this->Query_ID, ":" . $parameter_name, $this->Binds[$parameter_name][0],
$parameter_values[1], $parameter_values[2]);
}
}
OCIExecute($this->Query_ID);
$this->Error = OCIError($this->Query_ID);
}
$this->Row = 0;
if ($this->Debug)
{
printf("Debug: query = %s<br> ", $Query_String);
}
if ($this->Error["code"] != 1403 && $this->Error["code"] != 0 && $this->sqoe)
echo "<BR><FONT color=red><B>" . $this->Error["message"] . "<BR>Query :\"$Query_String\"</B></FONT>";

if (sizeof($this->Binds) > 0)
{
$bi = 0;
foreach ($this->Binds as $parameter_name => $parameter_values)
{
if ($parameter_values[2] == OCI_B_CURSOR)
{
OCIExecute($this->Binds[$parameter_name][0]);
$this->Error = OCIError($this->Binds[$parameter_name][0]);
$this->Query_ID = $this->Binds[$parameter_name][0];
}
$this->Record[$parameter_name] = $parameter_values[0];
$this->Record[$bi++] = $parameter_values[0];
}
$this->Binds = array();
}


if ($this->queryCount++ <= 99)
{
$this->queryLog[] = $Query_String;
}
if ($this->queryTime == '')
{
if (PHP_VERSION >= '5.0.0')
{
$this->queryTime = microtime(true);
}
else
{
$this->queryTime = microtime();
}
}



return $this->Query_ID;

}
function next_record()
{
if (!$this->Query_ID)
return 0;
if (0 == @OCIFetchInto($this->Query_ID, $result, OCI_ASSOC + OCI_RETURN_NULLS))
{
if ($this->Debug)
{
printf("<br>ID: %d,Rows: %d<br> ", $this->Link_ID, $this->num_rows());
}
$this->Row += 1;
$errno = OCIError($this->Query_ID);
if (1403 == $errno)
{ # 1043 means no more records found
$this->Error = "";
$this->disconnect();
$stat = 0;
} else
{
$this->Error = OCIError($this->Query_ID);
if ($this->Debug)
{
printf("<br>Error: %s", $this->Error["message"]);
}
$stat = 0;
}
} else
{
for ($ix = 1; $ix <= OCINumcols($this->Query_ID); $ix++)
{
$col = OCIColumnname($this->Query_ID, $ix);
$colreturn = $col;
$this->Record["$colreturn"] = $result["$col"];
$this->Record[$ix - 1] = $result["$col"];
if ($this->Debug)
echo "<b>[$col]</b>:" . $result["$col"] . "<br> ";
}
$stat = 1;
}
return $stat;
}
function seek($pos)
{
$i = 0;
while ($i < $pos && @OCIFetchInto($this->Query_ID, $result, OCI_ASSOC +
OCI_RETURN_NULLS))
{
$i++;
}
$this->Row += $i;
return true;
}
function metadata($table, $full = false)
{
$count = 0;
$id = 0;
$res = array();

$this->connect();

$this->query("SELECT T.table_name,T.column_name,T.data_type," .
"T.data_length,T.data_precision,T.data_scale,T.nullable," .
"T.char_col_decl_length,I.index_name" .
" FROM ALL_TAB_COLUMNS T,ALL_IND_COLUMNS I" .
" WHERE T.column_name=I.column_name (+)" . " AND T.table_name=I.table_name (+)" .
" AND T.table_name=UPPER('$table') ORDER BY T.column_id");
$i = 0;
while ($this->next_record())
{
$res[$i]["table"] = $this->Record[table_name];
$res[$i]["name"] = strtolower($this->Record[column_name]);
$res[$i]["type"] = $this->Record[data_type];
$res[$i]["len"] = $this->Record[data_length];
if ($this->Record[index_name])
$res[$i]["flags"] = "INDEX ";
$res[$i]["flags"] .= ($this->Record[nullable] == 'N') ? '' : 'NOT NULL';
$res[$i]["format"] = (int)$this->Record[data_precision] . "," . (int)$this->
Record[data_scale];
if ("0,0" == $res[$i]["format"])
$res[$i]["format"] = '';
$res[$i]["index"] = $this->Record[index_name];
$res[$i]["chars"] = $this->Record[char_col_decl_length];
if ($full)
{
$j = $res[$i]["name"];
$res["meta"][$j] = $i;
$res["meta"][strtoupper($j)] = $i;
}
if ($full)
$res["meta"][$res[$i]["name"]] = $i;
$i++;
}
if ($full)
$res["num_fields"] = $i;
#$this->disconnect();
return $res;
}

function affected_rows()
{
return $this->num_rows();
}
function num_rows()
{
return OCIrowcount($this->Query_ID);
}
function num_fields()
{
return OCINumcols($this->Query_ID);
}
function nf()
{
return $this->num_rows();
}
function np()
{
print $this->num_rows();
}
function f($Name)
{
if ($this->Uppercase)
$Name = strtoupper($Name);
if (array_key_exists($Name, $this->Record) && is_object($this->Record[$Name]))
{
return $this->Record[$Name]->load();
} else
{
return $this->Record && array_key_exists($Name, $this->Record) ? $this->Record[$Name] :"";
}
}
function p($Name)
{
if ($this->Uppercase)
$Name = strtoupper($Name);
print $this->f($Name);
}

回答:
可以把ECSHOP 转为ORACLE 经过我的潜心研究 终于转成功了

php这样成熟的 数据库类库多得是

最简单的 就是 :ADOdb

直接支持 :Access, MySQL, PostgreSQL, MS SQL, Oracle


我们也准备把ecshop的数据库转到oracle,能提供下思路吗?谢谢