Completed
Push — master ( 7a51c1...7a51c1 )
by Maxence
04:21 queued 02:15
created

CircleProviderRequestBuilder::getBaseUpdateSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/**
4
 * Circles - Bring cloud-users closer together.
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2017
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Circles\Db;
29
30
31
use Doctrine\DBAL\Query\QueryBuilder;
32
use OCA\Circles\Model\Member;
33
use OCP\DB\QueryBuilder\IQueryBuilder;
34
use OCP\IDBConnection;
35
use OCP\Share;
36
37
class CircleProviderRequestBuilder {
38
39
40
	/** @var IDBConnection */
41
	protected $dbConnection;
42
43
44
	/**
45
	 * returns the SQL request to get a specific share from the fileId and circleId
46
	 *
47
	 * @param int $fileId
48
	 * @param int $circleId
49
	 *
50
	 * @return \OCP\DB\QueryBuilder\IQueryBuilder
51
	 * @internal param $share
52
	 *
53
	 */
54
	protected function findShareParentSql($fileId, $circleId) {
55
56
		$qb = $this->getBaseSelectSql();
57
		$this->limitToShareParent($qb);
58
		$this->limitToCircle($qb, $circleId);
59
		$this->limitToFile($qb, $fileId);
60
61
		return $qb;
62
	}
63
64
65
	/**
66
	 * Limit the request to a Circle.
67
	 *
68
	 * @param $qb
69
	 * @param $circleId
70
	 */
71
	protected function limitToCircle(& $qb, $circleId) {
72
		$expr = $qb->expr();
73
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : '';
74
75
		$qb->andWhere($expr->eq($pf . 'share_with', $qb->createNamedParameter($circleId)));
76
	}
77
78
79
	/**
80
	 * Limit the request to the Share by its Id.
81
	 *
82
	 * @param $qb
83
	 * @param $shareId
84
	 */
85
	protected function limitToShare(& $qb, $shareId) {
86
		$expr = $qb->expr();
87
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : '';
88
89
		$qb->andWhere($expr->eq($pf . 'id', $qb->createNamedParameter($shareId)));
90
	}
91
92
93
	/**
94
	 * Limit the request to the top share (no children)
95
	 *
96
	 * @param $qb
97
	 */
98
	protected function limitToShareParent(& $qb) {
99
		$expr = $qb->expr();
100
101
		$qb->andWhere($expr->isNull('parent'));
102
	}
103
104
105
	/**
106
	 * limit the request to the children of a share
107
	 *
108
	 * @param $qb
109
	 * @param $userId
110
	 * @param int $parentId
111
	 */
112
	protected function limitToShareChildren(& $qb, $userId, $parentId = -1) {
113
		$expr = $qb->expr();
114
		$qb->andWhere($expr->eq('share_with', $qb->createNamedParameter($userId)));
115
116
		if ($parentId > -1) {
117
			$qb->andWhere($expr->eq('parent', $qb->createNamedParameter($parentId)));
118
		} else {
119
			$qb->andWhere($expr->isNotNull('parent'));
120
		}
121
	}
122
123
124
	/**
125
	 * limit the request to the share itself AND its children.
126
	 * perfect if you want to delete everything related to a share
127
	 *
128
	 * @param $qb
129
	 * @param $circleId
130
	 */
131
	protected function limitToShareAndChildren(& $qb, $circleId) {
132
		$expr = $qb->expr();
133
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : '';
134
135
		$qb->andWhere(
136
			$expr->orX(
137
				$expr->eq($pf . 'parent', $qb->createNamedParameter($circleId)),
138
				$expr->eq($pf . 'id', $qb->createNamedParameter($circleId))
139
			)
140
		);
141
	}
142
143
144
	/**
145
	 * limit the request to a fileId.
146
	 *
147
	 * @param IQueryBuilder $qb
148
	 * @param $fileId
149
	 */
150
	protected function limitToFile(& $qb, $fileId) {
151
		$expr = $qb->expr();
152
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : '';
153
154
		$qb->andWhere($expr->eq($pf . 'file_source', $qb->createNamedParameter($fileId)));
155
	}
