<?php
class netsMySql extends netsDbCache {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public $sDbType='MySql';
	public $sHost='localhost';
	public $sPort='3306';
	public $sUName='root';
	public $sPxd='K3FCJKDX9WOEA5HZ';
	public $sDBName='nets_xf7vsfk2e';
	public $sCharset='utf-8';
	
	public $sysConnection='';
	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');
		}
		$this->sysConnection=mysql_connect($this->sHost.':'.$this->sPort,$this->sUName,$this->sPxd);
		if(mysql_error()!=''){
			$this->getError_0('',mysql_error(),'DATABASE ERROR','10002');
		}
		mysql_select_db($this->sDBName);
		if(mysql_error()!=''){
			$this->getError_0('',mysql_error(),'DATABASE ERROR','10003');
		}
		//SET NAMES <Nets.Charset>
		$tmpSTR=' SET NAMES '.str_replace('-','',$this->sysCharset);
		$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 SQL:<br />'.$tmpSTR,'10004');
			$this->getError_0('',mysql_error(),'DATABASE ERROR','10004');
		}
		//SET AUTOCOMMIT=0;
		/*
		$tmpSTR=' SET AUTOCOMMIT=1; ';
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		mysql_query($tmpSTR,$this->sysConnection);
		if(mysql_error()!=''){
			$this->getError_0('',mysql_error(),'<br />'.$this->sysConnection,'0');
		}
		*/
	}
	
	public function TestConnection(){
		//-- ------ ### ------ --
		$this->sysConnection=mysql_connect($this->sHost.':'.$this->sPort,$this->sUName,$this->sPxd);
		if(!$this->sysConnection){
			$this->sysLastError=mysql_error();
			return 1;
		}
		
		return 0;
	}
	public function TestDbExist(){
		//-- ------ ### ------ --
		$this->sysConnection=mysql_connect($this->sHost.':'.$this->sPort,$this->sUName,$this->sPxd);
		if(!$this->sysConnection){
			$this->sysLastError=mysql_error();
			return 1;
		}
		mysql_select_db($this->sDBName);
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
			return 1;
		}
		
		return 0;
	}
	public function TestTableExist($pTablename){
		$tmpSTR=' SELECT * FROM {Nets.Tablename} LIMIT 0,1; ';
		$tmpSTR=str_replace('{Nets.Tablename}',$pTablename,$tmpSTR);
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		$tmpResult=mysql_query($tmpSTR);
		if(mysql_error()!=''){
			return 1;
		}
		
		return 0;
	}
	public function Create($pDBName){
		//-- ------ ### ------ --
		$this->sysConnection=mysql_connect($this->sHost,$this->sUName,$this->sPxd);
		if(!$this->sysConnection){
			$this->sysLastError=mysql_error();
			return 1;
		}
		
		$tmpSQL=' CREATE DATABASE {Nets.DBName} ';
		$tmpSTR=str_replace('{Nets.DBName}',$this->sDBName,$tmpSQL);
		$tmpResult=mysql_query($tmpSTR,$this->sysConnection);
		/*
		if(mysql_error()!=''){
			$this->sysLastError=mysql_error();
			return 1;
		}
		*/
		
		return 0;
	}
	public function Search($pSQL){
		$tmpDataCollection=array();
		$tmpLine='';
		
		$tmpSTR=$pSQL;
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		$tmpResult=mysql_query($pSQL);
		if(mysql_error()!=''){
			$this->getError_0('',mysql_error(),'DATABASE ERROR SQL:<br />'.$tmpSTR,'10005');
			$this->getError_0('',mysql_error(),'DATABASE ERROR','10005');
		}
		$Coun0=0;
		while($tmpLine=mysql_fetch_array($tmpResult,MYSQL_ASSOC)){
			$tmpDataCollection[$Coun0]=$tmpLine;
			$Coun0++;
		}
		return  $tmpDataCollection;
	}
	/**
	 * $pSQL
	 * $pPages - PageIndex,PageCount,PageSize,RecordCount
	 */
	public function SearchP($pSQL,$pPages){
		$tmpDataCollection=array();
		$tmpAR0=array();
		//-- ------ #Cala record range ------ --
		if($pPages[3]==0){
			$tmpSTR=$pSQL;
			$this->sysSqlCount++;
			$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
			$tmpResult=mysql_query($tmpSTR);
			if(mysql_error()!=''){
				$this->getError_0('',mysql_error(),'DATABASE ERROR SQL:<br />'.$tmpSTR,'10006');
				$this->getError_0('',mysql_error(),'DATABASE ERROR','10006');
			}
			$pPages[3]=mysql_num_rows($tmpResult);
			if(mysql_error()!=''){
				$this->getError_0('',mysql_error(),'DATABASE ERROR SQL:<br />'.$tmpSTR,'10007');
				$this->getError_0('',mysql_error(),'DATABASE ERROR','10007');
			}
		}
		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].' ';
		$tmpSTR=$pSQL;
		$this->sysSqlCount++;
		$this->sysSQL.='#------ #'.$this->sysSqlCount.' ------<br />'.$tmpSTR.'<br /><br />';
		$tmpResult=mysql_query($tmpSTR);
		if(mysql_error()!=''){
			$this->getError_0('',mysql_error(),'DATABASE ERROR SQL:<br />'.$tmpSTR,'10008');
			$this->getError_0('',mysql_error(),'DATABASE ERROR','10008');
		}
		$Coun0=0;
		while($tmpLine=mysql_fetch_array($tmpResult,MYSQL_ASSOC)){
			$tmpDataCollection[$Coun0]=$tmpLine;
			$Coun0++;
		}
		$this->sysPageInfo=$pPages;
		return  $tmpDataCollection;
	}
	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 Execute($pSQL){
		$this->sysErrorFuncID=$this->sysErrorClassID.'01';
		
		$tmpSTR=$pSQL;
		$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<br />SQL:'.$tmpSTR,'10009');
			$this->sysLastError=mysql_error().'<br />SQL:'.$tmpSTR;
			$this->sysLastError=mysql_error();
			return 1;
		}
		return 0;
	}
	public function Executes($pSQLS,$pIndex,$pSize,$pType){
		$this->sysErrorFuncID=$this->sysErrorClassID.'01';
		//-- ------ #Get index ------ --
		$tmpSQLS_AR=explode(';'.$this->sysSPLIT4,$pSQLS);
		array_pop($tmpSQLS_AR);
		$tmpCount=$pSize;
		$tmpStartIndex=$pIndex*$pSize;
		if ($pSize==0){
			$pSize=count($tmpSQLS_AR);
			$tmpStartIndex=0;
		}
		//-- ------ #Executes ------ --
		$Coun0=$tmpStartIndex;
		while (($tmpStartIndex+$pSize)>$Coun0 && count($tmpSQLS_AR)>$Coun0){
			$tmpSTR=$tmpSQLS_AR[$Coun0];
			$this->sysSqlCount++;
			mysql_query($tmpSTR,$this->sysConnection);
			if(mysql_error()!='' && $pType==1){
				$this->sysLastError=mysql_error();
				return 1;
			}
			$Coun0++;
		}
		
		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 Close(){
		$this->sysErrorFuncID=$this->sysErrorClassID.'05';
		
		mysql_close($this->sysConnection);
		if(mysql_error()!=''){
			//$this->getError_0('',mysql_error(),'Database error','10013');
			$this->sysLastError=mysql_error().'<br />SQL:'.$tmpSTR;
			$this->sysLastError=mysql_error();
			return $this->sysErrorFuncID.'001';
		}
	}
}
?>