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

[10.x] Binding order is incorrect when using cursor paginate with multiple unions with a where #50810

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
10 changes: 5 additions & 5 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,14 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
$cursor->parameter($previousColumn)
);

$unionBuilders->each(function ($unionBuilder) use ($previousColumn, $cursor) {
$unionBuilders->each(function ($unionBuilder, $builderIndex) use ($previousColumn, $cursor) {
$unionBuilder->where(
$this->getOriginalColumnNameForCursorPagination($this, $previousColumn),
'=',
$cursor->parameter($previousColumn)
);

$this->addBinding($unionBuilder->getRawBindings()['where'], 'union');
$this->addUnionBinding($unionBuilder->getRawBindings()['where'], $builderIndex);
});
}

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

$unionBuilders->each(function ($unionBuilder) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions) {
$unionBuilder->where(function ($unionBuilder) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions) {
$unionBuilders->each(function ($unionBuilder, $builderIndex) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions) {
$unionBuilder->where(function ($unionBuilder) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions, $builderIndex) {
$unionBuilder->where(
$this->getOriginalColumnNameForCursorPagination($this, $column),
$direction === 'asc' ? '>' : '<',
Expand All @@ -433,7 +433,7 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
});
}

$this->addBinding($unionBuilder->getRawBindings()['where'], 'union');
$this->addUnionBinding($unionBuilder->getRawBindings()['where'], $builderIndex);
});
});
});
Expand Down
32 changes: 31 additions & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2666,7 +2666,7 @@ public function union($query, $all = false)

$this->unions[] = compact('query', 'all');

$this->addBinding($query->getBindings(), 'union');
$this->addUnionBinding($query->getBindings());

return $this;
}
Expand Down Expand Up @@ -3882,6 +3882,36 @@ public function addBinding($value, $type = 'where')
return $this;
}

/**
* Add a union binding to the query.
*
* @param mixed $value
* @param int $index
* @return $this
*
* @throws \InvalidArgumentException
*/
public function addUnionBinding($value, $index = null)
{
$unionBindings = [];
if (is_array($value)) {
$unionBindings = array_values(array_map(
[$this, 'castBinding'],
$value,
));
} else {
$unionBindings[] = $this->castBinding($value);
}

if (is_null($index)) {
$this->bindings['union'][] = $unionBindings;
} else {
$this->bindings['union'][$index] = array_merge($this->bindings['union'][$index], $unionBindings);
}

return $this;
}

/**
* Cast the given binding value.
*
Expand Down
57 changes: 53 additions & 4 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5275,7 +5275,56 @@ public function testCursorPaginateWithUnionWheres()
'(select "id", "start_time" as "created_at", \'video\' as type from "videos" where ("start_time" > ?)) union (select "id", "created_at", \'news\' as type from "news" where ("start_time" > ?)) order by "created_at" asc limit 17',
$builder->toSql());
$this->assertEquals([$ts], $builder->bindings['where']);
$this->assertEquals([$ts], $builder->bindings['union']);
$this->assertEquals([[$ts]], $builder->bindings['union']);

return $results;
});

Paginator::currentPathResolver(function () use ($path) {
return $path;
});

$result = $builder->cursorPaginate($perPage, $columns, $cursorName, $cursor);

$this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
'path' => $path,
'cursorName' => $cursorName,
'parameters' => ['created_at'],
]), $result);
}

public function testCursorPaginateWithMultipleUnionsAndMultipleWheres()
{
$ts = now()->toDateTimeString();

$perPage = 16;
$columns = ['test'];
$cursorName = 'cursor-name';
$cursor = new Cursor(['created_at' => $ts]);
$builder = $this->getMockQueryBuilder();
$builder->select('id', 'start_time as created_at')->selectRaw("'video' as type")->from('videos');
$builder->union($this->getBuilder()->select('id', 'created_at')->selectRaw("'news' as type")->from('news')->where('extra', 'first'));
$builder->union($this->getBuilder()->select('id', 'created_at')->selectRaw("'podcast' as type")->from('podcasts')->where('extra', 'second'));
$builder->orderBy('created_at');

$builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
return new Builder($builder->connection, $builder->grammar, $builder->processor);
});

$path = 'http://foo.bar?cursor='.$cursor->encode();

$results = collect([
['id' => 1, 'created_at' => now(), 'type' => 'video'],
['id' => 2, 'created_at' => now(), 'type' => 'news'],
['id' => 3, 'created_at' => now(), 'type' => 'podcasts'],
]);

$builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results, $ts) {
$this->assertEquals(
'(select "id", "start_time" as "created_at", \'video\' as type from "videos" where ("start_time" > ?)) union (select "id", "created_at", \'news\' as type from "news" where "extra" = ? and ("start_time" > ?)) union (select "id", "created_at", \'podcast\' as type from "podcasts" where "extra" = ? and ("start_time" > ?)) order by "created_at" asc limit 17',
$builder->toSql());
$this->assertEquals([$ts], $builder->bindings['where']);
$this->assertEquals([['first', $ts], ['second', $ts]], $builder->bindings['union']);

return $results;
});
Expand Down Expand Up @@ -5322,7 +5371,7 @@ public function testCursorPaginateWithUnionWheresWithRawOrderExpression()
'(select "id", "is_published", "start_time" as "created_at", \'video\' as type from "videos" where "is_published" = ? and ("start_time" > ?)) union (select "id", "is_published", "created_at", \'news\' as type from "news" where "is_published" = ? and ("start_time" > ?)) order by case when (id = 3 and type="news" then 0 else 1 end), "created_at" asc limit 17',
$builder->toSql());
$this->assertEquals([true, $ts], $builder->bindings['where']);
$this->assertEquals([true, $ts], $builder->bindings['union']);
$this->assertEquals([[true, $ts]], $builder->bindings['union']);

return $results;
});
Expand Down Expand Up @@ -5369,7 +5418,7 @@ public function testCursorPaginateWithUnionWheresReverseOrder()
'(select "id", "start_time" as "created_at", \'video\' as type from "videos" where ("start_time" < ?)) union (select "id", "created_at", \'news\' as type from "news" where ("start_time" < ?)) order by "created_at" desc limit 17',
$builder->toSql());
$this->assertEquals([$ts], $builder->bindings['where']);
$this->assertEquals([$ts], $builder->bindings['union']);
$this->assertEquals([[$ts]], $builder->bindings['union']);

return $results;
});
Expand Down Expand Up @@ -5416,7 +5465,7 @@ public function testCursorPaginateWithUnionWheresMultipleOrders()
'(select "id", "start_time" as "created_at", \'video\' as type from "videos" where ("start_time" < ? or ("start_time" = ? and ("id" > ?)))) union (select "id", "created_at", \'news\' as type from "news" where ("start_time" < ? or ("start_time" = ? and ("id" > ?)))) order by "created_at" desc, "id" asc limit 17',
$builder->toSql());
$this->assertEquals([$ts, $ts, 1], $builder->bindings['where']);
$this->assertEquals([$ts, $ts, 1], $builder->bindings['union']);
$this->assertEquals([[$ts, $ts, 1]], $builder->bindings['union']);

return $results;
});
Expand Down