1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Bernhard Posselt <[email protected]> |
6
|
|
|
* @author Christoph Wurst <[email protected]> |
7
|
|
|
* @author Joas Schilling <[email protected]> |
8
|
|
|
* @author Lukas Reschke <[email protected]> |
9
|
|
|
* @author Morris Jobke <[email protected]> |
10
|
|
|
* @author Roeland Jago Douma <[email protected]> |
11
|
|
|
* @author Thomas Müller <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @license AGPL-3.0 |
14
|
|
|
* |
15
|
|
|
* This code is free software: you can redistribute it and/or modify |
16
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
17
|
|
|
* as published by the Free Software Foundation. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU Affero General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
namespace OCA\Music\Db; |
29
|
|
|
|
30
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
31
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException; |
32
|
|
|
use OCP\IDBConnection; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Simple parent class for inheriting your data access layer from. This class |
36
|
|
|
* may be subject to change in the future |
37
|
|
|
* @since 7.0.0 |
38
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
39
|
|
|
*/ |
40
|
|
|
abstract class OldNextcloudMapper { |
41
|
|
|
protected $tableName; |
42
|
|
|
protected $entityClass; |
43
|
|
|
protected $db; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param IDBConnection $db Instance of the Db abstraction layer |
47
|
|
|
* @param string $tableName the name of the table. set this to allow entity |
48
|
|
|
* @param string $entityClass the name of the entity that the sql should be |
49
|
|
|
* mapped to queries without using sql |
50
|
|
|
* @since 7.0.0 |
51
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
52
|
|
|
*/ |
53
|
|
|
public function __construct(IDBConnection $db, $tableName, $entityClass = null) { |
54
|
|
|
$this->db = $db; |
55
|
|
|
$this->tableName = '*PREFIX*' . $tableName; |
56
|
|
|
|
57
|
|
|
// if not given set the entity name to the class without the mapper part |
58
|
|
|
// cache it here for later use since reflection is slow |
59
|
|
|
if ($entityClass === null) { |
60
|
|
|
$this->entityClass = str_replace('Mapper', '', get_class($this)); |
61
|
|
|
} else { |
62
|
|
|
$this->entityClass = $entityClass; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string the table name |
69
|
|
|
* @since 7.0.0 |
70
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
71
|
|
|
*/ |
72
|
|
|
public function getTableName() { |
73
|
|
|
return $this->tableName; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Deletes an entity from the table |
79
|
|
|
* @param Entity $entity the entity that should be deleted |
80
|
|
|
* @return Entity the deleted entity |
81
|
|
|
* @since 7.0.0 - return value added in 8.1.0 |
82
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
83
|
|
|
*/ |
84
|
|
|
public function delete(Entity $entity) { |
85
|
|
|
$sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
86
|
|
|
$stmt = $this->execute($sql, [$entity->getId()]); |
|
|
|
|
87
|
|
|
$stmt->closeCursor(); |
88
|
|
|
return $entity; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Creates a new entry in the db from an entity |
94
|
|
|
* @param Entity $entity the entity that should be created |
95
|
|
|
* @return Entity the saved entity with the set id |
96
|
|
|
* @since 7.0.0 |
97
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
98
|
|
|
*/ |
99
|
|
|
public function insert(Entity $entity) { |
100
|
|
|
// get updated fields to save, fields have to be set using a setter to |
101
|
|
|
// be saved |
102
|
|
|
$properties = $entity->getUpdatedFields(); |
103
|
|
|
$values = ''; |
104
|
|
|
$columns = ''; |
105
|
|
|
$params = []; |
106
|
|
|
|
107
|
|
|
// build the fields |
108
|
|
|
$i = 0; |
109
|
|
|
foreach ($properties as $property => $updated) { |
110
|
|
|
$column = $entity->propertyToColumn($property); |
111
|
|
|
$getter = 'get' . ucfirst($property); |
112
|
|
|
|
113
|
|
|
$columns .= '`' . $column . '`'; |
114
|
|
|
$values .= '?'; |
115
|
|
|
|
116
|
|
|
// only append colon if there are more entries |
117
|
|
|
if ($i < count($properties) - 1) { |
118
|
|
|
$columns .= ','; |
119
|
|
|
$values .= ','; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$params[] = $entity->$getter(); |
123
|
|
|
$i++; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$sql = 'INSERT INTO `' . $this->tableName . '`(' . |
127
|
|
|
$columns . ') VALUES(' . $values . ')'; |
128
|
|
|
|
129
|
|
|
$stmt = $this->execute($sql, $params); |
|
|
|
|
130
|
|
|
|
131
|
|
|
$entity->setId((int) $this->db->lastInsertId($this->tableName)); |
132
|
|
|
|
133
|
|
|
$stmt->closeCursor(); |
134
|
|
|
|
135
|
|
|
return $entity; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Updates an entry in the db from an entity |
142
|
|
|
* @throws \InvalidArgumentException if entity has no id |
143
|
|
|
* @param Entity $entity the entity that should be created |
144
|
|
|
* @return Entity the saved entity with the set id |
145
|
|
|
* @since 7.0.0 - return value was added in 8.0.0 |
146
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
147
|
|
|
*/ |
148
|
|
|
public function update(Entity $entity) { |
149
|
|
|
// if entity wasn't changed it makes no sense to run a db query |
150
|
|
|
$properties = $entity->getUpdatedFields(); |
151
|
|
|
if (count($properties) === 0) { |
152
|
|
|
return $entity; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// entity needs an id |
156
|
|
|
$id = $entity->getId(); |
157
|
|
|
if ($id === null) { |
|
|
|
|
158
|
|
|
throw new \InvalidArgumentException( |
159
|
|
|
'Entity which should be updated has no id'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// get updated fields to save, fields have to be set using a setter to |
163
|
|
|
// be saved |
164
|
|
|
// do not update the id field |
165
|
|
|
unset($properties['id']); |
166
|
|
|
|
167
|
|
|
$columns = ''; |
168
|
|
|
$params = []; |
169
|
|
|
|
170
|
|
|
// build the fields |
171
|
|
|
$i = 0; |
172
|
|
|
foreach ($properties as $property => $updated) { |
173
|
|
|
$column = $entity->propertyToColumn($property); |
174
|
|
|
$getter = 'get' . ucfirst($property); |
175
|
|
|
|
176
|
|
|
$columns .= '`' . $column . '` = ?'; |
177
|
|
|
|
178
|
|
|
// only append colon if there are more entries |
179
|
|
|
if ($i < count($properties) - 1) { |
180
|
|
|
$columns .= ','; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$params[] = $entity->$getter(); |
184
|
|
|
$i++; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$sql = 'UPDATE `' . $this->tableName . '` SET ' . |
188
|
|
|
$columns . ' WHERE `id` = ?'; |
189
|
|
|
$params[] = $id; |
190
|
|
|
|
191
|
|
|
$stmt = $this->execute($sql, $params); |
|
|
|
|
192
|
|
|
$stmt->closeCursor(); |
193
|
|
|
|
194
|
|
|
return $entity; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Checks if an array is associative |
199
|
|
|
* @param array $array |
200
|
|
|
* @return bool true if associative |
201
|
|
|
* @since 8.1.0 |
202
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
203
|
|
|
*/ |
204
|
|
|
private function isAssocArray(array $array) { |
205
|
|
|
return array_values($array) !== $array; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the correct PDO constant based on the value type |
210
|
|
|
* @param $value |
211
|
|
|
* @return int PDO constant |
212
|
|
|
* @since 8.1.0 |
213
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
214
|
|
|
*/ |
215
|
|
|
private function getPDOType($value) { |
216
|
|
|
switch (gettype($value)) { |
217
|
|
|
case 'integer': |
218
|
|
|
return \PDO::PARAM_INT; |
219
|
|
|
case 'boolean': |
220
|
|
|
return \PDO::PARAM_BOOL; |
221
|
|
|
default: |
222
|
|
|
return \PDO::PARAM_STR; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Runs an sql query |
229
|
|
|
* @param string $sql the prepare string |
230
|
|
|
* @param array $params the params which should replace the ? in the sql query |
231
|
|
|
* @param int $limit the maximum number of rows |
232
|
|
|
* @param int $offset from which row we want to start |
233
|
|
|
* @return \PDOStatement the database query result |
234
|
|
|
* @since 7.0.0 |
235
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
236
|
|
|
*/ |
237
|
|
|
protected function execute($sql, array $params = [], $limit = null, $offset = null) { |
238
|
|
|
$query = $this->db->prepare($sql, $limit, $offset); |
239
|
|
|
|
240
|
|
|
if ($this->isAssocArray($params)) { |
|
|
|
|
241
|
|
|
foreach ($params as $key => $param) { |
242
|
|
|
$pdoConstant = $this->getPDOType($param); |
|
|
|
|
243
|
|
|
$query->bindValue($key, $param, $pdoConstant); |
244
|
|
|
} |
245
|
|
|
} else { |
246
|
|
|
$index = 1; // bindParam is 1 indexed |
247
|
|
|
foreach ($params as $param) { |
248
|
|
|
$pdoConstant = $this->getPDOType($param); |
|
|
|
|
249
|
|
|
$query->bindValue($index, $param, $pdoConstant); |
250
|
|
|
$index++; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$query->execute(); |
255
|
|
|
|
256
|
|
|
return $query; |
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Returns an db result and throws exceptions when there are more or less |
261
|
|
|
* results |
262
|
|
|
* @see findEntity |
263
|
|
|
* @param string $sql the sql query |
264
|
|
|
* @param array $params the parameters of the sql query |
265
|
|
|
* @param int $limit the maximum number of rows |
266
|
|
|
* @param int $offset from which row we want to start |
267
|
|
|
* @throws DoesNotExistException if the item does not exist |
268
|
|
|
* @throws MultipleObjectsReturnedException if more than one item exist |
269
|
|
|
* @return array the result as row |
270
|
|
|
* @since 7.0.0 |
271
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
272
|
|
|
*/ |
273
|
|
|
protected function findOneQuery($sql, array $params = [], $limit = null, $offset = null) { |
274
|
|
|
$stmt = $this->execute($sql, $params, $limit, $offset); |
|
|
|
|
275
|
|
|
$row = $stmt->fetch(); |
276
|
|
|
|
277
|
|
|
if ($row === false || $row === null) { |
278
|
|
|
$stmt->closeCursor(); |
279
|
|
|
$msg = $this->buildDebugMessage( |
|
|
|
|
280
|
|
|
'Did expect one result but found none when executing', $sql, $params, $limit, $offset |
281
|
|
|
); |
282
|
|
|
throw new DoesNotExistException($msg); |
283
|
|
|
} |
284
|
|
|
$row2 = $stmt->fetch(); |
285
|
|
|
$stmt->closeCursor(); |
286
|
|
|
//MDB2 returns null, PDO and doctrine false when no row is available |
287
|
|
|
if (! ($row2 === false || $row2 === null)) { |
288
|
|
|
$msg = $this->buildDebugMessage( |
|
|
|
|
289
|
|
|
'Did not expect more than one result when executing', $sql, $params, $limit, $offset |
290
|
|
|
); |
291
|
|
|
throw new MultipleObjectsReturnedException($msg); |
292
|
|
|
} else { |
293
|
|
|
return $row; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Builds an error message by prepending the $msg to an error message which |
299
|
|
|
* has the parameters |
300
|
|
|
* @see findEntity |
301
|
|
|
* @param string $sql the sql query |
302
|
|
|
* @param array $params the parameters of the sql query |
303
|
|
|
* @param int $limit the maximum number of rows |
304
|
|
|
* @param int $offset from which row we want to start |
305
|
|
|
* @return string formatted error message string |
306
|
|
|
* @since 9.1.0 |
307
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
308
|
|
|
*/ |
309
|
|
|
private function buildDebugMessage($msg, $sql, array $params = [], $limit = null, $offset = null) { |
310
|
|
|
return $msg . |
311
|
|
|
': query "' . $sql . '"; ' . |
312
|
|
|
'parameters ' . print_r($params, true) . '; ' . |
|
|
|
|
313
|
|
|
'limit "' . $limit . '"; '. |
314
|
|
|
'offset "' . $offset . '"'; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Creates an entity from a row. Automatically determines the entity class |
320
|
|
|
* from the current mapper name (MyEntityMapper -> MyEntity) |
321
|
|
|
* @param array $row the row which should be converted to an entity |
322
|
|
|
* @return Entity the entity |
323
|
|
|
* @since 7.0.0 |
324
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
325
|
|
|
*/ |
326
|
|
|
protected function mapRowToEntity($row) { |
327
|
|
|
return call_user_func($this->entityClass .'::fromRow', $row); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Runs a sql query and returns an array of entities |
333
|
|
|
* @param string $sql the prepare string |
334
|
|
|
* @param array $params the params which should replace the ? in the sql query |
335
|
|
|
* @param int $limit the maximum number of rows |
336
|
|
|
* @param int $offset from which row we want to start |
337
|
|
|
* @return array all fetched entities |
338
|
|
|
* @since 7.0.0 |
339
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
340
|
|
|
*/ |
341
|
|
|
protected function findEntities($sql, array $params = [], $limit = null, $offset = null) { |
342
|
|
|
$stmt = $this->execute($sql, $params, $limit, $offset); |
|
|
|
|
343
|
|
|
|
344
|
|
|
$entities = []; |
345
|
|
|
|
346
|
|
|
while ($row = $stmt->fetch()) { |
347
|
|
|
$entities[] = $this->mapRowToEntity($row); |
|
|
|
|
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$stmt->closeCursor(); |
351
|
|
|
|
352
|
|
|
return $entities; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Returns an db result and throws exceptions when there are more or less |
358
|
|
|
* results |
359
|
|
|
* @param string $sql the sql query |
360
|
|
|
* @param array $params the parameters of the sql query |
361
|
|
|
* @param int $limit the maximum number of rows |
362
|
|
|
* @param int $offset from which row we want to start |
363
|
|
|
* @throws DoesNotExistException if the item does not exist |
364
|
|
|
* @throws MultipleObjectsReturnedException if more than one item exist |
365
|
|
|
* @return Entity the entity |
366
|
|
|
* @since 7.0.0 |
367
|
|
|
* @deprecated 14.0.0 Move over to QBMapper |
368
|
|
|
*/ |
369
|
|
|
protected function findEntity($sql, array $params = [], $limit = null, $offset = null) { |
370
|
|
|
return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
|
|
|
|
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.