You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.4 KiB

<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage db
* @since 2011-11-15
*/
/**
* @property MongoDriver $driver
* @property MongoCursor $result
*/
class MongoStatement extends DbStatement
{
protected $insertId = false;
public function order($sort = array())
{
if ($this->result instanceof MongoCursor) {
$this->result->sort($sort);
return $this;
} else {
throw new Exception('MongoStatement error. Impossible order results of opened cursor.');
}
}
public function skip($skip = 0)
{
if ($this->result instanceof MongoCursor) {
$this->result->skip($skip);
return $this;
} else {
throw new Exception('MongoStatement error. Impossible skip results of opened cursor.');
}
}
public function limit($limit = 0)
{
if ($this->result instanceof MongoCursor) {
$this->result->limit($limit);
return $this;
} else {
throw new Exception('MongoStatement error. Impossible limit results of opened cursor.');
}
}
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()
{
$this->result = null;
}
/**
* @return int
*/
public function affectedRows()
{
if (is_array($this->result)) {
if (isset($this->result['ok']) && $this->result['ok'] == 1) {
if (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 MongoDbCommand $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;
if (is_array($result) && isset($result['value'])) {
$this->result = $result['value'];
}
if (is_array($result) && isset($result['values'])) {
$this->result = $result['values'];
}
}
if ($request instanceof InsertMongoCommand) {
$this->insertId = $request->getInsertId();
}
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;
}
public function getInsertId()
{
return $this->insertId;
}
}