Skip to content
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

[11.x] Introduce where[Date,Time,Year,Month,Day]Between methods in query builder #53105

Open
wants to merge 21 commits into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ccadc32
added date based where between in the main Sql grammar
malkhazidartsmelidze Oct 10, 2024
dc26434
added where[DateFunction]Between functions in builder
malkhazidartsmelidze Oct 10, 2024
c48f989
added date & time tests
malkhazidartsmelidze Oct 10, 2024
e949e95
fixed tests and query builder errors
malkhazidartsmelidze Oct 10, 2024
be9e4e7
more tests
malkhazidartsmelidze Oct 10, 2024
a2fc490
fixed styleci
malkhazidartsmelidze Oct 11, 2024
b9a8a48
finished all types of argument testing on all types of functions
malkhazidartsmelidze Oct 13, 2024
6383471
added posgres support
malkhazidartsmelidze Oct 13, 2024
456e394
added sqlite support
malkhazidartsmelidze Oct 13, 2024
430d5ce
added sqlserver support
malkhazidartsmelidze Oct 13, 2024
8cfb0c7
fixed styleci
malkhazidartsmelidze Oct 13, 2024
b598491
fixed styleci
malkhazidartsmelidze Oct 13, 2024
f79fe85
added orWhereDateBetween, orWhereTimeBetween, orWhereYearBetween, orW…
malkhazidartsmelidze Oct 15, 2024
793b98d
added whereDateNotBetween, whereTimeNotBetween, whereYearNotBetween, …
malkhazidartsmelidze Oct 15, 2024
97008c2
fixed styleci
malkhazidartsmelidze Oct 15, 2024
a719ab3
added query builder tests
malkhazidartsmelidze Oct 15, 2024
e645708
added \Carbon\Month Enum support
malkhazidartsmelidze Oct 15, 2024
703c5dd
check if month enum exists (since nestbot\carbon:^3.0)
malkhazidartsmelidze Oct 15, 2024
55176cd
fixed styleci
malkhazidartsmelidze Oct 15, 2024
ec742da
fixed tests
malkhazidartsmelidze Oct 15, 2024
4de68b1
styleci
malkhazidartsmelidze Oct 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
314 changes: 314 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BackedEnum;
use Carbon\CarbonPeriod;
use Carbon\Month;
use Closure;
use DateTimeInterface;
use Illuminate\Contracts\Database\Query\Builder as BuilderContract;
Expand Down Expand Up @@ -1550,6 +1551,62 @@ public function orWhereDate($column, $operator, $value = null)
return $this->whereDate($column, $operator, $value, 'or');
}

/**
* Add a "where date between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|null> $values
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereDateBetween($column, iterable $values, $boolean = 'and', $not = false)
{
if ($values instanceof CarbonPeriod) {
$values = [$values->getStartDate(), $values->getEndDate()];
}

$values = array_slice(Arr::flatten($values), 0, 2);

$values = collect($values)
->map(function ($value) {
if ($value instanceof DateTimeInterface) {
return $value->format('Y-m-d');
}

return $value;
})
->all();

return $this->addDateBasedWhereBetween('Date', $column, $values, $boolean, $not);
}

/**
* Add a "or where date between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|null> $values
* @param bool $not
* @return $this
*/
public function orWhereDateBetween($column, iterable $values, $not = false)
{
return $this->whereDateBetween($column, $values, 'or', $not);
}

/**
* Add a "where date not between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|null> $values
* @param string $boolean
* @return $this
*/
public function whereDateNotBetween($column, iterable $values, $boolean = 'or')
{
return $this->whereDateBetween($column, $values, $boolean, true);
}

/**
* Add a "where time" statement to the query.
*
Expand Down Expand Up @@ -1581,6 +1638,62 @@ public function whereTime($column, $operator, $value = null, $boolean = 'and')
return $this->addDateBasedWhere('Time', $column, $operator, $value, $boolean);
}

/**
* Add a "where time between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|null> $values
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereTimeBetween($column, iterable $values, $boolean = 'and', $not = false)
{
if ($values instanceof CarbonPeriod) {
$values = [$values->getStartDate(), $values->getEndDate()];
}

$values = array_slice(Arr::flatten($values), 0, 2);

$values = collect($values)
->map(function ($value) {
if ($value instanceof DateTimeInterface) {
return $value->format('H:i:s');
}

return $value;
})
->all();

return $this->addDateBasedWhereBetween('Time', $column, $values, $boolean, $not);
}

/**
* Add a "or where time between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|null> $values
* @param bool $not
* @return $this
*/
public function orWhereTimeBetween($column, iterable $values, $not = false)
{
return $this->whereTimeBetween($column, $values, 'or', $not);
}

/**
* Add a "where time not between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|null> $values
* @param string $boolean
* @return $this
*/
public function whereTimeNotBetween($column, iterable $values, $boolean = 'or')
{
return $this->whereTimeBetween($column, $values, $boolean, true);
}