156
157
158
	protected function limitToPage(& $qb, $limit = -1, $offset = 0) {
159
		if ($limit !== -1) {
160
			$qb->setMaxResults($limit);
161
		}
162
163
		$qb->setFirstResult($offset);
164
	}
165
166
	/**
167
	 * limit the request to a userId
168
	 *
169
	 * @param $qb
170
	 * @param $userId
171
	 * @param bool $reShares
172
	 */
173
	protected function limitToShareOwner(& $qb, $userId, $reShares = false) {
174
		$expr = $qb->expr();
175
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : '';
176
177
		if ($reShares === false) {
178
			$qb->andWhere($expr->eq($pf . 'uid_initiator', $qb->createNamedParameter($userId)));
179
		} else {
180
			$qb->andWhere(
181
				$expr->orX(
182
					$expr->eq($pf . 'uid_owner', $qb->createNamedParameter($userId)),
183
					$expr->eq($pf . 'uid_initiator', $qb->createNamedParameter($userId))
184
				)
185
			);
186
		}
187
	}
188
189
190
	/**
191
	 * link circle field
192
	 *
193
	 * @deprecated
194
	 *
195
	 * @param $qb
196
	 * @param $shareId
197
	 */
198
	protected function linkCircleField(& $qb, $shareId) {
199
		$expr = $qb->expr();
200
201
		$qb->from(CirclesMapper::TABLENAME, 'c')
202
		   ->andWhere(
203
			   $expr->orX(
204
				   $expr->eq('s.share_with', 'c.id'),
205
				   $expr->eq('s.parent', $qb->createNamedParameter($shareId))
206
			   )
207
		   );
208
		//->orderBy('c.circle_name');
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
209
	}
210
211
212
	/**
213
	 * @param $qb
214
	 */
215
	protected function linkToCircleOwner(& $qb) {
216
		$expr = $qb->expr();
217
218
		$qb->leftJoin(
219
			'c', 'circles_members', 'mo', $expr->andX(
220
			$expr->eq('c.id', 'mo.circle_id'),
221
			$expr->eq('mo.level', $qb->createNamedParameter(Member::LEVEL_OWNER))
222
		)
223
		);
224
	}
225
226
227
	/**
228
	 * Link to members (userId) of circle
229
	 *
230
	 * @param $qb
231
	 * @param $userId
232
	 */
233
	protected function linkToMember(& $qb, $userId) {
234
		$expr = $qb->expr();
235
236
		$qb->from(MembersMapper::TABLENAME, 'm')
237
		   ->andWhere($expr->eq('s.share_with', 'm.circle_id'))
238
		   ->andWhere($expr->eq('m.user_id', $qb->createNamedParameter($userId)))
239
		   ->andWhere($expr->gte('m.level', $qb->createNamedParameter(Member::LEVEL_MEMBER)));
240
	}
241
242
243
	/**
244
	 * Link to storage/filecache
245
	 *
246
	 * @param $qb
247
	 * @param $userId
248
	 */
249
	protected function linkToFileCache(& $qb, $userId) {
250
		$expr = $qb->expr();
251
252
		$qb->leftJoin('s', 'filecache', 'f', $expr->eq('s.file_source', 'f.fileid'))
253
		   ->leftJoin('f', 'storages', 'st', $expr->eq('f.storage', 'st.numeric_id'))
254
		   ->leftJoin(
255
			   's', 'share', 's2', $expr->andX(
256
			   $expr->eq('s.id', 's2.parent'),
257
			   $expr->eq('s2.share_with', $qb->createNamedParameter($userId))
258
		   )
259
		   );
260
	}
261
262
263
	/**
264
	 * add share to the database and return the ID
265
	 *
266
	 * @param $share
267
	 *
268
	 * @return IQueryBuilder
269
	 */
