You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileMetadataMapper.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright 2022 Carl Schwan <carl@carlschwan.eu>
  5. * @license AGPL-3.0-or-later
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License, version 3,
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. */
  20. namespace OC\Metadata;
  21. use OCP\AppFramework\Db\DoesNotExistException;
  22. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  23. use OCP\AppFramework\Db\QBMapper;
  24. use OCP\DB\Exception;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IDBConnection;
  27. class FileMetadataMapper extends QBMapper {
  28. public function __construct(IDBConnection $db) {
  29. parent::__construct($db, 'file_metadata', FileMetadata::class);
  30. }
  31. /**
  32. * @return FileMetadata[]
  33. * @throws Exception
  34. */
  35. public function findForFile(int $fileId): array {
  36. $qb = $this->db->getQueryBuilder();
  37. $qb->select('*')
  38. ->from($this->getTableName())
  39. ->where($qb->expr()->eq('id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  40. return $this->findEntities($qb);
  41. }
  42. /**
  43. * @throws DoesNotExistException
  44. * @throws MultipleObjectsReturnedException
  45. * @throws Exception
  46. */
  47. public function findForGroupForFile(int $fileId, string $groupName): FileMetadata {
  48. $qb = $this->db->getQueryBuilder();
  49. $qb->select('*')
  50. ->from($this->getTableName())
  51. ->where($qb->expr()->eq('id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY)))
  52. ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, IQueryBuilder::PARAM_STR)));
  53. return $this->findEntity($qb);
  54. }
  55. /**
  56. * @return array<int, FileMetadata>
  57. * @throws Exception
  58. */
  59. public function findForGroupForFiles(array $fileIds, string $groupName): array {
  60. $qb = $this->db->getQueryBuilder();
  61. $qb->select('*')
  62. ->from($this->getTableName())
  63. ->where($qb->expr()->in('id', $qb->createParameter('fileIds')))
  64. ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, IQueryBuilder::PARAM_STR)));
  65. $metadata = [];
  66. foreach (array_chunk($fileIds, 1000) as $fileIdsChunk) {
  67. $qb->setParameter('fileIds', $fileIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
  68. /** @var FileMetadata[] $rawEntities */
  69. $rawEntities = $this->findEntities($qb);
  70. foreach ($rawEntities as $entity) {
  71. $metadata[$entity->getId()] = $entity;
  72. }
  73. }
  74. foreach ($fileIds as $id) {
  75. if (isset($metadata[$id])) {
  76. continue;
  77. }
  78. $empty = new FileMetadata();
  79. $empty->setMetadata([]);
  80. $empty->setGroupName($groupName);
  81. $empty->setId($id);
  82. $metadata[$id] = $empty;
  83. }
  84. return $metadata;
  85. }
  86. public function clear(int $fileId): void {
  87. $qb = $this->db->getQueryBuilder();
  88. $qb->delete($this->getTableName())
  89. ->where($qb->expr()->eq('id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  90. $qb->executeStatement();
  91. }
  92. }