mirror of
https://github.com/nextcloud/server.git
synced 2026-06-11 01:30:50 -04:00
Merge pull request #45947 from nextcloud/feat/qbmapper-yield-entities
feat: Add yieldEntities wrapper for entity mapping in QBMapper
This commit is contained in:
commit
1b49c86480
1 changed files with 21 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ declare(strict_types=1);
|
|||
*/
|
||||
namespace OCP\AppFramework\Db;
|
||||
|
||||
use Generator;
|
||||
use OCP\DB\Exception;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\IDBConnection;
|
||||
|
|
@ -321,6 +322,26 @@ abstract class QBMapper {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a sql query and yields each resulting entity to obtain database entries in a memory-efficient way
|
||||
*
|
||||
* @param IQueryBuilder $query
|
||||
* @return Generator Generator of fetched entities
|
||||
* @psalm-return Generator<T> Generator of fetched entities
|
||||
* @throws Exception
|
||||
* @since 30.0.0
|
||||
*/
|
||||
protected function yieldEntities(IQueryBuilder $query): Generator {
|
||||
$result = $query->executeQuery();
|
||||
try {
|
||||
while ($row = $result->fetch()) {
|
||||
yield $this->mapRowToEntity($row);
|
||||
}
|
||||
} finally {
|
||||
$result->closeCursor();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns an db result and throws exceptions when there are more or less
|
||||
|
|
|
|||
Loading…
Reference in a new issue