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 MongoCollection ;
14
15
use MongoDB \BSON \Binary ;
15
16
use MongoDB \BSON \ObjectID ;
16
17
use MongoDB \BSON \Regex ;
17
18
use MongoDB \BSON \UTCDateTime ;
19
+ use RuntimeException ;
18
20
21
+ /**
22
+ * Class Builder
23
+ * @package Jenssegers\Mongodb\Query
24
+ */
19
25
class Builder extends BaseBuilder
20
26
{
21
27
/**
@@ -209,12 +215,25 @@ public function get($columns = [])
209
215
return $ this ->getFresh ($ columns );
210
216
}
211
217
218
+ /**
219
+ * @inheritdoc
220
+ */
221
+ public function cursor ($ columns = [])
222
+ {
223
+ $ result = $ this ->getFresh ($ columns , true );
224
+ if ($ result instanceof LazyCollection) {
225
+ return $ result ;
226
+ }
227
+ throw new RuntimeException ("Query not compatible with cursor " );
228
+ }
229
+
212
230
/**
213
231
* Execute the query as a fresh "select" statement.
214
232
* @param array $columns
215
- * @return array|static[]|Collection
233
+ * @param bool $returnLazy
234
+ * @return array|static[]|Collection|LazyCollection
216
235
*/
217
- public function getFresh ($ columns = [])
236
+ public function getFresh ($ columns = [], $ returnLazy = false )
218
237
{
219
238
// If no columns have been specified for the select statement, we will set them
220
239
// here to either the passed columns, or the standard default of retrieving
@@ -294,7 +313,7 @@ public function getFresh($columns = [])
294
313
}
295
314
}
296
315
}
297
-
316
+
298
317
// The _id field is mandatory when using grouping.
299
318
if ($ group && empty ($ group ['_id ' ])) {
300
319
$ group ['_id ' ] = null ;
@@ -402,6 +421,14 @@ public function getFresh($columns = [])
402
421
// Execute query and get MongoCursor
403
422
$ cursor = $ this ->collection ->find ($ wheres , $ options );
404
423
424
+ if ($ returnLazy ) {
425
+ return LazyCollection::make (function () use ($ cursor ) {
426
+ foreach ($ cursor as $ item ) {
427
+ yield $ item ;
428
+ }
429
+ });
430
+ }
431
+
405
432
// Return results as an array with numeric keys
406
433
$ results = iterator_to_array ($ cursor , false );
407
434
return $ this ->useCollections ? new Collection ($ results ) : $ results ;
@@ -930,18 +957,18 @@ protected function compileWheres()
930
957
if (is_array ($ where ['value ' ])) {
931
958
array_walk_recursive ($ where ['value ' ], function (&$ item , $ key ) {
932
959
if ($ item instanceof DateTime) {
933
- $ item = new UTCDateTime ($ item ->getTimestamp () * 1000 );
960
+ $ item = new UTCDateTime ($ item ->format ( ' Uv ' ) );
934
961
}
935
962
});
936
963
} else {
937
964
if ($ where ['value ' ] instanceof DateTime) {
938
- $ where ['value ' ] = new UTCDateTime ($ where ['value ' ]->getTimestamp () * 1000 );
965
+ $ where ['value ' ] = new UTCDateTime ($ where ['value ' ]->format ( ' Uv ' ) );
939
966
}
940
967
}
941
968
} elseif (isset ($ where ['values ' ])) {
942
969
array_walk_recursive ($ where ['values ' ], function (&$ item , $ key ) {
943
970
if ($ item instanceof DateTime) {
944
- $ item = new UTCDateTime ($ item ->getTimestamp () * 1000 );
971
+ $ item = new UTCDateTime ($ item ->format ( ' Uv ' ) );
945
972
}
946
973
});
947
974
}
@@ -993,6 +1020,7 @@ protected function compileWhereAll(array $where)
993
1020
protected function compileWhereBasic (array $ where )
994
1021
{
995
1022
extract ($ where );
1023
+ $ is_numeric = false ;
996
1024
997
1025
// Replace like or not like with a Regex instance.
998
1026
if (in_array ($ operator , ['like ' , 'not like ' ])) {
@@ -1004,15 +1032,21 @@ protected function compileWhereBasic(array $where)
1004
1032
1005
1033
// Convert to regular expression.
1006
1034
$ regex = preg_replace ('#(^|[^ \\\])%# ' , '$1.* ' , preg_quote ($ value ));
1035
+ $ plain_value = $ value ;
1007
1036
1008
1037
// Convert like to regular expression.
1009
1038
if (!Str::startsWith ($ value , '% ' )) {
1010
1039
$ regex = '^ ' . $ regex ;
1040
+ } else {
1041
+ $ plain_value = Str::replaceFirst ('% ' , null , $ plain_value );
1011
1042
}
1012
1043
if (!Str::endsWith ($ value , '% ' )) {
1013
1044
$ regex .= '$ ' ;
1045
+ } else {
1046
+ $ plain_value = Str::replaceLast ('% ' , null , $ plain_value );
1014
1047
}
1015
1048
1049
+ $ is_numeric = is_numeric ($ plain_value );
1016
1050
$ value = new Regex ($ regex , 'i ' );
1017
1051
} // Manipulate regexp operations.
1018
1052
elseif (in_array ($ operator , ['regexp ' , 'not regexp ' , 'regex ' , 'not regex ' ])) {
@@ -1032,7 +1066,11 @@ protected function compileWhereBasic(array $where)
1032
1066
}
1033
1067
1034
1068
if (!isset ($ operator ) || $ operator == '= ' ) {
1035
- $ query = [$ column => $ value ];
1069
+ if ($ is_numeric ) {
1070
+ $ query = ['$where ' => '/^ ' .$ value ->getPattern ().'/.test(this. ' .$ column .') ' ];
1071
+ } else {
1072
+ $ query = [$ column => $ value ];
1073
+ }
1036
1074
} elseif (array_key_exists ($ operator , $ this ->conversion )) {
1037
1075
$ query = [$ column => [$ this ->conversion [$ operator ] => $ value ]];
1038
1076
} else {
0 commit comments