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.

CacheQueryBuilder.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Files\Cache;
  26. use OC\DB\QueryBuilder\QueryBuilder;
  27. use OC\SystemConfig;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\FilesMetadata\IFilesMetadataManager;
  30. use OCP\FilesMetadata\IMetadataQuery;
  31. use OCP\IDBConnection;
  32. use Psr\Log\LoggerInterface;
  33. /**
  34. * Query builder with commonly used helpers for filecache queries
  35. */
  36. class CacheQueryBuilder extends QueryBuilder {
  37. private ?string $alias = null;
  38. public function __construct(
  39. IDBConnection $connection,
  40. SystemConfig $systemConfig,
  41. LoggerInterface $logger,
  42. private IFilesMetadataManager $filesMetadataManager,
  43. ) {
  44. parent::__construct($connection, $systemConfig, $logger);
  45. }
  46. public function selectTagUsage(): self {
  47. $this
  48. ->select('systemtag.name', 'systemtag.id', 'systemtag.visibility', 'systemtag.editable')
  49. ->selectAlias($this->createFunction('COUNT(filecache.fileid)'), 'number_files')
  50. ->selectAlias($this->createFunction('MAX(filecache.fileid)'), 'ref_file_id')
  51. ->from('filecache', 'filecache')
  52. ->leftJoin('filecache', 'systemtag_object_mapping', 'systemtagmap', $this->expr()->andX(
  53. $this->expr()->eq('filecache.fileid', $this->expr()->castColumn('systemtagmap.objectid', IQueryBuilder::PARAM_INT)),
  54. $this->expr()->eq('systemtagmap.objecttype', $this->createNamedParameter('files'))
  55. ))
  56. ->leftJoin('systemtagmap', 'systemtag', 'systemtag', $this->expr()->andX(
  57. $this->expr()->eq('systemtag.id', 'systemtagmap.systemtagid'),
  58. $this->expr()->eq('systemtag.visibility', $this->createNamedParameter(true))
  59. ))
  60. ->groupBy('systemtag.name', 'systemtag.id', 'systemtag.visibility', 'systemtag.editable');
  61. return $this;
  62. }
  63. public function selectFileCache(string $alias = null, bool $joinExtendedCache = true) {
  64. $name = $alias ?: 'filecache';
  65. $this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", "$name.name", 'mimetype', 'mimepart', 'size', 'mtime',
  66. 'storage_mtime', 'encrypted', 'etag', "$name.permissions", 'checksum', 'unencrypted_size')
  67. ->from('filecache', $name);
  68. if ($joinExtendedCache) {
  69. $this->addSelect('metadata_etag', 'creation_time', 'upload_time');
  70. $this->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
  71. }
  72. $this->alias = $name;
  73. return $this;
  74. }
  75. public function whereStorageId(int $storageId) {
  76. $this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  77. return $this;
  78. }
  79. public function whereFileId(int $fileId) {
  80. $alias = $this->alias;
  81. if ($alias) {
  82. $alias .= '.';
  83. } else {
  84. $alias = '';
  85. }
  86. $this->andWhere($this->expr()->eq("{$alias}fileid", $this->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  87. return $this;
  88. }
  89. public function wherePath(string $path) {
  90. $this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));
  91. return $this;
  92. }
  93. public function whereParent(int $parent) {
  94. $alias = $this->alias;
  95. if ($alias) {
  96. $alias .= '.';
  97. } else {
  98. $alias = '';
  99. }
  100. $this->andWhere($this->expr()->eq("{$alias}parent", $this->createNamedParameter($parent, IQueryBuilder::PARAM_INT)));
  101. return $this;
  102. }
  103. public function whereParentInParameter(string $parameter) {
  104. $alias = $this->alias;
  105. if ($alias) {
  106. $alias .= '.';
  107. } else {
  108. $alias = '';
  109. }
  110. $this->andWhere($this->expr()->in("{$alias}parent", $this->createParameter($parameter)));
  111. return $this;
  112. }
  113. /**
  114. * join metadata to current query builder and returns an helper
  115. *
  116. * @return IMetadataQuery|null NULL if no metadata have never been generated
  117. */
  118. public function selectMetadata(): ?IMetadataQuery {
  119. $metadataQuery = $this->filesMetadataManager->getMetadataQuery($this, $this->alias, 'fileid');
  120. $metadataQuery?->retrieveMetadata();
  121. return $metadataQuery;
  122. }
  123. }