Completed
Push — master ( 448b39...a46d9a )
by Maxence
02:01
created

CoreRequestBuilder::limitToDocumentIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
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 OCA\FullTextSearch\Db;
28
29
30
use Doctrine\DBAL\Query\QueryBuilder;
31
use OCA\FullTextSearch\Model\Index;
32
use OCA\FullTextSearch\Service\ConfigService;
33
use OCA\FullTextSearch\Service\MiscService;
34
use OCP\DB\QueryBuilder\IQueryBuilder;
35
use OCP\IDBConnection;
36
use OCP\IL10N;
37
38
class CoreRequestBuilder {
39
40
	const TABLE_INDEXES = 'fulltextsearch_indexes';
41
	const TABLE_TICKS = 'fulltextsearch_ticks';
42
43
	/** @var IDBConnection */
44
	protected $dbConnection;
45
46
	/** @var IL10N */
47
	protected $l10n;
48
49
	/** @var ConfigService */
50
	protected $configService;
51
52
	/** @var MiscService */
53
	protected $miscService;
54
55
	/** @var string */
56
	protected $defaultSelectAlias;
57
58
59
	/**
60
	 * CoreRequestBuilder constructor.
61
	 *
62
	 * @param IL10N $l10n
63
	 * @param IDBConnection $connection
64
	 * @param ConfigService $configService
65
	 * @param MiscService $miscService
66
	 */
67
	public function __construct(
68
		IL10N $l10n, IDBConnection $connection, ConfigService $configService,
69
		MiscService $miscService
70
	) {
71
		$this->l10n = $l10n;
72
		$this->dbConnection = $connection;
73
		$this->configService = $configService;
74
		$this->miscService = $miscService;
75
	}
76
77
78
	/**
79
	 * Limit the request to the Id
80
	 *
81
	 * @param IQueryBuilder $qb
82
	 * @param int $id
83
	 */
84
	protected function limitToId(IQueryBuilder &$qb, $id) {
85
		$this->limitToDBField($qb, 'id', $id);
86
	}
87
88
89
	/**
90
	 * Limit the request to the OwnerId
91
	 *
92
	 * @param IQueryBuilder $qb
93
	 * @param string $userId
94
	 */
95
	protected function limitToOwnerId(IQueryBuilder &$qb, $userId) {
96
		$this->limitToDBField($qb, 'owner_id', $userId);
97
	}
98
99
100
	/**
101
	 * Limit to the type
102
	 *
103
	 * @param IQueryBuilder $qb
104
	 * @param string $providerId
105
	 */
106
	protected function limitToProviderId(IQueryBuilder &$qb, $providerId) {
107
		$this->limitToDBField($qb, 'provider_id', $providerId);
108
	}
109
110
111
	/**
112
	 * Limit to the documentId
113
	 *
114
	 * @param IQueryBuilder $qb
115
	 * @param string $documentId
116
	 */
117
	protected function limitToDocumentId(IQueryBuilder &$qb, $documentId) {
118
		$this->limitToDBField($qb, 'document_id', $documentId);
119
	}
120
121
122
	/**
123
	 * Limit to documentIds
124
	 *
125
	 * @param IQueryBuilder $qb
126
	 * @param array $documentIds
127
	 */
128
	protected function limitToDocumentIds(IQueryBuilder &$qb, $documentIds) {
129
		$this->limitToDBField($qb, 'document_id', $documentIds);
130
	}
131
132
133
	/**
134
	 * Limit the request to the Source
135
	 *
136
	 * @param IQueryBuilder $qb
137
	 * @param string $source
138
	 */
139
	protected function limitToSource(IQueryBuilder &$qb, $source) {
140
		$this->limitToDBField($qb, 'id', $source);
141
	}
142
143
144
	/**
145
	 * Limit the request to the Source
146
	 *
147
	 * @param IQueryBuilder $qb
148
	 * @param string $status
149
	 */
150
	protected function limitToStatus(IQueryBuilder &$qb, $status) {
151
		$this->limitToDBField($qb, 'status', $status);
152
	}
153
154
155
	/**
156
	 * @param IQueryBuilder $qb
157
	 * @param string $field
158
	 * @param string|integer|array $values
159
	 */
160
	private function limitToDBField(IQueryBuilder &$qb, $field, $values) {
161
		$expr = $qb->expr();
162
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : '';
163
		$field = $pf . $field;
164
165
		if (!is_array($values)) {
166
			$values = [$values];
167
		}
168
169
		$orX = $expr->orX();
170
		foreach($values as $value) {
171
			$orX->add($expr->eq($field, $qb->createNamedParameter($value)));
172
		}
173
174
		$qb->andWhere($orX);
175
	}
176
177
178
	/**
179
	 * @param IQueryBuilder $qb
180
	 */
181
	protected function limitToQueuedIndexes(IQueryBuilder &$qb) {
182
		$expr = $qb->expr();
183
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : '';
184
		$qb->andWhere($expr->neq($pf . 'status', $qb->createNamedParameter(Index::INDEX_OK)));
185
	}
186
187
}
188
189
190
191