2019-12-03 13:57:53 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2019-10-25 10:55:53 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2019-10-25 10:55:53 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Files\Cache;
|
|
|
|
|
|
2024-07-04 13:20:46 -04:00
|
|
|
use OC\DB\QueryBuilder\ExtendedQueryBuilder;
|
2019-10-25 10:55:53 -04:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2023-11-08 06:35:01 -05:00
|
|
|
use OCP\FilesMetadata\IFilesMetadataManager;
|
2023-11-13 18:25:22 -05:00
|
|
|
use OCP\FilesMetadata\IMetadataQuery;
|
2019-10-25 10:55:53 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Query builder with commonly used helpers for filecache queries
|
|
|
|
|
*/
|
2024-07-04 13:20:46 -04:00
|
|
|
class CacheQueryBuilder extends ExtendedQueryBuilder {
|
2023-11-06 20:21:29 -05:00
|
|
|
private ?string $alias = null;
|
2019-10-25 10:55:53 -04:00
|
|
|
|
2023-11-08 06:35:01 -05:00
|
|
|
public function __construct(
|
2024-07-04 13:20:46 -04:00
|
|
|
IQueryBuilder $queryBuilder,
|
2023-11-08 06:35:01 -05:00
|
|
|
private IFilesMetadataManager $filesMetadataManager,
|
|
|
|
|
) {
|
2024-07-04 13:20:46 -04:00
|
|
|
parent::__construct($queryBuilder);
|
2019-10-25 10:55:53 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-27 16:24:16 -04:00
|
|
|
public function selectTagUsage(): self {
|
|
|
|
|
$this
|
2025-07-28 11:23:47 -04:00
|
|
|
->select('systemtag.name', 'systemtag.id', 'systemtag.visibility', 'systemtag.editable', 'systemtag.etag', 'systemtag.color')
|
2026-01-23 08:50:57 -05:00
|
|
|
->selectAlias($this->func()->count('filecache.fileid'), 'number_files')
|
|
|
|
|
->selectAlias($this->func()->max('filecache.fileid'), 'ref_file_id')
|
2023-04-27 16:24:16 -04:00
|
|
|
->from('filecache', 'filecache')
|
|
|
|
|
->leftJoin('filecache', 'systemtag_object_mapping', 'systemtagmap', $this->expr()->andX(
|
|
|
|
|
$this->expr()->eq('filecache.fileid', $this->expr()->castColumn('systemtagmap.objectid', IQueryBuilder::PARAM_INT)),
|
|
|
|
|
$this->expr()->eq('systemtagmap.objecttype', $this->createNamedParameter('files'))
|
|
|
|
|
))
|
|
|
|
|
->leftJoin('systemtagmap', 'systemtag', 'systemtag', $this->expr()->andX(
|
|
|
|
|
$this->expr()->eq('systemtag.id', 'systemtagmap.systemtagid'),
|
|
|
|
|
$this->expr()->eq('systemtag.visibility', $this->createNamedParameter(true))
|
|
|
|
|
))
|
|
|
|
|
->groupBy('systemtag.name', 'systemtag.id', 'systemtag.visibility', 'systemtag.editable');
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 11:13:19 -04:00
|
|
|
public function selectFileCache(?string $alias = null, bool $joinExtendedCache = true) {
|
2023-04-27 16:24:16 -04:00
|
|
|
$name = $alias ?: 'filecache';
|
2021-04-28 13:07:15 -04:00
|
|
|
$this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", "$name.name", 'mimetype', 'mimepart', 'size', 'mtime',
|
2024-11-21 04:34:39 -05:00
|
|
|
'storage_mtime', 'encrypted', "$name.etag", "$name.permissions", 'checksum', 'unencrypted_size')
|
2022-06-23 10:18:07 -04:00
|
|
|
->from('filecache', $name);
|
|
|
|
|
|
|
|
|
|
if ($joinExtendedCache) {
|
|
|
|
|
$this->addSelect('metadata_etag', 'creation_time', 'upload_time');
|
|
|
|
|
$this->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
|
|
|
|
|
}
|
2019-10-25 12:21:57 -04:00
|
|
|
|
|
|
|
|
$this->alias = $name;
|
2019-10-25 10:55:53 -04:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 13:06:02 -04:00
|
|
|
public function whereStorageId(int $storageId) {
|
|
|
|
|
$this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
|
2019-10-25 10:55:53 -04:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function whereFileId(int $fileId) {
|
2019-10-25 12:21:57 -04:00
|
|
|
$alias = $this->alias;
|
|
|
|
|
if ($alias) {
|
|
|
|
|
$alias .= '.';
|
|
|
|
|
} else {
|
|
|
|
|
$alias = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->andWhere($this->expr()->eq("{$alias}fileid", $this->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
|
2019-10-25 10:55:53 -04:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function wherePath(string $path) {
|
|
|
|
|
$this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function whereParent(int $parent) {
|
2019-10-25 12:21:57 -04:00
|
|
|
$alias = $this->alias;
|
|
|
|
|
if ($alias) {
|
|
|
|
|
$alias .= '.';
|
|
|
|
|
} else {
|
|
|
|
|
$alias = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->andWhere($this->expr()->eq("{$alias}parent", $this->createNamedParameter($parent, IQueryBuilder::PARAM_INT)));
|
2019-10-25 10:55:53 -04:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2020-04-29 10:07:51 -04:00
|
|
|
|
2021-10-17 14:52:38 -04:00
|
|
|
public function whereParentInParameter(string $parameter) {
|
2020-04-29 10:07:51 -04:00
|
|
|
$alias = $this->alias;
|
|
|
|
|
if ($alias) {
|
|
|
|
|
$alias .= '.';
|
|
|
|
|
} else {
|
|
|
|
|
$alias = '';
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 14:52:38 -04:00
|
|
|
$this->andWhere($this->expr()->in("{$alias}parent", $this->createParameter($parameter)));
|
2020-04-29 10:07:51 -04:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2023-11-08 06:35:01 -05:00
|
|
|
|
2023-11-14 05:28:47 -05:00
|
|
|
/**
|
|
|
|
|
* join metadata to current query builder and returns an helper
|
|
|
|
|
*
|
2024-02-14 09:30:11 -05:00
|
|
|
* @return IMetadataQuery
|
2023-11-14 05:28:47 -05:00
|
|
|
*/
|
2024-02-14 09:30:11 -05:00
|
|
|
public function selectMetadata(): IMetadataQuery {
|
2023-11-08 06:35:01 -05:00
|
|
|
$metadataQuery = $this->filesMetadataManager->getMetadataQuery($this, $this->alias, 'fileid');
|
2024-02-14 09:30:11 -05:00
|
|
|
$metadataQuery->retrieveMetadata();
|
2023-11-08 06:35:01 -05:00
|
|
|
return $metadataQuery;
|
|
|
|
|
}
|
2019-10-25 10:55:53 -04:00
|
|
|
}
|