<?php

$dal_info=array();
##foreach @BUILDER.DALInfo as @t##
$daltable_##@t.strTableName ag## = array();
	##foreach @t.fields as @f##
$daltable_##@t.strTableName ag##["##@f.strName s##"]=array();
$daltable_##@t.strTableName ag##["##@f.strName s##"]["nType"]=##@f.nType##;
	##if @f.bKey##
$daltable_##@t.strTableName ag##["##@f.strName s##"]["bKey"]=true;
	##endif##
$daltable_##@t.strTableName ag##["##@f.strName s##"]["varname"]="##@f.strName gf##";
	##endfor##
$dal_info["##@t.strTableName s##"]=&$daltable_##@t.strTableName ag##;
##endfor##



function CustomQuery($dalSQL)
{
	global $conn;
	$rs = db_query($dalSQL,$conn);
	  return $rs;
}

function UsersTableName()
{
	return "##@BUILDER.strLoginTable ts##";
}


class tDAL
{
##foreach @BUILDER.DALInfo as @t##
	var $##@t.strTableName ag##;
##endfor##
  function Table($strTable)
  {
      ##foreach @BUILDER.DALInfo as @t##
          if(strtoupper($strTable)==strtoupper("##@t.strTableName s##"))
              return $this->##@t.strTableName ag##;
      ##endfor##
//	check table names without dbo. and other prefixes
      ##foreach @BUILDER.DALInfo as @t##
          if(strtoupper(cutprefix($strTable))==strtoupper(cutprefix("##@t.strTableName s##")))
              return $this->##@t.strTableName ag##;
      ##endfor##
  }
}

$dal = new tDAL;

class tDALTable
{
	var $m_TableName;
	var $Param = array();
	var $Value = array();
	
	function TableName()
	{
		return AddTableWrappers($this->m_TableName);
	} 
	
