<?php
class backupModel extends model{
	
	public $base;
	public $database;
	public $backdir;
	function __construct(&$base){
		parent::__construct($base);
		$this->base=$base;
		$this->backdir=ROOT_PATH."attach/backup/";
	}
	
	/*
	创建表 语句
	*/
	function getTableCreate()
	{
		$tables=$this->getTables();
		$arr=array();
		if($tables)
		{
			foreach($tables as $table)
			{
				$sql=" show Create table  $table  ";
				$res= $this->query($sql);
				$rs=$this->fetch_row($res);
				$arr[]=preg_replace("/CREATE TABLE/i","CREATE TABLE IF NOT EXISTS",$rs[1],1);
			}
			 
		}
		return $arr;
	}
	
	/*是否合并表*/
	public function isMerge($table){
		$sql=" show Create table  $table  ";
		$res= $this->query($sql);
		$rs=$this->fetch_row($res);
		if(preg_match("/ENGINE=MRG_MyISAM/i",$rs[1])){
			return true;
		}
		return false;
		
	}
	
	/*列出所有表名称*/
	function getTables()
	{
		$res=$this->query("SHOW TABLES FROM ".$this->database." ");
		while($rs=mysql_fetch_array($res,MYSQL_NUM))
		{
			if(!$this->isMerge($rs[0])){
				$arr[]=$rs[0];
			}
		}
		
		return $arr;
	}
	
	
	/*备份表格式*/
	function backTable()
	{
		//备份创建表
		$arr=$this->getTableCreate();
		$str="";
		if($arr)
		{
			foreach($arr as $table)
			{
				$str.=$table.";\r\n--[sqlend]\r\n";
			}
		}
		file_put_contents($this->backdir."/".$this->database.".sql",$str);
	}
	/*还原表*/
	public function restoreTable(){
		$content=file_get_contents($this->backdir."/".$this->database.".sql",$str);
		$arr=explode("--[sqlend]",$content);
		foreach($arr as $sql){
			$sql=trim($sql);
			if(!empty($sql)){
				$this->query($sql);
			}
		}
	}
	
	/*选出表的所有列*/
	function getColumns($table)
	{
		$res=$this->query('SHOW COLUMNS FROM $table ');
		while($rs=$this->fetch_array($res,MYSQL_NUM))
		{
			$arr[$rs[0]]=$rs[0];
		}
		return $arr;
	}
	/*获取表的数据总数*/
	function getrscount($table)
	{		
		return $this->getOne("SELECT count(*) FROM $table");
	}
	
	/*选出表数据*/
	function backdata($table,$start=0,$limit=10000)
	{
		$str='';
		$res=$this->query("SELECT * FROM $table limit $start,$limit ");
		while($rs=$this->fetch_array($res,MYSQL_ASSOC))
		{
			$i=0;
			$num=count($rs);
			$s="REPLACE INTO $table SET";
			
			foreach($rs as $k=>$v)
			{
				$dot=($i==$num-1)?"":",";
				$s.=" $k='".addslashes($v)."'$dot";
				$i++;
			}
			$s.=";\r\n--[sqlend]\r\n";
			$str.=$s;
			
		}
		
	
		file_put_contents($this->backdir."/data_{$table}_$start.sql",$str);
	}
	/*备份数据*/
	function backAllData()
	{
		$tables=$this->getTables();
		foreach($tables as $table)
		{		
			$this->backdata($table);
		}
	}

	
	
	/*还原某个文件数据*/
	
	function restoreDb($file)
	{
		$content=file_get_contents($this->backdir."/$file");
		$arr=explode("--[sqlend]",$content);
		if($arr){
			foreach($arr as $sql){
				$sql=trim($sql);
				if(!empty($sql)){
					$this->query($sql);
				}
			}
		}
	}
	
	function getfiles()
	{
		$dh=opendir($this->backdir);
		$arr=array();
		while(($file=readdir($dh))!=false)
		{
			if($file!="." && $file!=".." && $file!=$this->database.".sql")
			{
				$arr[]=$file;
			}
		}
		return $arr;
	}
	
	
	
}
?>