<?php
class Mysql extends Db {
	public function __construct($config) {
		$func = $config['pconnect'] == 1 ? 'mysql_pconnect' : 'mysql_connect';
		if (!$this -> linkID = @$func($config['host'], $config['user'], $config['pwd'])) {
			$this -> halt('Can not connect to MySQL server');
			return false;
		}
		if ($this -> version() > '4.1') {
			$serverset = "character_set_connection='$config[charset]', character_set_results='$config[charset]', character_set_client=binary";
			if ($this -> version() > '5.0.1') $serverset .= ", sql_mode=''";
			mysql_query("SET $serverset", $this -> linkID);
		}
		if ($config['name'] && !@mysql_select_db($config['name'] , $this -> linkID)) {
			$sql = 'CREATE DATABASE IF NOT EXISTS ' . $config['name'];
			if (!(defined('AUTO_CREATE_DATABASE') && @mysql_query($sql, $this -> linkID) && @mysql_select_db($config['name'] , $this -> linkID))) {
				$this -> halt('Cannot use database ' . $config['name']);
				return false;
			}
		}
		$this -> dbname = $config['name'];
		return $this -> linkID;
	}

	public function selectDb($dbname) {
		if (!@mysql_select_db($dbname , $this -> linkID)) return false;
		$this -> dbname = $dbname;
		return true;
	}

	public function query($sql) {
		$this -> queryStr = $sql;
		if ($query = @mysql_query($sql, $this -> linkID)) {
			$this -> querynum++;
			return $query;
		}
		$this -> halt('MySQL Query Error', $sql);
		return false;
	}
	/**
	 * 取得数据表的字段信息
	 *
	 * @access public
	 */
	public function getFields($tableName) {
		$query = $this -> query('SHOW COLUMNS FROM `' . $tableName . '`');
		$info = array();
		while ($r = $this -> fetchArray($query))
		$info[$r['Field']] = array('name' => $r['Field'],
			'type' => strtolower($r['Type']),
			'isnull' => (bool) (strtolower($r['Null']) == 'yes'), // not null is empty, null is yes
			'default' => $r['Default'],
			'primary' => (strtolower($r['Key']) == 'pri'),
			'autoinc' => (strtolower($r['Extra']) == 'auto_increment'),
			);
		return $info;
	}

	/**
	 * 取得数据库的表信息
	 *
	 * @access public
	 */
	public function getTables($dbName = '') {
		$sql = empty($dbName)?'SHOW TABLES ':'SHOW TABLES FROM `' . $dbName . '`';
		$query = $this -> query($sql);
		$info = array();
		while ($r = $this -> fetchArray($query)) $info[] = current($r);
		return $info;
	}

	public function fetchArray($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	public function fetchRow($query) {
		return mysql_fetch_row($query);
	}

	public function fetchArrays($query, $result_type = MYSQL_ASSOC) {
		$array = array();
		while ($r = mysql_fetch_array($query, $result_type)) $array[] = $r;
		return $array;
	}

	public function affectedRows() {
		return mysql_affected_rows($this -> linkID);
	}

	public function numRows($query) {
		return mysql_num_rows($query);
	}

	public function numFields($query) {
		return mysql_num_fields($query);
	}

	public function result($query, $row) {
		return @mysql_result($query, $row);
	}

	public function freeResult(&$query) {
		return mysql_free_result($query);
	}

	public function insertId() {
		return mysql_insert_id($this -> linkID);
	}

	public function escapeString($str) {
		return mysql_escape_string($str);
	}

	public function version() {
		return mysql_get_server_info($this -> linkID);
	}

	public function close() {
		return mysql_close($this -> linkID);
	}

	public function error() {
		return @mysql_error($this -> linkID);
	}

	public function errno() {
		return intval(@mysql_errno($this -> linkID)) ;
	}

	public function halt($message = '', $sql = '') {
		$this -> errormsg = "<strong>MySQL Query : </strong>$sql <br /><strong> MySQL Error : </strong>" . $this -> error() . " <br /> <strong>MySQL Errno : </strong>" . $this -> errno() . " <br /><strong> Message : </strong> $message";
		if ($this -> debug) {
			$msg = (defined('IN_ADMIN') or C('APP_DEBUG', 1)) ? $this -> errormsg : "Bad Request.";
			show_error($msg);
		}
	}
}