<?php


function db_connect() {
    global $connstr;
	$conn=pg_connect($connstr);
	if(!$conn)
		trigger_error("Unable to connect",E_USER_ERROR);
	return $conn;
}

function db_close($conn)
{
  return pg_close($conn);
}

function db_query($qstring,$conn) {
	global $strLastSQL;
	$strLastSQL=$qstring;

	if(version_compare(phpversion(),"4.2.0")>=0)
		$ret=pg_query($conn,$qstring);
	else
		$ret=pg_exec($conn,$qstring);
	if(!$ret)
	{
	  trigger_error(db_error(), E_USER_ERROR);
	}
	return $ret;
	
}

function db_exec($qstring,$conn)
{
	return db_query($qstring,$conn);
}

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

function db_dataseek($qhandle,$row)
{
   pg_result_seek($qhandle,$row);
}

function db_numrows($qhandle) {
	// return only if qhandle exists, otherwise 0
	if ($qhandle) {
		return @pg_num_rows($qhandle);
	} else {
		return 0;
	}
}

function db_fetch_array($qhandle) {
	$ret=pg_fetch_array($qhandle);
//	remove numeric indexes
	if(!$ret)
		return false;
	foreach($ret as $key=>$value)
		if(is_int($key))
			unset($ret[$key]);
	return $ret;
}

function db_fetch_numarray($qhandle) {
	return @pg_fetch_row($qhandle);
}
	

function db_error() {
	global $conn;
	if(version_compare(phpversion(),"4.2.0")>=0)
		return @pg_last_error($conn);
	else
		return "PostgreSQL error happened";
		
}



function db_addslashes($str)
{
	return pg_escape_string($str);
}

function db_stripslashes($str)
{
	return stripslashes($str);
}


function db_addslashesbinary($str)
{
	return "'".pg_escape_bytea($str)."'";
}

function db_stripslashesbinary($str)
{
	return pg_unescape_bytea($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 db_numfields($lhandle) {
	return @pg_num_fields($lhandle);
}

function db_fieldname($lhandle,$fnumber) {
           return @pg_field_name($lhandle,$fnumber);
}

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



function db_fieldtypen($lhandle,$fnumber) {
  return pg_field_type($lhandle,$fnumber);
}

function FieldNeedQuotes($rs,$field)
{
	$type=db_fieldtype($rs,$field);
	$t=strtolower($type);
	if($t=="bigint" || $t=="double precision" || $t=="integer" || $t=="money" || $t=="numeric"
     || $t=="real" || $t=="smallint" || $t=="int" || $t=="int2" || $t=="int4" || $t=="int8"
	 || $t=="float4" || $t=="float8" 
	 )
		return false;
	return true;
}

/*


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

function db_fieldlen($rs,$i)
{
	return pg_field_size($rs,$i);
}
*/

/*

function IsBinaryType($t)
{
	$type=strtolower($t);
	if($type=="bytea")
		return true;
	return false;
}
function IsDateFieldType($stype)
{
	$type=strtolower($stype);
	if($type=="date" || $type=="time" || $type=="time with time zone" || $type=="timestamp")
		return true;
	return false;
}

function IsAutoIncrementField($field)
{
	global $strTableName;
	if(strpos($_SESSION[$strTableName."_fieldinfo"][$field]["default"],"nextval(")===FALSE)
		return false;
	return true;
}

function IsTextType($type)
{
	if($type=="text")
		return true;
	return false;
}

function IsCharType($type)
{
	if($type=="text" || $type=="char" || $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];
	}
	$where="table_name='".$table."' and table_schema='".$schema."' and table_catalog='".$dbname."'";
	$strSQL = "select column_name,column_default,is_nullable,data_type,character_maximum_length from information_schema.columns where ".$where;
	$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["character_maximum_length"];
		$f["default"]=$data["column_default"];
		$f["null"]=@$data["is_nullable"];
	}
}
*/
?>