This repository was archived by the owner on Jul 16, 2021. It is now read-only.
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
Builder::chunk may consider previously set offset and limit #103
Closed
Description
Currently, if one does this:
DB::table('records')->offset(1000)->limit(500)->chunk(50, $callback);
He might expect to retrieve only the records from 1000 to 1500, but that's not the case: the whole table is retrieved. This is particularly surprising because other clauses such as where
would be taken into consideration.
I have come with a first draft, not yet tested, and it still feels clumsy…
public function chunk($count, callable $callback)
{
$thisOffset = $this->getOffset(); // method to be implemented
$thisLimit = $this->getLimit(); // same
$page = 1;
$rowsfetched = 0;
do {
$skip = $thisOffset + ($page - 1) * $count;
if ($thisLimit) {
$remaining = $thisLimit - $rowsfetched;
if ($remaining == 0) {
return true;
}
$take = min($count, $remaining);
} else {
$take = $count;
}
$results = $this->skip($skip)->take($take)->get();
$countResults = $results->count();
if ($countResults == 0) {
break;
}
// On each chunk result set, we will pass them to the callback and then let the
// developer take care of everything within the callback, which allows us to
// keep the memory low for spinning through large result sets for working.
if (call_user_func($callback, $results) === false) {
return false;
}
$page++;
$rowsfetched += $countResults;
}
while ($countResults == $take);
return true;
}
So,
- Do you agree on the idea?
- Suggestions for improving the code?
Metadata
Metadata
Assignees
Labels
No labels