Completed
Push — master ( 347a51...ad246f )
by Maxence
02:23
created

CoreRequestBuilder::limitToDBFieldArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.7333
nc 8
cc 4
nop 3
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * FullTextSearch - Full text search framework for Nextcloud
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\FullTextSearch\Db;
32
33
34
use Doctrine\DBAL\Query\QueryBuilder;
35
use OCA\FullTextSearch\Model\Index;
36
use OCA\FullTextSearch\Service\ConfigService;
37
use OCA\FullTextSearch\Service\MiscService;
38
use OCP\DB\QueryBuilder\IQueryBuilder;
39
use OCP\IDBConnection;
40
use OCP\IL10N;
41
42
43
/**
44
 * Class CoreRequestBuilder
45
 *
46
 * @package OCA\FullTextSearch\Db
47
 */
48
class CoreRequestBuilder {
49
50
	const TABLE_INDEXES = 'fulltextsearch_indexes';
51
	const TABLE_TICKS = 'fulltextsearch_ticks';
52
53
	/** @var IDBConnection */
54
	protected $dbConnection;
55
56
	/** @var IL10N */
57
	protected $l10n;
58
59
	/** @var ConfigService */
60
	protected $configService;
61
62
	/** @var MiscService */
63
	protected $miscService;
64
65
	/** @var string */
66
	protected $defaultSelectAlias;
67
68
69
	/**
70
	 * CoreRequestBuilder constructor.
71
	 *
72
	 * @param IL10N $l10n
73
	 * @param IDBConnection $connection
74
	 * @param ConfigService $configService
75
	 * @param MiscService $miscService
76
	 */
77
	public function __construct(
78
		IL10N $l10n, IDBConnection $connection, ConfigService $configService,
79
		MiscService $miscService
80
	) {
81
		$this->l10n = $l10n;
82
		$this->dbConnection = $connection;
83
		$this->configService = $configService;
84
		$this->miscService = $miscService;
85
	}
86
87
88
	/**
89
	 * Limit the request to the Id
90
	 *
91
	 * @param IQueryBuilder $qb
92
	 * @param int $id
93
	 */
94
	protected function limitToId(IQueryBuilder &$qb, int $id) {
95
		$this->limitToDBFieldInt($qb, 'id', $id);
96
	}
97
98
99
	/**
100
	 * Limit the request to the OwnerId
101
	 *
102
	 * @param IQueryBuilder $qb
103
	 * @param string $userId
104
	 */
105
	protected function limitToOwnerId(IQueryBuilder &$qb, string $userId) {
106
		$this->limitToDBField($qb, 'owner_id', $userId);
107
	}
108
109
110
	/**
111
	 * Limit to the providerId
112
	 *
113
	 * @param IQueryBuilder $qb
114
	 * @param string $providerId
115
	 */
116
	protected function limitToProviderId(IQueryBuilder &$qb, string $providerId) {
117
		$this->limitToDBField($qb, 'provider_id', $providerId);
118
	}
119
120
121
	/**
122
	 * Limit to the documentId
123
	 *
124
	 * @param IQueryBuilder $qb
125
	 * @param string $documentId
126
	 */
127
	protected function limitToDocumentId(IQueryBuilder &$qb, string $documentId) {
128
		$this->limitToDBField($qb, 'document_id', $documentId);
129
	}
130
131
132
	/**
133
	 * Limit to the entry with at least one Error
134
	 *
135
	 * @param IQueryBuilder $qb
136
	 */
137
	protected function limitToErr(IQueryBuilder &$qb) {
138
		$expr = $qb->expr();
139
		$qb->andWhere($expr->gte('err', $qb->createNamedParameter(1)));
140
	}
141
142
143
	/**
144
	 * Limit to the entry with no error
145
	 *
146
	 * @param IQueryBuilder $qb
147
	 */
148
	protected function limitToNoErr(IQueryBuilder &$qb) {
149
		$expr = $qb->expr();
150
		$qb->andWhere($expr->eq('err', $qb->createNamedParameter(0)));
151
	}
152
153
154
	/**
155
	 * Limit to documentIds
156
	 *
157
	 * @param IQueryBuilder $qb
158
	 * @param array $documentIds
159
	 */
160
	protected function limitToDocumentIds(IQueryBuilder &$qb, array $documentIds) {
161
		$this->limitToDBFieldArray($qb, 'document_id', $documentIds);
162
	}
163
164
165
	/**
166
	 * Limit the request to source
167
	 *
168
	 * @param IQueryBuilder $qb
169
	 * @param string $source
170
	 */
171
	protected function limitToSource(IQueryBuilder &$qb, string $source) {
172
		$this->limitToDBField($qb, 'id', $source);
173
	}
174
175
176
	/**
177
	 * Limit the request to status
178
	 *
179
	 * @param IQueryBuilder $qb
180
	 * @param string $status
181
	 */
182
	protected function limitToStatus(IQueryBuilder &$qb, string $status) {
183
		$this->limitToDBField($qb, 'status', $status);
184
	}
185
186
187
	/**
188
	 * @param IQueryBuilder $qb
189
	 * @param string $field
190
	 * @param string $value
191
	 */
192 View Code Duplication
	private function limitToDBField(IQueryBuilder &$qb, string $field, string $value) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
		$expr = $qb->expr();
194
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : '';
195
		$field = $pf . $field;
196
197
		$qb->andWhere($expr->eq($field, $qb->createNamedParameter($value)));
198
	}
199
200
	/**
201
	 * @param IQueryBuilder $qb
202
	 * @param string $field
203
	 * @param int $value
204
	 */
205 View Code Duplication
	private function limitToDBFieldInt(IQueryBuilder &$qb, string $field, int $value) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
		$expr = $qb->expr();
207
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : '';
208
		$field = $pf . $field;
209
210
		$qb->andWhere($expr->eq($field, $qb->createNamedParameter($value)));
211
	}
212
213
214
	/**
215
	 * @param IQueryBuilder $qb
216
	 * @param string $field
217
	 * @param string|integer|array $values
218
	 */
219
	private function limitToDBFieldArray(IQueryBuilder &$qb, string $field, array $values) {
220
		$expr = $qb->expr();
221
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : '';
222
		$field = $pf . $field;
223
224
		if (!is_array($values)) {
225
			$values = [$values];
226
		}
227
228
		$orX = $expr->orX();
229
		foreach ($values as $value) {
230
			$orX->add($expr->eq($field, $qb->createNamedParameter($value)));
231
		}
232
233
		$qb->andWhere($orX);
234
	}
235
236
237
	/**
238
	 * @param IQueryBuilder $qb
239
	 */
240
	protected function limitToQueuedIndexes(IQueryBuilder &$qb) {
241
		$expr = $qb->expr();
242
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : '';
243
		$qb->andWhere($expr->neq($pf . 'status', $qb->createNamedParameter(Index::INDEX_OK)));
244
	}
245
246
}
247
248
249
250