Commit 84cd2418 authored by fso's avatar fso

Add cursor support

parent 4d8fb95d
...@@ -8,6 +8,7 @@ use Illuminate\Database\Query\Builder as BaseBuilder; ...@@ -8,6 +8,7 @@ use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression; use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Jenssegers\Mongodb\Connection; use Jenssegers\Mongodb\Connection;
use MongoCollection; use MongoCollection;
...@@ -15,7 +16,12 @@ use MongoDB\BSON\Binary; ...@@ -15,7 +16,12 @@ use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectID; use MongoDB\BSON\ObjectID;
use MongoDB\BSON\Regex; use MongoDB\BSON\Regex;
use MongoDB\BSON\UTCDateTime; use MongoDB\BSON\UTCDateTime;
use RuntimeException;
/**
* Class Builder
* @package Jenssegers\Mongodb\Query
*/
class Builder extends BaseBuilder class Builder extends BaseBuilder
{ {
/** /**
...@@ -209,12 +215,25 @@ class Builder extends BaseBuilder ...@@ -209,12 +215,25 @@ class Builder extends BaseBuilder
return $this->getFresh($columns); return $this->getFresh($columns);
} }
/**
* @inheritdoc
*/
public function cursor($columns = [])
{
$result = $this->getFresh($columns, true);
if ($result instanceof LazyCollection) {
return $result;
}
throw new RuntimeException("Query not compatible with cursor");
}
/** /**
* Execute the query as a fresh "select" statement. * Execute the query as a fresh "select" statement.
* @param array $columns * @param array $columns
* @return array|static[]|Collection * @param bool $returnLazy
* @return array|static[]|Collection|LazyCollection
*/ */
public function getFresh($columns = []) public function getFresh($columns = [], $returnLazy = false)
{ {
// If no columns have been specified for the select statement, we will set them // If no columns have been specified for the select statement, we will set them
// here to either the passed columns, or the standard default of retrieving // here to either the passed columns, or the standard default of retrieving
...@@ -402,6 +421,14 @@ class Builder extends BaseBuilder ...@@ -402,6 +421,14 @@ class Builder extends BaseBuilder
// Execute query and get MongoCursor // Execute query and get MongoCursor
$cursor = $this->collection->find($wheres, $options); $cursor = $this->collection->find($wheres, $options);
if ($returnLazy) {
return LazyCollection::make(function () use ($cursor) {
foreach ($cursor as $item) {
yield $item;
}
});
}
// Return results as an array with numeric keys // Return results as an array with numeric keys
$results = iterator_to_array($cursor, false); $results = iterator_to_array($cursor, false);
return $this->useCollections ? new Collection($results) : $results; return $this->useCollections ? new Collection($results) : $results;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment