Completed
Push — master ( 4f3689...17b456 )
by Maxence
02:12 queued 11s
created

CoreRequestBuilder::rightJoinCircles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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