-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ladislav Tomsa
committed
Mar 16, 2017
0 parents
commit 99a428b
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# | ||
|
||
## Installation | ||
|
||
via composer: | ||
|
||
```sh | ||
composer require adt/bulk-fetcher | ||
``` | ||
|
||
## Usage | ||
|
||
foreach (new \ADT\BulkFetcher($resultSet, 100) as $key => $value) { | ||
// code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "adt/bulk-fetcher", | ||
"description": "", | ||
"type": "library", | ||
"license": [ | ||
"MIT", | ||
"BSD-3-Clause", | ||
"GPL-2.0", | ||
"GPL-3.0" | ||
], | ||
"require-dev": { | ||
"kdyby/doctrine": "^3.1", | ||
"nette/utils": "^2.4", | ||
"nette/di": "^2.4" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"ADT\\BulkFetcher\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace ADT\BulkFetcher; | ||
|
||
class BulkFetcher extends \Nette\Object implements \Iterator { | ||
|
||
/** @var \Kdyby\Doctrine\ResultSet */ | ||
protected $resultSet; | ||
|
||
/** | ||
* Data from database | ||
* @var array | ||
*/ | ||
protected $bulkData; | ||
|
||
/** | ||
* Index of current fetched row | ||
* @var integer | ||
*/ | ||
protected $bulkDataIndex; | ||
|
||
protected $limit; | ||
|
||
protected $offset; | ||
|
||
public function __construct(\Kdyby\Doctrine\ResultSet $resultSet, $bulkCount = 100) { | ||
$this->resultSet = $resultSet; | ||
$this->limit = $bulkCount; | ||
} | ||
|
||
public function rewind() { | ||
$this->offset = 0; | ||
$this->loadNewData(); | ||
} | ||
|
||
public function current() { | ||
return current($this->bulkData); | ||
} | ||
|
||
public function key() { | ||
return key($this->bulkData); | ||
} | ||
|
||
public function next() { | ||
$this->bulkDataIndex++; | ||
|
||
if (next($this->bulkData) === FALSE) { | ||
// fetch next bulk | ||
|
||
if ($this->bulkDataIndex === $this->limit) { | ||
// maybe we have more data | ||
|
||
$this->offset += $this->limit; // next bulk | ||
$this->loadNewData(); | ||
} | ||
} | ||
} | ||
|
||
public function valid() { | ||
return current($this->bulkData) !== FALSE; | ||
} | ||
|
||
protected function loadNewData() { | ||
$this->bulkDataIndex = 0; | ||
$this->bulkData = $this->resultSet | ||
->applyPaging($this->offset, $this->limit) | ||
->toArray(); | ||
} | ||
|
||
} | ||
|