Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
3d58b4b3
Unverified
Commit
3d58b4b3
authored
Apr 21, 2020
by
Stas
Committed by
GitHub
Apr 21, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2024 from fsotomsk/master
Add cursor support
parents
4d8fb95d
0aff70ad
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
2 deletions
+47
-2
Builder.php
src/Jenssegers/Mongodb/Query/Builder.php
+29
-2
QueryBuilderTest.php
tests/QueryBuilderTest.php
+18
-0
No files found.
src/Jenssegers/Mongodb/Query/Builder.php
View file @
3d58b4b3
...
...
@@ -8,6 +8,7 @@ use Illuminate\Database\Query\Builder as BaseBuilder;
use
Illuminate\Database\Query\Expression
;
use
Illuminate\Support\Arr
;
use
Illuminate\Support\Collection
;
use
Illuminate\Support\LazyCollection
;
use
Illuminate\Support\Str
;
use
Jenssegers\Mongodb\Connection
;
use
MongoCollection
;
...
...
@@ -15,7 +16,12 @@ use MongoDB\BSON\Binary;
use
MongoDB\BSON\ObjectID
;
use
MongoDB\BSON\Regex
;
use
MongoDB\BSON\UTCDateTime
;
use
RuntimeException
;
/**
* Class Builder
* @package Jenssegers\Mongodb\Query
*/
class
Builder
extends
BaseBuilder
{
/**
...
...
@@ -209,12 +215,25 @@ class Builder extends BaseBuilder
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.
* @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
// here to either the passed columns, or the standard default of retrieving
...
...
@@ -402,6 +421,14 @@ class Builder extends BaseBuilder
// Execute query and get MongoCursor
$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
$results
=
iterator_to_array
(
$cursor
,
false
);
return
$this
->
useCollections
?
new
Collection
(
$results
)
:
$results
;
...
...
tests/QueryBuilderTest.php
View file @
3d58b4b3
...
...
@@ -3,6 +3,7 @@ declare(strict_types=1);
use
Illuminate\Support\Facades\Date
;
use
Illuminate\Support\Facades\DB
;
use
Illuminate\Support\LazyCollection
;
use
Jenssegers\Mongodb\Collection
;
use
Jenssegers\Mongodb\Query\Builder
;
use
MongoDB\BSON\ObjectId
;
...
...
@@ -759,4 +760,21 @@ class QueryBuilderTest extends TestCase
$this
->
assertEquals
(
'spork'
,
$results
[
1
][
'name'
]);
$this
->
assertEquals
(
'fork'
,
$results
[
0
][
'name'
]);
}
public
function
testCursor
()
{
$data
=
[
[
'name'
=>
'fork'
,
'tags'
=>
[
'sharp'
,
'pointy'
]],
[
'name'
=>
'spork'
,
'tags'
=>
[
'sharp'
,
'pointy'
,
'round'
,
'bowl'
]],
[
'name'
=>
'spoon'
,
'tags'
=>
[
'round'
,
'bowl'
]],
];
DB
::
collection
(
'items'
)
->
insert
(
$data
);
$results
=
DB
::
collection
(
'items'
)
->
orderBy
(
'_id'
,
'asc'
)
->
cursor
();
$this
->
assertInstanceOf
(
LazyCollection
::
class
,
$results
);
foreach
(
$results
as
$i
=>
$result
)
{
$this
->
assertEquals
(
$data
[
$i
][
'name'
],
$result
[
'name'
]);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment