<?php


function db_connect() 
{
    global $dsn,$uid,$pwd;
	$conn = odbc_connect($dsn,$uid,$pwd);
	if (!$conn) 
	{
	  trigger_error(db_error(), E_USER_ERROR);
	}
	return $conn;
}

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

function db_query($qstring,$conn) 
{
	global $strLastSQL;
	$strLastSQL=$qstring;
	$count=0;
	if(!($rs=odbc_exec($conn,$qstring)))
	  trigger_error(odbc_error(), E_USER_ERROR);
	odbc_binmode($rs,ODBC_BINMODE_RETURN);
	odbc_longreadlen($rs,1024*1024);
	$ret=array($count,$rs);
	return $ret;
	
}

function db_query_direct($qstring,$conn,$rowcount)
{
	global $strLastSQL;
	$strLastSQL=$qstring;
	if(!($rs=odbc_exec($conn,$qstring)))
	  trigger_error(odbc_error(), E_USER_ERROR);
	odbc_binmode($rs,ODBC_BINMODE_RETURN);
	odbc_longreadlen($rs,1024*1024);
	$ret=array($rowcount,$rs);
	return $ret;
}

function db_exec($qstring,$conn)
{
	global $strLastSQL;
	$strLastSQL=$qstring;
	return odbc_exec($conn,$qstring);
}

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

function db_dataseek(&$qhandle,$row)
{
   $i=0;
   while($i<$row)
   {
   		odbc_fetch_row($qhandle[1]);
		$i++;
   }
}

function db_numrows(&$qhandle) {
	return 0;
//	return $qhandle[0];
	// return only if qhandle exists, otherwise 0
	if ($qhandle) {
		return @odbc_num_rows($qhandle);
	} else {
		return 0;
	}
}




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

function db_fetch_numarray(&$qhandle) {
	$row=array();
	odbc_fetch_into($qhandle[1],$row);
	return $row;
}
	
function db_error() {
	return @odbc_errormsg();
}

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

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

function db_addslashesbinary($str)
{
	if(!strlen($str))
		return "''";
	return "0x".bin2hex($str);
}

function db_stripslashesbinary($str)
{
//	try to remove ole header for BMP pictures
	$pos = strpos($str,".Picture");
	if($pos===false || $pos>300)
		return $str;
	$pos1=strpos($str,"BM",$pos);
	if($pos1===false || $pos1>300)
		return $str;
	return substr($str,$pos1);
}

// 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)
{
	return AddFieldWrappers($strName);
}

// 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)
{
	return RemoveFieldWrappers($strName);
}

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

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

function db_numfields(&$lhandle) {
	return @odbc_num_fields($lhandle[1]);
}

function db_fieldname(&$lhandle,$fnumber) 
{
	return @odbc_field_name($lhandle[1],$fnumber+1);
}

function db_fieldtype(&$lhandle,$fname) 
{
	for($i=0;$i<db_numfields($lhandle);$i++)
		if(db_fieldname($lhandle,$i)==$fname)
		{
			$type=odbc_field_type($lhandle[1],$i+1);
			return $type;
		}
	return "";
}



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

function FieldNeedQuotes($rs,$field)
{
	$type=db_fieldtype($rs,$field);
	$t=strtoupper($type);
	if($t=="INTEGER" || $t=="BYTE" || $t=="SMALLINT" || $t=="REAL" || $t=="DOUBLE"
     || $t=="DECIMAL" || $t=="CURRENCY" || $t=="COUNTER" || $t=="BIT" )
		return false;
	return true;
}


?>