-
Notifications
You must be signed in to change notification settings - Fork 0
PHPORM-60 Fix Query on whereDate, whereDay, whereMonth, whereYear #28
Changes from all 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]]; | ||
|
@@ -1189,12 +1191,41 @@ protected function compileWhereBetween(array $where): array | |
*/ | ||
protected function compileWhereDate(array $where): array | ||
{ | ||
extract($where); | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
$startOfDay = new UTCDateTime(Carbon::parse($where['value'])->startOfDay()); | ||
$endOfDay = new UTCDateTime(Carbon::parse($where['value'])->endOfDay()); | ||
|
||
return $this->compileWhereBasic($where); | ||
return match($where['operator']) { | ||
'eq', '=' => [ | ||
$where['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 |
||
[ | ||
$where['column'] => [ | ||
'$lt' => $startOfDay, | ||
], | ||
], | ||
[ | ||
$where['column'] => [ | ||
'$gt' => $endOfDay, | ||
], | ||
], | ||
], | ||
], | ||
'lt', 'gte' => [ | ||
$where['column'] => [ | ||
'$'.$where['operator'] => $startOfDay, | ||
], | ||
], | ||
'gt', 'lte' => [ | ||
$where['column'] => [ | ||
'$'.$where['operator'] => $endOfDay, | ||
], | ||
], | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -1203,12 +1234,16 @@ protected function compileWhereDate(array $where): array | |
*/ | ||
protected function compileWhereMonth(array $where): array | ||
{ | ||
extract($where); | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
|
||
return $this->compileWhereBasic($where); | ||
return [ | ||
'$expr' => [ | ||
'$'.$where['operator'] => [ | ||
[ | ||
'$month' => '$'.$where['column'], | ||
], | ||
(int) $where['value'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -1217,12 +1252,16 @@ protected function compileWhereMonth(array $where): array | |
*/ | ||
protected function compileWhereDay(array $where): array | ||
{ | ||
extract($where); | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
|
||
return $this->compileWhereBasic($where); | ||
return [ | ||
'$expr' => [ | ||
'$'.$where['operator'] => [ | ||
[ | ||
'$dayOfMonth' => '$'.$where['column'], | ||
], | ||
(int) $where['value'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -1231,12 +1270,16 @@ protected function compileWhereDay(array $where): array | |
*/ | ||
protected function compileWhereYear(array $where): array | ||
{ | ||
extract($where); | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
|
||
return $this->compileWhereBasic($where); | ||
return [ | ||
'$expr' => [ | ||
'$'.$where['operator'] => [ | ||
[ | ||
'$year' => '$'.$where['column'], | ||
], | ||
(int) $where['value'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -1245,12 +1288,16 @@ protected function compileWhereYear(array $where): array | |
*/ | ||
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' => [ | ||
'$'.$where['operator'] => [ | ||
[ | ||
'$dateToString' => ['date' => '$'.$where['column'], 'format' => '%H:%M:%S'], | ||
], | ||
$where['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