Ejemplo n.º 1
0
 /**
  * 执行操作的底层接口
  *
  * @param string $sql
  * @param array $params
  * @return PDO Statement
  */
 protected function _autoExecute($sql, $params = array())
 {
     try {
         $this->_getChoiceDbConnect();
         if (!$this->_db) {
             exit('DB connection lost.');
         }
         // 调试模式打印SQL信息
         $explain = array();
         if (Com_Db::enableLogging() && DEBUG_EXPLAIN_SQL) {
             $explain = $this->_explain($sql, $params);
         }
         $sqlStartTime = microtime(true);
         // 预编译 SQL
         $stmt = $this->_db->prepare($sql);
         if (!$stmt) {
             exit(implode(',', $this->_db->errorInfo()));
         }
         // 绑定参数
         $params = $params ? (array) $params : array();
         // 执行 SQL
         if (!$stmt->execute($params)) {
             exit(implode(',', $stmt->errorInfo()));
         }
         $sqlCostTime = microtime(true) - $sqlStartTime;
         // 调试模式打印SQL信息
         if (Com_Db::enableLogging()) {
             Com_Db::sqlLog($this->_formatLogSql($sql, $params), $sqlCostTime, $explain);
         }
         return $stmt;
     } catch (Exception $e) {
         Com_Db_Exception::process($e, '[SQL Failed]', $this->_formatLogSql($sql, $params));
         return false;
     }
 }