Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Commit

Permalink
Moving query builder core out of odm.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Dec 1, 2010
1 parent 368f638 commit 98e70d3
Show file tree
Hide file tree
Showing 16 changed files with 2,645 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/Doctrine/MongoDB/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Collection
*
* @var Database
*/
protected $db;
protected $database;

/**
* The event manager that is the central point of the event system.
Expand Down Expand Up @@ -72,14 +72,14 @@ class Collection
* for a given ClassMetadata instance.
*
* @param MongoCollection $mongoCollection The MongoCollection instance.
* @param Database $db The Database instance.
* @param Database $database The Database instance.
* @param EventManager $evm The EventManager instance.
* @param mixed $loggerCallable The logger callable.
*/
public function __construct(\MongoCollection $mongoCollection, Database $db, EventManager $evm, $loggerCallable, $cmd)
public function __construct(\MongoCollection $mongoCollection, Database $database, EventManager $evm, $loggerCallable, $cmd)
{
$this->mongoCollection = $mongoCollection;
$this->db = $db;
$this->database = $database;
$this->eventManager = $evm;
$this->loggerCallable = $loggerCallable;
$this->cmd = $cmd;
Expand All @@ -92,7 +92,7 @@ public function __construct(\MongoCollection $mongoCollection, Database $db, Eve
*/
public function log(array $log)
{
$log['db'] = $this->db->getName();
$log['db'] = $this->database->getName();
$log['collection'] = $this->getName();
call_user_func_array($this->loggerCallable, array($log));
}
Expand All @@ -109,7 +109,7 @@ public function getMongoCollection()

public function getDatabase()
{
return $this->db;
return $this->database;
}

/** @override */
Expand Down Expand Up @@ -248,7 +248,7 @@ public function findAndRemove(array $query, array $options = array())

$document = null;

$result = $this->db->command($command);
$result = $this->database->command($command);
if (isset($result['value'])) {
$document = $result['value'];
if ($this->mongoCollection instanceof \MongoGridFS) {
Expand Down Expand Up @@ -283,7 +283,7 @@ public function findAndModify(array $query, array $newObj, array $options = arra
unset($options['new']);
}
$command['options'] = $options;
$result = $this->db->command($command);
$result = $this->database->command($command);
$document = isset($result['value']) ? $result['value'] : null;

if ($this->eventManager->hasListeners(Events::postFindAndModify)) {
Expand Down Expand Up @@ -549,6 +549,11 @@ public function validate($scanData = false)
return $this->mongoCollection->validate($scanData);
}

public function createQueryBuilder()
{
return new Query\Builder($this->database, $this, $this->cmd);
}

/** @proxy */
public function __toString()
{
Expand Down
180 changes: 180 additions & 0 deletions lib/Doctrine/MongoDB/Query/AbstractQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?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\Query;

use Doctrine\MongoDB\Iterator;
use Doctrine\MongoDB\Database;
use Doctrine\MongoDB\Collection;

/**
* Abstract executable query class for the different types of queries to implement.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @since 1.0
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
abstract class AbstractQuery implements QueryInterface, Iterator
{
/**
* The Database instance.
*
* @var Database
*/
protected $database;

/**
* The Collection instance.
*
* @var Collection
*/
protected $collection;

/**
* Mongo command prefix
*
* @var string
*/
protected $cmd;

/**
* @var Iterator
*/
protected $iterator;

public function __construct(Database $database, Collection $collection, $cmd)
{
$this->database = $database;
$this->collection = $collection;
$this->cmd = $cmd;
}

/**
* Gets an array of information about this query for debugging.
*
* @param string $name
* @return array $debug
*/
public function debug($name = null)
{
$debug = get_object_vars($this);

unset($debug['database'], $debug['collection'], $debug['cmd']);
if ($name !== null) {
return $debug[$name];
}
foreach ($debug as $key => $value) {
if ( ! $value) {
unset($debug[$key]);
}
}
return $debug;
}

public function getIterator(array $options = array())
{
if ($this->iterator === null) {
$iterator = $this->execute($options);
if ($iterator !== null && !$iterator instanceof Iterator) {
throw new \BadMethodCallException('Query execution did not return an iterator. This query may not support returning iterators. ');
}
$this->iterator = $iterator;
}
return $this->iterator;
}

/**
* Count the number of results for this query.
*
* @param bool $all
* @return integer $count
*/
public function count($all = false)
{
return $this->getIterator()->count($all);
}

/**
* Execute the query and get a single result
*
* @return object $document The single document.
*/
public function getSingleResult(array $options = array())
{
return $this->getIterator($options)->getSingleResult();
}

/**
* Iterator over the query using the Cursor.
*
* @return Cursor $cursor
*/
public function iterate()
{
return $this->getIterator();
}

/** @inheritDoc */
public function first()
{
return $this->getIterator()->first();
}

/** @inheritDoc */
public function last()
{
return $this->getIterator()->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();
}
}
Loading

0 comments on commit 98e70d3

Please sign in to comment.