/**
* Add an "or where time" statement to the query.
*
Expand Down Expand Up @@ -1633,6 +1746,66 @@ public function whereDay($column, $operator, $value = null, $boolean = 'and')
return $this->addDateBasedWhere('Day', $column, $operator, $value, $boolean);
}

/**
* Add a "where day between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|int|null> $values
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereDayBetween($column, iterable $values, $boolean = 'and', $not = false)
{
if ($values instanceof CarbonPeriod) {
$values = [$values->getStartDate(), $values->getEndDate()];
}

$values = array_slice(Arr::flatten($values), 0, 2);

$values = collect($values)
->map(function ($value) {
if ($value instanceof DateTimeInterface) {
return $value->format('d');
}

if (! $value instanceof ExpressionContract) {
return sprintf('%02d', $value);
}

return $value;
})
->all();

return $this->addDateBasedWhereBetween('Day', $column, $values, $boolean, $not);
}

/**
* Add a "or where day between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|int|null> $values
* @param bool $not
* @return $this
*/
public function orWhereDayBetween($column, iterable $values, $not = false)
{
return $this->whereDayBetween($column, $values, 'or', $not);
}

/**
* Add a "where day not between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|int|null> $values
* @param string $boolean
* @return $this
*/
public function whereDayNotBetween($column, iterable $values, $boolean = 'or')
{
return $this->whereDayBetween($column, $values, $boolean, true);
}

/**
* Add an "or where day" statement to the query.
*
Expand Down Expand Up @@ -1685,6 +1858,70 @@ public function whereMonth($column, $operator, $value = null, $boolean = 'and')
return $this->addDateBasedWhere('Month', $column, $operator, $value, $boolean);
}

/**
* Add a "where month between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|int|null> $values
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereMonthBetween($column, iterable $values, $boolean = 'and', $not = false)
{
if ($values instanceof CarbonPeriod) {
$values = [$values->getStartDate(), $values->getEndDate()];
}

$values = array_slice(Arr::flatten($values), 0, 2);

$values = collect($values)
->map(function ($value) {
if ($value instanceof DateTimeInterface) {
return $value->format('m');
}

if (class_exists(Month::class) && $value instanceof Month) {
$value = $value->value;
}

if (! $value instanceof ExpressionContract) {
return sprintf('%02d', $value);
}

return $value;
})
->all();

return $this->addDateBasedWhereBetween('Month', $column, $values, $boolean, $not);
}

/**
* Add a "or where month between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|int|null> $values
* @param bool $not
* @return $this
*/
public function orWhereMonthBetween($column, iterable $values, $not = false)
{
return $this->whereMonthBetween($column, $values, 'or', $not);
}

/**
* Add a "where month not between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|int|null> $values
* @param string $boolean
* @return $this
*/
public function whereMonthNotBetween($column, iterable $values, $boolean = 'or')
{
return $this->whereMonthBetween($column, $values, $boolean, true);
}

/**
* Add an "or where month" statement to the query.
*
Expand Down Expand Up @@ -1733,6 +1970,62 @@ public function whereYear($column, $operator, $value = null, $boolean = 'and')
return $this->addDateBasedWhere('Year', $column, $operator, $value, $boolean);
}

/**
* Add a "where year between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|int|null> $values
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereYearBetween($column, iterable $values, $boolean = 'and', $not = false)
{
if ($values instanceof CarbonPeriod) {
$values = [$values->getStartDate(), $values->getEndDate()];
}

$values = array_slice(Arr::flatten($values), 0, 2);

$values = collect($values)
->map(function ($value) {
if ($value instanceof DateTimeInterface) {
return $value->format('Y');
}

return $value;
})
->all();

return $this->addDateBasedWhereBetween('Year', $column, $values, $boolean, $not);
}

/**
* Add a "or where year between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|int|null> $values
* @param bool $not
* @return $this
*/
public function orWhereYearBetween($column, iterable $values, $not = false)
{
return $this->whereYearBetween($column, $values, 'or', $not);
}

/**
* Add a "where year not between" statement to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param iterable<\DateTimeInterface|string|int|null> $values
* @param string $boolean
* @return $this
*/
public function whereYearNotBetween($column, iterable $values, $boolean = 'or')
{
return $this->whereYearBetween($column, $values, $boolean, true);
}

/**
* Add an "or where year" statement to the query.
*
Expand Down Expand Up @@ -1771,6 +2064,27 @@ protected function addDateBasedWhere($type, $column, $operator, $value, $boolean
return $this;
}

/**
* Add a date based (year, month, day, time) where between statement to the query.
*
* @param string $type
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param string $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
protected function addDateBasedWhereBetween($type, $column, iterable $values, $boolean = 'and', $not = false)
{
$type = $type.'Between';

$this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not');

$this->addBinding($this->cleanBindings($values), 'where');

return $this;
}

/**
* Add a nested where statement to the query.
*
Expand Down
Loading