Passed
Push — master ( 420a37...67ecdc )
by Morris
13:26 queued 12s
created

CacheQueryBuilder::whereStorageId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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
27
namespace OC\Files\Cache;
28
29
use OC\DB\QueryBuilder\QueryBuilder;
30
use OC\SystemConfig;
31
use OCP\DB\QueryBuilder\IQueryBuilder;
32
use OCP\IDBConnection;
33
use OCP\ILogger;
34
35
/**
36
 * Query builder with commonly used helpers for filecache queries
37
 */
38
class CacheQueryBuilder extends QueryBuilder {
39
	private $cache;
40
	private $alias = null;
41
42
	public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger, Cache $cache) {
43
		parent::__construct($connection, $systemConfig, $logger);
44
45
		$this->cache = $cache;
46
	}
47
48
	public function selectFileCache(string $alias = null) {
49
		$name = $alias ? $alias : 'filecache';
50
		$this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", 'name', 'mimetype', 'mimepart', 'size', 'mtime',
51
			'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum', 'metadata_etag', 'creation_time', 'upload_time')
52
			->from('filecache', $name)
53
			->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
54
55
		$this->alias = $name;
56
57
		return $this;
58
	}
59
60
	public function whereStorageId() {
61
		$this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)));
62
63
		return $this;
64
	}
65
66
	public function whereFileId(int $fileId) {
67
		$alias = $this->alias;
68
		if ($alias) {
69
			$alias .= '.';
70
		} else {
71
			$alias = '';
72
		}
73
74
		$this->andWhere($this->expr()->eq("{$alias}fileid", $this->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
75
76
		return $this;
77
	}
78
79
	public function wherePath(string $path) {
80
		$this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));
81
82
		return $this;
83
	}
84
85
	public function whereParent(int $parent) {
86
		$alias = $this->alias;
87
		if ($alias) {
88
			$alias .= '.';
89
		} else {
90
			$alias = '';
91
		}
92
93
		$this->andWhere($this->expr()->eq("{$alias}parent", $this->createNamedParameter($parent, IQueryBuilder::PARAM_INT)));
94
95
		return $this;
96
	}
97
98
	public function whereParentIn(array $parents) {
99
		$alias = $this->alias;
100
		if ($alias) {
101
			$alias .= '.';
102
		} else {
103
			$alias = '';
104
		}
105
106
		$this->andWhere($this->expr()->in("{$alias}parent", $this->createNamedParameter($parents, IQueryBuilder::PARAM_INT_ARRAY)));
107
108
		return $this;
109
	}
110
}
111