Completed
Pull Request — master (#362)
by Maxence
02:42
created

CoreRequestBuilder::limitToContactId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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