<?php

$dal_info = array();

/**
 * User API function
 * @param String sql
 */
function UsersTableName()
{
	global $cman;
	$connection = $cman->getForLogin();
	return $connection->addTableWrappers("sys_manage");
}

/**
 * User API function
 * It uses the default db connection
 * @param String dalSQL
 */
function CustomQuery($dalSQL)
{
	$connection = getDefaultConnection();
	$result = $connection->query( $dalSQL );
	if($result)
		return $result;
}

/**
 * User API function
 * It uses the default db connection 
 * @param String sql
 */
function DBLookup($sql)
{
	$connection = getDefaultConnection();
	$data = $connection->query( $sql )->fetchAssoc();

	if( $data )
		return reset($data);
	  
	return null;
}

/**
  * Data Access Layer.
  */
class tDAL
{
	var $tbltest2_at_localhost__advances_to_supplier;
	var $tbltest2_at_localhost__attendance;
	var $tbltest2_at_localhost__attendance_master;
	var $tbltest2_at_localhost__bank_accounts;
	var $tbltest2_at_localhost__bank_activity;
	var $tbltest2_at_localhost__brand;
	var $tbltest2_at_localhost__buyers;
	var $tbltest2_at_localhost__buyers_advance;
	var $tbltest2_at_localhost__category;
	var $tbltest2_at_localhost__cheque_register;
	var $tbltest2_at_localhost__cheque_register_m;
	var $tbltest2_at_localhost__company_profile;
	var $tbltest2_at_localhost__customer;
	var $tbltest2_at_localhost__daily_report;
	var $tbltest2_at_localhost__expen_detail;
	var $tbltest2_at_localhost__expen_source;
	var $tbltest2_at_localhost__expenditure_data;
	var $tbltest2_at_localhost__expens;
	var $tbltest2_at_localhost__expense_category;
	var $tbltest2_at_localhost__loa_types;
	var $tbltest2_at_localhost__loans;
	var $tbltest2_at_localhost__new_start6_audit;
	var $tbltest2_at_localhost__new_start6_locking;
	var $tbltest2_at_localhost__new_start8_settings;
	var $tbltest2_at_localhost__opening_balance;
	var $tbltest2_at_localhost__payment_for_pur;
	var $tbltest2_at_localhost__payment_of_loan_installments;
	var $tbltest2_at_localhost__payments;
	var $tbltest2_at_localhost__product;
	var $tbltest2_at_localhost__purchasing;
	var $tbltest2_at_localhost__purchasing_details;
	var $tbltest2_at_localhost__salaries_and_allowances;
	var $tbltest2_at_localhost__salary_advance;
	var $tbltest2_at_localhost__salary_list;
	var $tbltest2_at_localhost__salary_summary;
	var $tbltest2_at_localhost__salary_voucher;
	var $tbltest2_at_localhost__sales;
	var $tbltest2_at_localhost__sales_details;
	var $tbltest2_at_localhost__seasonal_discounts;
	var $tbltest2_at_localhost__special_discount;
	var $tbltest2_at_localhost__stock_transfer;
	var $tbltest2_at_localhost__supplier_advance_cut;
	var $tbltest2_at_localhost__sys_manage;
	var $tbltest2_at_localhost__theme_list;
	var $tbltest2_at_localhost__units;
	var $lstTables;
	var $Table = array();

