Passed
Push — master ( 7b42d0...d80bc5 )
by Pauli
04:15 queued 21s
created

Mapper::isAssocArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 * @copyright Copyright (c) 2023 - 2025, Pauli Järvinen
5
 *
6
 * @author Bernhard Posselt <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Pauli Järvinen <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
namespace OCA\Music\AppFramework\Db;
31
32
use OCP\AppFramework\Db\DoesNotExistException;
33
use OCP\AppFramework\Db\Entity;
34
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
35
use OCP\IDBConnection;
36
37
/**
38
 * The base class OCP\AppFramework\Db\Mapper is no longer shipped by NC26+.
39
 * This is a slightly modified copy of that class on NC25, the modifications are just stylistic.
40
 * OwnCloud still ships the platform version of the class and it's almost identical to this one;
41
 * the difference is just that the OC version still accepts also IDb type of handle in the constructor.
42
 * However, IDBConnection has been available since OC 8.1 and that's what we always use.
43
 * We use this copy of ours both on NC and OC.
44
 */
45
abstract class Mapper {
46
	protected string $tableName;
47
	protected string $entityClass;
48
	protected IDBConnection $db;
49
50
	/**
51
	 * @param IDBConnection $db Instance of the Db abstraction layer
52
	 * @param string $tableName the name of the table. set this to allow entity
53
	 * @param ?string $entityClass the name of the entity that the sql should be
54
	 * mapped to queries without using sql
55
	 * @since 7.0.0
56
	 */
57
	public function __construct(IDBConnection $db, $tableName, $entityClass=null) {
58
		$this->db = $db;
59
		$this->tableName = '*PREFIX*' . $tableName;
60
61
		// if not given set the entity name to the class without the mapper part
62
		// cache it here for later use since reflection is slow
63
		if ($entityClass === null) {
64
			$this->entityClass = \str_replace('Mapper', '', \get_class($this));
65
		} else {
66
			$this->entityClass = $entityClass;
67
		}
68
	}
69
70
	/**
71
	 * @return string the table name
72
	 * @since 7.0.0
73
	 */
74
	public function getTableName() {
75
		return $this->tableName;
76
	}
77
78
	/**
79
	 * Deletes an entity from the table
80
	 * @param Entity $entity the entity that should be deleted
81
	 * @return Entity the deleted entity
82
	 * @since 7.0.0 - return value added in 8.1.0
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
	 * Creates a new entry in the db from an entity
93
	 * @param Entity $entity the entity that should be created
94
	 * @return Entity the saved entity with the set id
95
	 * @since 7.0.0
96
	 */
97
	public function insert(Entity $entity) {
98
		// get updated fields to save, fields have to be set using a setter to
99
		// be saved
100
		$properties = $entity->getUpdatedFields();
101
		$values = '';
102
		$columns = '';
103
		$params = [];
104
105
		// build the fields
106
		$i = 0;
107
		foreach ($properties as $property => $updated) {
108
			$column = $entity->propertyToColumn($property);
109
			$getter = 'get' . \ucfirst($property);
110
111
			$columns .= '`' . $column . '`';
112
			$values .= '?';
113
114
			// only append colon if there are more entries
115
			if ($i < \count($properties)-1) {
116
				$columns .= ',';
117
				$values .= ',';
118
			}
119
120
			$params[] = $entity->$getter();
121
			$i++;
122
		}
123
124
		$sql = 'INSERT INTO `' . $this->tableName . '`(' .
125
				$columns . ') VALUES(' . $values . ')';
126
127
		$stmt = $this->execute($sql, $params);
128
129
		$entity->setId((int) $this->db->lastInsertId($this->tableName));
130
131
		$stmt->closeCursor();
132
133
		return $entity;
134
	}
135
136
	/**
137
	 * Updates an entry in the db from an entity
138
	 * @throws \InvalidArgumentException if entity has no id
139
	 * @param Entity $entity the entity that should be created
140
	 * @return Entity the saved entity with the set id
141
	 * @since 7.0.0 - return value was added in 8.0.0
142
	 */
143
	public function update(Entity $entity) {
144
		// if entity wasn't changed it makes no sense to run a db query
145
		$properties = $entity->getUpdatedFields();
146
		if (\count($properties) === 0) {
147
			return $entity;
148
		}
149
150
		// entity needs an id
151
		$id = $entity->getId();
152
		if ($id === null) {
0 ignored issues
show
introduced by
The condition $id === null is always false.
Loading history...
153
			throw new \InvalidArgumentException(
154
				'Entity which should be updated has no id'
155
			);
156
		}
157
158
		// get updated fields to save, fields have to be set using a setter to
159
		// be saved
160
		// do not update the id field
161
		unset($properties['id']);
162
163
		$columns = '';
164
		$params = [];
165
166
		// build the fields
167
		$i = 0;
168
		foreach ($properties as $property => $updated) {
169
			$column = $entity->propertyToColumn($property);
170
			$getter = 'get' . \ucfirst($property);
171
172
			$columns .= '`' . $column . '` = ?';
173
174
			// only append colon if there are more entries
175
			if ($i < \count($properties)-1) {
176
				$columns .= ',';
177
			}
178
179
			$params[] = $entity->$getter();
180
			$i++;
181
		}
182
183
		$sql = 'UPDATE `' . $this->tableName . '` SET ' .
184
				$columns . ' WHERE `id` = ?';
185
		$params[] = $id;
186
187
		$stmt = $this->execute($sql, $params);
188
		$stmt->closeCursor();
189
190
		return $entity;
191
	}
192
193
	/**
194
	 * Checks if an array is associative
195
	 * @param array $array
196
	 * @return bool true if associative
197
	 * @since 8.1.0
198
	 */
199
	private function isAssocArray(array $array) {
200
		return \array_values($array) !== $array;
201
	}
202
203
	/**
204
	 * Returns the correct PDO constant based on the value type
205
	 * @param mixed $value
206
	 * @return int PDO constant
207
	 * @since 8.1.0
208
	 */
209
	private function getPDOType($value) {
210
		switch (\gettype($value)) {
211
			case 'integer':
212
				return \PDO::PARAM_INT;
213
			case 'boolean':
214
				return \PDO::PARAM_BOOL;
215
			default:
216
				return \PDO::PARAM_STR;
217
		}
218
	}
219
220
	/**
221
	 * Runs an sql query
222
	 * @param string $sql the prepare string
223
	 * @param array $params the params which should replace the ? in the sql query
224
	 * @param int $limit the maximum number of rows
225
	 * @param int $offset from which row we want to start
226
	 * @return \Doctrine\DBAL\Driver\Statement the database query result
227
	 * @since 7.0.0
228
	 */
229
	protected function execute($sql, array $params=[], $limit=null, $offset=null) {
230
		$query = $this->db->prepare($sql, $limit, $offset);
231
232
		if ($this->isAssocArray($params)) {
233
			foreach ($params as $key => $param) {
234
				$pdoConstant = $this->getPDOType($param);
235
				$query->bindValue($key, $param, $pdoConstant);
236
			}
237
		} else {
238
			$index = 1;  // bindParam is 1 indexed
239
			foreach ($params as $param) {
240
				$pdoConstant = $this->getPDOType($param);
241
				$query->bindValue($index, $param, $pdoConstant);
242
				$index++;
243
			}
244
		}
245
246
		$query->execute();
247
248
		return $query;
249
	}
250
251
	/**
252
	 * Returns an db result and throws exceptions when there are more or less
253
	 * results
254
	 * @see findEntity
255
	 * @param string $sql the sql query
256
	 * @param array $params the parameters of the sql query
257
	 * @param int $limit the maximum number of rows
258
	 * @param int $offset from which row we want to start
259
	 * @throws DoesNotExistException if the item does not exist
260
	 * @throws MultipleObjectsReturnedException if more than one item exist
261
	 * @return array the result as row
262
	 * @since 7.0.0
263
	 */
264
	protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null) {
265
		$stmt = $this->execute($sql, $params, $limit, $offset);
266
		$row = $stmt->fetch();
267
268
		if ($row === false || $row === null) {
269
			$stmt->closeCursor();
270
			$msg = $this->buildDebugMessage(
271
				'Did expect one result but found none when executing',
272
				$sql,
273
				$params,
274
				$limit,
275
				$offset
276
			);
277
			throw new DoesNotExistException($msg);
278
		}
279
		$row2 = $stmt->fetch();
280
		$stmt->closeCursor();
281
		//MDB2 returns null, PDO and doctrine false when no row is available
282
		if (! ($row2 === false || $row2 === null)) {
283
			$msg = $this->buildDebugMessage(
284
				'Did not expect more than one result when executing',
285
				$sql,
286
				$params,
287
				$limit,
288
				$offset
289
			);
290
			throw new MultipleObjectsReturnedException($msg);
291
		} else {
292
			return $row;
293
		}
294
	}
