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 OC\L10N\L10N; |
33
|
|
|
use OCA\Circles\Model\Circle; |
34
|
|
|
use OCA\Circles\Model\Member; |
35
|
|
|
use OCA\Circles\Model\SharingFrame; |
36
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
37
|
|
|
use OCP\IDBConnection; |
38
|
|
|
|
39
|
|
|
class CirclesRequestBuilder { |
|
|
|
|
40
|
|
|
|
41
|
|
|
const TABLE_CIRCLES = 'circles_circles'; |
42
|
|
|
const TABLE_MEMBERS = 'circles_members'; |
43
|
|
|
|
44
|
|
|
/** @var IDBConnection */ |
45
|
|
|
protected $dbConnection; |
46
|
|
|
|
47
|
|
|
/** @var L10N */ |
48
|
|
|
protected $l10n; |
49
|
|
|
|
50
|
|
|
private $default_select_alias; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Join the Circles table |
55
|
|
|
* |
56
|
|
|
* @param IQueryBuilder $qb |
57
|
|
|
*/ |
58
|
|
|
protected function joinCircles(& $qb, $field) { |
59
|
|
|
$expr = $qb->expr(); |
60
|
|
|
|
61
|
|
|
$qb->from(self::TABLE_CIRCLES, 'c') |
62
|
|
|
->andWhere($expr->eq('c.id', $field)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Limit the request to the Share by its Id. |
68
|
|
|
* |
69
|
|
|
* @param IQueryBuilder $qb |
70
|
|
|
* @param int $circleId |
71
|
|
|
*/ |
72
|
|
|
protected function limitToCircle(IQueryBuilder &$qb, $circleId) { |
73
|
|
|
$expr = $qb->expr(); |
74
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; |
75
|
|
|
|
76
|
|
|
$qb->andWhere($expr->eq($pf . 'circle_id', $qb->createNamedParameter($circleId))); |
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
|
|
|
$expr = $qb->expr(); |
88
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; |
89
|
|
|
|
90
|
|
|
$qb->andWhere($expr->eq($pf . 'id', $qb->createNamedParameter($id))); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Limit the request by its UniqueId. |
96
|
|
|
* |
97
|
|
|
* @param IQueryBuilder $qb |
98
|
|
|
* @param int $uniqueId |
99
|
|
|
*/ |
100
|
|
|
protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) { |
101
|
|
|
$expr = $qb->expr(); |
102
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; |
103
|
|
|
|
104
|
|
|
$qb->andWhere($expr->eq($pf . 'unique_id', $qb->createNamedParameter($uniqueId))); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Limit the request to a minimum member level. |
110
|
|
|
* |
111
|
|
|
* @param IQueryBuilder $qb |
112
|
|
|
* @param $level |
113
|
|
|
*/ |
114
|
|
|
protected function limitToMemberLevel(IQueryBuilder &$qb, $level) { |
115
|
|
|
$qb->where( |
116
|
|
|
$qb->expr() |
117
|
|
|
->gte('m.level', $qb->createNamedParameter($level)) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* add a request to the members list, using the current user ID. |
124
|
|
|
* will returns level and stuff. |
125
|
|
|
* |
126
|
|
|
* @param IQueryBuilder $qb |
127
|
|
|
* @param string $userId |
128
|
|
|
*/ |
129
|
|
|
protected function leftJoinUserIdAsMember(IQueryBuilder &$qb, $userId) { |
130
|
|
|
|
131
|
|
|
if ($qb->getType() !== QueryBuilder::SELECT) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$expr = $qb->expr(); |
136
|
|
|
$pf = $this->default_select_alias . '.'; |
137
|
|
|
|
138
|
|
|
$qb->selectAlias('u.level', 'user_level'); |
139
|
|
|
$qb->leftJoin( |
140
|
|
|
$this->default_select_alias, MembersMapper::TABLENAME, 'u', |
141
|
|
|
$expr->andX( |
142
|
|
|
$expr->eq($pf . 'id', 'u.circle_id'), |
143
|
|
|
$expr->eq('u.user_id', $qb->createNamedParameter($userId)) |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param IQueryBuilder $qb |
150
|
|
|
* |
151
|
|
|
* @deprecated |
152
|
|
|
* never used in fact. |
153
|
|
|
*/ |
154
|
|
|
protected function leftJoinOwner(IQueryBuilder &$qb) { |
155
|
|
|
|
156
|
|
|
if ($qb->getType() !== QueryBuilder::SELECT) { |
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$expr = $qb->expr(); |
161
|
|
|
$pf = $this->default_select_alias . '.'; |
162
|
|
|
|
163
|
|
|
$qb->leftJoin( |
164
|
|
|
$this->default_select_alias, MembersMapper::TABLENAME, 'o', |
165
|
|
|
$expr->andX( |
166
|
|
|
$expr->eq($pf . 'id', 'o.circle_id'), |
167
|
|
|
$expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Base of the Sql Select request for Shares |
175
|
|
|
* |
176
|
|
|
* @return IQueryBuilder |
177
|
|
|
*/ |
178
|
|
|
protected function getSharesSelectSql() { |
179
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
180
|
|
|
|
181
|
|
|
$qb->select( |
182
|
|
|
'circle_id', 'source', 'type', 'author', 'sharer', 'payload', 'creation', 'header', |
183
|
|
|
'unique_id' |
184
|
|
|
) |
185
|
|
|
->from('circles_shares', 's'); |
186
|
|
|
|
187
|
|
|
$this->default_select_alias = 's'; |
188
|
|
|
|
189
|
|
|
return $qb; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Base of the Sql Insert request for Shares |
194
|
|
|
* |
195
|
|
|
* @return IQueryBuilder |
196
|
|
|
*/ |
197
|
|
|
protected function getSharesInsertSql() { |
198
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
199
|
|
|
$qb->insert('circles_shares') |
200
|
|
|
->setValue('creation', $qb->createFunction('NOW()')); |
201
|
|
|
|
202
|
|
|
return $qb; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return IQueryBuilder |
208
|
|
|
*/ |
209
|
|
|
protected function getMembersSelectSql() { |
210
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
211
|
|
|
|
212
|
|
|
$qb->select('user_id', 'circle_id', 'level', 'status', 'joined') |
213
|
|
|
->from('circles_members', 'm'); |
214
|
|
|
|
215
|
|
|
$this->default_select_alias = 'm'; |
216
|
|
|
|
217
|
|
|
return $qb; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return IQueryBuilder |
223
|
|
|
*/ |
224
|
|
|
protected function getCirclesSelectSql() { |
225
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
226
|
|
|
|
227
|
|
|
$qb->select('c.id', 'c.unique_id', 'c.name', 'c.description', 'c.type', 'c.creation') |
228
|
|
|
->from('circles_circles', 'c'); |
229
|
|
|
$this->default_select_alias = 'c'; |
230
|
|
|
|
231
|
|
|
return $qb; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param array $data |
236
|
|
|
* |
237
|
|
|
* @return Member |
238
|
|
|
*/ |
239
|
|
|
protected function parseMembersSelectSql(array $data) { |
240
|
|
|
$member = new Member($this->l10n); |
241
|
|
|
$member->setUserId($data['user_id']); |
242
|
|
|
$member->setCircleId($data['circle_id']); |
243
|
|
|
$member->setLevel($data['level']); |
244
|
|
|
$member->setStatus($data['status']); |
245
|
|
|
$member->setJoined($data['joined']); |
246
|
|
|
|
247
|
|
|
return $member; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param array $data |
253
|
|
|
* |
254
|
|
|
* @return Circle |
|
|
|
|
255
|
|
|
*/ |
256
|
|
|
protected function parseCirclesSelectSql(array $data) { |
257
|
|
|
if ($data === null) { |
258
|
|
|
return null; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$circle = new Circle($this->l10n); |
262
|
|
|
$circle->setId($data['id']); |
263
|
|
|
$circle->setUniqueId($data['unique_id']); |
264
|
|
|
$circle->setName($data['name']); |
265
|
|
|
$circle->setDescription($data['description']); |
266
|
|
|
$circle->setType($data['type']); |
267
|
|
|
$circle->setCreation($data['creation']); |
268
|
|
|
|
269
|
|
|
if (key_exists('user_level', $data)) { |
270
|
|
|
$user = new Member($this->l10n); |
271
|
|
|
$user->setLevel($data['user_level']); |
272
|
|
|
$circle->setUser($user); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $circle; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param array $data |
281
|
|
|
* |
282
|
|
|
* @return null|SharingFrame |
283
|
|
|
*/ |
284
|
|
|
protected function parseSharesSelectSql(array $data) { |
285
|
|
|
if ($data === null) { |
286
|
|
|
return null; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$frame = new SharingFrame($data['source'], $data['type']); |
290
|
|
|
$frame->setCircleId($data['circle_id']); |
291
|
|
|
$frame->setAuthor($data['author']); |
292
|
|
|
$frame->setSharer($data['sharer']); |
293
|
|
|
$frame->setPayload($data['payload']); |
294
|
|
|
$frame->setCreation($data['creation']); |
295
|
|
|
$frame->setHeaders($data['headers']); |
296
|
|
|
$frame->setUniqueId($data['unique_id']); |
297
|
|
|
|
298
|
|
|
return $frame; |
299
|
|
|
} |
300
|
|
|
} |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.