<?php

/**
 *  Power By Tmac PHP MVC framework
 *  $Author: zhangwentao $
 *  $Id: DbMySQL.class.php 55 2011-12-06 12:00:14Z zhangwentao $
 */
class DbMySQL extends Database
{

    /**
     * 初始化
     *
     * @global array $TmacConfig
     */
    public function __construct($config)
    {
        $this->config = $config;
        //连接数据库
        $this->link = $this->connect();
        if ($this->link) {
            //选择数据库
            $this->selectDatabase($this->config['database']);
            //设置编码与sql_mode
            mysql_query("SET NAMES '{$this->config['char_set']}', sql_mode=''", $this->link);
        } else {
            throw new TmacException('无法连接到数据库:' . $this->getError());
        }
    }

    /**
     * 连接数据库
     *
     * @global array $TmacConfig
     * @return bool
     */
    public function connect()
    {
        $fun = $this->config['pconnect'] ? 'mysql_pconnect' : 'mysql_connect';
        return $fun($this->config['hostname'],
                $this->config['username'],
                $this->config['password'], true);
    }

    /**
     * 选择数据库
     *
     * @param string $database
     * @return bool
     */
    public function selectDatabase($database)
    {
        return mysql_select_db($database, $this->link);
    }

    /**
     * 执行一条SQL查询语句 返回资源标识符
     *
     * @param string $sql
     */
    public function query($sql)
    {
        $rs = mysql_query($sql, $this->link);
        if ($rs) {
            $this->queryNum++;
            $this->debug($sql);
            return $rs;
        } else {
            $this->debug($sql, false, $this->getError());
            $this->success = false;
            return false;
        }
    }

    /**
     * 执行一条SQL语句 返回似乎执行成功
     *
     * @param string $sql
     */
    public function execute($sql)
    {
        if (mysql_query($sql, $this->link)) {
            $this->queryNum++;
            $this->debug($sql);
            return true;
        } else {
            $this->debug($sql, false, $this->getError());
            $this->success = false;
            return false;
        }
    }

    /**
     * 从结果集中取出数据
     *
     * @param resource $rs
     */
    public function fetch($rs)
    {
        return mysql_fetch_array($rs, $this->config['resulttype']);
    }

    /**
     * 返回结果集的数组形式row
     * @param <type> $result
     * @return <type>
     */
    public function fetch_row($result)
    {
        return mysql_fetch_row($result);
    }

    /**
     * 开始事务
     *
     * @return bool
     */
    public function startTrans()
    {
        return $this->execute('START TRANSACTION');
    }

    /**
     * 提交事务
     *
     * @return bool
     */
    public function commit()
    {
        return $this->execute('COMMIT');
    }

    /**
     * 回滚事务
     *
     * @return bool
     */
    public function rollback()
    {
        return $this->execute('ROLLBACK');
    }

    /**
     * 执行INSERT命令.返回AUTO_INCREMENT
     * 返回0为没有插入成功
     *
     * @param string $sql  SQL语句
     * @access public
     * @return integer
     */
    public function insert($sql)
    {
        $this->execute($sql);
        return mysql_insert_id($this->link);
    }

    /**
     * 释放结果集
     *
     * @param resource $rs 结果集
     * @access protected
     * @return boolean
     */
    protected function free($rs)
    {
        return mysql_free_result($rs);
    }

    /**
     * 关闭数据库
     *
     * @access public
     * @return boolean
     */
    public function close()
    {
        return mysql_close($this->link);
    }

    /**
     * 获取错误信息
     *
     * @return void
     * @access public
     */
    public function getError()
    {
        return mysql_errno($this->link) . " : " . mysql_error($this->link);
    }

}