295
296
	/**
297
	 * Builds an error message by prepending the $msg to an error message which
298
	 * has the parameters
299
	 * @see findEntity
300
	 * @param string $msg
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
	 */
308
	private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) {
309
		return $msg .
310
					': query "' .	$sql . '"; ' .
311
					'parameters ' . \print_r($params, true) . '; ' .
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

311
					'parameters ' . /** @scrutinizer ignore-type */ \print_r($params, true) . '; ' .
Loading history...
312
					'limit "' . $limit . '"; '.
313
					'offset "' . $offset . '"';
314
	}
315
316
	/**
317
	 * Creates an entity from a row. Automatically determines the entity class
318
	 * from the current mapper name (MyEntityMapper -> MyEntity)
319
	 * @param array $row the row which should be converted to an entity
320
	 * @return Entity the entity
321
	 * @since 7.0.0
322
	 */
323
	protected function mapRowToEntity($row) {
324
		return \call_user_func($this->entityClass .'::fromRow', $row);
325
	}
326
327
	/**
328
	 * Runs a sql query and returns an array of entities
329
	 * @param string $sql the prepare string
330
	 * @param array $params the params which should replace the ? in the sql query
331
	 * @param int $limit the maximum number of rows
332
	 * @param int $offset from which row we want to start
333
	 * @return array all fetched entities
334
	 * @since 7.0.0
335
	 */
336
	protected function findEntities($sql, array $params=[], $limit=null, $offset=null) {
337
		$stmt = $this->execute($sql, $params, $limit, $offset);
338
339
		$entities = [];
340
341
		while ($row = $stmt->fetch()) {
342
			$entities[] = $this->mapRowToEntity($row);
343
		}
344
345
		$stmt->closeCursor();
346
347
		return $entities;
348
	}
349
350
	/**
351
	 * Returns an db result and throws exceptions when there are more or less
352
	 * results
353
	 * @param string $sql the sql query
354
	 * @param array $params the parameters of the sql query
355
	 * @param int $limit the maximum number of rows
356
	 * @param int $offset from which row we want to start
357
	 * @throws DoesNotExistException if the item does not exist
358
	 * @throws MultipleObjectsReturnedException if more than one item exist
359
	 * @return Entity the entity
360
	 * @since 7.0.0
361
	 */
362
	protected function findEntity($sql, array $params=[], $limit=null, $offset=null) {
363
		return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset));
364
	}
365
}
366