-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHPORM-60 Fix Query on whereDate, whereDay, whereMonth, whereYear #2572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cc9dcca
384e9d0
94aaaeb
e95c0c8
7f198bd
5ce0d1f
0c525c4
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 Jenssegers\Mongodb\Connection; | ||
|
@@ -115,6 +116,7 @@ class Builder extends BaseBuilder | |
* @var array | ||
*/ | ||
protected $conversion = [ | ||
'=' => 'eq', | ||
'!=' => 'ne', | ||
'<>' => 'ne', | ||
'<' => 'lt', | ||
|
@@ -1075,7 +1077,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]]; | ||
|
@@ -1180,12 +1182,35 @@ 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' => [ | ||
$where['column'] => [ | ||
'$not' => [ | ||
'$gte' => $startOfDay, | ||
'$lte' => $endOfDay, | ||
], | ||
], | ||
], | ||
'lt', 'gte' => [ | ||
$where['column'] => [ | ||
'$'.$where['operator'] => $startOfDay, | ||
], | ||
], | ||
'gt', 'lte' => [ | ||
$where['column'] => [ | ||
'$'.$where['operator'] => $endOfDay, | ||
], | ||
], | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -1194,12 +1219,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'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -1208,12 +1237,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'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -1222,12 +1255,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'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -1236,12 +1273,26 @@ protected function compileWhereYear(array $where): array | |
*/ | ||
protected function compileWhereTime(array $where): array | ||
{ | ||
extract($where); | ||
if (! is_string($where['value']) || ! preg_match('/^[0-2][0-9](:[0-6][0-9](:[0-6][0-9])?)?$/', $where['value'], $matches)) { | ||
throw new \InvalidArgumentException(sprintf('Invalid time format, expected HH:MM:SS, HH:MM or HH, got "%s"', is_string($where['value']) ? $where['value'] : get_debug_type($where['value']))); | ||
} | ||
|
||
$where['operator'] = $operator; | ||
$where['value'] = $value; | ||
$format = match (count($matches)) { | ||
1 => '%H', | ||
2 => '%H:%M', | ||
3 => '%H:%M:%S', | ||
}; | ||
|
||
return $this->compileWhereBasic($where); | ||
return [ | ||
'$expr' => [ | ||
'$'.$where['operator'] => [ | ||
[ | ||
'$dateToString' => ['date' => '$'.$where['column'], 'format' => $format], | ||
], | ||
$where['value'], | ||
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. We talked about validating user input. Should we enforce a string in the format 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. Fixed with support for various time formats. |
||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
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.
Clever use of
count($matches)
.