1
0

CacheQueryBuilder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Files\Cache;
  8. use OC\DB\QueryBuilder\QueryBuilder;
  9. use OC\SystemConfig;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\FilesMetadata\IFilesMetadataManager;
  12. use OCP\FilesMetadata\IMetadataQuery;
  13. use OCP\IDBConnection;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Query builder with commonly used helpers for filecache queries
  17. */
  18. class CacheQueryBuilder extends QueryBuilder {
  19. private ?string $alias = null;
  20. public function __construct(
  21. IDBConnection $connection,
  22. SystemConfig $systemConfig,
  23. LoggerInterface $logger,
  24. private IFilesMetadataManager $filesMetadataManager,
  25. ) {
  26. parent::__construct($connection, $systemConfig, $logger);
  27. }
  28. public function selectTagUsage(): self {
  29. $this
  30. ->select('systemtag.name', 'systemtag.id', 'systemtag.visibility', 'systemtag.editable')
  31. ->selectAlias($this->createFunction('COUNT(filecache.fileid)'), 'number_files')
  32. ->selectAlias($this->createFunction('MAX(filecache.fileid)'), 'ref_file_id')
  33. ->from('filecache', 'filecache')
  34. ->leftJoin('filecache', 'systemtag_object_mapping', 'systemtagmap', $this->expr()->andX(
  35. $this->expr()->eq('filecache.fileid', $this->expr()->castColumn('systemtagmap.objectid', IQueryBuilder::PARAM_INT)),
  36. $this->expr()->eq('systemtagmap.objecttype', $this->createNamedParameter('files'))
  37. ))
  38. ->leftJoin('systemtagmap', 'systemtag', 'systemtag', $this->expr()->andX(
  39. $this->expr()->eq('systemtag.id', 'systemtagmap.systemtagid'),
  40. $this->expr()->eq('systemtag.visibility', $this->createNamedParameter(true))
  41. ))
  42. ->groupBy('systemtag.name', 'systemtag.id', 'systemtag.visibility', 'systemtag.editable');
  43. return $this;
  44. }
  45. public function selectFileCache(?string $alias = null, bool $joinExtendedCache = true) {
  46. $name = $alias ?: 'filecache';
  47. $this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", "$name.name", 'mimetype', 'mimepart', 'size', 'mtime',
  48. 'storage_mtime', 'encrypted', 'etag', "$name.permissions", 'checksum', 'unencrypted_size')
  49. ->from('filecache', $name);
  50. if ($joinExtendedCache) {
  51. $this->addSelect('metadata_etag', 'creation_time', 'upload_time');
  52. $this->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
  53. }
  54. $this->alias = $name;
  55. return $this;
  56. }
  57. public function whereStorageId(int $storageId) {
  58. $this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  59. return $this;
  60. }
  61. public function whereFileId(int $fileId) {
  62. $alias = $this->alias;
  63. if ($alias) {
  64. $alias .= '.';
  65. } else {
  66. $alias = '';
  67. }
  68. $this->andWhere($this->expr()->eq("{$alias}fileid", $this->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  69. return $this;
  70. }
  71. public function wherePath(string $path) {
  72. $this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));
  73. return $this;
  74. }
  75. public function whereParent(int $parent) {
  76. $alias = $this->alias;
  77. if ($alias) {
  78. $alias .= '.';
  79. } else {
  80. $alias = '';
  81. }
  82. $this->andWhere($this->expr()->eq("{$alias}parent", $this->createNamedParameter($parent, IQueryBuilder::PARAM_INT)));
  83. return $this;
  84. }
  85. public function whereParentInParameter(string $parameter) {
  86. $alias = $this->alias;
  87. if ($alias) {
  88. $alias .= '.';
  89. } else {
  90. $alias = '';
  91. }
  92. $this->andWhere($this->expr()->in("{$alias}parent", $this->createParameter($parameter)));
  93. return $this;
  94. }
  95. /**
  96. * join metadata to current query builder and returns an helper
  97. *
  98. * @return IMetadataQuery
  99. */
  100. public function selectMetadata(): IMetadataQuery {
  101. $metadataQuery = $this->filesMetadataManager->getMetadataQuery($this, $this->alias, 'fileid');
  102. $metadataQuery->retrieveMetadata();
  103. return $metadataQuery;
  104. }
  105. }