Skip to content

Commit

Permalink
Implement chunkById in descending order
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiancalara committed Oct 8, 2023
1 parent cddb4f3 commit 6459f6c
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
Original file line number Diff line number Diff line change
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, 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
Original file line number Diff line number Diff line change
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 6459f6c

Please sign in to comment.