	function FillTablesList()
	{
		if($this->lstTables)
			return;
		$this->lstTables[] = array("name" => "advances_to_supplier", "varname" => "test2_at_localhost__advances_to_supplier", "altvarname" => "advances_to_supplier", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "attendance", "varname" => "test2_at_localhost__attendance", "altvarname" => "attendance", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "attendance_master", "varname" => "test2_at_localhost__attendance_master", "altvarname" => "attendance_master", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "bank_accounts", "varname" => "test2_at_localhost__bank_accounts", "altvarname" => "bank_accounts", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "bank_activity", "varname" => "test2_at_localhost__bank_activity", "altvarname" => "bank_activity", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "brand", "varname" => "test2_at_localhost__brand", "altvarname" => "brand", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "buyers", "varname" => "test2_at_localhost__buyers", "altvarname" => "buyers", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "buyers_advance", "varname" => "test2_at_localhost__buyers_advance", "altvarname" => "buyers_advance", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "category", "varname" => "test2_at_localhost__category", "altvarname" => "category", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "cheque_register", "varname" => "test2_at_localhost__cheque_register", "altvarname" => "cheque_register", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "cheque_register_m", "varname" => "test2_at_localhost__cheque_register_m", "altvarname" => "cheque_register_m", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "company_profile", "varname" => "test2_at_localhost__company_profile", "altvarname" => "company_profile", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "customer", "varname" => "test2_at_localhost__customer", "altvarname" => "customer", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "daily_report", "varname" => "test2_at_localhost__daily_report", "altvarname" => "daily_report", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "expen_detail", "varname" => "test2_at_localhost__expen_detail", "altvarname" => "expen_detail", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "expen_source", "varname" => "test2_at_localhost__expen_source", "altvarname" => "expen_source", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "expenditure_data", "varname" => "test2_at_localhost__expenditure_data", "altvarname" => "expenditure_data", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "expens", "varname" => "test2_at_localhost__expens", "altvarname" => "expens", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "expense_category", "varname" => "test2_at_localhost__expense_category", "altvarname" => "expense_category", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "loa_types", "varname" => "test2_at_localhost__loa_types", "altvarname" => "loa_types", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "loans", "varname" => "test2_at_localhost__loans", "altvarname" => "loans", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "new_start6_audit", "varname" => "test2_at_localhost__new_start6_audit", "altvarname" => "new_start6_audit", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "new_start6_locking", "varname" => "test2_at_localhost__new_start6_locking", "altvarname" => "new_start6_locking", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "new_start8_settings", "varname" => "test2_at_localhost__new_start8_settings", "altvarname" => "new_start8_settings", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "opening_balance", "varname" => "test2_at_localhost__opening_balance", "altvarname" => "opening_balance", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "payment_for_pur", "varname" => "test2_at_localhost__payment_for_pur", "altvarname" => "payment_for_pur", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "payment_of_loan_installments", "varname" => "test2_at_localhost__payment_of_loan_installments", "altvarname" => "payment_of_loan_installments", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "payments", "varname" => "test2_at_localhost__payments", "altvarname" => "payments", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "product", "varname" => "test2_at_localhost__product", "altvarname" => "product", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "purchasing", "varname" => "test2_at_localhost__purchasing", "altvarname" => "purchasing", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "purchasing_details", "varname" => "test2_at_localhost__purchasing_details", "altvarname" => "purchasing_details", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "salaries_and_allowances", "varname" => "test2_at_localhost__salaries_and_allowances", "altvarname" => "salaries_and_allowances", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "salary_advance", "varname" => "test2_at_localhost__salary_advance", "altvarname" => "salary_advance", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "salary_list", "varname" => "test2_at_localhost__salary_list", "altvarname" => "salary_list", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "salary_summary", "varname" => "test2_at_localhost__salary_summary", "altvarname" => "salary_summary", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "salary_voucher", "varname" => "test2_at_localhost__salary_voucher", "altvarname" => "salary_voucher", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "sales", "varname" => "test2_at_localhost__sales", "altvarname" => "sales", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "sales_details", "varname" => "test2_at_localhost__sales_details", "altvarname" => "sales_details", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "seasonal discounts", "varname" => "test2_at_localhost__seasonal_discounts", "altvarname" => "seasonal_discounts", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "special_discount", "varname" => "test2_at_localhost__special_discount", "altvarname" => "special_discount", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "stock_transfer", "varname" => "test2_at_localhost__stock_transfer", "altvarname" => "stock_transfer", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "supplier_advance_cut", "varname" => "test2_at_localhost__supplier_advance_cut", "altvarname" => "supplier_advance_cut", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "sys_manage", "varname" => "test2_at_localhost__sys_manage", "altvarname" => "sys_manage", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "theme_list", "varname" => "test2_at_localhost__theme_list", "altvarname" => "theme_list", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		$this->lstTables[] = array("name" => "units", "varname" => "test2_at_localhost__units", "altvarname" => "units", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
	}

	/**
      * Returns table object by table name.
      * @intellisense
      */
	function & Table($strTable, $schema = "", $connName = "")
	{
		$this->FillTablesList();
		foreach($this->lstTables as $tbl)
		{
			if(strtoupper($strTable)==strtoupper($tbl["name"]) &&
				( $schema == "" || strtoupper($schema) == strtoupper($tbl["schema"]) ) &&
				($connName == "" || strtoupper($connName) == strtoupper($tbl["connName"]) ) )
			{
				$this->CreateClass($tbl);
				return $this->{"tbl".$tbl["varname"]};
			}
		}
//	check table names without dbo. and other prefixes
		foreach($this->lstTables as $tbl)
		{
			if(strtoupper(cutprefix($strTable))==strtoupper(cutprefix($tbl["name"])))
			{
				$this->CreateClass($tbl);
				return $this->{"tbl".$tbl["varname"]};
			}
		}
		$dummy=null;
		return $dummy;
	}
	
	function CreateClass(&$tbl)
	{
		if($this->{"tbl".$tbl["varname"]})
			return;
//	load table info
		global $dal_info;
		include(getabspath("include/dal/".($tbl["varname"]).".php"));
//	create class and object

		$classname="class_".$tbl["varname"];
		$str = "class ".$classname." extends tDALTable  {";
		foreach($dal_info[$tbl["varname"]] as $fld)
		{
			$str.=' var $'.$fld["varname"].'; ';
		}
		
		$tableName = $tbl["name"];
		if( $tbl["schema"] )
			$tableName = $tbl["schema"] . "." . $tbl["name"];
		$str.=' function __construct()
			{
				$this->m_TableName = \''.escapesq( $tableName ).'\';
				$this->infoKey = \'' . $tbl["varname"] . '\';
				$this->setConnection(\''. $tbl["connId"] .'\');
			}
		};';
		eval($str);
		$this->{"tbl".$tbl["varname"]} = new $classname;
		$this->{$tbl["altvarname"]} = $this->{"tbl".$tbl["varname"]};
		$this->Table[$tbl["name"]]=&$this->{"tbl".$tbl["varname"]};
	}
	
	/**
      * Returns list of table names.
      * @intellisense
      */
	function GetTablesList()
	{
		$this->FillTablesList();
		$res=array();
		foreach($this->lstTables as $tbl)
			$res[]=$tbl["name"];
		return $res;
	}
	
	/**
      * Get list of table fields by table name.
      * @intellisense
      */
	function GetFieldsList($table)
	{
		$tbl = $this->Table($table);
		return $tbl->GetFieldsList();
	}
	
	/**
      * Get field type by table name and field name.
      * @intellisense
      */
	function GetFieldType($table,$field)
	{
		$tbl = $this->Table($table);
		return $tbl->GetFieldType($field);
	}

	/**
      * Get table key fields by table name.
      * @intellisense
      */
	function GetDBTableKeys($table)
	{
		$tbl = $this->Table($table);
		return $tbl->GetDBTableKeys();
	}
}

$dal = new tDAL;

/**
 * Data Access Layer table class.
 */ 
class tDALTable
{
	var $m_TableName;
	var $infoKey;
	var $Param = array();
	var $Value = array();
	/**
	 * @type Connection
	 */
	var $_connection;
	
	/**
	 * Set the connection property
	 * @param String connId
	 */
	function setConnection( $connId )
	{
		global $cman;
		$this->_connection = $cman->byId( $connId );
	}
	
	/**
      * Get table key fields.
      * @intellisense
      */
	function GetDBTableKeys()
	{
		global $dal_info;
		if( !array_key_exists($this->infoKey, $dal_info) || !is_array($dal_info[ $this->infoKey ]) )
			return array();
		
		$ret = array();
		foreach($dal_info[ $this->infoKey ] as $fname=>$f)
		{
			if( @$f["key"] )
				$ret[] = $fname;
		}
		return $ret;
	}
	
	/**
      * Get list of table fields.
      * @intellisense
      */
	function GetFieldsList()
	{
		global $dal_info;
		return array_keys( $dal_info[ $this->infoKey ] );
	}

	/**
      * Get field type.
      * @intellisense
      */
	function GetFieldType($field)
	{
		global $dal_info;
		
		if( !array_key_exists( $field, $dal_info[ $this->infoKey ]) )
			return 200;
			
		return $dal_info[ $this->infoKey ][ $field ]["type"];
	}
	
	/**
	 *
	 */
	function PrepareValue($value, $type)
	{
		if( $this->_connection->dbType == nDATABASE_Oracle || $this->_connection->dbType == nDATABASE_DB2 || $this->_connection->dbType == nDATABASE_Informix )
		{
			if( IsBinaryType($type) )
			{
				if( $this->_connection->dbType == nDATABASE_Oracle )
					return "EMPTY_BLOB()";
					
				return "?";
			}
			
			if( $this->_connection->dbType == nDATABASE_Informix  && IsTextType($type) )
				return "?";
		}
	
		if( IsDateFieldType($type) )
		{
			if( !$value )
				return "null";
			else
				$this->_connection->addDateQuotes( $value );
		}
		
		if( NeedQuotes($type) )
			return $this->_connection->prepareString( $value );

		return 0 + $value;
	}
	
	/**
      * Get table name.
      * @intellisense
      */
	function TableName()
	{
		return $this->_connection->addTableWrappers( $this->m_TableName );
	} 

	/**
	 * @param Array blobs
	 * @param String dalSQL
	 * @param Array tableinfo
	 */
	protected function Execute_Query($blobs, $dalSQL, $tableinfo)
	{		
		$blobTypes = array();
		if( $this->_connection->dbType == nDATABASE_Informix )
		{		
			foreach( $blobs as $fname => $fvalue )
			{
				$blobTypes[ $fname ] = $tableinfo[ $fname ]["type"];
			}
		}

		$this->_connection->execWithBlobProcessing( $dalSQL, $blobs, $blobTypes );
	}

	/**
      * Add new record to the table.
      * @intellisense
      */
	function Add() 
	{
		global $dal_info;
		
		$insertFields = "";
		$insertValues = "";
		$tableinfo = &$dal_info[ $this->infoKey ];
		$blobs = array();
		//	prepare parameters		
		foreach($tableinfo as $fieldname => $fld)
		{
			if( isset($this->{$fld['varname']}) )
			{
				$this->Value[ $fieldname ] = $this->{$fld['varname']};
			}
			
			foreach($this->Value as $field => $value)
			{
				if( strtoupper($field) != strtoupper($fieldname) )
					continue;
					
				$insertFields.= $this->_connection->addFieldWrappers( $fieldname ).",";
				$insertValues.= $this->PrepareValue($value,$fld["type"]) . ",";
				
				if( $this->_connection->dbType == nDATABASE_Oracle || $this->_connection->dbType == nDATABASE_DB2 || $this->_connection->dbType == nDATABASE_Informix )
				{
					if( IsBinaryType( $fld["type"] ) )
						$blobs[ $fieldname ] = $value;
						
					if( $this->_connection->dbType == nDATABASE_Informix && IsTextType( $fld["type"] ) )
						$blobs[ $fieldname ] = $value;
				}
				break;
			}
		}
		//	prepare and exec SQL
		if( $insertFields != "" && $insertValues != "" )		
		{
			$insertFields = substr($insertFields, 0, -1);
			$insertValues = substr($insertValues, 0, -1);
			$dalSQL = "insert into ".$this->_connection->addTableWrappers( $this->m_TableName )." (".$insertFields.") values (".$insertValues.")";
			$this->Execute_Query($blobs, $dalSQL, $tableinfo);
		}
		//	cleanup		
	    $this->Reset();
	}

	/**
      * Query all records from the table.
      * @intellisense
      */
	function QueryAll()
	{
		$dalSQL = "select * from ".$this->_connection->addTableWrappers( $this->m_TableName );
		return $this->_connection->query( $dalSQL );
	}

	/**
      * Do a custom query on the table.
      * @intellisense
      */
	function Query($swhere = "", $orderby = "")
	{
		if ($swhere)
			$swhere = " where ".$swhere;
			
		if ($orderby)
			$orderby = " order by ".$orderby;
			
		$dalSQL = "select * from ".$this->_connection->addTableWrappers( $this->m_TableName ).$swhere.$orderby;
		return $this->_connection->query( $dalSQL );
	}

	/**
      * Delete a record from the table.
      * @intellisense
      */
	function Delete()
	{
		global $dal_info;
		
		$deleteFields = "";
		$tableinfo = &$dal_info[ $this->infoKey ];
		//	prepare parameters		
		foreach($tableinfo as $fieldname => $fld)
		{
			if( isset($this->{$fld['varname']}) )
			{
				$this->Param[ $fieldname ] = $this->{$fld['varname']};
			}
			
			foreach($this->Param as $field => $value)
			{
				if( strtoupper($field) != strtoupper($fieldname) )
					continue;
					
				$deleteFields.= $this->_connection->addFieldWrappers( $fieldname )."=". $this->PrepareValue($value, $fld["type"]) . " and ";
				break;
			}
		}
		//	do delete
		if ($deleteFields)
		{
			$deleteFields = substr($deleteFields, 0, -5);
			$dalSQL = "delete from ".$this->_connection->addTableWrappers( $this->m_TableName )." where ".$deleteFields;
			$this->_connection->exec( $dalSQL );
		}
	
		//	cleanup
	    $this->Reset();
	}

	/**
      * Reset table object.
      * @intellisense
      */
	function Reset()
	{
		global $dal_info;
		
		$this->Value = array();
		$this->Param = array();
		
		$tableinfo = &$dal_info[ $this->infoKey ];
		//	prepare parameters		
		foreach($tableinfo as $fieldname => $fld)
		{
			unset($this->{$fld["varname"]});
		}
	}	

	/**
      * Update record in the table.
      * @intellisense
      */
	function Update()
	{
		global $dal_info;
		
		$tableinfo = &$dal_info[ $this->infoKey ];
		$updateParam = "";
		$updateValue = "";
		$blobs = array();

		foreach($tableinfo as $fieldname => $fld)
		{
			$command = 'if(isset($this->'.$fld['varname'].')) { ';
			if( $fld["key"] )
				$command.= '$this->Param[\''.escapesq($fieldname).'\'] = $this->'.$fld['varname'].';';
			else
				$command.= '$this->Value[\''.escapesq($fieldname).'\'] = $this->'.$fld['varname'].';';
			$command.= ' }';
			
			eval($command);
			
			if( !$fld["key"] && !array_key_exists( strtoupper($fieldname), array_change_key_case($this->Param, CASE_UPPER) ) )
			{
				foreach($this->Value as $field => $value)
				{
					if( strtoupper($field) != strtoupper($fieldname) )
						continue;
						
					$updateValue.= $this->_connection->addFieldWrappers( $fieldname )."=".$this->PrepareValue($value, $fld["type"]) . ", ";
					
					if( $this->_connection->dbType == nDATABASE_Oracle || $this->_connection->dbType == nDATABASE_DB2 || $this->_connection->dbType == nDATABASE_Informix )
					{
						if( IsBinaryType( $fld["type"] ) )
							$blobs[ $fieldname ] = $value;
							
						if( $this->_connection->dbType == nDATABASE_Informix && IsTextType( $fld["type"] ) )	
							$blobs[ $fieldname ] = $value;		
					}
					break;
				}
			}
			else
			{
				foreach($this->Param as $field=>$value)
				{
					if( strtoupper($field) != strtoupper($fieldname) )
						continue;
						
					$updateParam.= $this->_connection->addFieldWrappers( $fieldname )."=".$this->PrepareValue($value, $fld["type"]) . " 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 ".$this->_connection->addTableWrappers( $this->m_TableName )." set ".$updateValue." where ".$updateParam;
			$this->Execute_Query($blobs, $dalSQL, $tableinfo);
		}

		//	cleanup
		$this->Reset();
	}
	
	/**
	 * Select one or more records matching the condition
	 */
	function FetchByID()
	{
		global $dal_info;
		$tableinfo = &$dal_info[ $this->infoKey ];

		$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->Param as $field => $value)
			{
				if( strtoupper($field) != strtoupper($fieldname) )
					continue;
					
				$dal_where.= $this->_connection->addFieldWrappers( $fieldname )."=".$this->PrepareValue($value, $fld["type"]) . " and ";
				break;
			}
		}
		// cleanup
		$this->Reset();
		// construct and run SQL
		if ($dal_where)
			$dal_where = " where ".substr($dal_where, 0, -5);
			
		$dalSQL = "select * from ".$this->_connection->addTableWrappers( $this->m_TableName ).$dal_where;
		return $this->_connection->query( $dalSQL );
	}
}

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);
}

?>