Completed
Push — master ( 5a6198...a64e14 )
by Maxence
17s queued 10s
created

CoreRequestBuilder::limitToId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: maxence
5
 * Date: 7/4/17
6
 * Time: 5:01 PM
7
 */
8
9
namespace OCA\Circles\Db;
10
11
12
use Doctrine\DBAL\Query\QueryBuilder;
13
use OCA\Circles\Model\Circle;
14
use OCA\Circles\Model\Member;
15
use OCA\Circles\Service\ConfigService;
16
use OCA\Circles\Service\MiscService;
17
use OCA\Circles\Service\TimezoneService;
18
use OCP\DB\QueryBuilder\IQueryBuilder;
19
use OCP\IDBConnection;
20
use OCP\IL10N;
21
22
class CoreRequestBuilder {
23
24
	const TABLE_FILE_SHARES = 'share';
25
	const SHARE_TYPE = 7;
26
27
	const TABLE_CIRCLES = 'circles_circles';
28
	const TABLE_MEMBERS = 'circles_members';
29
	const TABLE_GROUPS = 'circles_groups';
30
	const TABLE_SHARES = 'circles_shares';
31
	const TABLE_LINKS = 'circles_links';
32
	const TABLE_TOKENS = 'circles_tokens';
33
34
	const NC_TABLE_GROUP_USER = 'group_user';
35
36
	/** @var IDBConnection */
37
	protected $dbConnection;
38
39
	/** @var IL10N */
40
	protected $l10n;
41
42
	/** @var ConfigService */
43
	protected $configService;
44
45
	/** @var TimezoneService */
46
	protected $timezoneService;
47
48
	/** @var MiscService */
49
	protected $miscService;
50
51
	/** @var string */
52
	protected $default_select_alias;
53
54
	/** @var bool */
55
	protected $leftJoinedNCGroupAndUser = false;
56
57
	/**
58
	 * RequestBuilder constructor.
59
	 *
60
	 * @param IL10N $l10n
61
	 * @param IDBConnection $connection
62
	 * @param ConfigService $configService
63
	 * @param TimezoneService $timezoneService
64
	 * @param MiscService $miscService
65
	 */
66
	public function __construct(
67
		IL10N $l10n, IDBConnection $connection, ConfigService $configService,
68
		TimezoneService $timezoneService, MiscService $miscService
69
	) {
70
		$this->l10n = $l10n;
71
		$this->dbConnection = $connection;
72
		$this->configService = $configService;
73
		$this->timezoneService = $timezoneService;
74
		$this->miscService = $miscService;
75
	}
76
77
78
	/**
79
	 * Limit the request by its 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 by its UniqueId.
91
	 *
92
	 * @param IQueryBuilder $qb
93
	 * @param int $uniqueId
94
	 */
95
	protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) {
96
		$this->limitToDBField($qb, 'unique_id', $uniqueId);
97
	}
98
99
100
	/**
101
	 * Limit the request by its Token.
102
	 *
103
	 * @param IQueryBuilder $qb
104
	 * @param string $token
105
	 */
106
	protected function limitToToken(IQueryBuilder &$qb, $token) {
107
		$this->limitToDBField($qb, 'token', $token);
108
	}
109
110
111
	/**
112
	 * Limit the request to the User by its Id.
113
	 *
114
	 * @param IQueryBuilder $qb
115
	 * @param $userId
116
	 */
117
	protected function limitToUserId(IQueryBuilder &$qb, $userId) {
118
		$this->limitToDBField($qb, 'user_id', $userId);
119
	}
120
121
122
	/**
123
	 * Limit the request to the Type entry.
124
	 *
125
	 * @param IQueryBuilder $qb
126
	 * @param int $type
127
	 */
128
	protected function limitToUserType(IQueryBuilder &$qb, $type) {
129
		$this->limitToDBField($qb, 'user_type', $type);
130
	}
131
132
133
	/**
134
	 * Limit the request to the Circle by its Id.
135
	 *
136
	 * @param IQueryBuilder $qb
137
	 * @param string $circleUniqueId
138
	 */
