您现在的位置: 军旅同心-旅游自驾-军旅文学 >> 读书赏析 >> 学习园地 >> 电脑网络 >> 技术文章 >> 正文
oracle资料库函式库
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005-9-10 14:29:31
h,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;
switch ($res[$i]["type"]) {
case "VARCHAR2" :
case "VARCHAR" :
case "CHAR" :
$res["php_type"]="string";
$res["php_subtype"]="";
break;
case "DATE" :
$res["php_type"]="string";
$res["php_subtype"]="date";
break;
case "BLOB" :
case "CLOB" :
case "BFILE" :
case "RAW" :
case "LONG" :
case "LONG RAW" :
$res["php_type"]="string";
$res["php_subtype"]="blob";
break;
case "NUMBER" :
if ($res[$i]["format"]) {
$res["php_type"]="double";
$res["php_subtype"]="";
} else {
$res["php_type"]="int";
$res["php_subtype"]="";
}
break;
default :
$this->halt("metadata(): Type is not a valid value: '$res[$i][type]'");
break;
}
}
if ($full) $res["meta"][$res[$i]["name"]] = $i;
$i++;
}
if ($full) $res["num_fields"]=$i;
# $this->disconnect();
return $res;
}

## THIS FUNCTION IS UNSTESTED!
function affected_rows() {
if ($this->Debug) echo "<BR>Debug: affected_rows=". ora_numrows($this->Query_ID)."<BR>";
return ora_numrows($this->Query_ID);
}

## 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[^)]*()*)", "1",  
$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;
}


}
?> 

上一页  [1] [2] 


更多
免责声明:作品版权归所属媒体与作者所有!!本站刊载此文不代表同意其说法或描述,仅为提供更多信息。如果您认为我们侵犯了您的版权,请告知!本站立即删除。有异议请联系我们。
文章录入:烟灰缸    责任编辑:烟灰缸 
  • 上一篇文章:
  • 下一篇文章:
  • 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 网站地图 | 版权申明 | 网站公告 | 管理登录 |