Skip to content

Commit

Permalink
feat: Add yieldEntities wrapper for entity mapping in QBMapper
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Jun 20, 2024
1 parent efe3244 commit 7324687
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/public/AppFramework/Db/QBMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCP\AppFramework\Db;

use Generator;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
Expand Down Expand Up @@ -321,6 +322,26 @@ protected function findEntities(IQueryBuilder $query): array {
}
}

/**
* 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
Expand Down

0 comments on commit 7324687

Please sign in to comment.