Completed
Push — master ( 0350f4...6814cf )
by Pauli
26s queued 14s
created

OldNextcloudMapper::buildDebugMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\AppFramework\Db;
29
30
use OCP\AppFramework\Db\DoesNotExistException;
31
use OCP\AppFramework\Db\Entity;
32
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
33
use OCP\IDBConnection;
34
35
/**
36
 * Simple parent class for inheriting your data access layer from. This class
37
 * may be subject to change in the future
38
 * @since 7.0.0
39
 * @deprecated 14.0.0 Move over to QBMapper
40
 */
41
abstract class OldNextcloudMapper {
42
	protected $tableName;
43
	protected $entityClass;
44
	protected $db;
45
46
	/**
47
	 * @param IDBConnection $db Instance of the Db abstraction layer
48
	 * @param string $tableName the name of the table. set this to allow entity
49
	 * @param string $entityClass the name of the entity that the sql should be
50
	 * mapped to queries without using sql
51
	 * @since 7.0.0
52
	 * @deprecated 14.0.0 Move over to QBMapper
53
	 */
54
	public function __construct(IDBConnection $db, $tableName, $entityClass = null) {
55
		$this->db = $db;
56
		$this->tableName = '*PREFIX*' . $tableName;
57
58
		// if not given set the entity name to the class without the mapper part
59
		// cache it here for later use since reflection is slow
60
		if ($entityClass === null) {
61
			$this->entityClass = str_replace('Mapper', '', get_class($this));
62
		} else {
63
			$this->entityClass = $entityClass;
64
		}
65
	}
66
67
68
	/**
69
	 * @return string the table name
70
	 * @since 7.0.0
71
	 * @deprecated 14.0.0 Move over to QBMapper
72
	 */
73
	public function getTableName() {
74
		return $this->tableName;
75
	}
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
	 * @deprecated 14.0.0 Move over to QBMapper
84
	 */
85
	public function delete(Entity $entity) {
86
		$sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?';
87
		$stmt = $this->execute($sql, [$entity->getId()]);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

87
		$stmt = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$entity->getId()]);

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.

Loading history...
88
		$stmt->closeCursor();
89
		return $entity;
90
	}
91
92
93
	/**
94
	 * Creates a new entry in the db from an entity
95
	 * @param Entity $entity the entity that should be created
96
	 * @return Entity the saved entity with the set id
97
	 * @since 7.0.0
98
	 * @deprecated 14.0.0 Move over to QBMapper
99
	 */
100
	public function insert(Entity $entity) {
101
		// get updated fields to save, fields have to be set using a setter to
102
		// be saved
103
		$properties = $entity->getUpdatedFields();
104
		$values = '';
105
		$columns = '';
106
		$params = [];
107
108
		// build the fields
109
		$i = 0;
110
		foreach ($properties as $property => $updated) {
111
			$column = $entity->propertyToColumn($property);
112
			$getter = 'get' . ucfirst($property);
113
114
			$columns .= '`' . $column . '`';
115
			$values .= '?';
116
117
			// only append colon if there are more entries
118
			if ($i < count($properties) - 1) {
119
				$columns .= ',';
120
				$values .= ',';
121
			}
122
123
			$params[] = $entity->$getter();
124
			$i++;
125
		}
126
127
		$sql = 'INSERT INTO `' . $this->tableName . '`(' .
128
				$columns . ') VALUES(' . $values . ')';
129
130
		$stmt = $this->execute($sql, $params);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

130
		$stmt = /** @scrutinizer ignore-deprecated */ $this->execute($sql, $params);

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.

Loading history...
131
132
		$entity->setId((int) $this->db->lastInsertId($this->tableName));
133
134
		$stmt->closeCursor();
135
136
		return $entity;
137
	}
138
139
140
141
	/**
142
	 * Updates an entry in the db from an entity
143
	 * @throws \InvalidArgumentException if entity has no id
144
	 * @param Entity $entity the entity that should be created
145
	 * @return Entity the saved entity with the set id
146
	 * @since 7.0.0 - return value was added in 8.0.0
147
	 * @deprecated 14.0.0 Move over to QBMapper
148
	 */
