Passed
Push — master ( ccc0a5...5195be )
by Roeland
10:43 queued 11s
created

CacheQueryBuilder::wherePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * @copyright Copyright (c) 2019 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Files\Cache;
23
24
use OC\DB\QueryBuilder\QueryBuilder;
25
use OC\SystemConfig;
26
use OCP\DB\QueryBuilder\IQueryBuilder;
27
use OCP\IDBConnection;
28
use OCP\ILogger;
29
30
/**
31
 * Query builder with commonly used helpers for filecache queries
32
 */
33
class CacheQueryBuilder extends QueryBuilder {
34
	private $cache;
35
	private $alias = null;
36
37
	public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger, Cache $cache) {
38
		parent::__construct($connection, $systemConfig, $logger);
39
40
		$this->cache = $cache;
41
	}
42
43
	public function selectFileCache(string $alias = null) {
44
		$name = $alias ? $alias : 'filecache';
45
		$this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", 'name', 'mimetype', 'mimepart', 'size', 'mtime',
46
			'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum', 'metadata_etag', 'creation_time', 'upload_time')
47
			->from('filecache', $name)
48
			->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
49
50
		$this->alias = $name;
51
52
		return $this;
53
	}
54
55
	public function whereStorageId() {
56
		$this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)));
57
58
		return $this;
59
	}
60
61
	public function whereFileId(int $fileId) {
62
		$alias = $this->alias;
63
		if ($alias) {
64
			$alias .= '.';
65
		} else {
66
			$alias = '';
67
		}
68
69
		$this->andWhere($this->expr()->eq("{$alias}fileid", $this->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
70
71
		return $this;
72
	}
73
74
	public function wherePath(string $path) {
75
		$this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));
76
77
		return $this;
78
	}
79
80
	public function whereParent(int $parent) {
81
		$alias = $this->alias;
82
		if ($alias) {
83
			$alias .= '.';
84
		} else {
85
			$alias = '';
86
		}
87
88
		$this->andWhere($this->expr()->eq("{$alias}parent", $this->createNamedParameter($parent, IQueryBuilder::PARAM_INT)));
89
90
		return $this;
91
	}
92
}
93