oracle资料函式库(2)

分类: php基本   出处:iocblog整理  更新时间:2008-12-17   添加到收藏  

## known bugs: it will not work for select distinct and any
## other constructs which are depending on the resulting rows.
## so you *really need* to check every query you make, if it
## will work with it!
##
## also, for a qualified replacement you need to parse the
## selection, cause this will fail: "select id, from from ...").
## "from" is - as far as i know a keyword in oracle, so it can
## only be used in this way. but you have been warned.
function num_rows() {
$curs=ora_open($this->link_id);

## this is the important part and it is also the hack!
if (eregi("^[[:space:]]*select[[:space:]]",$this->lastquery) )  
{

# this works for all?? cases, including select distinct case.
# we just make select count(*) from original sql expression
# and remove order by (if any) for speed
# i like regular expressions too ;-)))  
$q = sprintf("select count(*) from (%s)",
@eregi_replace("order[[:space:]]+by[^)]*()*)", "",  
$this->lastquery)  
);

# works also for subselects:
# if (eregi("[[:space:]]+from([[:space:]]+.*[[:space:]]+from)",$this->lastquery,$r))
# $areplace=$r[1];
# $q=eregi_replace("^[[:space:]]*select[[:space:]]+".
# ".*[[:space:]]+from",
# "select count(*) from$areplace",
# $this->lastquery);

if ($this->debug) echo "<br>debug: num_rows: $q<br>";

ora_parse($curs,$q);
ora_exec($curs);
ora_fetch($curs);
$result = ora_getcolumn($curs,0);
ora_close($curs);
if ($this->debug)
{  
echo "<br>debug: id ".$this->queryid.
" num_rows=". $result ."<br>";
}
return $result;
}  
else  
{
$this->halt("last query was not a select: $this->lastquery");
}
}

function num_fields() {
if ($this->debug) echo "<br>debug: num_fields=". ora_numcols($this->query_id) . "<br>";
return ora_numcols($this->query_id);
}

function nf() {
return $this->num_rows();
}

function np() {
print $this->num_rows();
}

function f($name) {
return $this->record[$name];
}

function p($name) {
print $this->record[$name];
}

/* public: sequence number */
function nextid($seq_name)
{
$this->connect();

/* independent query_id */
$query_id = ora_open($this->link_id);

if(!@ora_parse($query_id,"select $seq_name.nextval from dual"))  
{
// there is no such sequence yet, then create it
if(!@ora_parse($query_id,"create sequence $seq_name")  
||
!@ora_exec($query_id)
)
{
$this->halt("<br> nextid() function - unable to create sequence");
return 0;
}
@ora_parse($query_id,"select $seq_name.nextval from dual");
}  
if (!@ora_exec($query_id)) {
$this->halt("<br>ora_exec() failed:<br>nextid function");
}
if (@ora_fetch($query_id) ) {
$next_id = ora_getcolumn($query_id, 0);
}
else {
$next_id = 0;
}
if ( $query_id > 0 ) {
ora_close($query_id);
}

return $next_id;
}

function disconnect() {
if($this->debug) {
echo "debug: disconnecting $this->query_id...<br>n";
}
if ( $this->query_id < 1 ) {
echo "<b>warning</b>: disconnect(): cannot free id $this->query_idn";
# return();
}
ora_close($this->query_id);
$this->query_id=0;
}

/* private: error handling */
function halt($msg) {
if ($this->halt_on_error == "no")
return;

$this->haltmsg($msg);

if ($this->halt_on_error != "report")
die("session halted.");
}

function haltmsg($msg) {
printf("</td></tr></table><br><b>database error:</b> %s<br>n", $msg);
printf("<b>oracle error</b>: %s (%s)<br>n",
$this->errno,
$this->error);
}

function table_names() {
$this->connect();
$this->query("
select table_name,tablespace_name
from user_tables");
$i=0;
while ($this->next_record())
{
$info[$i]["table_name"] =$this->record["table_name"];
$info[$i]["tablespace_name"]=$this->record["tablespace_name"];
$i++;
}  
return $info;
}


// some transaction support
// methods are used in ct_oracle.inc
function begin_transaction()  
{
$this->connect();
// now, disable autocommit
ora_commitoff($this->link_id);
if ($this->debug)
{
print "begin transaction<br>";
}
}  
function end_transaction()  
{
if ($this->debug)
{
print "begin transaction<br>";
}

$res = 1;
if(!@ora_commit($this->link_id))
{
ora_commiton($this->link_id);
$this->halt("unable to finish transaction");
$res = 0;
}
// enable autocommit again
ora_commiton($this->link_id);

if ($this->debug)
{
print "end transaction : $res<br>";
}
return $res;
}


}
?> [www.iocblog.net 来源]

上一页 [1] [2]


Tag: Oracle



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。