270
	protected function getBaseInsertSql($share) {
271
		$qb = $this->dbConnection->getQueryBuilder();
272
		$qb->insert('share')
273
		   ->setValue('share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE))
274
		   ->setValue('item_type', $qb->createNamedParameter($share->getNodeType()))
275
		   ->setValue('item_source', $qb->createNamedParameter($share->getNodeId()))
276
		   ->setValue('file_source', $qb->createNamedParameter($share->getNodeId()))
277
		   ->setValue('file_target', $qb->createNamedParameter($share->getTarget()))
278
		   ->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()))
279
		   ->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
280
		   ->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
281
		   ->setValue('permissions', $qb->createNamedParameter($share->getPermissions()))
282
		   ->setValue('token', $qb->createNamedParameter($share->getToken()))
283
		   ->setValue('stime', $qb->createNamedParameter(time()));
284
285
		return $qb;
286
	}
287
288
289
	/**
290
	 * generate and return a base sql request.
291
	 *
292
	 * @param int $shareId
293
	 *
294
	 * @return IQueryBuilder
295
	 */
296
	protected function getBaseSelectSql($shareId = -1) {
297
		$qb = $this->dbConnection->getQueryBuilder();
298
299
		/** @noinspection PhpMethodParametersCountMismatchInspection */
300
		$qb->select(
301
			's.id', 's.share_type', 's.share_with', 's.uid_owner', 's.uid_initiator',
302
			's.parent', 's.item_type', 's.item_source', 's.item_target', 's.file_source',
303
			's.file_target', 's.permissions', 's.stime', 's.accepted', 's.expiration',
304
			's.token', 's.mail_send', 'c.type AS circle_type', 'c.name AS circle_name',
305
			'mo.user_id AS circle_owner'
306
		);
307
		$this->linkToCircleOwner($qb);
308
		$this->linkToShare($qb);
309
310
		// TODO: Left-join circle and REMOVE this line
311
		$this->linkCircleField($qb, $shareId);
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Db\CirclePro...lder::linkCircleField() has been deprecated.

This method has been deprecated.

Loading history...
312
313
		return $qb;
314
	}
315
316
317
	protected function getCompleteSelectSql() {
318
		$qb = $this->dbConnection->getQueryBuilder();
319
320
		/** @noinspection PhpMethodParametersCountMismatchInspection */
321
		$qb->select(
322
			's.*', 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage',
323
			'f.path_hash', 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart',
324
			'f.size', 'f.mtime', 'f.storage_mtime', 'f.encrypted', 'f.unencrypted_size',
325
			'f.etag', 'f.checksum', 's2.id AS parent_id', 's2.file_target AS parent_target',
326
			's2.permissions AS parent_perms'
327
		)
328
		   ->selectAlias('st.id', 'storage_string_id');
329
330
		$this->linkToShare($qb);
331
332
		return $qb;
333
	}
334
335
336
	private function linkToShare(& $qb) {
337
		$expr = $qb->expr();
338
339
		$qb->from('share', 's')
340
		   ->where($expr->eq('s.share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE)))
341
		   ->andWhere(
342
			   $expr->orX(
343
				   $expr->eq('s.item_type', $qb->createNamedParameter('file')),
344
				   $expr->eq('s.item_type', $qb->createNamedParameter('folder'))
345
			   )
346
		   );
347
	}
348
349
350
	/**
351
	 * generate and return a base sql request.
352
	 *
353
	 * @return \OCP\DB\QueryBuilder\IQueryBuilder
354
	 */
355 View Code Duplication
	protected function getBaseDeleteSql() {
356
		$qb = $this->dbConnection->getQueryBuilder();
357
		$expr = $qb->expr();
358
359
		$qb->delete('share')
360
		   ->where($expr->eq('share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE)));
361
362
		return $qb;
363
	}
364
365
366
	/**
367
	 * generate and return a base sql request.
368
	 *
369
	 * @return \OCP\DB\QueryBuilder\IQueryBuilder
370
	 */
371 View Code Duplication
	protected function getBaseUpdateSql() {
372
		$qb = $this->dbConnection->getQueryBuilder();
373
		$expr = $qb->expr();
374
375
		$qb->update('share')
376
		   ->where($expr->eq('share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE)));
377
378
		return $qb;
379
	}
380
}
381