139
	protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) {
140
		$this->limitToDBField($qb, 'circle_id', $circleUniqueId);
141
	}
142
143
144
	/**
145
	 * Limit the request to the Circle by its Id.
146
	 *
147
	 * @param IQueryBuilder $qb
148
	 * @param int $shareId
149
	 */
150
	protected function limitToShareId(IQueryBuilder &$qb, int $shareId) {
151
		$this->limitToDBField($qb, 'share_id', $shareId);
152
	}
153
154
155
	/**
156
	 * Limit the request to the Circle by its Shorten Unique Id.
157
	 *
158
	 * @param IQueryBuilder $qb
159
	 * @param string $circleUniqueId
160
	 * @param $length
161
	 */
162
	protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) {
163
		$expr = $qb->expr();
164
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? '`' . $this->default_select_alias . '`.' : '';
165
166
		$qb->andWhere(
167
			$expr->eq(
168
				$qb->createNamedParameter($circleUniqueId),
169
				$qb->createFunction('SUBSTR(' . $pf . '`unique_id`' . ', 1, ' . $length . ')')
170
			)
171
		);
172
173
	}
174
175
176
	/**
177
	 * Limit the request to the Group by its Id.
178
	 *
179
	 * @param IQueryBuilder $qb
180
	 * @param int $groupId
181
	 */
182
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
183
		$this->limitToDBField($qb, 'group_id', $groupId);
184
	}
185
186
187
	/**
188
	 * Limit the search by its Name
189
	 *
190
	 * @param IQueryBuilder $qb
191
	 * @param string $name
192
	 */
193
	protected function limitToName(IQueryBuilder &$qb, $name) {
194
		$this->limitToDBField($qb, 'name', $name);
195
	}
196
197
198
	/**
199
	 * Limit the search by its Status (or greater)
200
	 *
201
	 * @param IQueryBuilder $qb
202
	 * @param string $name
203
	 */
204
	protected function limitToStatus(IQueryBuilder &$qb, $name) {
205
		$this->limitToDBFieldOrGreater($qb, 'status', $name);
206
	}
207
208
209
	/**
210
	 * Limit the request by its Id.
211
	 *
212
	 * @param IQueryBuilder $qb
213
	 * @param string $type
214
	 */
215
	protected function limitToShareType(IQueryBuilder &$qb, string $type) {
216
		$this->limitToDBField($qb, 'share_type', $type);
217
	}
218
219
220
	/**
221
	 * Limit the request by its Id.
222
	 *
223
	 * @param IQueryBuilder $qb
224
	 * @param string $with
225
	 */
226
	protected function limitToShareWith(IQueryBuilder &$qb, string $with) {
227
		$this->limitToDBField($qb, 'share_with', $with);
228
	}
229
230
231
	/**
232
	 * Limit the request to a minimum member level.
233
	 *
234
	 * if $pf is an array, will generate an SQL OR request to limit level in multiple tables
235
	 *
236
	 * @param IQueryBuilder $qb
237
	 * @param int $level
238
	 * @param string|array $pf
239
	 */
240
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
241
		$expr = $qb->expr();
242
243
		if ($pf === '') {
244
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
245
			$qb->andWhere($expr->gte($p . 'level', $qb->createNamedParameter($level)));
246
247
			return;
248
		}
249
250
		if (!is_array($pf)) {
251
			$pf = [$pf];
252
		}
253
254
		$orX = $this->generateLimitToLevelMultipleTableRequest($qb, $level, $pf);
255
		$qb->andWhere($orX);
256
	}
257
258
259
	/**
260
	 * @param IQueryBuilder $qb
261
	 * @param array $pf
262
	 *
263
	 * @return mixed
264
	 */
265
	private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) {
266
		$expr = $qb->expr();
267
		$orX = $expr->orX();
268
269
		foreach ($pf as $p) {
270
			if ($p === 'g' && !$this->leftJoinedNCGroupAndUser) {
271
				continue;
272
			}
273
			$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
274
		}
275
276
		return $orX;
277
	}
278
279
280
	/**
281
	 * Limit the search to Members and Almost members
282
	 *
283
	 * @param IQueryBuilder $qb
284
	 */
