Permalink
Browse files

moved to Query::__construct, implemented IteratorAggregate interface

  • Loading branch information...
1 parent d5adb31 commit 2892cd9fad03c89d37b3e9da677d38951355eefe @avalanche123 avalanche123 committed Dec 3, 2010
@@ -0,0 +1,37 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\MongoDB;
+
+use Countable, IteratorAggregate as BaseIterator;
+
+/**
+ * Iterator interface.
+ *
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @link www.doctrine-project.com
+ * @since 1.0
+ * @author Jonathan H. Wage <jonwage@gmail.com>
+ * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
+ */
+interface IteratorAggregate extends BaseIterator, Countable
+{
+ function toArray();
+ function getSingleResult();
+}
@@ -881,12 +881,12 @@ public function setNewObj(array $newObj)
* @param array $options
* @return QueryInterface $query
*/
- public function getQuery()
+ public function getQuery(array $options = array())
@henrikbjorn

henrikbjorn Dec 6, 2010

Contributor

This broke the ODM since the two builder classes now have different method signatures for this method.

@avalanche123

avalanche123 Dec 6, 2010

Member

submodule pointer from odm master wasn't updated to this commit at that point
so that shouldn't have broken anything

@henrikbjorn

henrikbjorn Dec 6, 2010

Contributor

i dont use the submodules else i would have 4 of each repository :)

@avalanche123

avalanche123 Dec 6, 2010

Member

then don't update as frequently, we'll tag stuff when its ready.

@avalanche123

avalanche123 Dec 6, 2010

Member

updated here for your development pleasure:) - 44d21f4b35a0c13c1c40453b0854413083e73eea

