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

[8.x] Fix null values for cursor pagination #38371

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,18 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
$builder->where(function (self $builder) use ($addCursorConditions, $cursor, $orders, $i) {
['column' => $column, 'direction' => $direction] = $orders[$i];

$builder->where(
$this->getOriginalColumnNameForCursorPagination($this, $column),
$direction === 'asc' ? '>' : '<',
$cursor->parameter($column)
);
$param = $cursor->parameter($column);
$originalColumn = $this->getOriginalColumnNameForCursorPagination($this, $column);

if (is_null($param)) {
$this->whereClauseForCursorNullValue($originalColumn, $builder, $direction);
} else {
$builder->where(
$originalColumn,
$direction === 'asc' ? '>' : '<',
$param
);
}

if ($i < $orders->count() - 1) {
$builder->orWhere(function (self $builder) use ($addCursorConditions, $column, $i) {
Expand All @@ -341,6 +348,27 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
]);
}

/**
* Handle creating a where clause for when a cursor value is null.
*
* @param string $column
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder
* @param string $direction
* @return void
*/
protected function whereClauseForCursorNullValue($column, $builder, $direction)
{
$ascending = $direction === 'asc';
if (! $builder->getGrammar()->nullsAreOrderedFirst()) {
$ascending = ! $ascending;
}
if ($ascending) {
$builder->whereNotNull($column);
} else {
$builder->whereNull($column);
}
}

/**
* Get the original column name of the given column, without any aliasing.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -1284,4 +1284,14 @@ public function getOperators()
{
return $this->operators;
}

/**
* Nulls are ordered first when ordring in ascending order.
*
* @return bool
*/
public function nullsAreOrderedFirst()
{
return true;
}
}
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,14 @@ protected function wrapJsonPathAttributes($path)
: "'$attribute'";
}, $path);
}

/**
* Nulls are ordered first when ordring in ascending order.
*
* @return bool
*/
public function nullsAreOrderedFirst()
{
return false;
}
}