Skip to content

Fix Query on whereDate, whereDay, whereMonth, whereYear #2376

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

Closed
wants to merge 14 commits into from
Closed
158 changes: 111 additions & 47 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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;
Expand Down Expand Up @@ -140,7 +141,7 @@ public function __construct(Connection $connection, Processor $processor)
/**
* Set the projections.
*
* @param array $columns
* @param array $columns
* @return $this
*/
public function project($columns)
Expand All @@ -152,7 +153,8 @@ public function project($columns)

/**
* Set the cursor timeout in seconds.
* @param int $seconds
*
* @param int $seconds
* @return $this
*/
public function timeout($seconds)
Expand All @@ -165,7 +167,7 @@ public function timeout($seconds)
/**
* Set the cursor hint.
*
* @param mixed $index
* @param mixed $index
* @return $this
*/
public function hint($index)
Expand Down Expand Up @@ -216,8 +218,8 @@ public function cursor($columns = [])
/**
* Execute the query as a fresh "select" statement.
*
* @param array $columns
* @param bool $returnLazy
* @param array $columns
* @param bool $returnLazy
* @return array|static[]|Collection|LazyCollection
*/
public function getFresh($columns = [], $returnLazy = false)
Expand Down Expand Up @@ -521,10 +523,10 @@ public function orderBy($column, $direction = 'asc')
/**
* Add a "where all" clause to the query.
*
* @param string $column
* @param array $values
* @param string $boolean
* @param bool $not
* @param string $column
* @param array $values
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereAll($column, array $values, $boolean = 'and', $not = false)
Expand Down Expand Up @@ -728,9 +730,10 @@ public function truncate(): bool
/**
* Get an array with the values of a given column.
*
* @param string $column
* @param string $key
* @param string $column
* @param string $key
* @return array
*
* @deprecated
*/
public function lists($column, $key = null)
Expand Down Expand Up @@ -760,9 +763,9 @@ public function raw($expression = null)
/**
* Append one or more values to an array.
*
* @param mixed $column
* @param mixed $value
* @param bool $unique
* @param mixed $column
* @param mixed $value
* @param bool $unique
* @return int
*/
public function push($column, $value = null, $unique = false)
Expand All @@ -787,8 +790,8 @@ public function push($column, $value = null, $unique = false)
/**
* Remove one or more values from an array.
*
* @param mixed $column
* @param mixed $value
* @param mixed $column
* @param mixed $value
* @return int
*/
public function pull($column, $value = null)
Expand All @@ -811,7 +814,7 @@ public function pull($column, $value = null)
/**
* Remove one or more fields.
*
* @param mixed $columns
* @param mixed $columns
* @return int
*/
public function drop($columns)
Expand Down Expand Up @@ -842,8 +845,8 @@ public function newQuery()
/**
* Perform an update query.
*
* @param array $query
* @param array $options
* @param array $query
* @param array $options
* @return int
*/
protected function performUpdate($query, array $options = [])
Expand All @@ -865,7 +868,7 @@ protected function performUpdate($query, array $options = [])
/**
* Convert a key to ObjectID if needed.
*
* @param mixed $id
* @param mixed $id
* @return mixed
*/
public function convertKey($id)
Expand Down Expand Up @@ -999,7 +1002,7 @@ protected function compileWheres()
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereAll(array $where)
Expand All @@ -1010,7 +1013,7 @@ protected function compileWhereAll(array $where)
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereBasic(array $where)
Expand Down Expand Up @@ -1066,7 +1069,7 @@ protected function compileWhereBasic(array $where)
}

/**
* @param array $where
* @param array $where
* @return mixed
*/
protected function compileWhereNested(array $where)
Expand All @@ -1077,7 +1080,7 @@ protected function compileWhereNested(array $where)
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereIn(array $where)
Expand All @@ -1088,7 +1091,7 @@ protected function compileWhereIn(array $where)
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereNotIn(array $where)
Expand All @@ -1099,7 +1102,7 @@ protected function compileWhereNotIn(array $where)
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereNull(array $where)
Expand All @@ -1111,7 +1114,7 @@ protected function compileWhereNull(array $where)
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereNotNull(array $where)
Expand All @@ -1123,7 +1126,7 @@ protected function compileWhereNotNull(array $where)
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereBetween(array $where)
Expand Down Expand Up @@ -1156,63 +1159,124 @@ protected function compileWhereBetween(array $where)
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereDate(array $where)
{
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);
$operator = $this->conversion[$operator];

return match ($operator) {
'=' => [
$column => [
'$gte' => $startOfDay,
'$lte' => $endOfDay,
],
],
'$ne' => [
$column => [
'$gt' => $endOfDay,
'$lt' => $startOfDay,
],
],
'$lt' => [
$column => [
'$lt' => $startOfDay,
],
],
'$gt' => [
$column => [
'$gt' => $endOfDay,
],
],
'$lte' => [
$column => [
'$lte' => $endOfDay,
],
],
'$gte' => [
$column => [
'$gte' => $startOfDay,
],
],
};
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereMonth(array $where)
{
extract($where);

$where['operator'] = $operator;
$where['value'] = $value;
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;

return $this->compileWhereBasic($where);
return [
'$expr' => [
$operator => [
[
'$month' => '$'.$column,
],
$value,
],
],
];
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereDay(array $where)
{
extract($where);

$where['operator'] = $operator;
$where['value'] = $value;
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;

return $this->compileWhereBasic($where);
return [
'$expr' => [
$operator => [
[
'$dayOfMonth' => '$'.$column,
],
$value,
],
],
];
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereYear(array $where)
{
extract($where);

$where['operator'] = $operator;
$where['value'] = $value;
$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];

return $this->compileWhereBasic($where);
return [
'$expr' => [
$operator => [
[
'$year' => '$'.$column,
],
$value,
],
],
];
}

/**
* @param array $where
* @param array $where
* @return array
*/
protected function compileWhereTime(array $where)
Expand All @@ -1226,7 +1290,7 @@ protected function compileWhereTime(array $where)
}

/**
* @param array $where
* @param array $where
* @return mixed
*/
protected function compileWhereRaw(array $where)
Expand All @@ -1237,7 +1301,7 @@ protected function compileWhereRaw(array $where)
/**
* Set custom options for the query.
*
* @param array $options
* @param array $options
* @return $this
*/
public function options(array $options)
Expand Down
Loading