打印本文 打印本文  关闭窗口 关闭窗口
oracle资料库函式库
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 14:29:31
<?
class DB_Sql {
var $Debug = false;
var $Home = "/u01/app/oracle/product/8.0.4";
var $Remote = 1;
/* This Query will be sent directly after the first connection
Example:
var $ConnectQuery="ALTER SESSION SET nls_date_language=german nls_date_format='DD.MM.RRRR'";
-> Set the date format for this session, this is fine when your ora-role
cannot be altered */
var $ConnectQuery=';
/* Due to a strange error with Oracle 8.0.5, Apache and PHP3.0.6
you don't need to set the ENV - on my system Apache
will change to a zombie, if I don't set this to FALSE!
Instead I set these ENV-vars before the startup of apache.
If unsure try it out, if it works. */
var $OraPutEnv = true;

var $Database = "";
var $User = "";
var $Password = "";

var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row;

var $Errno = 0;
var $Error = "";
var $ora_no_next_fetch=false;


/* copied from db_mysql for completeness */
/* public: identification constant. never change this. */
var $type = "oracle";
var $revision = "Revision: 1.3";
var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)

/* public: constructor */
function DB_Sql($query = "") {
$this->query($query);
}

/* public: some trivial reporting */
function link_id() {
return $this->Link_ID;
}

function query_id() {
return $this->Query_ID;
}

function connect() {
## see above why we do this
if ($this->OraPutEnv) {
PutEnv("ORACLE_SID=$this->Database");
PutEnv("ORACLE_HOME=$this->Home");
}
if ( 0 == $this->Link_ID ) {
if($this->Debug) {
printf("<br>Connect()ing to $this->Database...<br>n");
}
if($this->Remote) {
if($this->Debug) {
printf("<br>connect() $this->User/******@$this->Database<br>n");
}  
$this->Link_ID=ora_plogon
("$this->User/$this->Password@$this->Database","");
/************** (comment by SSilk)
this dosn't work on my system:
$this->Link_ID=ora_plogon
("$this->User@$this->Database.world","$this->Password");
***************/
} else {
if($this->Debug) {
printf("<br>connect() $this->User, $this->Password <br>n");
}  
$this->Link_ID=ora_plogon("$this->User","$this->Password");
/* (comment by SSilk: don't know how this could work, but I leave this untouched!) */
}
if($this->Debug) {
printf("<br>connect() Link_ID: $this->Link_ID<br>n");
}
if (!$this->Link_ID) {
$this->halt("connect() Link-ID == false " .
"($this->Link_ID), ora_plogon failed");
} else {
//echo "commit on<p>";
ora_commiton($this->Link_ID);
}
if($this->Debug) {
printf("<br>connect() Obtained the Link_ID: $this->Link_ID<br>n");
}
## Execute Connect Query
if ($this->ConnectQuery) {
$this->query($this->ConnectQuery);
}
}
}

## In order to increase the # of cursors per system/user go edit the
## init.ora file and increase the max_open_cursors parameter. Yours is on
## the default value, 100 per user.
## We tried to change the behaviour of query() in a way, that it tries
## to safe cursors, but on the other side be carefull with this, that you
## don't use an old result.
##  
## You can also make extensive use of ->disconnect()!
## The unused QueryIDs will be recycled sometimes.  

function query($Query_String)  
{

/* No empty query please. */
if (empty($Query_String))
{
return 0;
}

$this->connect();
$this->lastQuery=$Query_String;

if (!$this->Query_ID) {
$this->Query_ID= ora_open($this->Link_ID);
}
if($this->Debug) {
printf("Debug: query = %s<br>n", $Query_String);
printf("<br>Debug: Query_ID: %d<br>n", $this->Query_ID);
}

if(!@ora_parse($this->Query_ID,$Query_String)) {
$this->Errno=ora_errorcode($this->Query_ID);
$this->Error=ora_error($this->Query_ID);
$this->halt("<BR>ora_parse() failed:<BR>$Query_String<BR><small>Snap & paste this to sqlplus!</SMALL>");
} elseif (!@ora_exec($this->Query_ID)) {
$this->Errno=ora_errorcode($this->Query_ID);
$this->Error=ora_error($this->Query_ID);
$this->halt("<BR>n$Query_Stringn<BR><small>Snap & paste this to sqlplus!</SMALL>");
}

$this->Row=0;

if(!$this->Query_ID) {
$this->halt("Invalid SQL: ".$Query_String);
}

return $this->Query_ID;
}

