Example #1
0
 /**
  * Special handling for PDO query().
  * All bind parameter names must begin with ':'
  *
  * @param string|Select $sql The SQL statement with placeholders.
  * @param array $bind An array of data to bind to the placeholders.
  * @return PDOStatement
  * @throws PDOException.
  */
 public function query($sql, $bind = array())
 {
     if (empty($bind) && $sql instanceof Select) {
         $bind = $sql->getBind();
     }
     if (is_array($bind)) {
         foreach ($bind as $name => $value) {
             if (!is_int($name) && !preg_match('/^:/', $name)) {
                 $newName = ":{$name}";
                 unset($bind[$name]);
                 $bind[$newName] = $value;
             }
         }
     }
     //try {省略throw-catch-rethrow块,直接抛出PDOException
     // connect to the database if needed
     $this->_connect();
     // is the $sql a Select object?
     if ($sql instanceof Select) {
         if (empty($bind)) {
             $bind = $sql->getBind();
         }
         $sql = $sql->assemble();
     }
     // make sure $bind to an array;
     // don't use (array) typecasting because
     // because $bind may be a Expr object
     if (!is_array($bind)) {
         $bind = array($bind);
     }
     //将结果缓冲当中的结果集读出来
     Statement::flush();
     // prepare and execute the statement with profiling
     $stmt = $this->prepare($sql);
     // 由于取消了Statement,因此将Profiler的控制代码移动到这里
     // 由于所处的程序位置,省略了$qp->start(),简化了$qp->bindParams()的相关代码
     if ($this->_profiler === false) {
         $stmt->execute($bind);
     } else {
         $q = $this->_profiler->queryStart($sql);
         $qp = $this->_profiler->getQueryProfile($q);
         if ($qp->hasEnded()) {
             $q = $this->_profiler->queryClone($qp);
             $qp = $this->_profiler->getQueryProfile($q);
         }
         $qp->bindParams($bind);
         $stmt->execute($bind);
         $this->_profiler->queryEnd($q);
     }
     // return the results embedded in the prepared statement object
     $stmt->setFetchMode($this->_fetchMode);
     return $stmt;
     //} catch (PDOException $e) {
     /**
      * @see StatementException
      */
     //require_once 'Zend/Db/Statement/Exception.php';
     //throw new StatementException($e->getMessage(), $e->getCode(), $e);
     //}
 }