diff options
author | Julius Härtl <jus@bitgrid.net> | 2024-06-18 12:10:27 +0200 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2024-06-24 09:15:58 +0200 |
commit | 2fb0ca9cf7929b3527f2068e19a690c371191b72 (patch) | |
tree | 943b279bb49113648178fd79864dfb18f20edcc4 | |
parent | 4d1f9ae54e591dc0b9dab622684408632df562d7 (diff) | |
download | nextcloud-server-2fb0ca9cf7929b3527f2068e19a690c371191b72.tar.gz nextcloud-server-2fb0ca9cf7929b3527f2068e19a690c371191b72.zip |
feat: Add yieldEntities wrapper for entity mapping in QBMapper
Signed-off-by: Julius Härtl <jus@bitgrid.net>
-rw-r--r-- | lib/public/AppFramework/Db/QBMapper.php | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/public/AppFramework/Db/QBMapper.php b/lib/public/AppFramework/Db/QBMapper.php index badd2483b58..41160b29fa5 100644 --- a/lib/public/AppFramework/Db/QBMapper.php +++ b/lib/public/AppFramework/Db/QBMapper.php @@ -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 |