<?php
$mssql_dmy="mdy";

function db_connect()
{
	global $host,$user,$pwd,$dbname,$mssql_dmy;
    $connstr="PROVIDER=SQLOLEDB;SERVER=".$host.";UID=".$user.";PWD=".$pwd.";DATABASE=".$dbname;
	try {
	$conn = new COM("ADODB.Connection",NULL,##@BUILDER.nCodepage##);
	$conn->Open($connstr);
	$rs=$conn->Execute("select convert(datetime,'2000-11-22',121)");
	$str=$rs->Fields[0]->Value;
	$y=strpos($str,"2000");
	$m=strpos($str,"11");
	$d=strpos($str,"22");
	if($y<$m && $m<$d)
		$mssql_dmy="ymd";
	if($d<$m && $m<$y)
		$mssql_dmy="dmy";
	} catch(com_exception $e)
	{
		trigger_error($e->getMessage(),E_USER_ERROR);
	}
	return $conn;
}

function db_close($conn)
{
	$conn->Close();
}

function db_query($qstring,$conn)
{
	global $strLastSQL;
	$strLastSQL=$qstring;
	try{
	return $conn->Execute($qstring);
	} catch(com_exception $e)
	{
		trigger_error($e->getMessage(),E_USER_ERROR);
	}

}

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


function db_pageseek($qhandle,$pagesize,$page)
{
	if($page==1)
		return;
	if($qhandle->EOF())
		return;
   $qhandle->Move($pagesize*($page-1));
}

function db_numrows($qhandle)
{
	if($qhandle->RecordCount>=0)
		return $qhandle->RecordCount;
	if($qhandle->EOF())
		return 0;
	return 1;
}

function db_fetch_array($rs, $assoc=1)
{
	global $mssql_dmy;
	if( $rs->EOF() )
           return false;
	try {
	$ret=array();
	for( $i = 0; $i < db_numfields($rs); $i++ )
	{
		if(IsBinaryType($rs->Fields[$i]->Type) && $rs->Fields[$i]->Type!=128)
		{
			$str="";
			if($rs->Fields[$i]->ActualSize)
			{
				$val=$rs->Fields[$i]->GetChunk($rs->Fields[$i]->ActualSize);
				$str=str_pad("",count($val));
				$j=0;
				foreach($val as $byte)
					$str[$j++]=chr($byte);
			}
			if($assoc)
				$ret[$rs->Fields[$i]->Name] = $str;
			else
				$ret[$i] = $str;
		}
		else
		{
			$value = $rs->Fields[$i]->Value;
			if(isdatefieldtype($rs->Fields[$i]->Type))
				$value=localdatetime2db((string)$rs->Fields[$i]->Value,$mssql_dmy);
			if(IsNumberType($rs->Fields[$i]->Type))
				$val=floatval($value);
			else
				$val=strval($value);
			if($assoc)
				$ret[$rs->Fields[$i]->Name] = $val;
			else
				$ret[$i] = $val;
		}
	}
	$rs->MoveNext();
	} catch(com_exception $e)
	{
		trigger_error($e->getMessage(),E_USER_ERROR);
	}

	return $ret;
}

function db_fetch_numarray($qhandle)
{
	return db_fetch_array($qhandle,0);
}

function db_error()
{
	global $conn;
	return $conn->Errors[$conn->Errors->Count-1]->Description;
}


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

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

function db_addslashesbinary($str)
{
	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)
{
	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 "convert(datetime,'".$val."',120)";
}


function db_numfields($lhandle)
{
	return $lhandle->Fields->Count;
}

function db_fieldname($lhandle,$fnumber)
{
	return $lhandle->Fields($fnumber)->Name;
}

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 $lhandle->Fields[$fnumber]->Type;
}

function FieldNeedQuotes($rs,$field)
{
	$type=db_fieldtype($rs,$field);
	if(	$type==20 
	|| $type==128
	|| $type==11
	|| $type==6
	|| $type==14
	|| $type==5
	|| $type==3
	|| $type==131
	|| $type==4
	|| $type==2
	|| $type==16
	|| $type==21
	|| $type==19
	|| $type==18
	|| $type==17
	|| $type==139)
		return false;
	return true;
}

function db_insertid($qhandle)
{
$strSQL = "select @@IDENTITY as indent";
$rs = db_query($strSQL,$qhandle);
$row = db_fetch_array($rs);
return $row["indent"];
}

?>