VersionsMapper.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\AppFramework\Db\QBMapper;
  28. use OCP\DB\IResult;
  29. /**
  30. * @extends QBMapper<VersionEntity>
  31. */
  32. class VersionsMapper extends QBMapper {
  33. public function __construct(IDBConnection $db) {
  34. parent::__construct($db, 'files_versions', VersionEntity::class);
  35. }
  36. /**
  37. * @return VersionEntity[]
  38. */
  39. public function findAllVersionsForFileId(int $fileId): array {
  40. $qb = $this->db->getQueryBuilder();
  41. $qb->select('*')
  42. ->from($this->getTableName())
  43. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)));
  44. return $this->findEntities($qb);
  45. }
  46. /**
  47. * @return VersionEntity
  48. */
  49. public function findCurrentVersionForFileId(int $fileId): VersionEntity {
  50. $qb = $this->db->getQueryBuilder();
  51. $qb->select('*')
  52. ->from($this->getTableName())
  53. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  54. ->orderBy('timestamp', 'DESC')
  55. ->setMaxResults(1);
  56. return $this->findEntity($qb);
  57. }
  58. public function findVersionForFileId(int $fileId, int $timestamp): VersionEntity {
  59. $qb = $this->db->getQueryBuilder();
  60. $qb->select('*')
  61. ->from($this->getTableName())
  62. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  63. ->andWhere($qb->expr()->eq('timestamp', $qb->createNamedParameter($timestamp)));
  64. return $this->findEntity($qb);
  65. }
  66. public function deleteAllVersionsForFileId(int $fileId): int {
  67. $qb = $this->db->getQueryBuilder();
  68. return $qb->delete($this->getTableName())
  69. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId)))
  70. ->executeStatement();
  71. }
  72. }