Passed
Push — feature/329_Subsonic_API ( a9dee6...9d1353 )
by Pauli
14:33
created

TrackMapper::findAllByFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
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 Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2016 - 2019
13
 */
14
15
namespace OCA\Music\Db;
16
17
use OCP\IDBConnection;
0 ignored issues
show
Bug introduced by
The type OCP\IDBConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class TrackMapper extends BaseMapper {
20
	public function __construct(IDBConnection $db) {
21
		parent::__construct($db, 'music_tracks', '\OCA\Music\Db\Track');
22
	}
23
24
	/**
25
	 * @param string $condition
26
	 */
27
	private function makeSelectQueryWithoutUserId($condition) {
28
		return 'SELECT `track`.*, `file`.`name` AS `filename`, `file`.`size` '.
29
				'FROM `*PREFIX*music_tracks` `track` '.
30
				'INNER JOIN `*PREFIX*filecache` `file` '.
31
				'ON `track`.`file_id` = `file`.`fileid` '.
32
				'WHERE ' . $condition;
33
	}
34
35
	/**
36
	 * @param string $condition
37
	 */
38
	private function makeSelectQuery($condition=null) {
39
		return $this->makeSelectQueryWithoutUserId('`track`.`user_id` = ? ' . $condition);
40
	}
41
42
	/**
43
	 * @param string $userId
44
	 * @param SortBy $sortBy sort order of the result set
45
	 * @param integer $limit
46
	 * @param integer $offset
47
	 * @return Track[]
48
	 */
49
	public function findAll($userId, $sortBy=SortBy::None, $limit=null, $offset=null) {
50
		$sql = $this->makeSelectQuery(
51
				$sortBy == SortBy::Name ? 'ORDER BY LOWER(`track`.`title`)' : null);
52
		$params = [$userId];
53
		return $this->findEntities($sql, $params, $limit, $offset);
54
	}
55
56
	/**
57
	 * @param integer $artistId
58
	 * @param string $userId
59
	 * @return Track[]
60
	 */
61
	public function findAllByArtist($artistId, $userId) {
62
		$sql = $this->makeSelectQuery('AND `track`.`artist_id` = ? '.
63
			'ORDER BY LOWER(`track`.`title`)');
64
		$params = [$userId, $artistId];
65
		return $this->findEntities($sql, $params);
66
	}
67
68
	/**
69
	 * @param integer $albumId
70
	 * @param string $userId
71
	 * @param integer $artistId
72
	 * @return Track[]
73
	 */
74
	public function findAllByAlbum($albumId, $userId, $artistId = null) {
75
		$sql = $this->makeSelectQuery('AND `track`.`album_id` = ? ');
76
		$params = [$userId, $albumId];
77
		if ($artistId !== null) {
78
			$sql .= 'AND `track`.`artist_id` = ? ';
79
			\array_push($params, $artistId);
80
		}
81
		$sql .= 'ORDER BY `track`.`number`, LOWER(`track`.`title`)';
82
		return $this->findEntities($sql, $params);
83
	}
84
85
	/**
86
	 * @param integer $folderId
87
	 * @param string $userId
88
	 * @return Track[]
89
	 */
90
	public function findAllByFolder($folderId, $userId) {
91
		$sql = $this->makeSelectQuery('AND `file`.`parent` = ? ');
92
		$params = [$userId, $folderId];
93
		$sql .= 'ORDER BY LOWER(`track`.`title`)';
94
		return $this->findEntities($sql, $params);
95
	}
96
97
	/**
98
	 * @param string $userId
99
	 * @return int[]
100
	 */
101
	public function findAllFileIds($userId) {
102
		$sql = 'SELECT `file_id` FROM `*PREFIX*music_tracks` WHERE `user_id` = ?';
103
		$result = $this->execute($sql, [$userId]);
104
105
		return \array_map(function ($i) {
106
			return $i['file_id'];
107
		}, $result->fetchAll());
108
	}
109
110
	/**
111
	 * Find a track of user matching a file ID
112
	 * @param integer $fileId
113
	 * @param string $userId
114
	 * @return Track
115
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
116
	 */
117
	public function findByFileId($fileId, $userId) {
118
		$sql = $this->makeSelectQuery('AND `track`.`file_id` = ?');
119
		$params = [$userId, $fileId];
120
		return $this->findEntity($sql, $params);
121
	}
122
123
	/**
124
	 * Find tracks of user with multiple file IDs
125
	 * @param integer[] $fileIds
126
	 * @param string[] $userIds
127
	 * @return Track[]
128
	 */
129
	public function findByFileIds($fileIds, $userIds) {
130
		$sql = $this->makeSelectQueryWithoutUserId(
131
				'`track`.`user_id` IN ' . $this->questionMarks(\count($userIds)) .
132
				' AND `track`.`file_id` IN '. $this->questionMarks(\count($fileIds)));
133
		$params = \array_merge($userIds, $fileIds);
134
		return $this->findEntities($sql, $params);
135
	}
136
137
	/**
138
	 * Finds tracks of all users matching one or multiple file IDs
139
	 * @param integer[] $fileIds
140
	 * @return Track[]
141
	 */
142
	public function findAllByFileIds($fileIds) {
143
		$sql = $this->makeSelectQueryWithoutUserId('`track`.`file_id` IN '.
144
				$this->questionMarks(\count($fileIds)));
145
		return $this->findEntities($sql, $fileIds);
146
	}
147
148
	/**
149
	 * @param integer $artistId
150
	 * @return integer
151
	 */
152
	public function countByArtist($artistId) {
153
		$sql = 'SELECT COUNT(*) AS count FROM `*PREFIX*music_tracks` `track` '.
154
			'WHERE `track`.`artist_id` = ?';
155
		$result = $this->execute($sql, [$artistId]);
156
		$row = $result->fetch();
157
		return $row['count'];
158
	}
159
160
	/**
161
	 * @param integer $albumId
162
	 * @return integer
163
	 */
164
	public function countByAlbum($albumId) {
165
		$sql = 'SELECT COUNT(*) AS count FROM `*PREFIX*music_tracks` `track` '.
166
			'WHERE `track`.`album_id` = ?';
167
		$result = $this->execute($sql, [$albumId]);
168
		$row = $result->fetch();
169
		return $row['count'];
170
	}
171
172
	/**
173
	 * @param string $name
174
	 * @param string $userId
175
	 * @param bool $fuzzy
176
	 * @param integer $limit
177
	 * @param integer $offset
178
	 * @return Track[]
179
	 */
180
	public function findAllByName($name, $userId, $fuzzy = false, $limit=null, $offset=null) {
181
		if ($fuzzy) {
182
			$condition = 'AND LOWER(`track`.`title`) LIKE LOWER(?) ';
183
			$name = '%' . $name . '%';
184
		} else {
185
			$condition = 'AND `track`.`title` = ? ';
186
		}
187
		$sql = $this->makeSelectQuery($condition . 'ORDER BY LOWER(`track`.`title`)');
188
		$params = [$userId, $name];
189
		return $this->findEntities($sql, $params, $limit, $offset);
190
	}
191
192
	/**
193
	 * @param string $name
194
	 * @param string $userId
195
	 * @return Track[]
196
	 */
197
	public function findAllByNameRecursive($name, $userId) {
198
		$condition = ' AND (`track`.`artist_id` IN (SELECT `id` FROM `*PREFIX*music_artists` WHERE LOWER(`name`) LIKE LOWER(?)) OR '.
199
						' `track`.`album_id` IN (SELECT `id` FROM `*PREFIX*music_albums` WHERE LOWER(`name`) LIKE LOWER(?)) OR '.
200
						' LOWER(`track`.`title`) LIKE LOWER(?) ) ORDER BY LOWER(`track`.`title`)';
201
		$sql = $this->makeSelectQuery($condition);
202
		$name = '%' . $name . '%';
203
		$params = [$userId, $name, $name, $name];
204
		return $this->findEntities($sql, $params);
205
	}
206
207
	/**
208
	 * Finds all track IDs of the user along with the parent folder ID of each track
209
	 * @param string $userId
210
	 * @return array where keys are folder IDs and values are arrays of track IDs
211
	 */
212
	public function findTrackAndFolderIds($userId) {
213
		$sql = 'SELECT `track`.`id` AS id, `file`.`parent` AS parent '.
214
				'FROM `*PREFIX*music_tracks` `track` '.
215
				'JOIN `*PREFIX*filecache` `file` '.
216
				'ON `track`.`file_id` = `file`.`fileid` '.
217
				'WHERE `track`.`user_id` = ?';
218
219
		$rows = $this->execute($sql, [$userId])->fetchAll();
220
221
		$result = [];
222
		foreach ($rows as $row) {
223
			$result[$row['parent']][] = $row['id'];
224
		}
225
226
		return $result;
227
	}
228
229
	/**
230
	 * Find names and paths of the file system nodes with given IDs within the given storage
231
	 * @param int[] $nodeIds
232
	 * @param string $storageId
233
	 * @return array where keys are the node IDs and values are associative arrays
234
	 *         like { 'name' => string, 'path' => string };
235
	 */
236
	public function findNodeNamesAndPaths($nodeIds, $storageId) {
237
		$result = [];
238
239
		if (!empty($nodeIds)) {
240
			$sql = 'SELECT `fileid`, `name`, `path` '.
241
					'FROM `*PREFIX*filecache` `filecache` '.
242
					'JOIN `*PREFIX*storages` `storages` '.
243
					'ON `filecache`.`storage` = `storages`.`numeric_id` '.
244
					'WHERE `storages`.`id` = ? '.
245
					'AND `filecache`.`fileid` IN '. $this->questionMarks(\count($nodeIds));
246
247
			$rows = $this->execute($sql, \array_merge([$storageId], $nodeIds))->fetchAll();
248
249
			foreach ($rows as $row) {
250
				$result[$row['fileid']] = [
251
					'name' => $row['name'],
252
					'path' => $row['path']
253
				];
254
			}
255
		}
256
257
		return $result;
258
	}
259
260
	public function findUniqueEntity(Track $track) {
261
		return $this->findByFileId($track->getFileId(), $track->getUserId());
262
	}
263
}
264