1
0

CacheQueryBuilder.php 3.7 KB

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