Passed
Push — master ( 40b137...b03450 )
by Pauli
02:02
created

TrackMapper::makeSelectQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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