285
	protected function limitToMembersAndAlmost(IQueryBuilder &$qb) {
286
		$expr = $qb->expr();
287
288
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
289
290
		$orX = $expr->orX();
291
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_MEMBER)));
292
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_INVITED)));
293
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_REQUEST)));
294
295
		$qb->andWhere($orX);
296
	}
297
298
299
	/**
300
	 * @param IQueryBuilder $qb
301
	 * @param string $field
302
	 * @param string|integer $value
303
	 */
304
	private function limitToDBField(IQueryBuilder &$qb, $field, $value) {
305
		$expr = $qb->expr();
306
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
307
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
308
	}
309
310
311
	/**
312
	 * @param IQueryBuilder $qb
313
	 * @param string $field
314
	 * @param string|integer $value
315
	 */
316
	private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) {
317
		$expr = $qb->expr();
318
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
319
		$qb->andWhere($expr->gte($pf . $field, $qb->createNamedParameter($value)));
320
	}
321
322
323
	/**
324
	 * link to the groupId/UserId of the NC DB.
325
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
326
	 * alias
327
	 *
328
	 * @param IQueryBuilder $qb
329
	 * @param string $userId
330
	 */
331
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
332
		$expr = $qb->expr();
333
334
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
335
336
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
337
		if ($userId !== '') {
338
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
339
		} else {
340
			$qb->selectAlias('ncgu.uid', 'user_id');
341
		}
342
343
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
344
		$qb->andWhere($and);
345
	}
346
347
348
	/**
349
	 * Right Join the Circles table
350
	 *
351
	 * @param IQueryBuilder $qb
352
	 *
353
	 * @deprecated not used (14/07/17)
354
	 */
355
	protected function rightJoinCircles(IQueryBuilder &$qb) {
356
		$expr = $qb->expr();
357
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
358
359
		$qb->from(self::TABLE_CIRCLES, 'c')
360
		   ->andWhere(
361
			   $expr->eq(
362
				   $pf . 'circle_id',
363
				   $qb->createFunction(
364
					   'SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
365
				   )
366
			   )
367
		   );
368
	}
369
370
371
	/**
372
	 * Left Join circle table to get more information about the circle.
373
	 *
374
	 * @param IQueryBuilder $qb
375
	 */
376
	protected function leftJoinCircle(IQueryBuilder &$qb) {
377
378
		if ($qb->getType() !== QueryBuilder::SELECT) {
379
			return;
380
		}
381
382
		$expr = $qb->expr();
383
		$pf = $this->default_select_alias . '.';
384
385
		/** @noinspection PhpMethodParametersCountMismatchInspection */
386
		$qb->selectAlias('lc.type', 'circle_type')
387
		   ->selectAlias('lc.name', 'circle_name')
388
		   ->leftJoin(
389
			   $this->default_select_alias, CoreRequestBuilder::TABLE_CIRCLES, 'lc',
390
			   $expr->eq(
391
				   $pf . 'circle_id',
392
				   $qb->createFunction(
393
					   'SUBSTR(`lc`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
394
				   )
395
			   )
396
		   );
397
	}
398
399
400
	/**
401
	 * link to the groupId/UserId of the NC DB.
402
	 *
403
	 * @param IQueryBuilder $qb
404
	 * @param string $userId
405
	 * @param string $field
406
	 */
407
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) {
408
		if (!$this->configService->isLinkedGroupsAllowed()) {
409
			return;
410
		}
411
412
		$expr = $qb->expr();
413
		$qb->leftJoin(
414
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
415
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
416
		);
417
418
		/** @noinspection PhpMethodParametersCountMismatchInspection */
419
		$qb->leftJoin(
420
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
421
			$expr->andX(
422
				$expr->eq('ncgu.gid', 'g.group_id'),
423
				$expr->eq(
424
					'g.circle_id', $qb->createFunction(
425
					'SUBSTR(' . $field . ', 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
426
				)
427
				)
428
			)
429
		);
430
431
		$this->leftJoinedNCGroupAndUser = true;
432
	}
433
}
434
435
436
437