149
	public function update(Entity $entity) {
150
		// if entity wasn't changed it makes no sense to run a db query
151
		$properties = $entity->getUpdatedFields();
152
		if (count($properties) === 0) {
153
			return $entity;
154
		}
155
156
		// entity needs an id
157
		$id = $entity->getId();
158
		if ($id === null) {
0 ignored issues
show
introduced by
The condition $id === null is always false.
Loading history...
159
			throw new \InvalidArgumentException(
160
				'Entity which should be updated has no id');
161
		}
162
163
		// get updated fields to save, fields have to be set using a setter to
164
		// be saved
165
		// do not update the id field
166
		unset($properties['id']);
167
168
		$columns = '';
169
		$params = [];
170
171
		// build the fields
172
		$i = 0;
173
		foreach ($properties as $property => $updated) {
174
			$column = $entity->propertyToColumn($property);
175
			$getter = 'get' . ucfirst($property);
176
177
			$columns .= '`' . $column . '` = ?';
178
179
			// only append colon if there are more entries
180
			if ($i < count($properties) - 1) {
181
				$columns .= ',';
182
			}
183
184
			$params[] = $entity->$getter();
185
			$i++;
186
		}
187
188
		$sql = 'UPDATE `' . $this->tableName . '` SET ' .
189
				$columns . ' WHERE `id` = ?';
190
		$params[] = $id;
191
192
		$stmt = $this->execute($sql, $params);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

192
		$stmt = /** @scrutinizer ignore-deprecated */ $this->execute($sql, $params);

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.

Loading history...
193
		$stmt->closeCursor();
194
195
		return $entity;
196
	}
197
198
	/**
199
	 * Checks if an array is associative
200
	 * @param array $array
201
	 * @return bool true if associative
202
	 * @since 8.1.0
203
	 * @deprecated 14.0.0 Move over to QBMapper
204
	 */
205
	private function isAssocArray(array $array) {
206
		return array_values($array) !== $array;
207
	}
208
209
	/**
210
	 * Returns the correct PDO constant based on the value type
211
	 * @param $value
212
	 * @return int PDO constant
213
	 * @since 8.1.0
214
	 * @deprecated 14.0.0 Move over to QBMapper
215
	 */
216
	private function getPDOType($value) {
217
		switch (gettype($value)) {
218
			case 'integer':
219
				return \PDO::PARAM_INT;
220
			case 'boolean':
221
				return \PDO::PARAM_BOOL;
222
			default:
223
				return \PDO::PARAM_STR;
224
		}
225
	}
226
227
228
	/**
229
	 * Runs an sql query
230
	 * @param string $sql the prepare string
231
	 * @param array $params the params which should replace the ? in the sql query
232
	 * @param int $limit the maximum number of rows
233
	 * @param int $offset from which row we want to start
234
	 * @return \PDOStatement the database query result
235
	 * @since 7.0.0
236
	 * @deprecated 14.0.0 Move over to QBMapper
237
	 */
