/** * * @throws PDOException */ public function _query() { if ($this->_rowset === false) { if (self::$_stmt) { while (self::$_stmt->nextRowset()) { //如果已经在结果缓存中,则搜寻结果集 $query = array_shift(self::$_fetchingQueue); $query->_rowset = $query->_fetchAll(); if ($query === $this) { return; } } self::$_stmt = null; } //将当前的语句插到第一个,然后把所有语句一口气打包发送给mysql $keys = array_keys(self::$_waitingQueue, $this); if (count($keys)) { unset(self::$_waitingQueue[$keys[0]]); } $sql = $this->_select->assemble(); if (count(self::$_waitingQueue)) { $sql .= ";\n" . implode(";\n", self::$_waitingQueue); } implode(";\n", self::$_waitingQueue); self::$_stmt = $this->_select->getAdapter()->query($sql); $this->_rowset = $this->_fetchAll(); self::$_fetchingQueue = self::$_waitingQueue; self::$_waitingQueue = array(); } }
/** * 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); //} }
/** * Performs a validation on the select query before passing back to the parent class. * Ensures that only columns from the primary DataObject are returned in the result. * * @return string|null This object as a SELECT string (or null if a string cannot be produced) */ public function assemble() { if (count($this->_parts[self::UNION]) == 0) { $fields = $this->getPart(Select::COLUMNS); $primary = $this->_info[DataObject::NAME]; $schema = $this->_info[DataObject::SCHEMA]; // If no fields are specified we assume all fields from primary table if (!count($fields)) { $this->from($primary, self::SQL_WILDCARD, $schema); } } return parent::assemble(); }