Passed
Pull Request — master (#1078)
by Pauli
06:30 queued 03:46
created

BaseMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 4
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2016 - 2023
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\AppFramework\Db\DoesNotExistException;
16
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
17
use OCP\IDBConnection;
18
19
use OCA\Music\AppFramework\Db\CompatibleMapper;
20
use OCA\Music\AppFramework\Db\UniqueConstraintViolationException;
21
use OCA\Music\Utility\Util;
22
23
/**
24
 * Common base class for data access classes of the Music app
25
 * @phpstan-template EntityType of Entity
26
 * @phpstan-method EntityType findEntity(string $sql, array $params)
27
 * @phpstan-method EntityType[] findEntities(string $sql, array $params, ?int $limit=null, ?int $offset=null)
28
 */
29
abstract class BaseMapper extends CompatibleMapper {
0 ignored issues
show
Deprecated Code introduced by
The class OCA\Music\AppFramework\Db\OldNextcloudMapper 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

29
abstract class BaseMapper extends /** @scrutinizer ignore-deprecated */ CompatibleMapper {
Loading history...
30
	const SQL_DATE_FORMAT = 'Y-m-d H:i:s.v';
31
32
	protected $nameColumn;
33
	/** @phpstan-var class-string<EntityType> $entityClass */
34
	protected $entityClass;
35
36
	/**
37
	 * @phpstan-param class-string<EntityType> $entityClass
38
	 */
39
	public function __construct(IDBConnection $db, string $tableName, string $entityClass, string $nameColumn) {
40
		parent::__construct($db, $tableName, $entityClass);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...udMapper::__construct() 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

40
		/** @scrutinizer ignore-deprecated */ parent::__construct($db, $tableName, $entityClass);

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...
41
		$this->nameColumn = $nameColumn;
42
		// eclipse the base class property to help phpstan
43
		$this->entityClass = $entityClass;
44
	}
45
46
	/**
47
	 * Create an empty object of the entity class bound to this mapper
48
	 * @phpstan-return EntityType
49
	 */
50
	public function createEntity() : Entity {
51
		return new $this->entityClass();
52
	}
53
54
	/**
55
	 * Find a single entity by id and user_id
56
	 * @throws DoesNotExistException if the entity does not exist
57
	 * @throws MultipleObjectsReturnedException if more than one entity exists
58
	 * @phpstan-return EntityType
59
	 */
60
	public function find(int $id, string $userId) : Entity {
61
		$sql = $this->selectUserEntities("`{$this->getTableName()}`.`id` = ?");
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

61
		$sql = $this->selectUserEntities("`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`id` = ?");

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...
62
		return $this->findEntity($sql, [$userId, $id]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findEntity...l, array($userId, $id)) returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\Music\Db\Entity.
Loading history...
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...oudMapper::findEntity() 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

62
		return /** @scrutinizer ignore-deprecated */ $this->findEntity($sql, [$userId, $id]);

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...
63
	}
64
65
	/**
66
	 * Find all entities matching the given IDs. Specifying the owning user is optional.
67
	 * @param integer[] $ids  IDs of the entities to be found
68
	 * @param string|null $userId
69
	 * @return Entity[]
70
	 * @phpstan-return EntityType[]
71
	 */
72
	public function findById(array $ids, string $userId=null) : array {
73
		$count = \count($ids);
74
		$condition = "`{$this->getTableName()}`.`id` IN ". $this->questionMarks($count);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

74
		$condition = "`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`id` IN ". $this->questionMarks($count);

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...
75
76
		if (empty($userId)) {
77
			$sql = $this->selectEntities($condition);
78
		} else {
79
			$sql = $this->selectUserEntities($condition);
80
			$ids = \array_merge([$userId], $ids);
81
		}
82
83
		return $this->findEntities($sql, $ids);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::findEntities() 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

83
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $ids);

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...
84
	}
85
86
	/**
87
	 * Find all user's entities
88
	 * @param string|null $createdMin Optional minimum `created` timestamp.
89
	 * @param string|null $createdMax Optional maximum `created` timestamp.
90
	 * @param string|null $updatedMin Optional minimum `updated` timestamp.
91
	 * @param string|null $updatedMax Optional maximum `updated` timestamp.
92
	 * @return Entity[]
93
	 * @phpstan-return EntityType[]
94
	 */
95
	public function findAll(string $userId, int $sortBy=SortBy::None, int $limit=null, int $offset=null,
96
							?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null) : array {
97
		$sorting = $this->formatSortingClause($sortBy);
98
		[$condition, $params] = $this->formatTimestampConditions($createdMin, $createdMax, $updatedMin, $updatedMax);
99
		$sql = $this->selectUserEntities($condition, $sorting);
100
		\array_unshift($params, $userId);
101
		return $this->findEntities($sql, $params, $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::findEntities() 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

101
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($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...
102
	}
103
104
	/**
105
	 * Find all user's entities matching the given name
106
	 * @param string|null $createdMin Optional minimum `created` timestamp.
107
	 * @param string|null $createdMax Optional maximum `created` timestamp.
108
	 * @param string|null $updatedMin Optional minimum `updated` timestamp.
109
	 * @param string|null $updatedMax Optional maximum `updated` timestamp.
110
	 * @return Entity[]
111
	 * @phpstan-return EntityType[]
112
	 */
113
	public function findAllByName(
114
		?string $name, string $userId, int $matchMode=MatchMode::Exact, int $limit=null, int $offset=null,
115
		?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null) : array {
116
117
		$params = [$userId];
118
		$nameCol = "`{$this->getTableName()}`.`{$this->nameColumn}`";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

118
		$nameCol = "`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`{$this->nameColumn}`";

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...
119
		if ($name === null) {
120
			$condition = "$nameCol IS NULL";
121
		} else {
122
			if ($matchMode === MatchMode::Exact) {
123
				$condition = "LOWER($nameCol) = LOWER(?)";
124
			} else {
125
				$condition = "LOWER($nameCol) LIKE LOWER(?)";
126
			}
127
			if ($matchMode === MatchMode::Substring) {
128
				$params[] = self::prepareSubstringSearchPattern($name);
129
			} else {
130
				$params[] = $name;
131
			}
132
		}
133
134
		[$timestampConds, $timestampParams] = $this->formatTimestampConditions($createdMin, $createdMax, $updatedMin, $updatedMax);
135
		if (!empty($timestampConds)) {
136
			$condition .= ' AND ' . $timestampConds;
137
			$params = \array_merge($params, $timestampParams);
138
		}
139
140
		$sql = $this->selectUserEntities($condition, $this->formatSortingClause(SortBy::Name));
141
142
		return $this->findEntities($sql, $params, $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::findEntities() 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

142
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($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...
143
	}
144
145
	/**
146
	 * Find all user's starred entities. It is safe to call this also on entity types
147
	 * not supporting starring in which case an empty array will be returned.
148
	 * @return Entity[]
149
	 * @phpstan-return EntityType[]
150
	 */
151
	public function findAllStarred(string $userId, int $limit=null, int $offset=null) : array {
152
		if (\property_exists($this->entityClass, 'starred')) {
153
			$sql = $this->selectUserEntities(
154
				"`{$this->getTableName()}`.`starred` IS NOT NULL",
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

154
				"`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`starred` IS NOT NULL",

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...
155
				$this->formatSortingClause(SortBy::Name));
156
			return $this->findEntities($sql, [$userId], $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::findEntities() 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

156
			return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, [$userId], $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...
157
		} else {
158
			return [];
159
		}
160
	}
161
162
	/**
163
	 * Find all entities with user-given rating 1-5
164
	 * @return Entity[]
165
	 * @phpstan-return EntityType[]
166
	 */
167
	public function findAllRated(string $userId, int $limit=null, int $offset=null) : array {
168
		if (\property_exists($this->entityClass, 'rating')) {
169
			$sql = $this->selectUserEntities(
170
				"`{$this->getTableName()}`.`rating` > 0",
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

170
				"`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`rating` > 0",

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...
171
				$this->formatSortingClause(SortBy::Rating));
172
			return $this->findEntities($sql, [$userId], $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::findEntities() 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

172
			return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, [$userId], $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...
173
		} else {
174
			return [];
175
		}
176
	}
177
178
	/**
179
	 * Find all entities matching multiple criteria, as needed for the Ampache API method `advanced_search`
180
	 * @param string $conjunction Operator to use between the rules, either 'and' or 'or'
181
	 * @param array $rules Array of arrays: [['rule' => string, 'operator' => string, 'input' => string], ...]
182
	 * 				Here, 'rule' has dozens of possible values depending on the business layer in question
183
	 * 				(see https://ampache.org/api/api-advanced-search#available-search-rules, alias names not supported here),
184
	 * 				'operator' is one of 
185
	 * 				['contain', 'notcontain', 'start', 'end', 'is', 'isnot', '>=', '<=', '=', '!=', '>', '<', 'true', 'false', 'equal', 'ne', 'limit'],
186
	 * 				'input' is the right side value of the 'operator' (disregarded for the operators 'true' and 'false')
187
	 * @return Entity[]
188
	 * @phpstan-return EntityType[]
189
	 */
190
	public function findAllAdvanced(string $conjunction, array $rules, string $userId, ?int $limit=null, ?int $offset=null) : array {
191
		$sqlConditions = [];
192
		$sqlParams = [$userId];
193
194
		foreach ($rules as $rule) {
195
			list('op' => $sqlOp, 'param' => $param) = $this->advFormatSqlOperator($rule['operator'], $rule['input'], $userId);
196
			$cond = $this->advFormatSqlCondition($rule['rule'], $sqlOp);
197
			$sqlConditions[] = $cond;
198
			// On some conditions, the parameter may need to be repeated several times
199
			$paramCount = \substr_count($cond, '?');
200
			for ($i = 0; $i < $paramCount; ++$i) {
201
				$sqlParams[] = $param;
202
			}
203
		}
204
		$sqlConditions = \implode(" $conjunction ", $sqlConditions);
205
206
		$sql = $this->selectUserEntities($sqlConditions, $this->formatSortingClause(SortBy::Name));
207
		return $this->findEntities($sql, $sqlParams, $limit, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::findEntities() 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

207
		return /** @scrutinizer ignore-deprecated */ $this->findEntities($sql, $sqlParams, $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...
208
	}
209
210
	/**
211
	 * Optionally, limit to given IDs which may be used to check the validity of those IDs.
212
	 * @return int[]
213
	 */
214
	public function findAllIds(string $userId, ?array $ids = null) : array {
215
		$sql = "SELECT `id` FROM `{$this->getTableName()}` WHERE `user_id` = ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

215
		$sql = "SELECT `id` FROM `{/** @scrutinizer ignore-deprecated */ $this->getTableName()}` WHERE `user_id` = ?";

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...
216
		$params = [$userId];
217
218
		if ($ids !== null) {
219
			$sql .= ' AND `id` IN ' . $this->questionMarks(\count($ids));
220
			$params = \array_merge($params, $ids);
221
		}
222
223
		$result = $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

223
		$result = /** @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...
224
225
		return \array_map('intval', $result->fetchAll(\PDO::FETCH_COLUMN));
226
	}
227
228
	/**
229
	 * Find IDs of all users owning any entities of this mapper
230
	 * @return string[]
231
	 */
232
	public function findAllUsers() : array {
233
		$sql = "SELECT DISTINCT(`user_id`) FROM `{$this->getTableName()}`";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

233
		$sql = "SELECT DISTINCT(`user_id`) FROM `{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`";

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...
234
		$result = $this->execute($sql);
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

234
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql);

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...
235
236
		return $result->fetchAll(\PDO::FETCH_COLUMN);
237
	}
238
239
	/**
240
	 * Delete all entities with given IDs without specifying the user
241
	 * @param integer[] $ids  IDs of the entities to be deleted
242
	 */
243
	public function deleteById(array $ids) : void {
244
		$count = \count($ids);
245
		if ($count === 0) {
246
			return;
247
		}
248
		$this->deleteByCond('`id` IN ' . $this->questionMarks($count), $ids);
249
	}
250
251
	/**
252
	 * Delete all entities matching the given SQL condition
253
	 * @param string $condition SQL 'WHERE' condition (without the keyword 'WHERE')
254
	 * @param array $params SQL parameters for the condition
255
	 */
256
	protected function deleteByCond(string $condition, array $params) : void {
257
		$sql = "DELETE FROM `{$this->getTableName()}` WHERE ". $condition;
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

257
		$sql = "DELETE FROM `{/** @scrutinizer ignore-deprecated */ $this->getTableName()}` WHERE ". $condition;

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...
258
		$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

258
		/** @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...
259
	}
260
261
	/**
262
	 * Delete all entities of the given user
263
	 */
264
	public function deleteAll(string $userId) : void {
265
		$sql = "DELETE FROM `{$this->getTableName()}` WHERE `user_id` = ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

265
		$sql = "DELETE FROM `{/** @scrutinizer ignore-deprecated */ $this->getTableName()}` WHERE `user_id` = ?";

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...
266
		$this->execute($sql, [$userId]);
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

266
		/** @scrutinizer ignore-deprecated */ $this->execute($sql, [$userId]);

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...
267
	}
268
269
	/**
270
	 * Tests if entity with given ID and user ID exists in the database
271
	 */
272
	public function exists(int $id, string $userId) : bool {
273
		$sql = "SELECT 1 FROM `{$this->getTableName()}` WHERE `id` = ? AND `user_id` = ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

273
		$sql = "SELECT 1 FROM `{/** @scrutinizer ignore-deprecated */ $this->getTableName()}` WHERE `id` = ? AND `user_id` = ?";

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...
274
		$result = $this->execute($sql, [$id, $userId]);
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

274
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$id, $userId]);

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...
275
		return $result->rowCount() > 0;
276
	}
277
278
	/**
279
	 * Count all entities of a user
280
	 */
281
	public function count(string $userId) : int {
282
		$sql = "SELECT COUNT(*) AS count FROM `{$this->getTableName()}` WHERE `user_id` = ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

282
		$sql = "SELECT COUNT(*) AS count FROM `{/** @scrutinizer ignore-deprecated */ $this->getTableName()}` WHERE `user_id` = ?";

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...
283
		$result = $this->execute($sql, [$userId]);
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

283
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$userId]);

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...
284
		$row = $result->fetch();
285
		return \intval($row['count']);
286
	}
287
288
	/**
289
	 * {@inheritDoc}
290
	 * @see CompatibleMapper::insert()
291
	 * @phpstan-param EntityType $entity
292
	 * @phpstan-return EntityType
293
	 */
294
	public function insert(\OCP\AppFramework\Db\Entity $entity) : \OCP\AppFramework\Db\Entity {
295
		$now = new \DateTime();
296
		$nowStr = $now->format(self::SQL_DATE_FORMAT);
297
		$entity->setCreated($nowStr);
298
		$entity->setUpdated($nowStr);
299
300
		try {
301
			return parent::insert($entity); // @phpstan-ignore-line: no way to tell phpstan that the parent uses the template type
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...xtcloudMapper::insert() 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

301
			return /** @scrutinizer ignore-deprecated */ parent::insert($entity); // @phpstan-ignore-line: no way to tell phpstan that the parent uses the template type

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...
302
		} catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
303
			throw new UniqueConstraintViolationException($e->getMessage(), $e->getCode(), $e);
304
		} catch (\OCP\DB\Exception $e) {
305
			// Nextcloud 21+
306
			if ($e->getReason() == \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
307
				throw new UniqueConstraintViolationException($e->getMessage(), $e->getCode(), $e);
308
			} else {
309
				throw $e;
310
			}
311
		}
312
	}
313
314
	/**
315
	 * {@inheritDoc}
316
	 * @see CompatibleMapper::update()
317
	 * @phpstan-param EntityType $entity
318
	 * @phpstan-return EntityType
319
	 */
320
	public function update(\OCP\AppFramework\Db\Entity $entity) : \OCP\AppFramework\Db\Entity {
321
		$now = new \DateTime();
322
		$entity->setUpdated($now->format(self::SQL_DATE_FORMAT));
323
		return parent::update($entity); // @phpstan-ignore-line: no way to tell phpstan that the parent uses the template type
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...xtcloudMapper::update() 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

323
		return /** @scrutinizer ignore-deprecated */ parent::update($entity); // @phpstan-ignore-line: no way to tell phpstan that the parent uses the template type

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...
324
	}
325
326
	/**
327
	 * Insert an entity, or if an entity with the same identity already exists,
328
	 * update the existing entity.
329
	 * Note: The functions insertOrUpate and updateOrInsert get the exactly same thing done. The only difference is
330
	 * that the former is optimized for cases where the entity doens't exist and the latter for cases where it does exist.
331
	 * @return Entity The inserted or updated entity, containing also the id field
332
	 * @phpstan-param EntityType $entity
333
	 * @phpstan-return EntityType
334
	 */
335
	public function insertOrUpdate(Entity $entity) : Entity {
336
		try {
337
			return $this->insert($entity);
338
		} catch (UniqueConstraintViolationException $ex) {
339
			$existingEntity = $this->findUniqueEntity($entity);
340
			$entity->setId($existingEntity->getId());
341
			$entity->setCreated($existingEntity->getCreated());
342
			return $this->update($entity);
343
		}
344
	}
345
346
	/**
347
	 * Update an entity whose unique constraint fields match the given entity. If such entity is not found,
348
	 * a new entity is inserted.
349
	 * Note: The functions insertOrUpate and updateOrInsert get the exactly same thing done. The only difference is
350
	 * that the former is optimized for cases where the entity doens't exist and the latter for cases where it does exist.
351
	 * @return Entity The inserted or updated entity, containing also the id field
352
	 * @phpstan-param EntityType $entity
353
	 * @phpstan-return EntityType
354
	 */
355
	public function updateOrInsert(Entity $entity) : Entity {
356
		try {
357
			$existingEntity = $this->findUniqueEntity($entity);
358
			$entity->setId($existingEntity->getId());
359
			return $this->update($entity);
360
		} catch (DoesNotExistException $ex) {
361
			try {
362
				return $this->insert($entity);
363
			} catch (UniqueConstraintViolationException $ex) {
364
				// the conflicting entry didn't exist an eyeblink ago but now it does
365
				// => this is essentially a concurrent update and it is anyway non-deterministic, which
366
				//    update happens last; cancel this update
367
				return $this->findUniqueEntity($entity);
368
			}
369
		}
370
	}
371
372
	/**
373
	 * Set the "starred" column of the given entities
374
	 * @param \DateTime|null $date
375
	 * @param integer[] $ids
376
	 * @param string $userId
377
	 * @return int number of modified entities
378
	 */
379
	public function setStarredDate(?\DateTime $date, array $ids, string $userId) : int {
380
		$count = \count($ids);
381
		if (!empty($date)) {
382
			$date = $date->format(self::SQL_DATE_FORMAT);
383
		}
384
385
		$sql = "UPDATE `{$this->getTableName()}` SET `starred` = ?
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

385
		$sql = "UPDATE `{/** @scrutinizer ignore-deprecated */ $this->getTableName()}` SET `starred` = ?

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...
386
				WHERE `id` IN {$this->questionMarks($count)} AND `user_id` = ?";
387
		$params = \array_merge([$date], $ids, [$userId]);
388
		return $this->execute($sql, $params)->rowCount();
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

388
		return /** @scrutinizer ignore-deprecated */ $this->execute($sql, $params)->rowCount();

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...
389
	}
390
391
	public function latestInsertTime(string $userId) : ?\DateTime {
392
		$sql = "SELECT MAX(`{$this->getTableName()}`.`created`) FROM `{$this->getTableName()}` WHERE `user_id` = ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

392
		$sql = "SELECT MAX(`{$this->getTableName()}`.`created`) FROM `{/** @scrutinizer ignore-deprecated */ $this->getTableName()}` WHERE `user_id` = ?";

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...
393
		$result = $this->execute($sql, [$userId]);
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

393
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$userId]);

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...
394
		$createdTime = $result->fetch(\PDO::FETCH_COLUMN);
395
396
		return ($createdTime === null) ? null : new \DateTime($createdTime);
397
	}
398
399
	public function latestUpdateTime(string $userId) : ?\DateTime {
400
		$sql = "SELECT MAX(`{$this->getTableName()}`.`updated`) FROM `{$this->getTableName()}` WHERE `user_id` = ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

400
		$sql = "SELECT MAX(`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`updated`) FROM `{$this->getTableName()}` WHERE `user_id` = ?";

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...
401
		$result = $this->execute($sql, [$userId]);
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

401
		$result = /** @scrutinizer ignore-deprecated */ $this->execute($sql, [$userId]);

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...
402
		$createdTime = $result->fetch(\PDO::FETCH_COLUMN);
403
404
		return ($createdTime === null) ? null : new \DateTime($createdTime);
405
	}
406
407
	/**
408
	 * helper creating a string like '(?,?,?)' with the specified number of elements
409
	 */
410
	protected function questionMarks(int $count) : string {
411
		$questionMarks = [];
412
		for ($i = 0; $i < $count; $i++) {
413
			$questionMarks[] = '?';
414
		}
415
		return '(' . \implode(',', $questionMarks) . ')';
416
	}
417
418
	/**
419
	 * Build a SQL SELECT statement which selects all entities of the given user,
420
	 * and optionally applies other conditions, too.
421
	 * This is built upon `selectEntities` which may be overridden by the derived class.
422
	 * @param string|null $condition Optional extra condition. This will get automatically
423
	 *                               prefixed with ' AND ', so don't include that.
424
	 * @param string|null $extension Any extension (e.g. ORDER BY, LIMIT) to be added after
425
	 *                               the conditions in the SQL statement
426
	 */
427
	protected function selectUserEntities(string $condition=null, string $extension=null) : string {
428
		$allConditions = "`{$this->getTableName()}`.`user_id` = ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

428
		$allConditions = "`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`user_id` = ?";

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...
429
430
		if (!empty($condition)) {
431
			$allConditions .= " AND ($condition)";
432
		}
433
434
		return $this->selectEntities($allConditions, $extension);
435
	}
436
437
	/**
438
	 * Build a SQL SELECT statement which selects all entities matching the given condition.
439
	 * The derived class may override this if necessary.
440
	 * @param string $condition This will get automatically prefixed with ' WHERE '
441
	 * @param string|null $extension Any extension (e.g. ORDER BY, LIMIT) to be added after
442
	 *                               the conditions in the SQL statement
443
	 */
444
	protected function selectEntities(string $condition, string $extension=null) : string {
445
		return "SELECT * FROM `{$this->getTableName()}` WHERE $condition $extension ";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

445
		return "SELECT * FROM `{/** @scrutinizer ignore-deprecated */ $this->getTableName()}` WHERE $condition $extension ";

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...
446
	}
447
448
	/**
449
	 * @return array with two values: The SQL condition as string and the SQL parameters as string[]
450
	 */
451
	protected function formatTimestampConditions(?string $createdMin, ?string $createdMax, ?string $updatedMin, ?string $updatedMax) : array {
452
		$conditions = [];
453
		$params = [];
454
455
		if (!empty($createdMin)) {
456
			$conditions[] = "`{$this->getTableName()}`.`created` >= ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

456
			$conditions[] = "`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`created` >= ?";

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...
457
			$params[] = $createdMin;
458
		}
459
460
		if (!empty($createdMax)) {
461
			$conditions[] = "`{$this->getTableName()}`.`created` <= ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

461
			$conditions[] = "`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`created` <= ?";

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...
462
			$params[] = $createdMax;
463
		}
464
465
		if (!empty($updatedMin)) {
466
			$conditions[] = "`{$this->getTableName()}`.`updated` >= ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

466
			$conditions[] = "`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`updated` >= ?";

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...
467
			$params[] = $updatedMin;
468
		}
469
470
		if (!empty($updatedMax)) {
471
			$conditions[] = "`{$this->getTableName()}`.`updated` <= ?";
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

471
			$conditions[] = "`{/** @scrutinizer ignore-deprecated */ $this->getTableName()}`.`updated` <= ?";

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...
472
			$params[] = $updatedMax;
473
		}
474
475
		return [\implode(' AND ', $conditions), $params];
476
	}
477
478
	/**
479
	 * Convert given sorting condition to an SQL clause. Derived class may overide this if necessary.
480
	 * @param int $sortBy One of the constants defined in the class SortBy
481
	 */
482
	protected function formatSortingClause(int $sortBy, bool $invertSort = false) : ?string {
483
		$table = $this->getTableName();
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

483
		$table = /** @scrutinizer ignore-deprecated */ $this->getTableName();

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...
484
		if ($sortBy == SortBy::Name) {
485
			$dir = $invertSort ? 'DESC' : 'ASC';
486
			return "ORDER BY LOWER(`$table`.`{$this->nameColumn}`) $dir";
487
		} elseif ($sortBy == SortBy::Newest) {
488
			$dir = $invertSort ? 'ASC' : 'DESC';
489
			return "ORDER BY `$table`.`id` $dir"; // abuse the fact that IDs are ever-incrementing values
490
		} elseif ($sortBy == SortBy::Rating) {
491
			if (\property_exists($this->entityClass, 'rating')) {
492
				$dir = $invertSort ? 'ASC' : 'DESC';
493
				return "ORDER BY `$table`.`rating` $dir";
494
			} else {
495
				return null;
496
			}
497
		} else {
498
			return null;
499
		}
500
	}
501
502
	protected static function prepareSubstringSearchPattern(string $input) : string {
503
		// possibly multiparted query enclosed in quotation marks is handled as a single substring,
504
		// while the default interpretation of multipart string is that each of the parts can be found
505
		// separately as substring in the given order
506
		if (Util::startsWith($input, '"') && Util::endsWith($input, '"')) {
507
			// remove the quotation
508
			$pattern = \substr($input, 1, -1);
509
		} else {
510
			// split to parts by whitespace
511
			$parts = \preg_split('/\s+/', $input, -1, PREG_SPLIT_NO_EMPTY);
512
			// glue the parts back together with a wildcard charater
513
			$pattern = \implode('%', $parts);
514
		}
515
		return "%$pattern%";
516
	}
517
518
	/**
519
	 * Format SQL operator and parameter matching the given advanced search operator.
520
	 * @return array like ['op' => string, 'param' => string]
521
	 */
522
	protected function advFormatSqlOperator(string $ruleOperator, string $ruleInput, string $userId) {
523
		switch ($ruleOperator) {
524
			case 'contain':		return ['op' => 'LIKE',						'param' => "%$ruleInput%"];
525
			case 'notcontain':	return ['op' => 'NOT LIKE',					'param' => "%$ruleInput%"];
526
			case 'start':		return ['op' => 'LIKE',						'param' => "$ruleInput%"];
527
			case 'end':			return ['op' => 'LIKE',						'param' => "%$ruleInput"];
528
			case 'is':			return ['op' => '=',						'param' => "$ruleInput"];
529
			case 'isnot':		return ['op' => '!=',						'param' => "$ruleInput"];
530
			case 'sounds':		return ['op' => 'SOUNDS LIKE',				'param' => $ruleInput]; // MySQL-specific syntax
531
			case 'notsounds':	return ['op' => 'NOT SOUNDS LIKE',			'param' => $ruleInput]; // MySQL-specific syntax
532
			case 'regexp':		return ['op' => 'REGEXP',					'param' => $ruleInput]; // MySQL-specific syntax
533
			case 'notregexp':	return ['op' => 'NOT REGEXP',				'param' => $ruleInput]; // MySQL-specific syntax
534
			case 'true':		return ['op' => 'IS NOT NULL',				'param' => null];
535
			case 'false':		return ['op' => 'IS NULL',					'param' => null];
536
			case 'equal':		return ['op' => '',							'param' => $ruleInput];
537
			case 'ne':			return ['op' => 'NOT',						'param' => $ruleInput];
538
			case 'limit':		return ['op' => (string)(int)$ruleInput,	'param' => $userId];	// this is a bit hacky, userId needs to be passed as an SQL param while simple sanitation suffices for the limit
539
			default:			return ['op' => $ruleOperator,				'param' => $ruleInput]; // all numerical operators fall here
540
		}
541
	}
542
543
	/**
544
	 * Format SQL condition matching the given advanced search rule and SQL operator.
545
	 * Derived classes should override this to provide support for table-specific rules.
546
	 */
547
	protected function advFormatSqlCondition(string $rule, string $sqlOp) : string {
548
		$table = $this->getTableName();
0 ignored issues
show
Deprecated Code introduced by
The function OCA\Music\AppFramework\D...dMapper::getTableName() 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

548
		$table = /** @scrutinizer ignore-deprecated */ $this->getTableName();

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...
549
		$nameCol = $this->nameColumn;
550
551
		switch ($rule) {
552
			case 'title':			return "LOWER(`$table`.`$nameCol`) $sqlOp LOWER(?)";
553
			case 'my_flagged':		return "`$table`.`starred` $sqlOp";
554
			case 'favorite':		return "(LOWER(`$table`.`$nameCol`) $sqlOp LOWER(?) AND `$table`.`starred` IS NOT NULL)"; // title search among flagged
555
			case 'myrating':		// fall throuhg, we provide no access to other people's data
556
			case 'rating':			return "`$table`.`rating` $sqlOp ?";
557
			case 'added':			return "`$table`.`created` $sqlOp ?";
558
			case 'updated':			return "`$table`.`updated` $sqlOp ?";
559
			case 'mbid':			return "`$table`.`mbid` $sqlOp ?";
560
			case 'recent_added':	return "`$table`.`id` IN (SELECT * FROM (SELECT `id` FROM `$table` WHERE `user_id` = ? ORDER BY `created` DESC LIMIT $sqlOp) mysqlhack)";
561
			case 'recent_updated':	return "`$table`.`id` IN (SELECT * FROM (SELECT `id` FROM `$table` WHERE `user_id` = ? ORDER BY `updated` DESC LIMIT $sqlOp) mysqlhack)";
562
			default:				throw new \DomainException("Rule '$rule' not supported on this entity type");
563
		}
564
	}
565
566
	/**
567
	 * Find an entity which has the same identity as the supplied entity.
568
	 * How the identity of the entity is defined, depends on the derived concrete class.
569
	 * @phpstan-param EntityType $entity
570
	 * @phpstan-return EntityType
571
	 */
572
	abstract protected function findUniqueEntity(Entity $entity) : Entity;
573
}
574