<?php

function db_connect()
{
    global $user,$pwd,$sid;
	$conn=ociplogon($user,$pwd,$sid);
	if(!$conn)
		trigger_error(db_error(),E_USER_ERROR);
	$stmt=ociparse($conn,"alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'");
	ociexecute($stmt);
	ocifreestatement($stmt);
	return $conn;
}

function db_close($conn)
{
	return @ocilogoff($conn);
}

function db_query($qstring,$conn)
{
	global $strLastSQL;
	$strLastSQL=$qstring;
	$rowcount=0;
	$stmt=ociparse($conn,$qstring);
	$stmt_type=ocistatementtype($stmt);
	if(!ociexecute($stmt))
		trigger_error(db_error(), E_USER_ERROR);
	return array($stmt,$rowcount);
}

function db_query_direct($qstring,$conn,$rowcount)
{
	global $strLastSQL;
	$strLastSQL=$qstring;
	$stmt=ociparse($conn,$qstring);
	$stmt_type=ocistatementtype($stmt);
	if(!ociexecute($stmt))
		trigger_error(db_error(), E_USER_ERROR);
	return array($stmt,$rowcount);
}


function db_exec($qstring,$conn)
{
	global $strLastSQL;
	$strLastSQL=$qstring;
	$stmt=ociparse($conn,$qstring);
	$stmt_type=ocistatementtype($stmt);
	if(!ociexecute($stmt))
	{
		trigger_error(db_error(), E_USER_ERROR);
		return 0;
	}
	else
		return 1;
}

function db_pageseek($qhandle,$pagesize,$page)
{
	db_dataseek($qhandle,($page-1)*$pagesize);
}

function db_dataseek(&$qhandle,$row)
{
	for($i=0;$i<$row;$i++)
		myoci_fetch_array($qhandle[0],OCI_NUM+OCI_RETURN_NULLS);
}

function db_numrows(&$qhandle)
{
	return $qhandle[1];
}


function db_fetch_array($qhandle)
{
	return myoci_fetch_array($qhandle[0],OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS);
}

function db_fetch_numarray($qhandle)
{
	return myoci_fetch_array($qhandle[0],OCI_NUM+OCI_RETURN_NULLS+OCI_RETURN_LOBS);
}

function db_error()
{
	$arr=ocierror();
	if(count($arr)>1)
		return $arr["code"]." - ".$arr["message"];
}


function db_addslashes($str)
{
	return str_replace("'","''",$str);
}

function db_stripslashes($str)
{
	return str_replace("''","'",$str);
}

function db_addslashesbinary($str)
{
	return $str;
}

function db_stripslashesbinary($str)
{
	return $str;
}


// adds wrappers to field name if required
function AddFieldWrappers($strName)
{
	global $strLeftWrapper,$strRightWrapper;
	if(substr($strName,0,1)==$strLeftWrapper)
		return $strName;
	return $strLeftWrapper.$strName.$strRightWrapper;
}

function AddTableWrappers($strName)
{
	global $strLeftWrapper,$strRightWrapper;
	if(substr($strName,0,1)==$strLeftWrapper)
		return $strName;
	$arr=explode(".",$strName);
	$ret=$strLeftWrapper.$arr[0].$strRightWrapper;
	if(count($arr)>1)
		$ret.=".".$strLeftWrapper.$arr[1].$strRightWrapper;
	return $ret;
}

// removes wrappers from field name if required
function RemoveFieldWrappers($strName)
{
	global $strLeftWrapper,$strRightWrapper;
	if(substr($strName,0,1)==$strLeftWrapper)
		return substr($strName,1,strlen($strName)-2);
	return $strName;
}

function RemoveTableWrappers($strName)
{
	global $strLeftWrapper,$strRightWrapper;
	if(substr($strName,0,1)!=$strLeftWrapper)
		return $strName;
	$arr=explode(".",$strName);
	$ret=substr($arr[0],1,strlen($arr[0])-2);
	if(count($arr)>1)
		$ret.=".".substr($arr[1],1,strlen($arr[1])-2);
	return $ret;
}

function db_upper($dbval)
{
	return "upper(".$dbval.")";
}

function db_datequotes($val)
{
	return "'".$val."'";
}

function myoci_fetch_array($qhandle,$flags)
{
	if(function_exists("oci_fetch_array"))
		return oci_fetch_array($qhandle,$flags);
	$data=array();
	if(ocifetchinto($qhandle,$data,$flags))
		return $data;
	return false;
}

function db_numfields($lhandle)
{
	return OCINumCols($lhandle[0]);
}

function db_fieldname($lhandle,$fnumber)
{
	return OCIColumnName($lhandle[0], $fnumber+1);
}

function db_fieldtype($lhandle,$fname)
{
	for($i=0;$i<db_numfields($lhandle);$i++)
		if(db_fieldname($lhandle,$i)==$fname)
		{
			$type=db_fieldtypen($lhandle,$i);
			return $type;
		}
	return "";
}

function db_fieldtypen($lhandle,$fnumber)
{
	return OCIColumnType($lhandle[0],$fnumber+1);
}

function FieldNeedQuotes($rs,$field)
{
	$type=db_fieldtype($rs,$field);
	$t=strtolower($type);
	if($t=="number")
		return false;
	return true;
}


/*


function db_fieldnull($rs,$i)
{
	return true;
}

function db_fieldlen($rs,$i)
{
	return ocicolumnsize($rs[0],$i+1);
}
*/


/*

function IsBinaryType($t)
{
	$type=strtolower($t);
	if($type=="blob" || $type=="raw" || $type=="long raw" || $type=="bfile")
		return true;
	return false;
}

function IsDateFieldType($stype)
{
	$type=strtolower($stype);
	if($type=="date" || substr($type,0,8)=="timestamp")
		return true;
	return false;
}

function IsAutoIncrementField($field)
{
	return false;
}



function IsTextType($stype)
{
	$type=strtolower($stype);
	if($type=="text" || $type=="long" || $type=="clob")
		return true;
	return false;
}

function IsCharType($stype)
{
	$type=strtolower($stype);
	if($type=="char" || $type=="varchar2" || $type=="varchar")
		return true;
	return false;
}

function GetTableInfo()
{
	global $conn,$dbname,$strTableName;
	$arr=explode(".",$strTableName);
	$schema="public";
	$table=$strTableName;
	if(count($arr)>1)
	{
		$table=$arr[1];
		$schema=$arr[0];
	}
	$strSQL = "select column_name,data_type,data_length,nullable from all_tab_columns where upper(owner)=upper('".$schema."') and upper(table_name)=upper('".$table."') order by column_id";
	$rs = db_query($strSQL,$conn);
	$_SESSION[$strTableName."_fieldinfo"]=array();
	while($data=db_fetch_array($rs))
	{
		$_SESSION[$strTableName."_fieldinfo"][$data["COLUMN_NAME"]] = array();
		$f = & $_SESSION[$strTableName."_fieldinfo"][$data["COLUMN_NAME"]];
		$f["type"]=$data["DATA_TYPE"];
		$f["size"]=$data["DATA_LENGTH"];
		$f["null"]=@$data["NULLABLE"];
	}
}

*/
?>