Passed
Push — master ( e17684...b6c034 )
by Blizzz
35:05 queued 17:14
created

CacheQueryBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

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