{
$query = $this->query;
$query['query'] = $this->expr->getQuery();
$query['newObj'] = $this->expr->getNewObj();
- return new Query($this->database, $this->collection, $query, $this->cmd);
+ return new Query($this->database, $this->collection, $query, $options, $this->cmd);
}
/**
@@ -19,7 +19,7 @@
namespace Doctrine\MongoDB\Query;
-use Doctrine\MongoDB\Iterator;
+use Doctrine\MongoDB\IteratorAggregate;
use Doctrine\MongoDB\Database;
use Doctrine\MongoDB\Collection;
use Doctrine\MongoDB\Cursor;
@@ -31,8 +31,9 @@
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @since 1.0
* @author Jonathan H. Wage <jonwage@gmail.com>
+ * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
-class Query implements Iterator
+class Query implements IteratorAggregate
{
const TYPE_FIND = 1;
const TYPE_FIND_AND_UPDATE = 2;
@@ -78,12 +79,20 @@ class Query implements Iterator
*/
protected $iterator;
- public function __construct(Database $database, Collection $collection, array $query, $cmd)
+ /**
+ * Query options
+ *
+ * @var array
+ */
+ protected $options;
+
+ public function __construct(Database $database, Collection $collection, array $query, array $options, $cmd)
{
- $this->database = $database;
+ $this->database = $database;
$this->collection = $collection;
- $this->query = $query;
- $this->cmd = $cmd;
+ $this->query = $query;
+ $this->cmd = $cmd;
+ $this->options = $options;
}
/**
@@ -111,10 +120,10 @@ public function getType()
return $this->query['type'];
}
- public function getIterator(array $options = array())
+ public function getIterator()
{
if ($this->iterator === null) {
- $iterator = $this->execute($options);
+ $iterator = $this->execute();
if ($iterator !== null && !$iterator instanceof Iterator) {
throw new \BadMethodCallException('Query execution did not return an iterator. This query may not support returning iterators. ');
}
@@ -123,40 +132,40 @@ public function getIterator(array $options = array())
return $this->iterator;
}
- public function execute(array $options = array())
+ public function execute()
{
switch ($this->query['type']) {
case self::TYPE_FIND:
if (isset($this->query['mapReduce']['reduce'])) {
$this->query['query'][$this->cmd . 'where'] = $this->query['mapReduce']['reduce'];
}
- $cursor = $this->collection->find($this->query['query'], $this->query['select'], $options);
+ $cursor = $this->collection->find($this->query['query'], $this->query['select']);
$this->prepareCursor($cursor);
return $cursor;
case self::TYPE_FIND_AND_UPDATE:
if ($this->query['sort']) {
- $options['sort'] = $this->query['sort'];
+ $this->options['sort'] = $this->query['sort'];
}
if ($this->query['select']) {
- $options['fields'] = $this->query['select'];
+ $this->options['fields'] = $this->query['select'];
}
if ($this->query['upsert']) {
- $options['upsert'] = true;
+ $this->options['upsert'] = true;
}
if ($this->query['new']) {
- $options['new'] = true;
+ $this->options['new'] = true;
}
- return $this->collection->findAndUpdate($this->query['query'], $this->query['newObj'], $options);
+ return $this->collection->findAndUpdate($this->query['query'], $this->query['newObj'], $this->options);
case self::TYPE_FIND_AND_REMOVE:
if ($this->query['sort']) {
- $options['sort'] = $this->query['sort'];
+ $this->options['sort'] = $this->query['sort'];
}
if ($this->query['select']) {
- $options['fields'] = $this->query['select'];
+ $this->options['fields'] = $this->query['select'];
}
- return $this->collection->findAndRemove($this->query['query'], $options);
+ return $this->collection->findAndRemove($this->query['query'], $this->options);
case self::TYPE_INSERT:
return $this->collection->insert($this->query['newObj']);
@@ -165,24 +174,24 @@ public function execute(array $options = array())
return $this->collection->update($this->query['query'], $this->query['newObj']);
case self::TYPE_REMOVE:
- return $this->collection->remove($this->query['query'], $options);
+ return $this->collection->remove($this->query['query'], $this->options);
case self::TYPE_GROUP:
return $this->collection->group($this->query['group']['keys'], $this->query['group']['initial'], $this->query['mapReduce']['reduce'], $this->query['query']);
case self::TYPE_MAP_REDUCE:
- $cursor = $this->collection->mapReduce($this->query['mapReduce']['map'], $this->query['mapReduce']['reduce'], $this->query['query'], $options);
+ $cursor = $this->collection->mapReduce($this->query['mapReduce']['map'], $this->query['mapReduce']['reduce'], $this->query['query'], $this->options);
$this->prepareCursor($cursor);
return $cursor;
case self::TYPE_DISTINCT_FIELD:
- return $this->collection->distinct($this->query['distinctField'], $this->query['query'], $options);
+ return $this->collection->distinct($this->query['distinctField'], $this->query['query'], $this->options);
case self::TYPE_GEO_LOCATION:
if (isset($this->query['limit']) && $this->query['limit']) {
- $options['num'] = $this->query['limit'];
+ $this->options['num'] = $this->query['limit'];
}
- return $this->collection->near($this->query['near'], $this->query['query'], $options);
+ return $this->collection->near($this->query['near'], $this->query['query'], $this->options);
}
}
@@ -245,36 +254,6 @@ public function last()
}
/** @inheritDoc */
- public function key()
- {
- return $this->getIterator()->key();
- }
-
- /** @inheritDoc */
- public function next()
- {
- return $this->getIterator()->next();
- }
-
- /** @inheritDoc */
- public function current()
- {
- return $this->getIterator()->current();
- }
-
- /** @inheritDoc */
- public function rewind()
- {
- return $this->getIterator()->rewind();
- }
-
- /** @inheritDoc */
- public function valid()
- {
- return $this->getIterator()->valid();
- }
-
- /** @inheritDoc */
public function toArray()
{
return $this->getIterator()->toArray();
@@ -302,8 +302,8 @@ public function testQueryIsIterable()
{
$qb = $this->getTestQueryBuilder();
$query = $qb->getQuery();
- $this->assertInstanceOf('Iterator', $query);
- $this->assertInstanceOf('Doctrine\MongoDB\Iterator', $query);
+ $this->assertInstanceOf('IteratorAggregate', $query);
+ $this->assertInstanceOf('Doctrine\MongoDB\IteratorAggregate', $query);
}
private function getTestQueryBuilder()

0 comments on commit 2892cd9

Please sign in to comment.