* @link http://netmonsters.ru * @package Majestic * @subpackage db * @since 2011-11-15 */ /** * @property MongoDriver $driver * @property MongoCursor $result */ class MongoStatement extends DbStatement { public function fetch($style = Db::FETCH_OBJ) { if (!$this->result) { return false; } $row = false; switch ($style) { case Db::FETCH_OBJ: $row = $this->fetchObject(); break; case Db::FETCH_ASSOC: if ($this->result instanceof MongoCursor) { $row = $this->result->getNext(); } else { $row = $this->result; } break; default: throw new Exception('Invalid fetch mode "' . $style . '" specified'); } return $row; } public function fetchObject($class = 'stdClass') { if ($this->result instanceof MongoCursor) { $row = $this->result->getNext(); } else { $row = $this->result; } if (is_array($row) && isset($row['_id'])) { $row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS); } else { $row = false; } return $row; } public function close() { // TODO: Implement close() method. } /** * @return int */ public function affectedRows() { if (is_array($this->result)) { if (isset($this->result['ok']) && $this->result['ok'] == 1 && isset($this->result['n'])) { return $this->result['n']; } else { return false; } } return false; } public function numRows() { if ($this->result instanceof MongoCursor) { return $this->result->count(); } else { return false; } } /** * @param MongoDbComand $request * @return bool */ protected function driverExecute($request) { $mongo = $this->driver->getConnection(); if ($mongo instanceof Mongo) { if (DEBUG) { $profiler = Profiler::getInstance()->profilerCommand('Mongo', $request); $result = $request->execute(); $profiler->end(); } else { $result = $request->execute(); } if ($result === false) { throw new Exception('MongoDB request error.'); } if ($result instanceof MongoCursor || is_array($result)) { $this->result = $result; } return true; } else { throw new Exception('No connection to MongoDB server.'); } } public function bindParam($param, &$value) { $this->request->bindParam($param, $value); } protected function assemble() { return $this->request; } }