8
8
use Illuminate \Database \Query \Expression ;
9
9
use Illuminate \Support \Arr ;
10
10
use Illuminate \Support \Collection ;
11
+ use Illuminate \Support \LazyCollection ;
11
12
use Illuminate \Support \Str ;
12
13
use Jenssegers \Mongodb \Connection ;
13
14
use MongoDB \BSON \Binary ;
14
15
use MongoDB \BSON \ObjectID ;
15
16
use MongoDB \BSON \Regex ;
16
17
use MongoDB \BSON \UTCDateTime ;
18
+ use RuntimeException ;
17
19
20
+ /**
21
+ * Class Builder
22
+ * @package Jenssegers\Mongodb\Query
23
+ */
18
24
class Builder extends BaseBuilder
19
25
{
20
26
/**
@@ -208,12 +214,25 @@ public function get($columns = [])
208
214
return $ this ->getFresh ($ columns );
209
215
}
210
216
217
+ /**
218
+ * @inheritdoc
219
+ */
220
+ public function cursor ($ columns = [])
221
+ {
222
+ $ result = $ this ->getFresh ($ columns , true );
223
+ if ($ result instanceof LazyCollection) {
224
+ return $ result ;
225
+ }
226
+ throw new RuntimeException ("Query not compatible with cursor " );
227
+ }
228
+
211
229
/**
212
230
* Execute the query as a fresh "select" statement.
213
231
* @param array $columns
214
- * @return array|static[]|Collection
232
+ * @param bool $returnLazy
233
+ * @return array|static[]|Collection|LazyCollection
215
234
*/
216
- public function getFresh ($ columns = [])
235
+ public function getFresh ($ columns = [], $ returnLazy = false )
217
236
{
218
237
// If no columns have been specified for the select statement, we will set them
219
238
// here to either the passed columns, or the standard default of retrieving
@@ -401,6 +420,14 @@ public function getFresh($columns = [])
401
420
// Execute query and get MongoCursor
402
421
$ cursor = $ this ->collection ->find ($ wheres , $ options );
403
422
423
+ if ($ returnLazy ) {
424
+ return LazyCollection::make (function () use ($ cursor ) {
425
+ foreach ($ cursor as $ item ) {
426
+ yield $ item ;
427
+ }
428
+ });
429
+ }
430
+
404
431
// Return results as an array with numeric keys
405
432
$ results = iterator_to_array ($ cursor , false );
406
433
return $ this ->useCollections ? new Collection ($ results ) : $ results ;
@@ -988,6 +1015,7 @@ protected function compileWhereAll(array $where)
988
1015
protected function compileWhereBasic (array $ where )
989
1016
{
990
1017
extract ($ where );
1018
+ $ is_numeric = false ;
991
1019
992
1020
// Replace like or not like with a Regex instance.
993
1021
if (in_array ($ operator , ['like ' , 'not like ' ])) {
@@ -999,15 +1027,21 @@ protected function compileWhereBasic(array $where)
999
1027
1000
1028
// Convert to regular expression.
1001
1029
$ regex = preg_replace ('#(^|[^ \\\])%# ' , '$1.* ' , preg_quote ($ value ));
1030
+ $ plain_value = $ value ;
1002
1031
1003
1032
// Convert like to regular expression.
1004
1033
if (!Str::startsWith ($ value , '% ' )) {
1005
1034
$ regex = '^ ' . $ regex ;
1035
+ } else {
1036
+ $ plain_value = Str::replaceFirst ('% ' , null , $ plain_value );
1006
1037
}
1007
1038
if (!Str::endsWith ($ value , '% ' )) {
1008
1039
$ regex .= '$ ' ;
1040
+ } else {
1041
+ $ plain_value = Str::replaceLast ('% ' , null , $ plain_value );
1009
1042
}
1010
1043
1044
+ $ is_numeric = is_numeric ($ plain_value );
1011
1045
$ value = new Regex ($ regex , 'i ' );
1012
1046
} // Manipulate regexp operations.
1013
1047
elseif (in_array ($ operator , ['regexp ' , 'not regexp ' , 'regex ' , 'not regex ' ])) {
@@ -1027,7 +1061,11 @@ protected function compileWhereBasic(array $where)
1027
1061
}
1028
1062
1029
1063
if (!isset ($ operator ) || $ operator == '= ' ) {
1030
- $ query = [$ column => $ value ];
1064
+ if ($ is_numeric ) {
1065
+ $ query = ['$where ' => '/^ ' .$ value ->getPattern ().'/.test(this. ' .$ column .') ' ];
1066
+ } else {
1067
+ $ query = [$ column => $ value ];
1068
+ }
1031
1069
} elseif (array_key_exists ($ operator , $ this ->conversion )) {
1032
1070
$ query = [$ column => [$ this ->conversion [$ operator ] => $ value ]];
1033
1071
} else {
0 commit comments