Skip to content

Commit 0681ced

Browse files
committed
fix(previews): use createParameter/setParameter to reuse query in chunk loop
AI-Assisted-By: claude-sonnet-4-6 <noreply@anthropic.com> Signed-off-by: Anna Larch <anna@nextcloud.com> AI-Assisted-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 59144ac commit 0681ced

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

lib/private/Preview/Storage/LocalPreviewStorage.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,14 @@ private function fetchFilecacheByFileIds(array $fileIds): array {
310310
}
311311

312312
$result = [];
313+
$qb = $this->connection->getTypedQueryBuilder();
314+
$qb->selectColumns('fileid', 'storage', 'etag', 'mimetype')
315+
->from('filecache')
316+
->where($qb->expr()->in('fileid', $qb->createParameter('fileIds')))
317+
->runAcrossAllShards();
313318
foreach (array_chunk($fileIds, 1000) as $chunk) {
314-
$qb = $this->connection->getTypedQueryBuilder();
315-
$qb->selectColumns('fileid', 'storage', 'etag', 'mimetype')
316-
->from('filecache')
317-
->where($qb->expr()->in('fileid', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
318-
$rows = $qb->runAcrossAllShards()
319-
->executeQuery();
319+
$qb->setParameter('fileIds', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
320+
$rows = $qb->executeQuery();
320321
while ($row = $rows->fetchAssociative()) {
321322
$result[(int)$row['fileid']] = $row;
322323
}
@@ -336,13 +337,14 @@ private function fetchFilecacheByPathHashes(array $pathHashes): array {
336337
}
337338

338339
$result = [];
340+
$qb = $this->connection->getTypedQueryBuilder();
341+
$qb->selectColumns('fileid', 'storage', 'etag', 'mimetype', 'parent', 'path_hash')
342+
->from('filecache')
343+
->where($qb->expr()->in('path_hash', $qb->createParameter('pathHashes')))
344+
->runAcrossAllShards();
339345
foreach (array_chunk($pathHashes, 1000) as $chunk) {
340-
$qb = $this->connection->getTypedQueryBuilder();
341-
$qb->selectColumns('fileid', 'storage', 'etag', 'mimetype', 'parent', 'path_hash')
342-
->from('filecache')
343-
->where($qb->expr()->in('path_hash', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY)));
344-
$rows = $qb->runAcrossAllShards()
345-
->executeQuery();
346+
$qb->setParameter('pathHashes', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
347+
$rows = $qb->executeQuery();
346348
while ($row = $rows->fetchAssociative()) {
347349
$result[$row['path_hash']] = $row;
348350
}

tests/lib/Preview/Storage/LocalPreviewStorageTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ private function buildQueryBuilderMock(array $rows): IQueryBuilder&MockObject {
129129
$qbMock = $this->createMock(ITypedQueryBuilder::class);
130130
$qbMock->method('selectColumns')->willReturnSelf();
131131
$qbMock->method('from')->willReturnSelf();
132+
$qbMock->method('where')->willReturnSelf();
132133
$qbMock->method('andWhere')->willReturnSelf();
133134
$qbMock->method('runAcrossAllShards')->willReturnSelf();
134135
$qbMock->method('executeQuery')->willReturn($resultMock);

0 commit comments

Comments
 (0)