<?php
# mysql执行静态态.
defined('IN_SYSTEM') === false && exit('PHPnew Blog.');
class mysql {
	protected static $link,$config,$query,$sql_error;
	public static $querynum=array();

	protected static function connect(){
		if(is_resource(self::$link) === false || empty(self::$link) === true){
			if(empty(self::$config) === true){
				self::$config = array('dbhost'=>DBHOST,'dbuser'=>DBUSER,'dbpw'=>DBPW,'pconnect'=>PCONNECT,'dbcharset'=>DBCHARSET,'dbname'=>DBNAME);
			}
            
			$_config = self::$config;
			$fun = empty($_config['pconnect']) === true?'mysql_connect':'mysql_pconnect';
			$link = $fun($_config['dbhost'],$_config['dbuser'],$_config['dbpw']);
            $sedb = mysql_select_db($_config['dbname'],$link);
            
            if(empty($sedb) === true){
               die('mysql can not connect');
            }
            
			function_exists('mysql_set_charset') === true && mysql_set_charset((empty($_config['dbcharset']) === false?$_config['dbcharset']:'UTF8'),$link);
			mysql_query('SET NAMES '.(empty($_config['dbcharset']) === false?$_config['dbcharset']:'UTF8'));
            
            if(is_resource($link) === true)
			     self::$link = $link;
		}
	}

	public static function query($query_sql,$mysql_type = false) {
	   $query_sql = strtr($query_sql,array('DB_'=>TABLEPRE));
	   if($mysql_type === 1){
           exit("$query_sql");
	   }
        if(empty($query_sql) === false){
		  self::connect();
        }
		if(is_resource(self::$link) === false)
			return false;
		if(empty($mysql_type) === true)
			self::$query = mysql_query($query_sql,self::$link);

		if(empty($mysql_type) === false)
			self::$query = mysql_unbuffered_query($query_sql,self::$link);
        self::$querynum[] = $query_sql;
        self::get_error(1);
		return self::$query;
	}
 
	public static function fetch_array($query){
		if(is_resource($query) === true)
		  return mysql_fetch_array($query,MYSQL_ASSOC);
		return false;
	}
	public static function fetch_row($query){
		if(is_resource($query) === true)
		  return mysql_fetch_row($query);
		return false;
	}
	public static function affected_rows() {
		return mysql_affected_rows(self::$link);
	}
    
	public static function num_fields($query) {
	    if(is_resource($query) === true)
		  return mysql_num_fields($query);
        return false;
	}
    
	public static function num_rows($query) {
	    if(is_resource($query) === true)
		  return mysql_num_rows($query);
        return false; 
	}
	public static function insert_id() {
	   $id = mysql_insert_id(self::$link);
		if($id > 0){
			return $id;
		}else {
			$mysql_data = self::fetch_row(self::query('SELECT last_insert_id()'));
			return $mysql_data[0];
		}
	}

	public static function close(){
		if(is_resource(self::$link) === true)
			mysql_close(self::$link);
	}
    
	# 一些特殊封装
	public static function get_one($query_sql){
		return self::fetch_array(self::query($query_sql));
	}

	public static function explain($query_sql){
		return self::fetch_array(self::query('EXPLAIN '.$query_sql));
	}
    public static function get_error($err = 0){
        if($err === 0)
            return self::$sql_error;
        $err_tem = mysql_error();
        if($err_tem)
            self::$sql_error[] = $err_tem;
    }
}