1
0

FileMetadataMapper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright 2022 Carl Schwan <carl@carlschwan.eu>
  5. * @copyright Copyright 2022 Louis Chmn <louis@chmn.me>
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Metadata;
  22. use OCP\AppFramework\Db\DoesNotExistException;
  23. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  24. use OCP\AppFramework\Db\QBMapper;
  25. use OCP\AppFramework\Db\Entity;
  26. use OCP\DB\Exception;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\IDBConnection;
  29. class FileMetadataMapper extends QBMapper {
  30. public function __construct(IDBConnection $db) {
  31. parent::__construct($db, 'file_metadata', FileMetadata::class);
  32. }
  33. /**
  34. * @return FileMetadata[]
  35. * @throws Exception
  36. */
  37. public function findForFile(int $fileId): array {
  38. $qb = $this->db->getQueryBuilder();
  39. $qb->select('*')
  40. ->from($this->getTableName())
  41. ->where($qb->expr()->eq('id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  42. return $this->findEntities($qb);
  43. }
  44. /**
  45. * @throws DoesNotExistException
  46. * @throws MultipleObjectsReturnedException
  47. * @throws Exception
  48. */
  49. public function findForGroupForFile(int $fileId, string $groupName): FileMetadata {
  50. $qb = $this->db->getQueryBuilder();
  51. $qb->select('*')
  52. ->from($this->getTableName())
  53. ->where($qb->expr()->eq('id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY)))
  54. ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, IQueryBuilder::PARAM_STR)));
  55. return $this->findEntity($qb);
  56. }
  57. /**
  58. * @return array<int, FileMetadata>
  59. * @throws Exception
  60. */
  61. public function findForGroupForFiles(array $fileIds, string $groupName): array {
  62. $qb = $this->db->getQueryBuilder();
  63. $qb->select('*')
  64. ->from($this->getTableName())
  65. ->where($qb->expr()->in('id', $qb->createParameter('fileIds')))
  66. ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, IQueryBuilder::PARAM_STR)));
  67. $metadata = [];
  68. foreach (array_chunk($fileIds, 1000) as $fileIdsChunk) {
  69. $qb->setParameter('fileIds', $fileIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
  70. /** @var FileMetadata[] $rawEntities */
  71. $rawEntities = $this->findEntities($qb);
  72. foreach ($rawEntities as $entity) {
  73. $metadata[$entity->getId()] = $entity;
  74. }
  75. }
  76. foreach ($fileIds as $id) {
  77. if (isset($metadata[$id])) {
  78. continue;
  79. }
  80. $empty = new FileMetadata();
  81. $empty->setMetadata([]);
  82. $empty->setGroupName($groupName);
  83. $empty->setId($id);
  84. $metadata[$id] = $empty;
  85. }
  86. return $metadata;
  87. }
  88. public function clear(int $fileId): void {
  89. $qb = $this->db->getQueryBuilder();
  90. $qb->delete($this->getTableName())
  91. ->where($qb->expr()->eq('id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  92. $qb->executeStatement();
  93. }
  94. /**
  95. * Updates an entry in the db from an entity
  96. *
  97. * @param Entity $entity the entity that should be created
  98. * @return Entity the saved entity with the set id
  99. * @throws Exception
  100. * @throws \InvalidArgumentException if entity has no id
  101. */
  102. public function update(Entity $entity): Entity {
  103. if (!($entity instanceof FileMetadata)) {
  104. throw new \Exception("Entity should be a FileMetadata entity");
  105. }
  106. // entity needs an id
  107. $id = $entity->getId();
  108. if ($id === null) {
  109. throw new \InvalidArgumentException('Entity which should be updated has no id');
  110. }
  111. // entity needs an group_name
  112. $groupName = $entity->getGroupName();
  113. if ($groupName === null) {
  114. throw new \InvalidArgumentException('Entity which should be updated has no group_name');
  115. }
  116. $idType = $this->getParameterTypeForProperty($entity, 'id');
  117. $groupNameType = $this->getParameterTypeForProperty($entity, 'groupName');
  118. $metadataValue = $entity->getMetadata();
  119. $metadataType = $this->getParameterTypeForProperty($entity, 'metadata');
  120. $qb = $this->db->getQueryBuilder();
  121. $qb->update($this->tableName)
  122. ->set('metadata', $qb->createNamedParameter($metadataValue, $metadataType))
  123. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $idType)))
  124. ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, $groupNameType)))
  125. ->executeStatement();
  126. return $entity;
  127. }
  128. }