<?php
	/*
		�Ա����ֵ�ͨ����
		2011-11-20
	*/
	class Ass
	{
 		var $table_pre = PRE_TABLE;
		var $msg = array();
		function Ass($table_pre=PRE_TABLE)
		{
			$this->table_pre = $table_pre;
		}

		function table($table_name)
		{
			return $this->table_pre.$table_name;
		}
		
		function one($table_name,$filed,$where,$order='',$orderby='asc')
		{
			global $db;
			return $db->fetch_one($this->table($table_name),$filed,$where,$order,$orderby);
		}

		function number($table_name,$where)
		{
			global $db;
			return $db->number($this->table($table_name),$where);
		}

		function insert($table_name,$data)
		{
			global $db;
			return $db->insert_one($this->table($table_name),$data);
		}

		function update($table_name,$data,$where)
		{
			global $db;
			return $db->update_one($this->table($table_name),$data,$where);
		}

		function getMax($table_name,$filed,$where)
		{
			global $db;
			return $db->table_max($this->table($table_name),$filed,$where);
		}
		
		function delete($table_name,$where)
		{
			global $db;
			return $db->table_del($this->table($table_name),$where);
		}

		function sum($table_name,$field,$where='')
		{
			global $db;
			$sql = 'select sum('.$field.') from '.$this->table($table_name);
			if($where)
			{
				$sql.= ' where '.$where;
			}
 			$row = $db->fetch_array($db->query($sql));
			return isset($row[0])?$row[0]:0;
		}

		//��ȡ����
		function getCode($table,$field,$where)
		{
			$where = 'user_id='.$_SESSION['user_id'];
			return sprintf("%05s",$this->getMax($table,$field,$where));
		}
		
		//����һ�ű�
		function table_copy($newtable,$oldtable)
		{
			global $db;
			$newtable = str_replace('-','',$newtable);
			$newtable = str_replace('/','',$newtable);
			$oldtable = str_replace('-','',$oldtable);
			$oldtable = str_replace('/','',$oldtable);
			if($this->table_exists($newtable)){
				return true;
			}else{
				$sql = 'create table '.$this->table($newtable).' like '.$this->table($oldtable);
				return $db->query($sql);
			}
		}
		
		//�жϱ��Ƿ����
		function table_exists($table)
		{
			global $db;
			$table = str_replace('-','',$table);
			$table = str_replace('/','',$table);
			$sql = 'show tables like "'.$this->table($table).'"';
			return @mysql_num_rows($db->query($sql));
		}

		function table_export($table_from,$table_to,$field_array,$value_array,$where_array,$order_array)
		{
			global $db;
			$sql = 'INSERT INTO '.$this->table($table_to).' ('.$field_array.') SELECT '.$value_array.' FROM '.$this->table($table_from);
			if($where_array)
			{
				$sql.= " WHERE ".$where_array;
			}
			if($order_array)
			{
				$sql.= " ORDER BY ".$order_array;
			}
			return $db->query($sql);
		}

		//-------------------------------------------
		//Transaction Processing Function
		//-------------------------------------------
		//Begin Transaction
		//-------------------------------------------
		function TransBegin()
		{
			global $db;
			$sql_tran = "SET AUTOCOMMIT = 0";
			$rst_tran = $db->query($sql_tran);
		}

		//-------------------------------------------
		//End Transaction
		//-------------------------------------------
		function TransEnd()
		{
			global $db;
			$sql_tran = "SET AUTOCOMMIT = 1";
			$rst_tran = $db->query($sql_tran);
		}

		//-------------------------------------------
		//Commit Transaction
		//-------------------------------------------
		function TransCommit()
		{
			global $db;
			$sql_commit = "COMMIT";
			$rst_commit = $db->query($sql_commit);
		}

		//-------------------------------------------
		//Rollback Transaction
		//-------------------------------------------
		function TransRollback()
		{
			global $db;
			$sql_rall = "ROLLBACK";
			$rst_rall = $db->query($sql_rall);
		}

		function data($sql)
		{
			global $db;
			return $db->fetch_data($sql);
		}

		function count($table_name,$field,$where='')
		{
			global $db;
			$sql = 'select count('.$field.') from '.$this->table($table_name);
			if($where)
			{
				$sql.= ' where '.$where;
			}
 			$row = $db->fetch_array($db->query($sql));
			return isset($row[0])?$row[0]:0;
		}

	}
?>