1
0

VersionsMapper.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Versions\Db;
  8. use OCP\AppFramework\Db\QBMapper;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\IDBConnection;
  11. /**
  12. * @extends QBMapper<VersionEntity>
  13. */
  14. class VersionsMapper extends QBMapper {
  15. public function __construct(IDBConnection $db) {
  16. parent::__construct($db, 'files_versions', VersionEntity::class);
  17. }
  18. /**
  19. * @return VersionEntity[]
  20. */
  21. public function findAllVersionsForFileId(int $fileId): array {
  22. $qb = $this->db->getQueryBuilder();
  23. $qb->select('*')
  24. ->from($this->getTableName())
  25. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)));
  26. return $this->findEntities($qb);
  27. }
  28. /**
  29. * @return VersionEntity
  30. */
  31. public function findCurrentVersionForFileId(int $fileId): VersionEntity {
  32. $qb = $this->db->getQueryBuilder();
  33. $qb->select('*')
  34. ->from($this->getTableName())
  35. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  36. ->orderBy('timestamp', 'DESC')
  37. ->setMaxResults(1);
  38. return $this->findEntity($qb);
  39. }
  40. public function findVersionForFileId(int $fileId, int $timestamp): VersionEntity {
  41. $qb = $this->db->getQueryBuilder();
  42. $qb->select('*')
  43. ->from($this->getTableName())
  44. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  45. ->andWhere($qb->expr()->eq('timestamp', $qb->createNamedParameter($timestamp)));
  46. return $this->findEntity($qb);
  47. }
  48. public function deleteAllVersionsForFileId(int $fileId): int {
  49. $qb = $this->db->getQueryBuilder();
  50. return $qb->delete($this->getTableName())
  51. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  52. ->executeStatement();
  53. }
  54. public function deleteAllVersionsForUser(int $storageId, ?string $path = null): void {
  55. $fileIdsGenerator = $this->getFileIdsGenerator($storageId, $path);
  56. $versionEntitiesDeleteQuery = $this->db->getQueryBuilder();
  57. $versionEntitiesDeleteQuery->delete($this->getTableName())
  58. ->where($versionEntitiesDeleteQuery->expr()->in('file_id', $versionEntitiesDeleteQuery->createParameter('file_ids')));
  59. foreach ($fileIdsGenerator as $fileIds) {
  60. $versionEntitiesDeleteQuery->setParameter('file_ids', $fileIds, IQueryBuilder::PARAM_INT_ARRAY);
  61. $versionEntitiesDeleteQuery->executeStatement();
  62. }
  63. }
  64. private function getFileIdsGenerator(int $storageId, ?string $path): \Generator {
  65. $offset = 0;
  66. do {
  67. $filesIdsSelect = $this->db->getQueryBuilder();
  68. $filesIdsSelect->select('fileid')
  69. ->from('filecache')
  70. ->where($filesIdsSelect->expr()->eq('storage', $filesIdsSelect->createNamedParameter($storageId, IQueryBuilder::PARAM_STR)))
  71. ->andWhere($filesIdsSelect->expr()->like('path', $filesIdsSelect->createNamedParameter('files' . ($path ? '/' . $this->db->escapeLikeParameter($path) : '') . '/%', IQueryBuilder::PARAM_STR)))
  72. ->andWhere($filesIdsSelect->expr()->gt('fileid', $filesIdsSelect->createParameter('offset')))
  73. ->setMaxResults(1000)
  74. ->orderBy('fileid', 'ASC');
  75. $filesIdsSelect->setParameter('offset', $offset, IQueryBuilder::PARAM_INT);
  76. $result = $filesIdsSelect->executeQuery();
  77. $fileIds = $result->fetchAll(\PDO::FETCH_COLUMN);
  78. $offset = end($fileIds);
  79. yield $fileIds;
  80. } while (!empty($fileIds));
  81. }
  82. }