Skip to content

Commit

Permalink
[10.x] Implement chunkById in descending order (laravel#48666)
Browse files Browse the repository at this point in the history
* Implement chunkById in descending order

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
2 people authored and timacdonald committed Oct 24, 2023
1 parent e3f754a commit 39e86e7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Illuminate/Database/Concerns/BuildsQueries.php
Expand Up @@ -112,6 +112,35 @@ public function each(callable $callback, $count = 1000)
* @return bool
*/
public function chunkById($count, callable $callback, $column = null, $alias = null)
{
return $this->orderedChunkById($count, $callback, $column, $alias);
}

/**
* Chunk the results of a query by comparing IDs in descending order.
*
* @param int $count
* @param callable $callback
* @param string|null $column
* @param string|null $alias
* @return bool
*/
public function chunkByIdDesc($count, callable $callback, $column = null, $alias = null)
{
return $this->orderedChunkById($count, $callback, $column, $alias, descending: true);
}

/**
* Chunk the results of a query by comparing IDs in a given order.
*
* @param int $count
* @param callable $callback
* @param string|null $column
* @param string|null $alias
* @param bool $descending
* @return bool
*/
public function orderedChunkById($count, callable $callback, $column = null, $alias = null, $descending = false)
{
$column ??= $this->defaultKeyName();

Expand All @@ -127,7 +156,11 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
// We'll execute the query for the given page and get the results. If there are
// no results we can just break and return from here. When there are results
// we will call the callback with the current chunk of these results here.
$results = $clone->forPageAfterId($count, $lastId, $column)->get();
if ($descending) {
$results = $clone->forPageBeforeId($count, $lastId, $column)->get();
} else {
$results = $clone->forPageAfterId($count, $lastId, $column)->get();
}

$countResults = $results->count();

Expand Down
20 changes: 20 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Expand Up @@ -4421,6 +4421,26 @@ public function testChunkPaginatesUsingIdWithAlias()
}, 'table.id', 'table_id');
}

public function testChunkPaginatesUsingIdDesc()
{
$builder = $this->getMockQueryBuilder();
$builder->orders[] = ['column' => 'foobar', 'direction' => 'desc'];

$chunk1 = collect([(object) ['someIdField' => 10], (object) ['someIdField' => 1]]);
$chunk2 = collect([]);
$builder->shouldReceive('forPageBeforeId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
$builder->shouldReceive('forPageBeforeId')->once()->with(2, 1, 'someIdField')->andReturnSelf();
$builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);

$callbackAssertor = m::mock(stdClass::class);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->never()->with($chunk2);

$builder->chunkByIdDesc(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
}, 'someIdField');
}

public function testPaginate()
{
$perPage = 16;
Expand Down

0 comments on commit 39e86e7

Please sign in to comment.