<?php
class netsAdodb extends netsDbCache {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public $sDbType='Adodb';
	public $sFixPath='';
	public $sName='';
	public $sCharset='utf-8';
	public $sToCharset='gb2312';
	
	public $sysConnectionString;
	public $sysConnectionObject;
	public $sysRecordsetObject;
	public $sysSQL='';
	public $sysSqlCount=0;
	public $sysPageInfo=array();
	
	public function netsAdodb($pConnection){
		$this->netsDbCache();
		
		$this->sysConnectionString=$pConnection;
		$tmpAR0=explode($this->sysSPLIT1,$this->sysConnectionString);
		if(count($tmpAR0)==3){
			$this->sFixPath=$tmpAR0[0];
			$this->sName=$tmpAR0[1];
			$this->sCharset=$tmpAR0[2];
		}
		$tmpSTR='DRIVER={Microsoft Access Driver (*.mdb)}; DBQ='.realpath($this->sFixPath).'\\'.$this->sName;;
		$this->sysSqlCount++;
		$this->sysSQL.='------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		try {
			$this->sysConnectionObject=new com('ADODB.Connection');
		}catch (Exception $except){
			$this->getError_0('',iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$except->getMessage()),'Class:netsAdodb Error'.$this->sysConnectionObject,'0');
		}
		try {
			$tmpSTR=$this->sysConnectionObject->Open($tmpSTR);
		}catch (Exception $except){
			$this->getError_0('',iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$except->getMessage()),'Class:netsAdodb Error'.$this->sysConnectionObject,'0');
		}
		try {
			$this->sysRecordsetObject=new com('ADODB.Recordset');
		}catch (Exception $except){
			$this->getError_0('',iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$except->getMessage()),'Class:netsAdodb Error'.$this->sysConnectionObject,'0');
		}
		return;
	}
	
	public function Search($pSQL){
		$this->sysErrorFuncID=$this->sysErrorClassID.'01';
		//Initial parameter
		$Coun=0;
		$lOArray=array();
		$this->sysSqlCount++;
		$this->sysSQL.='------ #'.$this->sysSqlCount.' ------<br />'.$pSQL.'<br /><br />';
		//Open Recordset
		try {
			$pSQL=iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$pSQL);
			$this->sysRecordsetObject->Open($pSQL,$this->sysConnectionObject,1,1);
		}catch (Exception $except){
			$this->getError_0('',iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$except->getMessage()),'Class:netsAdodb Error'.$pSQL,'0');
		}
		//Fill to ARRAY
		$tmpCoun=0;
		while(!$this->sysRecordsetObject->Eof){
			$Coun=0;
			while($this->sysRecordsetObject->Fields->Count()>$Coun){
				$pColumnAR[$Coun]=$this->sysRecordsetObject->Fields($Coun)->Name;
				$tmpSTR=$this->sysRecordsetObject->Fields($Coun)->Value;
				$tmpSTR=iconv($this->sToCharset.'//IGNORE',$this->sysCharset,$tmpSTR);
				$lOArray[$tmpCoun][$this->sysRecordsetObject->Fields($Coun)->Name]=$tmpSTR;
				$Coun++;
			}
			$tmpCoun++;
			$this->sysRecordsetObject->MoveNext();
		}
		
		$this->sysRecordsetObject->Close();
		return $lOArray;
	}
	/**
	 * $pPagePara - PageIndex,PageCount,PageSize,RecordCount
	 */
	public function SearchP($pSQL,$pPagePara){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		//Initial parameter
		$Coun=0;
		$lNowRecordCount=array();
		$lOArray=array();
		$this->sysSqlCount++;
		$this->sysSQL.='------ #'.$this->sysSqlCount.' ------<br />'.$pSQL.'<br /><br />';
		//------------ Cala record range ------------ ------------ ------------ ------------ 
		//
		if($pPagePara[3]=='0'){
			try {
				$pSQL=iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$pSQL);
				$this->sysRecordsetObject->Open($pSQL,$this->sysConnectionObject,1,1);
			}catch (Exception $except){
				$this->getError_0('',iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$except->getMessage()),'Class:netsAdodb Error'.$pSQL,'0');
			}
			$pPagePara[3]=$this->sysRecordsetObject->recordcount;
			$this->sysRecordsetObject->Close();
		}
		//Check parameter rule
		if($pPagePara[0]=='0')
		{$pPagePara[0]='1';}
		if($pPagePara[1]=='0')
		{$pPagePara[1]='1';}
		//Cala page count
		if($pPagePara[2]==0){
			$pPagePara[2]=40;
		}
		$pPagePara[1]=intval($pPagePara[3]/$pPagePara[2]);
		if($pPagePara[3]%$pPagePara[2]>0)
		{$pPagePara[1]++;}
		//Recheck parameter rule
		if($pPagePara[0]=='0')
		{$pPagePara[0]='1';}
		if($pPagePara[1]=='0')
		{$pPagePara[1]='1';}
		//Get limit parameter
		$lNowRecordCount[0]=$pPagePara[0]*$pPagePara[2];
		$lNowRecordCount[1]=($pPagePara[0]-1)*$pPagePara[2];
		$pSQL=str_replace('*',' TOP '.$lNowRecordCount[0].' * ',$pSQL);
		//Open Recordset
		try {
			$pSQL=iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$pSQL);
			$this->sysRecordsetObject->Open($pSQL,$this->sysConnectionObject,1,1);
		}catch (Exception $except){
			$this->getError_0('',iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$except->getMessage()),'Class:netsAdodb Error'.$pSQL,'0');
		}
		//Fill to ARRAY
		$tmpCoun=array();
		$tmpCoun[0]=0;
		$tmpCoun[1]=0;
		while(!$this->sysRecordsetObject->Eof){
			if($tmpCoun[0]>$lNowRecordCount[1]-1 && 
				$tmpCoun[0]<$pPagePara[0]*$pPagePara[2] && 
				$tmpCoun[0]>=($pPagePara[0]-1)*$pPagePara[2]){
				$Coun=0;
				while($this->sysRecordsetObject->Fields->Count()>$Coun){
					$tmpSTR=$this->sysRecordsetObject->Fields($Coun)->Value;
					$tmpSTR=iconv($this->sToCharset.'//IGNORE',$this->sysCharset,$tmpSTR);
					$lOArray[$tmpCoun[1]][$this->sysRecordsetObject->Fields($Coun)->Name]=$tmpSTR;
					$Coun++;
				}
				$tmpCoun[1]++;
			}
			$tmpCoun[0]++;
			$this->sysRecordsetObject->MoveNext();
		}
		$this->sysPageInfo=$pPagePara;
		$this->sysRecordsetObject->Close();
		return $lOArray;
	}
	public function Execute($pSQL){
		$this->sysErrorFuncID=$this->sysErrorClassID.'03';
		//Initial parameter
		$this->sysSqlCount++;
		$this->sysSQL.='------ #'.$this->sysSqlCount.' ------<br />'.$pSQL.'<br /><br />';
		try {
			$pSQL=iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$pSQL);
			$this->sysConnectionObject->Execute($pSQL);
		}catch (Exception $except){
			$this->getError_0('',iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$except->getMessage()),'Class:netsAdodb Error'.$pSQL,'0');
			return 1;
		}
		
		return 0;
	}
	public function safeExecute($pSQL){
		$this->sysErrorFuncID=$this->sysErrorClassID.'03';
		//Initial parameter
		$this->sysSqlCount++;
		$this->sysSQL.='------ #'.$this->sysSqlCount.' ------<br />'.$pSQL.'<br /><br />';
		try {
			$pSQL=iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$pSQL);
			$this->sysConnectionObject->Execute($pSQL);
		}catch (Exception $except){
			$this->getError_0('',iconv($this->sysCharset,$this->sToCharset.'//IGNORE',$except->getMessage()),'Class:netsAdodb Error'.$pSQL,'0');
			return 1;
		}
		
		return 0;
	}
	public function TransStart(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		
		$tmpSTR=' START TRANSACTION; ';
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		/*
		mysql_query($tmpSTR,$this->sysConnection);
		if(mysql_error()!=''){
			//$this->getError_0('',mysql_error(),'Database error','10010');
			$this->sysLastError=mysql_error().'<br />SQL:'.$tmpSTR;
			$this->sysLastError=mysql_error();
			return $this->sysErrorFuncID.'001';
		}
		*/
		return 0;
	}
	public function TransCommit(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'03';
		
		$tmpSTR=' COMMIT; ';
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		/*
		mysql_query($tmpSTR,$this->sysConnection);
		if(mysql_error()!=''){
			//$this->getError_0('',mysql_error(),'Database error','10011');
			$this->sysLastError=mysql_error().'<br />SQL:'.$tmpSTR;
			$this->sysLastError=mysql_error();
			return $this->sysErrorFuncID.'001';
		}
		*/
		return 0;
	}
	public function TransRollback(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'04';
		
		$tmpSTR=' ROLLBACK; ';
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		/*
		mysql_query($tmpSTR,$this->sysConnection);
		if(mysql_error()!=''){
			//$this->getError_0('',mysql_error(),'Database error','10012');
			$this->sysLastError=mysql_error().'<br />SQL:'.$tmpSTR;
			$this->sysLastError=mysql_error();
			return $this->sysErrorFuncID.'001';
		}
		*/
		return 0;
	}
	public function Cache(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'04';
	}
	public function Close(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'05';
		//Close instance
		$this->sysConnectionObject->Close();
		return 0;
	}



}
?>