VersionsMapper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me>
  5. *
  6. * @author Louis Chmn <louis@chmn.me>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Versions\Db;
  25. use OCP\AppFramework\Db\QBMapper;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\IDBConnection;
  28. /**
  29. * @extends QBMapper<VersionEntity>
  30. */
  31. class VersionsMapper extends QBMapper {
  32. public function __construct(IDBConnection $db) {
  33. parent::__construct($db, 'files_versions', VersionEntity::class);
  34. }
  35. /**
  36. * @return VersionEntity[]
  37. */
  38. public function findAllVersionsForFileId(int $fileId): array {
  39. $qb = $this->db->getQueryBuilder();
  40. $qb->select('*')
  41. ->from($this->getTableName())
  42. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)));
  43. return $this->findEntities($qb);
  44. }
  45. /**
  46. * @return VersionEntity
  47. */
  48. public function findCurrentVersionForFileId(int $fileId): VersionEntity {
  49. $qb = $this->db->getQueryBuilder();
  50. $qb->select('*')
  51. ->from($this->getTableName())
  52. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  53. ->orderBy('timestamp', 'DESC')
  54. ->setMaxResults(1);
  55. return $this->findEntity($qb);
  56. }
  57. public function findVersionForFileId(int $fileId, int $timestamp): VersionEntity {
  58. $qb = $this->db->getQueryBuilder();
  59. $qb->select('*')
  60. ->from($this->getTableName())
  61. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  62. ->andWhere($qb->expr()->eq('timestamp', $qb->createNamedParameter($timestamp)));
  63. return $this->findEntity($qb);
  64. }
  65. public function deleteAllVersionsForFileId(int $fileId): int {
  66. $qb = $this->db->getQueryBuilder();
  67. return $qb->delete($this->getTableName())
  68. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  69. ->executeStatement();
  70. }
  71. public function deleteAllVersionsForUser(int $storageId, string $path = null): void {
  72. $fileIdsGenerator = $this->getFileIdsGenerator($storageId, $path);
  73. $versionEntitiesDeleteQuery = $this->db->getQueryBuilder();
  74. $versionEntitiesDeleteQuery->delete($this->getTableName())
  75. ->where($versionEntitiesDeleteQuery->expr()->in('file_id', $versionEntitiesDeleteQuery->createParameter('file_ids')));
  76. foreach ($fileIdsGenerator as $fileIds) {
  77. $versionEntitiesDeleteQuery->setParameter('file_ids', $fileIds, IQueryBuilder::PARAM_INT_ARRAY);
  78. $versionEntitiesDeleteQuery->executeStatement();
  79. }
  80. }
  81. private function getFileIdsGenerator(int $storageId, ?string $path): \Generator {
  82. $offset = 0;
  83. do {
  84. $filesIdsSelect = $this->db->getQueryBuilder();
  85. $filesIdsSelect->select('fileid')
  86. ->from('filecache')
  87. ->where($filesIdsSelect->expr()->eq('storage', $filesIdsSelect->createNamedParameter($storageId, IQueryBuilder::PARAM_STR)))
  88. ->andWhere($filesIdsSelect->expr()->like('path', $filesIdsSelect->createNamedParameter('files' . ($path ? '/' . $this->db->escapeLikeParameter($path) : '') . '/%', IQueryBuilder::PARAM_STR)))
  89. ->andWhere($filesIdsSelect->expr()->gt('fileid', $filesIdsSelect->createParameter('offset')))
  90. ->setMaxResults(1000)
  91. ->orderBy('fileid', 'ASC');
  92. $filesIdsSelect->setParameter('offset', $offset, IQueryBuilder::PARAM_INT);
  93. $result = $filesIdsSelect->executeQuery();
  94. $fileIds = $result->fetchAll(\PDO::FETCH_COLUMN);
  95. $offset = end($fileIds);
  96. yield $fileIds;
  97. } while (!empty($fileIds));
  98. }
  99. }