-
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.
NativeQueryBuilderFetcher and ResultSetFetcher
- Loading branch information
1 parent
34343a7
commit 173fe5a
Showing
6 changed files
with
179 additions
and
85 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
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,74 @@ | ||
<?php | ||
|
||
namespace ADT\BulkFetcher; | ||
|
||
abstract class AbstractFetcher extends \Nette\Object implements \Iterator { | ||
|
||
/** | ||
* Fetched data | ||
* @var array | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* Index of current fetched row | ||
* @var integer | ||
*/ | ||
protected $dataIndex; | ||
|
||
protected $limit; | ||
|
||
protected $offset; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $onBeforeFetch = []; | ||
|
||
public function __construct($batch = 100) { | ||
$this->limit = $batch; | ||
} | ||
|
||
public function rewind() { | ||
$this->offset = 0; | ||
$this->fetch(); | ||
} | ||
|
||
public function current() { | ||
return current($this->data); | ||
} | ||
|
||
public function key() { | ||
return key($this->data); | ||
} | ||
|
||
public function next() { | ||
$this->dataIndex++; | ||
|
||
if (next($this->data) === FALSE) { | ||
// fetch next bulk | ||
|
||
if ($this->dataIndex === $this->limit) { | ||
// maybe we have more data | ||
|
||
$this->offset += $this->limit; // next bulk | ||
$this->fetch(); | ||
} | ||
} | ||
} | ||
|
||
public function valid() { | ||
return current($this->data) !== FALSE; | ||
} | ||
|
||
protected function fetch() { | ||
$this->onBeforeFetch(); | ||
|
||
$this->dataIndex = 0; | ||
$this->data = $this->loadNewData(); | ||
} | ||
|
||
abstract protected function loadNewData(); | ||
|
||
} | ||
|
This file was deleted.
Oops, something went wrong.
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,26 @@ | ||
<?php | ||
|
||
namespace ADT\BulkFetcher; | ||
|
||
class Factory | ||
{ | ||
|
||
/** | ||
* @param \Kdyby\Doctrine\NativeQueryBuilder|\Kdyby\Doctrine\ResultSet $dataProvider | ||
* @param int $batch | ||
* @return NativeQueryBuilderFetcher|ResultSetFetcher | ||
*/ | ||
public static function create($dataProvider, $batch = 100) | ||
{ | ||
switch (true) { | ||
case $dataProvider instanceof \Kdyby\Doctrine\ResultSet: | ||
return new ResultSetFetcher($dataProvider, $batch); | ||
break; | ||
|
||
case $dataProvider instanceof \Kdyby\Doctrine\NativeQueryBuilder: | ||
return new NativeQueryBuilderFetcher($dataProvider, $batch); | ||
break; | ||
} | ||
} | ||
|
||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace ADT\BulkFetcher; | ||
|
||
use Kdyby\Doctrine\NativeQueryBuilder; | ||
|
||
class NativeQueryBuilderFetcher extends AbstractFetcher { | ||
|
||
/** @var NativeQueryBuilder */ | ||
protected $qb; | ||
|
||
public function __construct(NativeQueryBuilder $qb, $batch = 100) { | ||
parent::__construct($batch); | ||
|
||
$this->qb = $qb; | ||
} | ||
|
||
protected function loadNewData() { | ||
return $this->qb | ||
->getQuery() | ||
->setFirstResult($this->offset) | ||
->setMaxResults($this->limit) | ||
->getResult(); | ||
} | ||
|
||
} | ||
|
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,23 @@ | ||
<?php | ||
|
||
namespace ADT\BulkFetcher; | ||
|
||
class ResultSetFetcher extends AbstractFetcher { | ||
|
||
/** @var \Kdyby\Doctrine\ResultSet */ | ||
protected $resultSet; | ||
|
||
public function __construct(\Kdyby\Doctrine\ResultSet $resultSet, $bulkCount = 100) { | ||
parent::__construct($bulkCount); | ||
|
||
$this->resultSet = $resultSet; | ||
} | ||
|
||
protected function loadNewData() { | ||
return $this->resultSet | ||
->applyPaging($this->offset, $this->limit) | ||
->toArray(); | ||
} | ||
|
||
} | ||
|