1
0

MetadataQuery.php 5.3 KB

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