FileMetadataMapper.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  30. * @template-extends QBMapper<FileMetadata>
  31. */
  32. class FileMetadataMapper extends QBMapper {
  33. public function __construct(IDBConnection $db) {
  34. parent::__construct($db, 'file_metadata', FileMetadata::class);
  35. }
  36. /**
  37. * @return FileMetadata[]
  38. * @throws Exception
  39. */
  40. public function findForFile(int $fileId): array {
  41. $qb = $this->db->getQueryBuilder();
  42. $qb->select('*')
  43. ->from($this->getTableName())
  44. ->where($qb->expr()->eq('id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  45. return $this->findEntities($qb);
  46. }
  47. /**
  48. * @throws DoesNotExistException
  49. * @throws MultipleObjectsReturnedException
  50. * @throws Exception
  51. */
  52. public function findForGroupForFile(int $fileId, string $groupName): FileMetadata {
  53. $qb = $this->db->getQueryBuilder();
  54. $qb->select('*')
  55. ->from($this->getTableName())
  56. ->where($qb->expr()->eq('id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  57. ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, IQueryBuilder::PARAM_STR)));
  58. return $this->findEntity($qb);
  59. }
  60. /**
  61. * @return array<int, FileMetadata>
  62. * @throws Exception
  63. */
  64. public function findForGroupForFiles(array $fileIds, string $groupName): array {
  65. $qb = $this->db->getQueryBuilder();
  66. $qb->select('*')
  67. ->from($this->getTableName())
  68. ->where($qb->expr()->in('id', $qb->createParameter('fileIds')))
  69. ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, IQueryBuilder::PARAM_STR)));
  70. $metadata = [];
  71. foreach (array_chunk($fileIds, 1000) as $fileIdsChunk) {
  72. $qb->setParameter('fileIds', $fileIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
  73. /** @var FileMetadata[] $rawEntities */
  74. $rawEntities = $this->findEntities($qb);
  75. foreach ($rawEntities as $entity) {
  76. $metadata[$entity->getId()] = $entity;
  77. }
  78. }
  79. foreach ($fileIds as $id) {
  80. if (isset($metadata[$id])) {
  81. continue;
  82. }
  83. $empty = new FileMetadata();
  84. $empty->setValue('');
  85. $empty->setGroupName($groupName);
  86. $empty->setId($id);
  87. $metadata[$id] = $empty;
  88. }
  89. return $metadata;
  90. }
  91. public function clear(int $fileId): void {
  92. $qb = $this->db->getQueryBuilder();
  93. $qb->delete($this->getTableName())
  94. ->where($qb->expr()->eq('id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  95. $qb->executeStatement();
  96. }
  97. /**
  98. * Updates an entry in the db from an entity
  99. *
  100. * @param FileMetadata $entity the entity that should be created
  101. * @return FileMetadata the saved entity with the set id
  102. * @throws Exception
  103. * @throws \InvalidArgumentException if entity has no id
  104. */
  105. public function update(Entity $entity): FileMetadata {
  106. if (!($entity instanceof FileMetadata)) {
  107. throw new \Exception("Entity should be a FileMetadata entity");
  108. }
  109. // entity needs an id
  110. $id = $entity->getId();
  111. if ($id === null) {
  112. throw new \InvalidArgumentException('Entity which should be updated has no id');
  113. }
  114. // entity needs an group_name
  115. $groupName = $entity->getGroupName();
  116. if ($groupName === null) {
  117. throw new \InvalidArgumentException('Entity which should be updated has no group_name');
  118. }
  119. $idType = $this->getParameterTypeForProperty($entity, 'id');
  120. $groupNameType = $this->getParameterTypeForProperty($entity, 'groupName');
  121. $value = $entity->getValue();
  122. $valueType = $this->getParameterTypeForProperty($entity, 'value');
  123. $qb = $this->db->getQueryBuilder();
  124. $qb->update($this->tableName)
  125. ->set('value', $qb->createNamedParameter($value, $valueType))
  126. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $idType)))
  127. ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, $groupNameType)))
  128. ->executeStatement();
  129. return $entity;
  130. }
  131. /**
  132. * Override the insertOrUpdate as we could be in a transaction in which case we can not afford on error.
  133. *
  134. * @param FileMetadata $entity the entity that should be created/updated
  135. * @return FileMetadata the saved entity with the (new) id
  136. * @throws Exception
  137. * @throws \InvalidArgumentException if entity has no id
  138. */
  139. public function insertOrUpdate(Entity $entity): FileMetadata {
  140. try {
  141. $existingEntity = $this->findForGroupForFile($entity->getId(), $entity->getGroupName());
  142. } catch (\Throwable) {
  143. $existingEntity = null;
  144. }
  145. if ($existingEntity !== null) {
  146. if ($entity->getValue() !== $existingEntity->getValue()) {
  147. return $this->update($entity);
  148. } else {
  149. return $existingEntity;
  150. }
  151. } else {
  152. return parent::insertOrUpdate($entity);
  153. }
  154. }
  155. }