1
0

CacheQueryBuilder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  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\Files\Cache;
  25. use OC\DB\QueryBuilder\QueryBuilder;
  26. use OC\SystemConfig;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\IDBConnection;
  29. use Psr\Log\LoggerInterface;
  30. /**
  31. * Query builder with commonly used helpers for filecache queries
  32. */
  33. class CacheQueryBuilder extends QueryBuilder {
  34. private $alias = null;
  35. public function __construct(IDBConnection $connection, SystemConfig $systemConfig, LoggerInterface $logger) {
  36. parent::__construct($connection, $systemConfig, $logger);
  37. }
  38. public function selectTagUsage(): self {
  39. $this
  40. ->select('systemtag.name', 'systemtag.id', 'systemtag.visibility', 'systemtag.editable')
  41. ->selectAlias($this->createFunction('COUNT(filecache.fileid)'), 'number_files')
  42. ->selectAlias($this->createFunction('MAX(filecache.fileid)'), 'ref_file_id')
  43. ->from('filecache', 'filecache')
  44. ->leftJoin('filecache', 'systemtag_object_mapping', 'systemtagmap', $this->expr()->andX(
  45. $this->expr()->eq('filecache.fileid', $this->expr()->castColumn('systemtagmap.objectid', IQueryBuilder::PARAM_INT)),
  46. $this->expr()->eq('systemtagmap.objecttype', $this->createNamedParameter('files'))
  47. ))
  48. ->leftJoin('systemtagmap', 'systemtag', 'systemtag', $this->expr()->andX(
  49. $this->expr()->eq('systemtag.id', 'systemtagmap.systemtagid'),
  50. $this->expr()->eq('systemtag.visibility', $this->createNamedParameter(true))
  51. ))
  52. ->groupBy('systemtag.name', 'systemtag.id', 'systemtag.visibility', 'systemtag.editable');
  53. return $this;
  54. }
  55. public function selectFileCache(string $alias = null, bool $joinExtendedCache = true) {
  56. $name = $alias ?: 'filecache';
  57. $this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", "$name.name", 'mimetype', 'mimepart', 'size', 'mtime',
  58. 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum', 'unencrypted_size')
  59. ->from('filecache', $name);
  60. if ($joinExtendedCache) {
  61. $this->addSelect('metadata_etag', 'creation_time', 'upload_time');
  62. $this->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
  63. }
  64. $this->alias = $name;
  65. return $this;
  66. }
  67. public function whereStorageId(int $storageId) {
  68. $this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  69. return $this;
  70. }
  71. public function whereFileId(int $fileId) {
  72. $alias = $this->alias;
  73. if ($alias) {
  74. $alias .= '.';
  75. } else {
  76. $alias = '';
  77. }
  78. $this->andWhere($this->expr()->eq("{$alias}fileid", $this->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  79. return $this;
  80. }
  81. public function wherePath(string $path) {
  82. $this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));
  83. return $this;
  84. }
  85. public function whereParent(int $parent) {
  86. $alias = $this->alias;
  87. if ($alias) {
  88. $alias .= '.';
  89. } else {
  90. $alias = '';
  91. }
  92. $this->andWhere($this->expr()->eq("{$alias}parent", $this->createNamedParameter($parent, IQueryBuilder::PARAM_INT)));
  93. return $this;
  94. }
  95. public function whereParentInParameter(string $parameter) {
  96. $alias = $this->alias;
  97. if ($alias) {
  98. $alias .= '.';
  99. } else {
  100. $alias = '';
  101. }
  102. $this->andWhere($this->expr()->in("{$alias}parent", $this->createParameter($parameter)));
  103. return $this;
  104. }
  105. }