DbMysqli.class.php 7.6 KB
<?php
class Util_Db_DbMysqli extends Util_Db_BaseDB
{
    /**
     * 数据连接对象
     * @var object
     */
    private $mMysqli;
    
    /**
     * SQL语句
     * @var object
     */
    private $mSql;
    
    /**
     * 回滚次数
     * @var int
     */
    private $mTransTimes = 0;
    
    /**
     * 结果集
     * @var object
     */
    private $mRs; 

    /**
     * 模式
     */
    private $mFetchMode = MYSQLI_ASSOC;  //取得模式
    
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 执行多条语句或存储过程
     * 
     * @param string $sql
     * @param $fetchMode
     * @return array
     */
    public function multiQuery($sql, $fetchMode = MYSQLI_NUM)
    {
        $this->autoChoiseHost($sql);
        if ($this->mInstance->multi_query($sql)) 
        {
            $allRows = array();
            do
            {
                if($result = $this->mInstance->store_result()) 
                {
                    while ($rows = $result->fetch_array($fetchMode))
                    {
                        $allRows[] = $rows;
                    }
                    $result->close();
                }
                if(!$this->mInstance->more_results())
                {
                    break;
                }
            }while ($this->mInstance->next_result());
            
            return $allRows;
        }
        else
        {
            //echo "(sql or produre) error:".$sql;
        }
    }
    
    /**
     * 预处理更新
     * 
     * @param string $sql SQL语句
     * @param string $types  字段类型
     * @param $fieldParams   字段
     * @return int
     */
    public function stmtUpdate($sql, $types, $fieldParams)
    {
        $result = false;
        
        $this->autoChoiseHost($sql);
        $stmt = $this->mInstance->prepare($sql);
        if(strlen($types) == count($fieldParams) && is_object($stmt))
        {
            $params = array(
                               'stmtUpdatestmt' => $stmt, 
                               'stmtUpdatetypes' => $types
                           );
            $params = array_merge($params, $fieldParams);
            call_user_func_array('mysqli_stmt_bind_param', $this->_refValues($params));
            $stmt->execute();
            $result = $stmt->affected_rows;
            $stmt->close();
        }
        return $result;
    }
    
    private function _refValues($arr)
    {
        if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
        {
            $refs = array();
            foreach($arr as $key => $value)
            {
                $refs[$key] = &$arr[$key];
            }
            return $refs;
        }
        return $arr;
    }
    
    /**
     * 得到一条记录
     * 
     * @param string $sql
     * @return array
     */
    public function getOne($sql)
    { 
        $this->query($sql, 1); 
        $this->mFetchMode = MYSQLI_NUM; 
        $row = $this->fetch(); 
        $this->free(); 
        return $row; 
    } 
    
    /**
     * 获取多条记录
     * 
     * @param string $sql
     * @param string $limit
     * @param $fetchMode
     * @return array
     */
    public function getMore($sql, $limit = null, $fetchMode = MYSQLI_NUM)
    { 
        $this->query($sql, $limit); 
        $allRows = array(); 
        $rows = array();
        $this->mFetchMode = $fetchMode; 
        while($rows = $this->fetch())
        { 
            $allRows[] = $rows; 
        } 
        $this->free(); 
        return $allRows; 
    }
    
    /**
     * 获取所有记录
     * 
     * @param string $sql
     * @param string $limit
     * @param $fetchMode
     * @return array
     */
    public function getAll($sql, $limit = null, $fetchMode = MYSQLI_NUM)
    { 
        $this->query($sql, $limit); 
        $allRows = array(); 
        $this->mFetchMode = $fetchMode; 
        while($rows = $this->fetch())
        { 
            $allRows[] = $rows; 
        } 
        $this->free(); 
        return $allRows; 
    }
    
    /**
     * 获取插入的ID
     * 
     * @return int
     */
    public function insert_id()
    { 
        return $this->mInstance->insert_id; 
    } 
     
   /**
    * 更新操作
    * 
    * @param string $sql
    * @return int
    */ 
    public function update($sql)
    {
        $this->query($sql);
        return $this->mInstance->affected_rows;
    }
    
    /**
     * 获取数据连接对象
     * 
     * @return object
     */
    public function getMysqli()
    {
        return $this->mMysqli;
    }
    
    /**
     * 启动事务
     * 
     * @return void
     */
    public function startTrans()
    {
        //数据rollback 支持
        if ($this->mTransTimes == 0) 
        {
            //设置为主库
            $this->setDefaultConnect(true);
            $this->setHostSwitch(true);
            $this->mInstance->autocommit(false);
        }
        $this->mTransTimes++;
        return;
    }
    
    /**
     * 用于非自动提交状态下面的查询提交
     * 
     * @return boolean
     */
    public function commit()
    {
        if ($this->mTransTimes > 0) 
        {
            $result = $this->mInstance->commit();
            $this->mInstance->autocommit(true);
            $this->setHostSwitch(false);
            $this->mTransTimes = 0;
            if(!$result)
            {
                return false;
            }
        }
        return true;
    }
    
    /**
     * 事务回滚
     * 
     * @return boolean
     */
    public function rollback()
    {
        if ($this->mTransTimes > 0) 
        {
            $result = $this->mInstance->rollback();
            $this->setHostSwitch(false);
            $this->mTransTimes = 0;
            if(!$result)
            {
                return false;
            }
        }
        return true;
    }
    
    /**
     * 获取错误
     * 
     * @return string
     */
    public function error()
    {
       return $this->mInstance->error;     
    }
    
    /**
     * 查询语句
     * 
     * @param string $sql
     * @param string $limit
     * @return object
     */
    private function query($sql, $limit = null)
    { 
        $this->autoChoiseHost($sql);
        $sql = $this->getQuerySql($sql, $limit); 
        $this->mSql = $sql; 
        $this->mRs = $this->mInstance->query($sql); 
        if (!$this->mRs) 
        { 
            echo "<h2>".$this->mInstance->error."</h2>"; 
            die(); 
        }
        else
        {  
            return $this->mRs; 
        } 
    }
    
    /**
     * 获取结果集
     * 
     * @return array
     */
    private function fetch() 
    { 
        return @$this->mRs->fetch_array($this->mFetchMode); 
    }
    
    /**
     * 获取带limit的sql
     * 
     * @param strinng $sql
     * @param string $limit
     * @return string
     */
    private function getQuerySql($sql, $limit = null)
    { 
        if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql)) 
        { 
            $sql .= " LIMIT " . $limit; 
        } 
        return $sql; 
    }
    
    /**
     * 释放结果集
     */
    private function free()
    { 
        @$this->mRs->free(); 
    }
    
    /**
     * 关闭数据库
     */
    private function close() 
    { 
        if(is_resource($this->mInstance))
        {
            $this->mInstance->close(); 
        }
    }
    
    /**
     * 析构
     */
    public function  __destruct()
    { 
        if(is_object($this->mRs))
        {
            $this->free(); 
        }    
        $this->close();
        unset($this->mInstance);
        parent::__destruct();
    }
}
?>