Skip to content

Commit

Permalink
Set the right column names for the cursor on the unions
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsvdanker committed Mar 28, 2024
1 parent 8c5a884 commit d4e0d30
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,11 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
$orders = $this->ensureOrderForCursorPagination(! is_null($cursor) && $cursor->pointsToPreviousItems());

if (! is_null($cursor)) {
$addCursorConditions = function (self $builder, $previousColumn, $i) use (&$addCursorConditions, $cursor, $orders) {
$addCursorConditions = function (self $builder, $previousColumn, $originalColumn, $i) use (&$addCursorConditions, $cursor, $orders) {
$unionBuilders = isset($builder->unions) ? collect($builder->unions)->pluck('query') : collect();

if (! is_null($previousColumn)) {
$originalColumn = $this->getOriginalColumnNameForCursorPagination($this, $previousColumn);
$originalColumn ??= $this->getOriginalColumnNameForCursorPagination($this, $previousColumn);

$builder->where(
Str::contains($originalColumn, ['(', ')']) ? new Expression($originalColumn) : $originalColumn,
Expand All @@ -393,7 +393,7 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =

$unionBuilders->each(function ($unionBuilder) use ($previousColumn, $cursor) {
$unionBuilder->where(
$this->getOriginalColumnNameForCursorPagination($this, $previousColumn),
$this->getOriginalColumnNameForCursorPagination($unionBuilder, $previousColumn),
'=',
$cursor->parameter($previousColumn)
);
Expand All @@ -414,22 +414,23 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
);

if ($i < $orders->count() - 1) {
$builder->orWhere(function (self $builder) use ($addCursorConditions, $column, $i) {
$addCursorConditions($builder, $column, $i + 1);
$builder->orWhere(function (self $builder) use ($addCursorConditions, $column, $originalColumn, $i) {
$addCursorConditions($builder, $column, $originalColumn, $i + 1);
});
}

$unionBuilders->each(function ($unionBuilder) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions) {
$unionBuilder->where(function ($unionBuilder) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions) {
$originalColumn = $this->getOriginalColumnNameForCursorPagination($unionBuilder, $column);
$unionBuilder->where(
$this->getOriginalColumnNameForCursorPagination($this, $column),
$originalColumn,
$direction === 'asc' ? '>' : '<',
$cursor->parameter($column)
);

if ($i < $orders->count() - 1) {
$unionBuilder->orWhere(function (self $builder) use ($addCursorConditions, $column, $i) {
$addCursorConditions($builder, $column, $i + 1);
$unionBuilder->orWhere(function (self $builder) use ($addCursorConditions, $column, $originalColumn, $i) {
$addCursorConditions($builder, $column, $originalColumn, $i + 1);
});
}

Expand All @@ -439,7 +440,7 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
});
};

$addCursorConditions($this, null, 0);
$addCursorConditions($this, null, null, 0);
}

$this->limit($perPage + 1);
Expand Down

0 comments on commit d4e0d30

Please sign in to comment.