VersionsMapper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 OCA\Files_Versions\Db\VersionEntity;
  26. use OCP\IDBConnection;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\AppFramework\Db\QBMapper;
  29. use OCP\DB\IResult;
  30. /**
  31. * @extends QBMapper<VersionEntity>
  32. */
  33. class VersionsMapper extends QBMapper {
  34. public function __construct(IDBConnection $db) {
  35. parent::__construct($db, 'files_versions', VersionEntity::class);
  36. }
  37. /**
  38. * @return VersionEntity[]
  39. */
  40. public function findAllVersionsForFileId(int $fileId): array {
  41. $qb = $this->db->getQueryBuilder();
  42. $qb->select('*')
  43. ->from($this->getTableName())
  44. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)));
  45. return $this->findEntities($qb);
  46. }
  47. /**
  48. * @return VersionEntity
  49. */
  50. public function findCurrentVersionForFileId(int $fileId): VersionEntity {
  51. $qb = $this->db->getQueryBuilder();
  52. $qb->select('*')
  53. ->from($this->getTableName())
  54. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  55. ->orderBy('timestamp', 'DESC')
  56. ->setMaxResults(1);
  57. return $this->findEntity($qb);
  58. }
  59. public function findVersionForFileId(int $fileId, int $timestamp): VersionEntity {
  60. $qb = $this->db->getQueryBuilder();
  61. $qb->select('*')
  62. ->from($this->getTableName())
  63. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  64. ->andWhere($qb->expr()->eq('timestamp', $qb->createNamedParameter($timestamp)));
  65. return $this->findEntity($qb);
  66. }
  67. public function deleteAllVersionsForFileId(int $fileId): int {
  68. $qb = $this->db->getQueryBuilder();
  69. return $qb->delete($this->getTableName())
  70. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  71. ->executeStatement();
  72. }
  73. public function deleteAllVersionsForUser(int $storageId, string $path = null): void {
  74. $fileIdsGenerator = $this->getFileIdsGenerator($storageId, $path);
  75. $versionEntitiesDeleteQuery = $this->db->getQueryBuilder();
  76. $versionEntitiesDeleteQuery->delete($this->getTableName())
  77. ->where($versionEntitiesDeleteQuery->expr()->in('file_id', $versionEntitiesDeleteQuery->createParameter('file_ids')));
  78. foreach ($fileIdsGenerator as $fileIds) {
  79. $versionEntitiesDeleteQuery->setParameter('file_ids', $fileIds, IQueryBuilder::PARAM_INT_ARRAY);
  80. $versionEntitiesDeleteQuery->executeStatement();
  81. }
  82. }
  83. private function getFileIdsGenerator(int $storageId, ?string $path): \Generator {
  84. $offset = 0;
  85. do {
  86. $filesIdsSelect = $this->db->getQueryBuilder();
  87. $filesIdsSelect->select('fileid')
  88. ->from('filecache')
  89. ->where($filesIdsSelect->expr()->eq('storage', $filesIdsSelect->createNamedParameter($storageId, IQueryBuilder::PARAM_STR)))
  90. ->andWhere($filesIdsSelect->expr()->like('path', $filesIdsSelect->createNamedParameter('files' . ($path ? '/' . $this->db->escapeLikeParameter($path) : '') . '/%', IQueryBuilder::PARAM_STR)))
  91. ->andWhere($filesIdsSelect->expr()->gt('fileid', $filesIdsSelect->createParameter('offset')))
  92. ->setMaxResults(1000)
  93. ->orderBy('fileid', 'ASC');
  94. $filesIdsSelect->setParameter('offset', $offset, IQueryBuilder::PARAM_INT);
  95. $result = $filesIdsSelect->executeQuery();
  96. $fileIds = $result->fetchAll(\PDO::FETCH_COLUMN);
  97. $offset = end($fileIds);
  98. yield $fileIds;
  99. } while (!empty($fileIds));
  100. }
  101. }