<?php
class netsMySql extends netsDbCache {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public $sDbType='Mongo';
	public $sHost='localhost';
	public $sPort='27017';
	public $sUName='root';
	public $sPxd='K3FCJKDX9WOEA5HZ';
	public $sDBName='nets_xf7vsfk2e';
	public $sCharset='utf-8';
	
	public $sysConnection=array();
	public $sysDatabase=array();
	public $sysSQL='';
	public $sysSqlCount=0;
	public $sysPageInfo=array();
	
	public function netsMySql($pConnection){
		$this->netsDbCache();
		$tmpAR0=explode($this->sysSPLIT1,$pConnection);
		if(count($tmpAR0)==6){
			$this->sHost=$tmpAR0[0];
			$this->sPort=$tmpAR0[1];
			$this->sUName=$tmpAR0[2];
			$this->sPxd=$tmpAR0[3];
			$this->sDBName=$tmpAR0[4];
			$this->sCharset=$tmpAR0[5];
		}else {
			$this->getError_0('','数据库链接错误。','DATABASE ERROR','10001');
		}
		$tmpSTR='mongodb://{Nets.UName}:{Nets.UPxd}@{Nets.Host}:{Nets.Port}';
		$tmpSTR=str_replace('{Nets.UName}',$this->sUName,$tmpSTR);
		$tmpSTR=str_replace('{Nets.UPxd}',$this->sPxd,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Host}',$this->sHost,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Port}',$this->sPort,$tmpSTR);
		$this->sysConnection=new Mongo($tmpSTR);
		//$this->getError_0('',mysql_error(),'DATABASE ERROR','10002');
		$this->sysDatabase=$this->sysConnection->$this->sDBName;
		
	}
	
	public function TestConnection(){
		//-- ------ ### ------ --
		$tmpSTR='mongodb://{Nets.UName}:{Nets.UPxd}@{Nets.Host}:{Nets.Port}';
		$tmpSTR=str_replace('{Nets.UName}',$this->sUName,$tmpSTR);
		$tmpSTR=str_replace('{Nets.UPxd}',$this->sPxd,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Host}',$this->sHost,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Port}',$this->sPort,$tmpSTR);
		$this->sysConnection=new Mongo($tmpSTR);
		
		return 0;
	}
	public function TestDbExist(){
		//-- ------ ### ------ --
		$tmpSTR='mongodb://{Nets.UName}:{Nets.UPxd}@{Nets.Host}:{Nets.Port}';
		$tmpSTR=str_replace('{Nets.UName}',$this->sUName,$tmpSTR);
		$tmpSTR=str_replace('{Nets.UPxd}',$this->sPxd,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Host}',$this->sHost,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Port}',$this->sPort,$tmpSTR);
		$this->sysConnection=new Mongo($tmpSTR);
		$this->sysDatabase=$this->sysConnection->$this->sDBName;
		
		return 0;
	}
	public function TestTableExist($pTablename){
		$tmpSTR='';
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		$tmpTable=$this->sysDatabase->$pTablename;
		
		return 0;
	}
	public function Create($pDBName){
		//-- ------ ### ------ --

		return 0;
	}
	public function Search($pTable,$pCondition){
		$tmpData0=array();
		$tmpLine='';
		
		$tmpTable=$this->sysDatabase->$pTable;
		$tmpCursor=$tmpTable->find($pCondition);
		$Coun0=0;
		while($tmpCursor->hasNext()){
			$tmpData0[$Coun0]=$tmpCursor->getNext();
			$Coun0++;
		}

		return  $tmpData0;
	}
	/**
	 * $pSQL
	 * $pPages - PageIndex,PageCount,PageSize,RecordCount
	 */
	public function SearchP($pTable,$pCondition,$pPages){
		$tmpData0=array();
		$tmpAR0=array();
		$tmpTable=$this->sysDatabase->$pTable;
		//-- ------ #Cala record range ------ --
		if($pPages[3]==0){
			$pPages[3]=$tmpTable->count($pCondition);
		}
		if($pPages[0]==0){
			$pPages[0]=1;
		}
		if($pPages[1]==0){
			$pPages[1]=1;
		}
		if($pPages[2]==0){
			$pPages=40;
		}
		$pPages[1]=intval($pPages[3]/$pPages[2]);
		if($pPages[3]%$pPages[2]>0){
			$pPages[1]++;
		}
		if($pPages[0]==0){
			$pPages[0]=1;
		}
		if($pPages[1]==0){
			$pPages[1]=1;
		}
		//-- ------ #Get result ------ --
		$tmpAR0[0]=$pPages[0]*$pPages[2];
		$tmpAR0[1]=($pPages[0]-1)*$pPages[2];
		$pSQL.=' LIMIT '.$tmpAR0[1].','.$pPages[2].' ';

		
		$tmpCursor=$tmpTable->find($pCondition);
		$Coun0=0;
		while($tmpCursor->hasNext()){
			$tmpData0[$Coun0]=$tmpCursor->getNext();
			$Coun0++;
		}
		$this->sysPageInfo=$pPages;
		return  $tmpData0;
	}
	public function SearchARP($pAR,$pPages){
		$tmpDataCollection=array();
		$tmpAR0=array();
		//-- ------ #Cala record range ------ --
		if($pPages[3]==0){
			$pPages[3]=count($pAR);
		}
		if($pPages[0]==0){
			$pPages[0]=1;
		}
		if($pPages[1]==0){
			$pPages[1]=1;
		}
		if($pPages[2]==0){
			$pPages=40;
		}
		$pPages[1]=intval($pPages[3]/$pPages[2]);
		if($pPages[3]%$pPages[2]>0){
			$pPages[1]++;
		}
		if($pPages[0]==0){
			$pPages[0]=1;
		}
		if($pPages[1]==0){
			$pPages[1]=1;
		}
		//-- ------ #Get result ------ --
		$tmpEndindex=$pPages[0]*$pPages[2];
		$tmpStartindex=($pPages[0]-1)*$pPages[2];

		$Coun0=0;
		foreach ($pAR as $Row0){
			if ($Coun0>=$tmpStartindex && $Coun0<$tmpEndindex) {
				$tmpDataCollection[$Coun0]=$Row0;
			}
			$Coun0++;
		}
		$this->sysPageInfo=$pPages;
		return  $tmpDataCollection;
	}
	public function Insert_mongo($pRows){
		$this->sysErrorFuncID=$this->sysErrorClassID.'01';
		//-- ------ #Get index ------ --
		$tmpTable=$this->sysDatabase->$pTable;
		$tmpTable->insert($pRows);
		
		return 0;
	}
	public function Update_mongo($pRows,$pCondition){
		$this->sysErrorFuncID=$this->sysErrorClassID.'01';
		//-- ------ #Get index ------ --
		$tmpTable=$this->sysDatabase->$pTable;
		$tmpTable->update($pRows,$pCondition);
		
		return 0;
	}
	public function Delete_mongo($pCondition){
		$this->sysErrorFuncID=$this->sysErrorClassID.'01';
		//-- ------ #Get index ------ --
		$tmpTable=$this->sysDatabase->$pTable;
		$tmpTable->remove($pCondition);
		
		return 0;
	}
	public function TransStart(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';

		return 0;
	}
	public function TransCommit(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'03';

		return 0;
	}
	public function TransRollback(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'04';

		return 0;
	}
	public function Close(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'05';
		
		$this->sysConnection->close();
		/*
		//$this->getError_0('',mysql_error(),'Database error','10013');
		$this->sysLastError=mysql_error().'<br />SQL:'.$tmpSTR;
		$this->sysLastError=mysql_error();
		return $this->sysErrorFuncID.'001';
		*/
		return 0;
	}
}
?>