Completed
Pull Request — master (#362)
by Maxence
01:46
created

CoreRequestBuilder::limitToAddressBookId()   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 owner
160
	 *
161
	 * @param IQueryBuilder $qb
162
	 * @param $owner
163
	 */
164
	protected function limitToOwner(IQueryBuilder &$qb, $owner) {
165
		$this->limitToDBField($qb, 'owner', $owner);
166
	}
167
168
169
	/**
170
	 * Limit the request to the Type entry.
171
	 *
172
	 * @param IQueryBuilder $qb
173
	 * @param int $type
174
	 */
175
	protected function limitToUserType(IQueryBuilder &$qb, $type) {
176
		$this->limitToDBField($qb, 'user_type', $type);
177
	}
178
179
180
	/**
181
	 * Limit the request to the Instance.
182
	 *
183
	 * @param IQueryBuilder $qb
184
	 * @param string $instance
185
	 */
186
	protected function limitToInstance(IQueryBuilder &$qb, string $instance) {
187
		$this->limitToDBField($qb, 'instance', $instance);
188
	}
189
190
191
	/**
192
	 * Limit the request to the Circle by its Id.
193
	 *
194
	 * @param IQueryBuilder $qb
195
	 * @param string $circleUniqueId
196
	 */
197
	protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) {
198
		$this->limitToDBField($qb, 'circle_id', $circleUniqueId);
199
	}
200
201
202
	/**
203
	 * Limit the request to the Circle by its Id.
204
	 *
205
	 * @param IQueryBuilder $qb
206
	 * @param int $shareId
207
	 */
208
	protected function limitToShareId(IQueryBuilder &$qb, int $shareId) {
209
		$this->limitToDBField($qb, 'share_id', $shareId);
210
	}
211
212
213
	/**
214
	 * Limit the request to the Circle by its Id.
215
	 *
216
	 * @param IQueryBuilder $qb
217
	 * @param string $memberId
218
	 */
219
	protected function limitToMemberId(IQueryBuilder &$qb, string $memberId) {
220
		$this->limitToDBField($qb, 'member_id', $memberId);
221
	}
222
223
224
	/**
225
	 * Limit the request to the Circle by its Shorten Unique Id.
226
	 *
227
	 * @param IQueryBuilder $qb
228
	 * @param string $circleUniqueId
229
	 * @param $length
230
	 */
231
	protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) {
232
		$expr = $qb->expr();
233
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? '`' . $this->default_select_alias . '`.' : '';
234
235
		$qb->andWhere(
236
			$expr->eq(
237
				$qb->createNamedParameter($circleUniqueId),
238
				$qb->createFunction('SUBSTR(' . $pf . '`unique_id`' . ', 1, ' . $length . ')')
239
			)
240
		);
241
242
	}
243
244
245
	/**
246
	 * Limit the request to the Group by its Id.
247
	 *
248
	 * @param IQueryBuilder $qb
249
	 * @param int $groupId
250
	 */
251
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
252
		$this->limitToDBField($qb, 'group_id', $groupId);
253
	}
254
255
256
	/**
257
	 * Limit the search by its Name
258
	 *
259
	 * @param IQueryBuilder $qb
260
	 * @param string $name
261
	 */
262
	protected function limitToName(IQueryBuilder &$qb, $name) {
263
		$this->limitToDBField($qb, 'name', $name);
264
	}
265
266
267
	/**
268
	 * Limit the search by its Status (or greater)
269
	 *
270
	 * @param IQueryBuilder $qb
271
	 * @param string $name
272
	 */
273
	protected function limitToStatus(IQueryBuilder &$qb, $name) {
274
		$this->limitToDBFieldOrGreater($qb, 'status', $name);
275
	}
276
277
278
	/**
279
	 * Limit the request by its Id.
280
	 *
281
	 * @param IQueryBuilder $qb
282
	 * @param string $type
283
	 */
284
	protected function limitToShareType(IQueryBuilder &$qb, string $type) {
285
		$this->limitToDBField($qb, 'share_type', $type);
286
	}
287
288
289
	/**
290
	 * Limit the request by its Id.
291
	 *
292
	 * @param IQueryBuilder $qb
293
	 * @param string $with
294
	 */
295
	protected function limitToShareWith(IQueryBuilder &$qb, string $with) {
296
		$this->limitToDBField($qb, 'share_with', $with);
297
	}
298
299
300
	/**
301
	 * Limit the request to a minimum member level.
302
	 *
303
	 * if $pf is an array, will generate an SQL OR request to limit level in multiple tables
304
	 *
305
	 * @param IQueryBuilder $qb
306
	 * @param int $level
307
	 * @param string|array $pf
308
	 */
309
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
310
		$expr = $qb->expr();
311
312
		if ($pf === '') {
313
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
314
			$qb->andWhere($expr->gte($p . 'level', $qb->createNamedParameter($level)));
315
316
			return;
317
		}
