<?php

# mysqli执行静态态.
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_object(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;
            self::$link = new mysqli($_config['dbhost'], $_config['dbuser'], $_config['dbpw'], $_config['dbname']);
            if (self::$link->connect_error || mysqli_connect_error()){
                exit('mysqli connect_error');
            }
            if (!defined('MYSQL_OPT_READ_TIMEOUT')) {
                    define('MYSQL_OPT_READ_TIMEOUT',  11);
            }
            if (!defined('MYSQL_OPT_WRITE_TIMEOUT')) {
                    define('MYSQL_OPT_WRITE_TIMEOUT', 12);
            }
            self::$link->options(MYSQL_OPT_READ_TIMEOUT, 3);
            self::$link->options(MYSQL_OPT_WRITE_TIMEOUT, 2);
            self::$link->set_charset(empty($_config['dbcharset']) === false?$_config['dbcharset']:'UTF8');
		}
	   
       return self::$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_object(self::$link) === false)
			return false;
            
        self::$query = null;
		if(empty($mysql_type) === true)
			self::$query = self::$link->query($query_sql);
            
        self::$querynum[] = $query_sql;
        self::get_error(1);
		return self::$query;
	}
 
	public static function fetch_array($query){
		if(is_object($query) === true){
		  self::connect();
		  $returndata = $query->fetch_array(MYSQLI_ASSOC);
          if(!$returndata)
            $returndata = false;
          return $returndata;
		}
		return false;
	}
	public static function fetch_row($query = false){
		if(is_object($query) === true){
		  self::connect();
          $query === false && $query = self::$query;
		  $returndata = $query->fetch_row();
          if(!$returndata)
            $returndata = false;
          return $returndata;
        }
		return false;
	}
	public static function affected_rows() {
        self::connect();
        return self::$link->affected_rows;
	}
    
	public static function num_fields($query = false) {
	    if(is_object($query) === true){
	       self::connect();
            $query === false && $query = self::$query;
	       return $query->field_count;
	    }
        return false;
	}
    
	public static function num_rows($query = false) {
	     if(is_object($query) === true){
	       self::connect();
           $query === false && $query = self::$query;
	       return $query->num_rows;
	    }
        return false;
	}
	public static function insert_id() {
	   self::connect();
	   $id = self::$link->insert_id;
		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(){
	    self::connect();
		if(is_object(self::$link) === true)
			self::$link->close();
	}
    
	# 一些特殊封装
	public static function get_one($query_sql){
	    self::connect();
		return self::fetch_array(self::query($query_sql));
	}

	public static function explain($query_sql){
	    self::connect();
		return self::fetch_array(self::query('EXPLAIN '.$query_sql));
	}
    public static function get_error($err = 0){
        if($err === 0)
            return self::$sql_error;
        if(self::$link->error){
            $err_tem = self::$link->errno.'|'.self::$link->error;
            self::$sql_error[] = $err_tem;
        }
    }
    public static function server_info(){
        self::connect();
        return self::$link->server_info;
    }
}