<?php
/*
phpAccess
*/
class Easy_Db_Access implements Easy_Db_Interface{
	protected  static  $db;
	public $result = null;
	public   $sql;
	/*
	?
	*/
	public function __construct($array, $pconnect=false)
	{
		self::connect();
	}
	/*
	??
	@param array ?? array()'filename'=>'d:/1.access', 'userid'=>'ddd', 'passwd'=>'xxxx';
	*/
	public function __connect($array, $pconnect=false){
		if ($array['filename']){
			self::$db=new com("adodb.connection");  
			self::$db->open("driver=microsoft access driver (*.mdb);dbq=".$array['filename']);
		}else{
			self::error('??¦ÄÇ÷!');
			exit;
		}
	}
	public static function getInstance(){
		return new self();
	}
	/*
	?
		@param $table string
		@param $array array()
	*/
	public function __insert($params){
		$str = null;
		$val = null;
		$table = $params[0];
		$array = $params[1];
		foreach($array as $key=>$value){
			$str.= $str ? ",`$key`" : "`$key`";
			$val.= $val ? ','.self::__quote(trim($value)) : self::__quote(trim($value));
		}
		$this->sql = "INSERT INTO $table($str) VALUES($val)";
		//echo $sql;
		self::$db->exec($this->sql) or die(print_r(self::$db->errorInfo()));
		return self::$db->lastInsertId();
	}
	/*
	???
	@param $table 
	@where ??
	*/
	public function __delete($params){
		$table = $params[0];
		$array = $params[1];
		$debug = $params[2];
		if (is_array($array)){
			foreach($array as $key=>$value){
				$wheres.= " AND `$key` = ".self::__quote($value);
			}
		}else{
			$wheres = 'AND '. $where;
		}
		$this->sql = "DELETE FROM $table WHERE 1=1 $wheres";
		unset($array, $wheres, $table, $debug, $params);
		return self::$db->exec($this->sql);
	}
	/*
	??
	*/
	public function __update($params){
		unset($str);
		$table = $params[0];
		$array = $params[1];
		$where = $params[2];
		$str = '';
		$wheres = '';
		if (is_array($array)){
			foreach($array as $key=>$value){
				$str.= (empty($str)===false) ? ",`$key`=".self::__quote($value) : "`$key`=".self::__quote($value);
			}
		}else{
			$str = $array;
		}
		unset($key,$value);
		if (is_array($where)){
			foreach($where as $key=>$value){
				$wheres.= " AND `$key` = ".self::__quote($value);
			}
		}elseif (is_null($where)===false){
			$wheres = 'AND '. $where;
		}else{
			unset($wheres);
		}
		//echo $str.'<br>';
		$this->sql = "UPDATE ".$table." SET $str WHERE 1=1 $wheres";
		unset($wheres);
		$rows = self::$db->exec($this->sql);
		self::__error(self::$db->errorInfo());
		return $rows;
	}
	/*
	?§ÖSQL
	$sql string
	return int
	*/
	public function __upsql($params){
		$this->sql = $params[0];
		if ($this->sql){
			$rows = self::$db->exec($this->sql);
			self::__error(self::$db->errorInfo());
			return $rows;
		}
	}
	/*
	???
	*/
	public function __fetAll(){
		return $this->result->fetchAll(PDO::FETCH_ASSOC);
	}
	/*
	???
	*/
	public function __fetRows()
	{
		$result = $this->result->fetch(PDO::FETCH_ASSOC);
		$this->result = NULL;
		return $result;
	}
	/*
	???¦Å?
	$pos int
	return string
	*/
	public function __fetOne($params)
	{
		$result = $params[0];
		$pos = $params[1];
		return self::$resultfetchColumn($pos);;
	}
	public function __query($params)
	{
		$this->sql = $params[0];
		$array = $params[1];
		$this->result = null;
		$sth = self::$db->prepare($this->sql,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
		self::__error(self::$db->errorInfo());
		if (is_array($array)){
			$sth->execute($array);
		}else{
			$sth->execute();
		}
		$this->result = $sth;
	}
	public function __beginTransaction()
	{
		$this->db->beginTransaction();
	}
	/*
		?
	*/
	public function __commit()
	{
		self::$db->commit();	
	}
	public function __rollBack(){
		self::$db->rollBack();
	}
	public function __quote($params){
		return self::$db->quote($params);
	}
	public function __error($params){
		if (reset($params) !== '00000'){
			echo '??!';
			echo ''.print_r($params);
		}
	}
	public function __showtable(){
		self::query("show table status");
		return self::fetAll();
	}
	public function __debug(){
		var_dump(self);
	}
	public function __call($fun, $args){
		$function = '__'.$fun;
		return self::$function($args);
	}
}
?>