-
Notifications
You must be signed in to change notification settings - Fork 0
Changes from 4 commits
4ec7a5f
40ee389
9ffc492
4cc05b3
5695e38
0e018b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use Illuminate\Database\Query\Builder as BaseBuilder; | ||
use Illuminate\Database\Query\Expression; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\LazyCollection; | ||
use Illuminate\Support\Str; | ||
|
@@ -116,6 +117,7 @@ class Builder extends BaseBuilder | |
* @var array | ||
*/ | ||
protected $conversion = [ | ||
'=' => 'eq', | ||
'!=' => 'ne', | ||
'<>' => 'ne', | ||
'<' => 'lt', | ||
|
@@ -1084,7 +1086,7 @@ protected function compileWhereBasic(array $where): array | |
$operator = $operator === 'regex' ? '=' : 'not'; | ||
} | ||
|
||
if (! isset($operator) || $operator == '=') { | ||
if (! isset($operator) || $operator === '=' || $operator === 'eq') { | ||
$query = [$column => $value]; | ||
} else { | ||
$query = [$column => ['$'.$operator => $value]]; | ||
|
@@ -1191,10 +1193,41 @@ protected function compileWhereDate(array $where): array | |
{ | ||
extract($where); | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
$startOfDay = new UTCDateTime(Carbon::parse($value)->startOfDay()); | ||
$endOfDay = new UTCDateTime(Carbon::parse($value)->endOfDay()); | ||
|
||
return $this->compileWhereBasic($where); | ||
return match($operator) { | ||
'eq', '=' => [ | ||
$column => [ | ||
'$gte' => $startOfDay, | ||
'$lte' => $endOfDay, | ||
], | ||
], | ||
'ne' => [ | ||
'$or' => [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this alternative, but got segfaults. 'ne' => ['$not' => [
$column => [
'$gte' => $startOfDay,
'$lte' => $endOfDay,
],
]], There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Segfaults in I'm trying to think about how this would trigger a segfault in PHP. The only thing I can think of is that If this is actually a |
||
[ | ||
$column => [ | ||
'$lt' => $startOfDay, | ||
], | ||
], | ||
[ | ||
$column => [ | ||
'$gt' => $endOfDay, | ||
], | ||
], | ||
], | ||
], | ||
'lt', 'gte' => [ | ||
$column => [ | ||
'$'.$operator => $startOfDay, | ||
], | ||
], | ||
'gt', 'lte' => [ | ||
$column => [ | ||
'$'.$operator => $endOfDay, | ||
], | ||
], | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -1205,10 +1238,18 @@ protected function compileWhereMonth(array $where): array | |
{ | ||
extract($where); | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
$value = (int) ltrim($value, '0'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is In my local testing with a PHP 8.2 REPL, the integer cast seems to ignore leading zeroes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly, I blindly adopted the PR code without even testing it. Fixed. |
||
|
||
return $this->compileWhereBasic($where); | ||
return [ | ||
'$expr' => [ | ||
'$'.$operator => [ | ||
[ | ||
'$month' => '$'.$column, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume Is it possible for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing all this |
||
], | ||
$value, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -1219,10 +1260,18 @@ protected function compileWhereDay(array $where): array | |
{ | ||
extract($where); | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
$value = (int) ltrim($value, '0'); | ||
|
||
return $this->compileWhereBasic($where); | ||
return [ | ||
'$expr' => [ | ||
'$'.$operator => [ | ||
[ | ||
'$dayOfMonth' => '$'.$column, | ||
], | ||
$value, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -1233,10 +1282,18 @@ protected function compileWhereYear(array $where): array | |
{ | ||
extract($where); | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
$value = (int) $value; | ||
|
||
return $this->compileWhereBasic($where); | ||
return [ | ||
'$expr' => [ | ||
'$'.$operator => [ | ||
[ | ||
'$year' => '$'.$column, | ||
], | ||
$value, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -1247,10 +1304,16 @@ protected function compileWhereTime(array $where): array | |
{ | ||
extract($where); | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
|
||
return $this->compileWhereBasic($where); | ||
return [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noted the original author's comment in mongodb/laravel-mongodb#2376:
Does this address that concern? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, fixed. There is no dedicated operator, but this was possible using |
||
'$expr' => [ | ||
'$'.$operator => [ | ||
[ | ||
'$dateToString' => ['date' => '$'.$column, 'format' => '%H:%M:%S'], | ||
], | ||
$value, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could result in a subtle behavioral change when using
MongoDB\BSON\Regex
objects. Quoting$eq
docs:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ field: { eq: Regex } }
is not supported ATM as it will always end to{ field: Regex }
for matching the regex. I don't think storing Regex in database is common enough to fix it know. PHPORM-74