Merge pull request #45947 from nextcloud/feat/qbmapper-yield-entities

feat: Add yieldEntities wrapper for entity mapping in QBMapper
This commit is contained in:
Joas Schilling 2024-06-27 14:48:26 +02:00 committed by GitHub
commit 1b49c86480
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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