function next_record() {
if (!$this->ora_no_next_fetch &&  
0 == ora_fetch($this->Query_ID)) {
if ($this->Debug) {
printf("<br>next_record(): ID: %d Row: %d<br>n",
$this->Query_ID,$this->Row+1);
// more info for $this->Row+1 is $this->num_rows(),
// but dosn't work in all cases (complicated selects)
// and it is very slow here
}
$this->Row +=1;

$errno=ora_errorcode($this->Query_ID);
if(1403 == $errno) { # 1043 means no more records found
$this->Errno=0;
$this->Error="";
$this->disconnect();
$stat=0;
} else {
$this->Error=ora_error($this->Query_ID);
$this->Errno=$errno;
if($this->Debug) {
printf("<br>%d Error: %s",
$this->Errno,
$this->Error);
}
$stat=0;
}
} else {
$this->ora_no_next_fetch=false;
for($ix=0;$ix<ora_numcols($this->Query_ID);$ix++) {
$col=strtolower(ora_columnname($this->Query_ID,$ix));
$value=ora_getcolumn($this->Query_ID,$ix);
$this->Record[ "$col" ] = $value;
$this->Record[ $ix ] = $value;
#DBG echo"<b>[$col]</b>: $value <br>n";
}
$stat=1;
}

return $stat;
}

## seek() works only for $pos - 1 and $pos
## Perhaps I make a own implementation, but my
## opinion is, that this should be done by PHP3
function seek($pos) {
if ($this->Row - 1 == $pos) {
$this->ora_no_next_fetch=true;
} elseif ($this->Row == $pos ) {
## do nothing
} else {
$this->halt("Invalid seek(): Position is cannot be handled by API.<BR>".
"Only a seek to the last element is allowed in this version<BR>".
"Difference too big. Wanted: $pos Current pos: $this->Row");
}
if ($this->Debug) echo "<BR>Debug: seek = $pos<BR>";
$this->Row=$pos;
}

function lock($table, $mode = "write") {
if ($mode == "write") {
$result = ora_do($this->Link_ID, "lock table $table in row exclusive mode");
} else {
$result = 1;
}
return $result;
}

function unlock() {
return ora_do($this->Link_ID, "commit");
}

// Important note: This function dosn't work with Oracle-Database-Links!
// You are free to get a better method. :)
function metadata($table,$full=false) {
$count = 0;
$id = 0;
$res = array();

/*
* Due to compatibility problems with Table we changed the behavior
* of metadata();
* depending on $full, metadata returns the following values:
*
* - full is false (default):
* $result[]:
* [0]["table"] table name
* [0]["name"] field name
* [0]["type"] field type
* [0]["len"] field length
* [0]["flags"] field flags ("NOT NULL", "INDEX")
* [0]["format"] precision and scale of number (eg. "10,2") or empty
* [0]["index"] name of index (if has one)
* [0]["chars"] number of chars (if any char-type)
*
* - full is true
* $result[]:
* ["num_fields"] number of metadata records
* [0]["table"] table name
* [0]["name"] field name
* [0]["type"] field type
* [0]["len"] field length
* [0]["flags"] field flags ("NOT NULL", "INDEX")
* [0]["format"] precision and scale of number (eg. "10,2") or empty
* [0]["index"] name of index (if has one)
* [0]["chars"] number of chars (if any char-type)
* [0]["php_type"] the correspondig PHP-type
* [0]["php_subtype"] the subtype of PHP-type
* ["meta"][field name] index of field named "field name"
* This could used, if you have the name, but no index-num - very fast
* Test: if (isset($result['meta']['myfield'])) {} ...
*/

$this->connect();

## This is a RIGHT OUTER JOIN: "(+)", if you want to see, what
## this query results try the following:
## $table = new Table; $db = new my_DB_Sql; # you have to make
## # your own class
## $table->show_results($db->query(see query vvvvvv))
##
$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_lengt

[1] [2]  下一页



打印本文 打印本文  关闭窗口 关闭窗口