318
319
		if (!is_array($pf)) {
320
			$pf = [$pf];
321
		}
322
323
		$orX = $this->generateLimitToLevelMultipleTableRequest($qb, $level, $pf);
324
		$qb->andWhere($orX);
325
	}
326
327
328
	/**
329
	 * @param IQueryBuilder $qb
330
	 * @param array $pf
331
	 *
332
	 * @return mixed
333
	 */
334
	private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) {
335
		$expr = $qb->expr();
336
		$orX = $expr->orX();
337
338
		foreach ($pf as $p) {
339
			if ($p === 'g' && !$this->leftJoinedNCGroupAndUser) {
340
				continue;
341
			}
342
			$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
343
		}
344
345
		return $orX;
346
	}
347
348
349
	/**
350
	 * Limit the search to Members and Almost members
351
	 *
352
	 * @param IQueryBuilder $qb
353
	 */
354
	protected function limitToMembersAndAlmost(IQueryBuilder &$qb) {
355
		$expr = $qb->expr();
356
357
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
358
359
		$orX = $expr->orX();
360
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_MEMBER)));
361
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_INVITED)));
362
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_REQUEST)));
363
364
		$qb->andWhere($orX);
365
	}
366
367
368
	/**
369
	 * @param IQueryBuilder $qb
370
	 * @param string $field
371
	 * @param string|integer $value
372
	 */
373
	private function limitToDBField(IQueryBuilder &$qb, $field, $value) {
374
		$expr = $qb->expr();
375
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
376
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
377
	}
378
379
380
	/**
381
	 * @param IQueryBuilder $qb
382
	 * @param string $field
383
	 * @param string|integer $value
384
	 */
385
	private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) {
386
		$expr = $qb->expr();
387
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
388
		$qb->andWhere($expr->gte($pf . $field, $qb->createNamedParameter($value)));
389
	}
390
391
392
	/**
393
	 * link to the groupId/UserId of the NC DB.
394
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
395
	 * alias
396
	 *
397
	 * @param IQueryBuilder $qb
398
	 * @param string $userId
399
	 */
400
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
401
		$expr = $qb->expr();
402
403
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
404
405
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
406
		if ($userId !== '') {
407
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
408
		} else {
409
			$qb->selectAlias('ncgu.uid', 'user_id');
410
		}
411
412
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
413
		$qb->andWhere($and);
414
	}
415
416
417
	/**
418
	 * Right Join the Circles table
419
	 *
420
	 * @param IQueryBuilder $qb
421
	 *
422
	 * @deprecated not used (14/07/17)
423
	 */
424
	protected function rightJoinCircles(IQueryBuilder &$qb) {
425
		$expr = $qb->expr();
426
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
427
428
		$qb->from(self::TABLE_CIRCLES, 'c')
429
		   ->andWhere(
430
			   $expr->eq(
431
				   $pf . 'circle_id',
432
				   $qb->createFunction(
433
					   'SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
434
				   )
435
			   )
436
		   );
437
	}
438
439
440
	/**
441
	 * Left Join circle table to get more information about the circle.
442
	 *
443
	 * @param IQueryBuilder $qb
444
	 */
445
	protected function leftJoinCircle(IQueryBuilder &$qb) {
446
		if ($qb->getType() !== QueryBuilder::SELECT) {
447
			return;
448
		}
449
450
		$expr = $qb->expr();
451
		$pf = $this->default_select_alias . '.';
452
453
		/** @noinspection PhpMethodParametersCountMismatchInspection */
454
		$qb->selectAlias('lc.type', 'circle_type')
455
		   ->selectAlias('lc.name', 'circle_name')
456
		   ->leftJoin(
457
			   $this->default_select_alias, CoreRequestBuilder::TABLE_CIRCLES, 'lc',
458
			   $expr->eq(
459
				   $pf . 'circle_id',
460
				   $qb->createFunction(
461
					   'SUBSTR(`lc`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
462
				   )
463
			   )
464
		   );
465
	}
466
467
468
	/**
469
	 * link to the groupId/UserId of the NC DB.
470
	 *
471
	 * @param IQueryBuilder $qb
472
	 * @param string $userId
473
	 * @param string $field
474
	 */
475
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) {
476
		if (!$this->configService->isLinkedGroupsAllowed()) {
477
			return;
478
		}
479
480
		$expr = $qb->expr();
481
		$qb->leftJoin(
482
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
483
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
484
		);
485
486
		/** @noinspection PhpMethodParametersCountMismatchInspection */
487
		$qb->leftJoin(
488
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
489
			$expr->andX(
490
				$expr->eq('ncgu.gid', 'g.group_id'),
491
				$expr->eq(
492
					'g.circle_id', $qb->createFunction(
493
					'SUBSTR(' . $field . ', 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
494
				)
495
				)
496
			)
497
		);
498
499
		$this->leftJoinedNCGroupAndUser = true;
500
	}
501
}
502
503
504
505