238
	protected function execute($sql, array $params = [], $limit = null, $offset = null) {
239
		$query = $this->db->prepare($sql, $limit, $offset);
240
241
		if ($this->isAssocArray($params)) {
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::isAssocArray() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

241
		if (/** @scrutinizer ignore-deprecated */ $this->isAssocArray($params)) {

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.

Loading history...
242
			foreach ($params as $key => $param) {
243
				$pdoConstant = $this->getPDOType($param);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...oudMapper::getPDOType() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

243
				$pdoConstant = /** @scrutinizer ignore-deprecated */ $this->getPDOType($param);

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.

Loading history...
244
				$query->bindValue($key, $param, $pdoConstant);
245
			}
246
		} else {
247
			$index = 1;  // bindParam is 1 indexed
248
			foreach ($params as $param) {
249
				$pdoConstant = $this->getPDOType($param);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...oudMapper::getPDOType() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

249
				$pdoConstant = /** @scrutinizer ignore-deprecated */ $this->getPDOType($param);

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.

Loading history...
250
				$query->bindValue($index, $param, $pdoConstant);
251
				$index++;
252
			}
253
		}
254
255
		$query->execute();
256
257
		return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query returns the type Doctrine\DBAL\Driver\Statement which is incompatible with the documented return type PDOStatement.
Loading history...
258
	}
259
260
	/**
261
	 * Returns an db result and throws exceptions when there are more or less
262
	 * results
263
	 * @see findEntity
264
	 * @param string $sql the sql query
265
	 * @param array $params the parameters of the sql query
266
	 * @param int $limit the maximum number of rows
267
	 * @param int $offset from which row we want to start
268
	 * @throws DoesNotExistException if the item does not exist
269
	 * @throws MultipleObjectsReturnedException if more than one item exist
270
	 * @return array the result as row
271
	 * @since 7.0.0
272
	 * @deprecated 14.0.0 Move over to QBMapper
273
	 */
274
	protected function findOneQuery($sql, array $params = [], $limit = null, $offset = null) {
275
		$stmt = $this->execute($sql, $params, $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

275
		$stmt = /** @scrutinizer ignore-deprecated */ $this->execute($sql, $params, $limit, $offset);

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.

Loading history...
276
		$row = $stmt->fetch();
277
278
		if ($row === false || $row === null) {
279
			$stmt->closeCursor();
280
			$msg = $this->buildDebugMessage(
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...er::buildDebugMessage() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

280
			$msg = /** @scrutinizer ignore-deprecated */ $this->buildDebugMessage(

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.

Loading history...
281
				'Did expect one result but found none when executing', $sql, $params, $limit, $offset
282
			);
283
			throw new DoesNotExistException($msg);
284
		}
285
		$row2 = $stmt->fetch();
286
		$stmt->closeCursor();
287
		//MDB2 returns null, PDO and doctrine false when no row is available
288
		if (! ($row2 === false || $row2 === null)) {
289
			$msg = $this->buildDebugMessage(
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...er::buildDebugMessage() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

289
			$msg = /** @scrutinizer ignore-deprecated */ $this->buildDebugMessage(

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.

Loading history...
290
				'Did not expect more than one result when executing', $sql, $params, $limit, $offset
291
			);
292
			throw new MultipleObjectsReturnedException($msg);
293
		} else {
294
			return $row;
295
		}
296
	}
297
298
	/**
299
	 * Builds an error message by prepending the $msg to an error message which
300
	 * has the parameters
301
	 * @see findEntity
302
	 * @param string $sql the sql query
303
	 * @param array $params the parameters of the sql query
304
	 * @param int $limit the maximum number of rows
305
	 * @param int $offset from which row we want to start
306
	 * @return string formatted error message string
307
	 * @since 9.1.0
308
	 * @deprecated 14.0.0 Move over to QBMapper
309
	 */
310
	private function buildDebugMessage($msg, $sql, array $params = [], $limit = null, $offset = null) {
311
		return $msg .
312
					': query "' .	$sql . '"; ' .
313
					'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

313
					'parameters ' . /** @scrutinizer ignore-type */ print_r($params, true) . '; ' .
Loading history...
314
					'limit "' . $limit . '"; '.
315
					'offset "' . $offset . '"';
316
	}
317
318
319
	/**
320
	 * Creates an entity from a row. Automatically determines the entity class
321
	 * from the current mapper name (MyEntityMapper -> MyEntity)
322
	 * @param array $row the row which should be converted to an entity
323
	 * @return Entity the entity
324
	 * @since 7.0.0
325
	 * @deprecated 14.0.0 Move over to QBMapper
326
	 */
327
	protected function mapRowToEntity($row) {
328
		return call_user_func($this->entityClass .'::fromRow', $row);
329
	}
330
331
332
	/**
333
	 * Runs a sql query and returns an array of entities
334
	 * @param string $sql the prepare string
335
	 * @param array $params the params which should replace the ? in the sql query
336
	 * @param int $limit the maximum number of rows
337
	 * @param int $offset from which row we want to start
338
	 * @return array all fetched entities
339
	 * @since 7.0.0
340
	 * @deprecated 14.0.0 Move over to QBMapper
341
	 */
342
	protected function findEntities($sql, array $params = [], $limit = null, $offset = null) {
343
		$stmt = $this->execute($sql, $params, $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...tcloudMapper::execute() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

343
		$stmt = /** @scrutinizer ignore-deprecated */ $this->execute($sql, $params, $limit, $offset);

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.

Loading history...
344
345
		$entities = [];
346
347
		while ($row = $stmt->fetch()) {
348
			$entities[] = $this->mapRowToEntity($row);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...apper::mapRowToEntity() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

348
			$entities[] = /** @scrutinizer ignore-deprecated */ $this->mapRowToEntity($row);

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.

Loading history...
349
		}
350
351
		$stmt->closeCursor();
352
353
		return $entities;
354
	}
355
356
357
	/**
358
	 * Returns an db result and throws exceptions when there are more or less
359
	 * results
360
	 * @param string $sql the sql query
361
	 * @param array $params the parameters of the sql query
362
	 * @param int $limit the maximum number of rows
363
	 * @param int $offset from which row we want to start
364
	 * @throws DoesNotExistException if the item does not exist
365
	 * @throws MultipleObjectsReturnedException if more than one item exist
366
	 * @return Entity the entity
367
	 * @since 7.0.0
368
	 * @deprecated 14.0.0 Move over to QBMapper
369
	 */
370
	protected function findEntity($sql, array $params = [], $limit = null, $offset = null) {
371
		return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset));
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...apper::mapRowToEntity() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

371
		return /** @scrutinizer ignore-deprecated */ $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset));

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.

Loading history...
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::findOneQuery() has been deprecated: 14.0.0 Move over to QBMapper ( Ignorable by Annotation )

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

371
		return $this->mapRowToEntity(/** @scrutinizer ignore-deprecated */ $this->findOneQuery($sql, $params, $limit, $offset));

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.

Loading history...
372
	}
373
}
374