Skip to content

Commit

Permalink
NativeQueryBuilderFetcher and ResultSetFetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
michallohnisky committed May 7, 2018
1 parent 34343a7 commit 173fe5a
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 85 deletions.
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#
# BulkFetcher

`\ADT\BulkFetcher\Factory` can be used with:

- `\Kdyby\Doctrine\ResultSet`
- `\Kdyby\Doctrine\NativeQueryBuilder`

## Installation

Expand All @@ -10,13 +15,30 @@ composer require adt/bulk-fetcher

## Full example

Whole batch is in transaction.

```php
$bulkedResultSet = new \ADT\BulkFetcher($resultSet, 100);
$bulkedResultSet->onBeforeLoadNewData[] = function() use ($entityManager) {
$entityManager->clear();
};

foreach ($bulkedResultSet as $key => $row) {
// code
$qb = $entityManager->createQueryBuilder('user');

try {
$entityManager->beginTransaction();

$data = \ADT\BulkFetcher\Factory::create($qb, 100);
$data->onBeforeFetch[] = function() use ($entityManager) {
$entityManager->commit();
$entityManager->clear();
$entityManager->beginTransaction();
};

foreach ($data as $key => $row) {
// code
}

$entityManager->commit();

} catch (\Exception $e) {
$entityManager->rollback();
throw $e;
}
```
74 changes: 74 additions & 0 deletions src/AbstractFetcher.php
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();

}

78 changes: 0 additions & 78 deletions src/BulkFetcher.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/Factory.php
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;
}
}

}
27 changes: 27 additions & 0 deletions src/NativeQueryBuilderFetcher.php
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();
}

}

23 changes: 23 additions & 0 deletions src/ResultSetFetcher.php
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();
}

}

0 comments on commit 173fe5a

Please sign in to comment.