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\IDBConnection; |
16
|
|
|
|
17
|
|
|
class TrackMapper extends BaseMapper { |
18
|
|
|
|
19
|
|
|
public function __construct(IDBConnection $db){ |
20
|
|
|
parent::__construct($db, 'music_tracks', '\OCA\Music\Db\Track'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $condition |
25
|
|
|
*/ |
26
|
|
|
private function makeSelectQueryWithoutUserId($condition){ |
27
|
|
|
return 'SELECT `track`.`title`, `track`.`number`, `track`.`id`, '. |
28
|
|
|
'`track`.`artist_id`, `track`.`album_id`, `track`.`length`, '. |
29
|
|
|
'`track`.`file_id`, `track`.`bitrate`, `track`.`mimetype`, '. |
30
|
|
|
'`track`.`mbid` FROM `*PREFIX*music_tracks` `track` '. |
31
|
|
|
'WHERE ' . $condition; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $condition |
36
|
|
|
*/ |
37
|
|
|
private function makeSelectQuery($condition=null){ |
38
|
|
|
return $this->makeSelectQueryWithoutUserId('`track`.`user_id` = ? ' . $condition); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $userId |
43
|
|
|
* @param integer $limit |
44
|
|
|
* @param integer $offset |
45
|
|
|
* @return Track[] |
46
|
|
|
*/ |
47
|
|
|
public function findAll($userId, $limit=null, $offset=null){ |
48
|
|
|
$sql = $this->makeSelectQuery('ORDER BY LOWER(`track`.`title`)'); |
49
|
|
|
$params = array($userId); |
50
|
|
|
return $this->findEntities($sql, $params, $limit, $offset); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param integer $artistId |
55
|
|
|
* @param string $userId |
56
|
|
|
* @return Track[] |
57
|
|
|
*/ |
58
|
|
|
public function findAllByArtist($artistId, $userId){ |
59
|
|
|
$sql = $this->makeSelectQuery('AND `track`.`artist_id` = ? '. |
60
|
|
|
'ORDER BY LOWER(`track`.`title`)'); |
61
|
|
|
$params = array($userId, $artistId); |
62
|
|
|
return $this->findEntities($sql, $params); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param integer $albumId |
67
|
|
|
* @param string $userId |
68
|
|
|
* @param integer $artistId |
69
|
|
|
* @return Track[] |
70
|
|
|
*/ |
71
|
|
|
public function findAllByAlbum($albumId, $userId, $artistId = null){ |
72
|
|
|
$sql = $this->makeSelectQuery('AND `track`.`album_id` = ? '); |
73
|
|
|
$params = array($userId, $albumId); |
74
|
|
|
if($artistId !== null) { |
75
|
|
|
$sql .= 'AND `track`.`artist_id` = ? '; |
76
|
|
|
array_push($params, $artistId); |
77
|
|
|
} |
78
|
|
|
$sql .= 'ORDER BY LOWER(`track`.`title`)'; |
79
|
|
|
return $this->findEntities($sql, $params); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $userId |
84
|
|
|
* @return int[] |
85
|
|
|
*/ |
86
|
|
|
public function findAllFileIds($userId){ |
87
|
|
|
$sql = 'SELECT `file_id` FROM `*PREFIX*music_tracks` WHERE `user_id` = ?'; |
88
|
|
|
$result = $this->execute($sql, [$userId]); |
89
|
|
|
|
90
|
|
|
return array_map(function($i) { return $i['file_id']; }, $result->fetchAll()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param integer $id |
95
|
|
|
* @param string $userId |
96
|
|
|
* @return Track |
97
|
|
|
*/ |
98
|
|
|
public function find($id, $userId){ |
99
|
|
|
$sql = $this->makeSelectQuery('AND `track`.`id` = ?'); |
100
|
|
|
$params = array($userId, $id); |
101
|
|
|
return $this->findEntity($sql, $params); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param integer $fileId |
106
|
|
|
* @param string $userId |
107
|
|
|
* @return Track |
108
|
|
|
*/ |
109
|
|
|
public function findByFileId($fileId, $userId){ |
110
|
|
|
$sql = $this->makeSelectQuery('AND `track`.`file_id` = ?'); |
111
|
|
|
$params = array($userId, $fileId); |
112
|
|
|
return $this->findEntity($sql, $params); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param integer $fileId |
117
|
|
|
* @return Track[] |
118
|
|
|
*/ |
119
|
|
|
public function findAllByFileId($fileId){ |
120
|
|
|
$sql = $this->makeSelectQueryWithoutUserId('`track`.`file_id` = ? '. |
121
|
|
|
'ORDER BY LOWER(`track`.`title`)'); |
122
|
|
|
$params = array($fileId); |
123
|
|
|
return $this->findEntities($sql, $params); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param integer $artistId |
128
|
|
|
* @param string $userId |
129
|
|
|
* @return integer |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public function countByArtist($artistId, $userId){ |
132
|
|
|
$sql = 'SELECT COUNT(*) AS count FROM `*PREFIX*music_tracks` `track` '. |
133
|
|
|
'WHERE `track`.`user_id` = ? AND `track`.`artist_id` = ?'; |
134
|
|
|
$params = array($userId, $artistId); |
135
|
|
|
$result = $this->execute($sql, $params); |
136
|
|
|
$row = $result->fetch(); |
137
|
|
|
return $row['count']; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param integer $albumId |
142
|
|
|
* @param string $userId |
143
|
|
|
* @return integer |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function countByAlbum($albumId, $userId){ |
146
|
|
|
$sql = 'SELECT COUNT(*) AS count FROM `*PREFIX*music_tracks` `track` '. |
147
|
|
|
'WHERE `track`.`user_id` = ? AND `track`.`album_id` = ?'; |
148
|
|
|
$params = array($userId, $albumId); |
149
|
|
|
$result = $this->execute($sql, $params); |
150
|
|
|
$row = $result->fetch(); |
151
|
|
|
return $row['count']; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $name |
156
|
|
|
* @param string $userId |
157
|
|
|
* @param bool $fuzzy |
158
|
|
|
* @return Track[] |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function findAllByName($name, $userId, $fuzzy = false){ |
161
|
|
|
if ($fuzzy) { |
162
|
|
|
$condition = 'AND LOWER(`track`.`title`) LIKE LOWER(?) '; |
163
|
|
|
$name = '%' . $name . '%'; |
164
|
|
|
} else { |
165
|
|
|
$condition = 'AND `track`.`title` = ? '; |
166
|
|
|
} |
167
|
|
|
$sql = $this->makeSelectQuery($condition . 'ORDER BY LOWER(`track`.`title`)'); |
168
|
|
|
$params = array($userId, $name); |
169
|
|
|
return $this->findEntities($sql, $params); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $name |
174
|
|
|
* @param string $userId |
175
|
|
|
* @return Track[] |
176
|
|
|
*/ |
177
|
|
|
public function findAllByNameRecursive($name, $userId){ |
178
|
|
|
$condition = ' AND (`track`.`artist_id` IN (SELECT `id` FROM `*PREFIX*music_artists` WHERE LOWER(`name`) LIKE LOWER(?)) OR '. |
179
|
|
|
' `track`.`album_id` IN (SELECT `id` FROM `*PREFIX*music_albums` WHERE LOWER(`name`) LIKE LOWER(?)) OR '. |
180
|
|
|
' LOWER(`track`.`title`) LIKE LOWER(?) ) ORDER BY LOWER(`track`.`title`)'; |
181
|
|
|
$sql = $this->makeSelectQuery($condition); |
182
|
|
|
$name = '%' . $name . '%'; |
183
|
|
|
$params = array($userId, $name, $name, $name); |
184
|
|
|
return $this->findEntities($sql, $params); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|