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
|
|
|
* @copyright Morris Jobke 2013, 2014 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace OCA\Music\Db; |
14
|
|
|
|
15
|
|
|
use OCP\AppFramework\Db\Mapper; |
16
|
|
|
use OCP\IDb; |
17
|
|
|
|
18
|
|
|
class TrackMapper extends Mapper { |
19
|
|
|
|
20
|
|
|
public function __construct(IDb $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`.`title`, `track`.`number`, `track`.`id`, '. |
29
|
|
|
'`track`.`artist_id`, `track`.`album_id`, `track`.`length`, '. |
30
|
|
|
'`track`.`file_id`, `track`.`bitrate`, `track`.`mimetype`, '. |
31
|
|
|
'`track`.`mbid`, `track`.`added`, `track`.`updated` '. |
32
|
|
|
'FROM `*PREFIX*music_tracks` `track` '. |
33
|
|
|
'WHERE ' . $condition; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $condition |
38
|
|
|
*/ |
39
|
|
|
private function makeSelectQuery($condition=null){ |
40
|
|
|
return $this->makeSelectQueryWithoutUserId('`track`.`user_id` = ? ' . $condition); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $userId |
45
|
|
|
* @param integer $limit |
46
|
|
|
* @param integer $offset |
47
|
|
|
* @return Track[] |
48
|
|
|
*/ |
49
|
|
|
public function findAll($userId, $limit=null, $offset=null){ |
50
|
|
|
$sql = $this->makeSelectQuery('ORDER BY LOWER(`track`.`title`)'); |
51
|
|
|
$params = array($userId); |
52
|
|
|
return $this->findEntities($sql, $params, $limit, $offset); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param integer $artistId |
57
|
|
|
* @param string $userId |
58
|
|
|
* @return Track[] |
59
|
|
|
*/ |
60
|
|
|
public function findAllByArtist($artistId, $userId){ |
61
|
|
|
$sql = $this->makeSelectQuery('AND `track`.`artist_id` = ? '. |
62
|
|
|
'ORDER BY LOWER(`track`.`title`)'); |
63
|
|
|
$params = array($userId, $artistId); |
64
|
|
|
return $this->findEntities($sql, $params); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param integer $albumId |
69
|
|
|
* @param string $userId |
70
|
|
|
* @param integer $artistId |
71
|
|
|
* @return Track[] |
72
|
|
|
*/ |
73
|
|
|
public function findAllByAlbum($albumId, $userId, $artistId = null){ |
74
|
|
|
$sql = $this->makeSelectQuery('AND `track`.`album_id` = ? '); |
75
|
|
|
$params = array($userId, $albumId); |
76
|
|
|
if($artistId !== null) { |
77
|
|
|
$sql .= 'AND `track`.`artist_id` = ? '; |
78
|
|
|
array_push($params, $artistId); |
79
|
|
|
} |
80
|
|
|
$sql .= 'ORDER BY LOWER(`track`.`title`)'; |
81
|
|
|
return $this->findEntities($sql, $params); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param integer $id |
86
|
|
|
* @param string $userId |
87
|
|
|
* @return Track |
88
|
|
|
*/ |
89
|
|
|
public function find($id, $userId){ |
90
|
|
|
$sql = $this->makeSelectQuery('AND `track`.`id` = ?'); |
91
|
|
|
$params = array($userId, $id); |
92
|
|
|
return $this->findEntity($sql, $params); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param integer $fileId |
97
|
|
|
* @param string $userId |
98
|
|
|
* @return Track |
99
|
|
|
*/ |
100
|
|
|
public function findByFileId($fileId, $userId){ |
101
|
|
|
$sql = $this->makeSelectQuery('AND `track`.`file_id` = ?'); |
102
|
|
|
$params = array($userId, $fileId); |
103
|
|
|
return $this->findEntity($sql, $params); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param integer $fileId |
108
|
|
|
* @return Track[] |
109
|
|
|
*/ |
110
|
|
|
public function findAllByFileId($fileId){ |
111
|
|
|
$sql = $this->makeSelectQueryWithoutUserId('`track`.`file_id` = ? '. |
112
|
|
|
'ORDER BY LOWER(`track`.`title`)'); |
113
|
|
|
$params = array($fileId); |
114
|
|
|
return $this->findEntities($sql, $params); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param integer $artistId |
119
|
|
|
* @param string $userId |
120
|
|
|
* @return integer |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public function countByArtist($artistId, $userId){ |
123
|
|
|
$sql = 'SELECT COUNT(*) AS count FROM `*PREFIX*music_tracks` `track` '. |
124
|
|
|
'WHERE `track`.`user_id` = ? AND `track`.`artist_id` = ?'; |
125
|
|
|
$params = array($userId, $artistId); |
126
|
|
|
$result = $this->execute($sql, $params); |
127
|
|
|
$row = $result->fetch(); |
128
|
|
|
return $row['count']; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param integer $albumId |
133
|
|
|
* @param string $userId |
134
|
|
|
* @return integer |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function countByAlbum($albumId, $userId){ |
137
|
|
|
$sql = 'SELECT COUNT(*) AS count FROM `*PREFIX*music_tracks` `track` '. |
138
|
|
|
'WHERE `track`.`user_id` = ? AND `track`.`album_id` = ?'; |
139
|
|
|
$params = array($userId, $albumId); |
140
|
|
|
$result = $this->execute($sql, $params); |
141
|
|
|
$row = $result->fetch(); |
142
|
|
|
return $row['count']; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $userId |
147
|
|
|
* @return integer |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function count($userId){ |
150
|
|
|
$sql = 'SELECT COUNT(*) AS count FROM `*PREFIX*music_tracks` '. |
151
|
|
|
'WHERE `user_id` = ?'; |
152
|
|
|
$params = array($userId); |
153
|
|
|
$result = $this->execute($sql, $params); |
154
|
|
|
$row = $result->fetch(); |
155
|
|
|
return $row['count']; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $name |
160
|
|
|
* @param string $userId |
161
|
|
|
* @param bool $fuzzy |
162
|
|
|
* @return Track[] |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
public function findAllByName($name, $userId, $fuzzy = false){ |
165
|
|
|
if ($fuzzy) { |
166
|
|
|
$condition = 'AND LOWER(`track`.`title`) LIKE LOWER(?) '; |
167
|
|
|
$name = '%' . $name . '%'; |
168
|
|
|
} else { |
169
|
|
|
$condition = 'AND `track`.`title` = ? '; |
170
|
|
|
} |
171
|
|
|
$sql = $this->makeSelectQuery($condition . 'ORDER BY LOWER(`track`.`title`)'); |
172
|
|
|
$params = array($userId, $name); |
173
|
|
|
return $this->findEntities($sql, $params); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $name |
178
|
|
|
* @param string $userId |
179
|
|
|
* @return Track[] |
180
|
|
|
*/ |
181
|
|
|
public function findAllByNameRecursive($name, $userId){ |
182
|
|
|
$condition = ' AND (`track`.`artist_id` IN (SELECT `id` FROM `*PREFIX*music_artists` WHERE LOWER(`name`) LIKE LOWER(?)) OR '. |
183
|
|
|
' `track`.`album_id` IN (SELECT `id` FROM `*PREFIX*music_albums` WHERE LOWER(`name`) LIKE LOWER(?)) OR '. |
184
|
|
|
' LOWER(`track`.`title`) LIKE LOWER(?) ) ORDER BY LOWER(`track`.`title`)'; |
185
|
|
|
$sql = $this->makeSelectQuery($condition); |
186
|
|
|
$name = '%' . $name . '%'; |
187
|
|
|
$params = array($userId, $name, $name, $name); |
188
|
|
|
return $this->findEntities($sql, $params); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $userId |
193
|
|
|
* @return string[] |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
public function lastChange($userId){ |
|
|
|
|
196
|
|
|
$sql = 'SELECT MAX(added) last_added, MAX(updated) last_updated FROM `*PREFIX*music_tracks` '. |
197
|
|
|
'WHERE `user_id` = ?'; |
198
|
|
|
$params = array($userId); |
199
|
|
|
$result = $this->execute($sql, $params); |
200
|
|
|
$row = $result->fetch(); |
201
|
|
|
return $row; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.