-
Notifications
You must be signed in to change notification settings - Fork 0
Changes from 3 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, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Jenssegers\Mongodb\Tests; | ||
|
||
use DateTimeImmutable; | ||
use Jenssegers\Mongodb\Tests\Models\Birthday; | ||
use Jenssegers\Mongodb\Tests\Models\Scoped; | ||
use Jenssegers\Mongodb\Tests\Models\User; | ||
|
@@ -24,12 +25,13 @@ public function setUp(): void | |
User::create(['name' => 'Tommy Toe', 'age' => 33, 'title' => 'user']); | ||
User::create(['name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin']); | ||
User::create(['name' => 'Error', 'age' => null, 'title' => null]); | ||
Birthday::create(['name' => 'Mark Moe', 'birthday' => '2020-04-10', 'day' => '10', 'month' => '04', 'year' => '2020', 'time' => '10:53:11']); | ||
Birthday::create(['name' => 'Jane Doe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', 'year' => '2021', 'time' => '10:53:12']); | ||
Birthday::create(['name' => 'Harry Hoe', 'birthday' => '2021-05-11', 'day' => '11', 'month' => '05', 'year' => '2021', 'time' => '10:53:13']); | ||
Birthday::create(['name' => 'Robert Doe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', 'year' => '2021', 'time' => '10:53:14']); | ||
Birthday::create(['name' => 'Mark Moe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', 'year' => '2021', 'time' => '10:53:15']); | ||
Birthday::create(['name' => 'Mark Moe', 'birthday' => '2022-05-12', 'day' => '12', 'month' => '05', 'year' => '2022', 'time' => '10:53:16']); | ||
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2020-04-10 10:53:11'), 'time' => '10:53:11']); | ||
Birthday::create(['name' => 'Jane Doe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:12'), 'time' => '10:53:12']); | ||
Birthday::create(['name' => 'Harry Hoe', 'birthday' => new DateTimeImmutable('2021-05-11 10:53:13'), 'time' => '10:53:13']); | ||
Birthday::create(['name' => 'Robert Doe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:14'), 'time' => '10:53:14']); | ||
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:15'), 'time' => '10:53:15']); | ||
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2022-05-12 10:53:16'), 'time' => '10:53:16']); | ||
Birthday::create(['name' => 'Boo']); | ||
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. |
||
} | ||
|
||
public function tearDown(): void | ||
|
@@ -204,36 +206,63 @@ public function testWhereDate(): void | |
|
||
$birthdayCount = Birthday::whereDate('birthday', '2021-05-11')->get(); | ||
$this->assertCount(1, $birthdayCount); | ||
|
||
$birthdayCount = Birthday::whereDate('birthday', '>', '2021-05-11')->get(); | ||
$this->assertCount(4, $birthdayCount); | ||
|
||
$birthdayCount = Birthday::whereDate('birthday', '>=', '2021-05-11')->get(); | ||
$this->assertCount(5, $birthdayCount); | ||
|
||
$birthdayCount = Birthday::whereDate('birthday', '<', '2021-05-11')->get(); | ||
$this->assertCount(1, $birthdayCount); | ||
|
||
$birthdayCount = Birthday::whereDate('birthday', '<=', '2021-05-11')->get(); | ||
$this->assertCount(2, $birthdayCount); | ||
|
||
$birthdayCount = Birthday::whereDate('birthday', '<>', '2021-05-11')->get(); | ||
$this->assertCount(5, $birthdayCount); | ||
} | ||
|
||
public function testWhereDay(): void | ||
{ | ||
$day = Birthday::whereDay('day', '12')->get(); | ||
$day = Birthday::whereDay('birthday', '12')->get(); | ||
$this->assertCount(4, $day); | ||
|
||
$day = Birthday::whereDay('day', '11')->get(); | ||
$day = Birthday::whereDay('birthday', '11')->get(); | ||
$this->assertCount(1, $day); | ||
} | ||
|
||
public function testWhereMonth(): void | ||
{ | ||
$month = Birthday::whereMonth('month', '04')->get(); | ||
$month = Birthday::whereMonth('birthday', '04')->get(); | ||
$this->assertCount(1, $month); | ||
|
||
$month = Birthday::whereMonth('month', '05')->get(); | ||
$month = Birthday::whereMonth('birthday', '05')->get(); | ||
$this->assertCount(5, $month); | ||
|
||
$month = Birthday::whereMonth('birthday', '>=', '5')->get(); | ||
$this->assertCount(5, $month); | ||
|
||
$month = Birthday::whereMonth('birthday', '<', '10')->get(); | ||
$this->assertCount(7, $month); | ||
|
||
$month = Birthday::whereMonth('birthday', '<>', '5')->get(); | ||
$this->assertCount(2, $month); | ||
} | ||
|
||
public function testWhereYear(): void | ||
{ | ||
$year = Birthday::whereYear('year', '2021')->get(); | ||
$year = Birthday::whereYear('birthday', '2021')->get(); | ||
$this->assertCount(4, $year); | ||
|
||
$year = Birthday::whereYear('year', '2022')->get(); | ||
$year = Birthday::whereYear('birthday', '2022')->get(); | ||
$this->assertCount(1, $year); | ||
|
||
$year = Birthday::whereYear('year', '<', '2021')->get(); | ||
$this->assertCount(1, $year); | ||
$year = Birthday::whereYear('birthday', '<', '2021')->get(); | ||
$this->assertCount(2, $year); | ||
|
||
$year = Birthday::whereYear('birthday', '<>', '2021')->get(); | ||
$this->assertCount(3, $year); | ||
} | ||
|
||
public function testWhereTime(): void | ||
|
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