예제 #1
0
 /**
  * 直接SQL
  *
  * @param string $sql
  * @param array $args
  *
  * @throws \PDOException
  *
  * @return Statement
  *
  * 例子
  *   ->query('SELECT id FROM table WHERE id = :id' ,array(':id'=>1))
  *   ->fetchAll();
  */
 public function query($sql, array $args = array())
 {
     //清理上一个预处理对象
     $this->stmt = null;
     $this->expandArguments($sql, $args);
     try {
         //创建预处理对象
         $this->stmt = $this->prepareQuery($sql);
         //执行预处理语句
         $this->stmt->execute($args);
     } catch (\PDOException $e) {
         //处理错误,并记录日志
         $error = array('code' => $e->getCode(), 'prepareSQL' => $this->prepareSQL, 'sql' => $this->stmt->queryString, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'connectionName' => $this->name);
         $args = $args ? $args : array();
         $this->app->logger()->sql($error + $args);
         throw $e;
     }
     return $this->stmt;
 }