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.

165 lines
5.2 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  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 OC\FilesMetadata\Model;
  25. use OC\FilesMetadata\Service\IndexRequestService;
  26. use OC\FilesMetadata\Service\MetadataRequestService;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
  29. use OCP\FilesMetadata\Exceptions\FilesMetadataTypeException;
  30. use OCP\FilesMetadata\Model\IFilesMetadata;
  31. use OCP\FilesMetadata\Model\IMetadataQuery;
  32. use OCP\FilesMetadata\Model\IMetadataValueWrapper;
  33. /**
  34. * @inheritDoc
  35. * @since 28.0.0
  36. */
  37. class MetadataQuery implements IMetadataQuery {
  38. private array $knownJoinedIndex = [];
  39. public function __construct(
  40. private IQueryBuilder $queryBuilder,
  41. private IFilesMetadata $knownMetadata,
  42. private string $fileTableAlias = 'fc',
  43. private string $fileIdField = 'fileid',
  44. private string $alias = 'meta',
  45. private string $aliasIndexPrefix = 'meta_index'
  46. ) {
  47. }
  48. /**
  49. * @inheritDoc
  50. * @see self::extractMetadata()
  51. * @since 28.0.0
  52. */
  53. public function retrieveMetadata(): void {
  54. $this->queryBuilder->selectAlias($this->alias . '.json', 'meta_json');
  55. $this->queryBuilder->leftJoin(
  56. $this->fileTableAlias, MetadataRequestService::TABLE_METADATA, $this->alias,
  57. $this->queryBuilder->expr()->eq($this->fileTableAlias . '.' . $this->fileIdField, $this->alias . '.file_id')
  58. );
  59. }
  60. /**
  61. * @param array $row result row
  62. *
  63. * @inheritDoc
  64. * @return IFilesMetadata metadata
  65. * @see self::retrieveMetadata()
  66. * @since 28.0.0
  67. */
  68. public function extractMetadata(array $row): IFilesMetadata {
  69. $fileId = (array_key_exists($this->fileIdField, $row)) ? $row[$this->fileIdField] : 0;
  70. $metadata = new FilesMetadata((int)$fileId);
  71. try {
  72. $metadata->importFromDatabase($row, $this->alias . '_');
  73. } catch (FilesMetadataNotFoundException) {
  74. // can be ignored as files' metadata are optional and might not exist in database
  75. }
  76. return $metadata;
  77. }
  78. /**
  79. * @param string $metadataKey metadata key
  80. * @param bool $enforce limit the request only to existing metadata
  81. *
  82. * @inheritDoc
  83. * @since 28.0.0
  84. */
  85. public function joinIndex(string $metadataKey, bool $enforce = false): string {
  86. if (array_key_exists($metadataKey, $this->knownJoinedIndex)) {
  87. return $this->knownJoinedIndex[$metadataKey];
  88. }
  89. $aliasIndex = $this->aliasIndexPrefix . '_' . count($this->knownJoinedIndex);
  90. $this->knownJoinedIndex[$metadataKey] = $aliasIndex;
  91. $expr = $this->queryBuilder->expr();
  92. $andX = $expr->andX($expr->eq($aliasIndex . '.file_id', $this->fileTableAlias . '.' . $this->fileIdField));
  93. $andX->add($expr->eq($this->getMetadataKeyField($metadataKey), $this->queryBuilder->createNamedParameter($metadataKey)));
  94. if ($enforce) {
  95. $this->queryBuilder->rightJoin(
  96. $this->fileTableAlias,
  97. IndexRequestService::TABLE_METADATA_INDEX,
  98. $aliasIndex,
  99. $andX
  100. );
  101. } else {
  102. $this->queryBuilder->leftJoin(
  103. $this->fileTableAlias,
  104. IndexRequestService::TABLE_METADATA_INDEX,
  105. $aliasIndex,
  106. $andX
  107. );
  108. }
  109. return $aliasIndex;
  110. }
  111. /**
  112. * @throws FilesMetadataNotFoundException
  113. */
  114. public function joinedTableAlias(string $metadataKey): string {
  115. if (!array_key_exists($metadataKey, $this->knownJoinedIndex)) {
  116. throw new FilesMetadataNotFoundException('table related to ' . $metadataKey . ' not initiated, you need to use leftJoin() first.');
  117. }
  118. return $this->knownJoinedIndex[$metadataKey];
  119. }
  120. /**
  121. * @inheritDoc
  122. *
  123. * @param string $metadataKey metadata key
  124. *
  125. * @return string table field
  126. * @throws FilesMetadataNotFoundException
  127. * @since 28.0.0
  128. */
  129. public function getMetadataKeyField(string $metadataKey): string {
  130. return $this->joinedTableAlias($metadataKey) . '.meta_key';
  131. }
  132. /**
  133. * @inheritDoc
  134. *
  135. * @param string $metadataKey metadata key
  136. *
  137. * @return string table field
  138. * @throws FilesMetadataNotFoundException if metadataKey is not known
  139. * @throws FilesMetadataTypeException is metadataKey is not set as indexed
  140. * @since 28.0.0
  141. */
  142. public function getMetadataValueField(string $metadataKey): string {
  143. return match ($this->knownMetadata->getType($metadataKey)) {
  144. IMetadataValueWrapper::TYPE_STRING => $this->joinedTableAlias($metadataKey) . '.meta_value_string',
  145. IMetadataValueWrapper::TYPE_INT, IMetadataValueWrapper::TYPE_BOOL => $this->joinedTableAlias($metadataKey) . '.meta_value_int',
  146. default => throw new FilesMetadataTypeException('metadata is not set as indexed'),
  147. };
  148. }
  149. }