	function Add() 
	{
		global $conn,$dal_info;
		$insertFields="";
		$insertValues="";
		$tableinfo = &$dal_info[$this->m_TableName];
//	prepare parameters		
		foreach($tableinfo as $fieldname=>$fld)
		{
			$command='if(isset($this->'.$fld['varname'].'))
			{
				$this->Value[\''.escapesq($fieldname).'\'] = $this->'.$fld['varname'].';
			}';
			eval($command);
			foreach($this->Value as $field=>$value)
			{
				if (strtoupper($field)!=strtoupper($fieldname))
					continue;
				$insertFields.= AddFieldWrappers($fieldname).",";
				if (NeedQuotes($fld["nType"]))
					$insertValues.= "'".db_addslashes($value) . "',";
				else
					$insertValues.= "".(0+$value) . ",";		
				break;
			}
		}
//	prepare and exec SQL
		if ($insertFields!="" && $insertValues!="")		
		{
			$insertFields = substr($insertFields,0,-1);
			$insertValues = substr($insertValues,0,-1);
			$dalSQL = "insert into ".AddTableWrappers($this->m_TableName)." (".$insertFields.") values (".$insertValues.")";
			db_exec($dalSQL,$conn);
		}
//	cleanup		
	    $this->Reset();
	}

	function QueryAll()
	{
		global $conn;
		$dalSQL = "select * from ".AddFieldWrappers($this->m_TableName);
		$rs = db_query($dalSQL,$conn);
		return $rs;
	}

	function Query($swhere="",$orderby="")
	{
		global $conn;
		if ($swhere)
			$swhere = " where ".$swhere;
		if ($orderby)
			$orderby = " order by ".$orderby;
		$dalSQL = "select * from ".AddTableWrappers($this->m_TableName).$swhere.$orderby;
		$rs = db_query($dalSQL,$conn);
		return $rs;
	}

	function Delete()
	{
		global $conn,$dal_info;
		$deleteFields="";
		$tableinfo = &$dal_info[$this->m_TableName];
//	prepare parameters		
		foreach($tableinfo as $fieldname=>$fld)
		{
			$command='if(isset($this->'.$fld['varname'].'))
			{
				$this->Value[\''.escapesq($fieldname).'\'] = $this->'.$fld['varname'].';
			}
			';
			eval($command);
			foreach($this->Value as $field=>$value)
			{
				if (strtoupper($field)!=strtoupper($fieldname))
					continue;
				if (NeedQuotes($fld["nType"]))
					$deleteFields.= AddFieldWrappers($fieldname)."='".db_addslashes($value) . "' and ";
				else
					$deleteFields.= AddFieldWrappers($fieldname)."=". (0+$value) . " and ";		
				break;
			}
		}
//	do delete
		if ($deleteFields)
		{
			$deleteFields = substr($deleteFields,0,-5);
			$dalSQL = "delete from ".AddFieldWrappers($this->m_TableName)." where ".$deleteFields;
			db_exec($dalSQL,$conn);
		}
	
//	cleanup
	    $this->Reset();
	}

	function Reset()
	{
		$this->Value=array();
		$this->Param=array();
		global $dal_info;
		$tableinfo = &$dal_info[$this->m_TableName];
//	prepare parameters		
		foreach($tableinfo as $fieldname=>$fld)
		{
			$command='unset($this->'.$fld["varname"].");";
			eval($command);
		}
	}	

	function Update()
	{
		global $conn,$dal_info;
		$tableinfo = &$dal_info[$this->m_TableName];
		$updateParam = "";
		$updateValue = "";

		foreach($tableinfo as $fieldname=>$fld)
		{
			$command='if(isset($this->'.$fld['varname'].')) { ';
			if($fld["bKey"])
				$command.='$this->Param[\''.escapesq($fieldname).'\'] = $this->'.$fld['varname'].';';
			else
				$command.='$this->Value[\''.escapesq($fieldname).'\'] = $this->'.$fld['varname'].';';
			$command.=' }';
			eval($command);
			if(!$fld["bKey"])
			{
				foreach($this->Value as $field=>$value)
				{
					if (strtoupper($field)!=strtoupper($fieldname))
						continue;
					if (NeedQuotes($fld["nType"]))
						$updateValue.= AddFieldWrappers($fieldname)."='".db_addslashes($value) . "', ";
					else
						$updateValue.= AddFieldWrappers($fieldname)."=".(0+$value) . ", ";
					break;
				}
			}
			else
			{
				foreach($this->Param as $field=>$value)
				{
					if (strtoupper($field)!=strtoupper($fieldname))
						continue;
					if (NeedQuotes($fld["nType"]))
						$updateParam.= AddFieldWrappers($fieldname)."='".db_addslashes($value) . "' and ";
					else
						$updateParam.= AddFieldWrappers($fieldname)."=".(0+$value) . " and ";
					break;
				}
			}
		}

//	construct SQL and do update	
		if ($updateParam)
			$updateParam = substr($updateParam,0,-5);
		if ($updateValue)
			$updateValue = substr($updateValue,0,-2);
		if ($updateValue && $updateParam)
		{
			$dalSQL = "update ".AddTableWrappers($this->m_TableName)." set ".$updateValue." where ".$updateParam;
			db_exec($dalSQL,$conn);
		}

//	cleanup
		$this->Reset();
	}

	function FetchByID()
	{
		global $conn,$dal_info;
		$tableinfo = &$dal_info[$this->m_TableName];

		$dal_where="";
		foreach($tableinfo as $fieldname=>$fld)
		{
			$command='if(isset($this->'.$fld['varname'].')) { ';
			$command.='$this->Value[\''.escapesq($fieldname).'\'] = $this->'.$fld['varname'].';';
			$command.=' }';
			eval($command);
			foreach($this->Value as $field=>$value)
			{
				if (strtoupper($field)!=strtoupper($fieldname))
					continue;
				if (NeedQuotes($fld["nType"]))
					$dal_where.= AddFieldWrappers($fieldname)."='".db_addslashes($value) . "' and ";
				else
					$dal_where.= AddFieldWrappers($fieldname)."=".(0+$value) . " and ";
				break;
			}
		}
//	cleanup
		$this->Reset();
//	construct and run SQL
		if ($dal_where)
			$dal_where = " where ".substr($dal_where,0,-5);
		$dalSQL = "select * from ".AddTableWrappers($this->m_TableName).$dal_where;
		$rs = db_query($dalSQL,$conn);
		return $rs;
	}
}

##foreach @BUILDER.DALInfo as @t##
class class_##@t.strTableName ag## extends tDALTable
{
	##foreach @t.fields as @f##
	var $##@f.strName gf##;
	##endfor##

	function class_##@t.strTableName ag##()
	{
		$this->m_TableName = "##@t.strTableName s##";
	}
}
$dal->##@t.strTableName ag## = new class_##@t.strTableName ag##();
##endfor##

class DalRecordset
{
	
	var $m_rs;
	var $m_fields;
	var $m_eof;
	
	function Fields($field="")
	{
		if(!$field)
			return $this->m_fields;
		return $this->Field($field);
	}
	
	function Field($field)
	{
		if($this->m_eof)
			return false;
		foreach($this->m_fields as $name=>$value)
		{
			if(!strcasecmp($name,$field))
				return $value;
		}
		return false;
	}
	function DalRecordset($rs)
	{
		$this->m_rs=$rs;
		$this->MoveNext();
	}
	function EOF()
	{
		return $this->m_eof;
	}
	
	function MoveNext()
	{
		if(!$this->m_eof)
			$this->m_fields=db_fetch_array($this->m_rs);
		$this->m_eof = !$this->m_fields;
		return !$this->m_eof;
	}
}

function cutprefix($table)
{
	$pos=strpos($table,".");
	if($pos===false)
		return $table;
	return substr($table,$pos+1);
}

function escapesq($str)
{
	return str_replace(array("\\","'"),array("\\\\","\\'"),$str);
}

?>