| 94 | | query = """SELECT audio_song.id AS id, |
| 95 | | audio_song.path AS path, |
| 96 | | audio_artist.name AS artist, |
| 97 | | audio_song.title AS title, |
| 98 | | audio_album.title AS album, |
| 99 | | audio_song.track_no AS track, |
| 100 | | audio_genre.name AS genre, |
| 101 | | audio_song.time AS time |
| 102 | | FROM audio_song, audio_artist, audio_album, audio_genre |
| 103 | | WHERE audio_song.artist_id = audio_artist.id AND |
| | 99 | query = """ |
| | 100 | SELECT audio_song.id AS id, |
| | 101 | audio_song.path AS path, |
| | 102 | audio_artist.name AS artist, |
| | 103 | audio_song.title AS title, |
| | 104 | audio_album.title AS album, |
| | 105 | audio_song.track_no AS track, |
| | 106 | audio_genre.name AS genre, |
| | 107 | audio_song.time AS time |
| | 108 | FROM audio_song, audio_artist, audio_album, audio_genre |
| | 109 | WHERE audio_song.genre_id = (SELECT audio_genre.id FROM audio_genre WHERE audio_genre.name = ?) AND |
| 106 | | audio_song.artist_id = (SELECT id FROM audio_genre WHERE name = '%s') |
| 107 | | ORDER BY audio_album.title, audio_song.title;""" |
| 108 | | return self._iDB.execute(query%(aGenre)) |
| 109 | | |
| 110 | | |
| 111 | | |
| 112 | | |
| 113 | | |
| 114 | | |
| | 112 | audio_artist.id = audio_song.artist_id |
| | 113 | ORDER BY audio_album.title, audio_song.title; |
| | 114 | """ |
| | 115 | return self._iDB.execute(query, (aGenre,)) |
| | 116 | |
| | 117 | |
| | 118 | ## Add artist if not exists. |
| | 119 | # @param self The object pointer. |
| | 120 | def addArtist(self, aArtist): |
| | 121 | print "AddArtist: ", aArtist |
| | 122 | query = """INSERT INTO audio_artist(name) |
| | 123 | SELECT ? |
| | 124 | FROM mutex |
| | 125 | LEFT OUTER JOIN audio_artist |
| | 126 | ON audio_artist.name = ? |
| | 127 | WHERE mutex.i = 1 AND audio_artist.name IS NULL;""" |
| | 128 | return DBDriver().execute(query, (aArtist.title(), aArtist.title())) |
| | 129 | |
| | 130 | |
| | 131 | ## Add album if not exists. |
| | 132 | # @param self The object pointer. |
| | 133 | def addAlbum(self, aAlbum): |
| | 134 | query = """INSERT INTO audio_album(title) |
| | 135 | SELECT ? |
| | 136 | FROM mutex |
| | 137 | LEFT OUTER JOIN audio_album |
| | 138 | ON audio_album.title = ? |
| | 139 | WHERE mutex.i = 1 AND audio_album.title IS NULL;""" |
| | 140 | return DBDriver().execute(query, (aAlbum.title(), aAlbum.title())) |
| | 141 | |
| 152 | | values('%s', '%s')""" |
| 153 | | return self._iDB.execute(query%(aPlaylistId, aSongId)) |
| | 182 | values(?, ?)""" |
| | 183 | return self._iDB.execute(query, (aPlaylistId, aSongId)) |
| | 184 | |
| | 185 | |
| | 186 | ## Insert a whole artist into a playlist. |
| | 187 | def addArtistToPlaylist(self, aArtist, aPlaylist): |
| | 188 | query = """INSERT INTO audio_playlist_data(playlist_id, song_id) |
| | 189 | SELECT audio_playlist.id, audio_song.id |
| | 190 | FROM audio_playlist, audio_song |
| | 191 | WHERE audio_song.artist_id IN (SELECT audio_artist.id FROM audio_artist WHERE |
| | 192 | audio_artist.name = ?) AND audio_playlist.name = ?;""" |
| | 193 | return self._iDB.execute(query, (aArtist, aPlaylist)) |
| | 194 | |
| | 195 | |
| | 196 | ## Insert a whole album into a playlist. |
| | 197 | def addAlbumToPlaylist(self, aAlbum, aPlaylist): |
| | 198 | print "aAlbum: ", aAlbum |
| | 199 | print "aPlaylist: ", aPlaylist |
| | 200 | query = """INSERT INTO audio_playlist_data(playlist_id, song_id) |
| | 201 | SELECT audio_playlist.id, audio_song.id |
| | 202 | FROM audio_playlist, audio_song |
| | 203 | WHERE audio_song.album_id IN (SELECT audio_album.id FROM audio_album WHERE |
| | 204 | audio_album.title = ?) AND audio_playlist.name = ?;""" |
| | 205 | return self._iDB.execute(query, (aAlbum, aPlaylist)) |
| | 206 | |
| | 207 | |
| | 208 | ## Insert a whole genre into a playlist |
| | 209 | def addGenreToPlaylist(self, aGenre, aPlaylist): |
| | 210 | query = """INSERT INTO audio_playlist_data(playlist_id, song_id) |
| | 211 | SELECT audio_playlist.id, audio_song.id |
| | 212 | FROM audio_playlist, audio_song |
| | 213 | WHERE audio_song.genre_id IN (SELECT audio_genre.id FROM audio_genre WHERE |
| | 214 | audio_genre.name = ?) AND audio_playlist.name = ?;""" |
| | 215 | return self._iDB.execute(query,(aGenre, aPlaylist)) |
| | 216 | |
| | 217 | |
| | 218 | ## Delete a playlist |
| | 219 | # @note: since SQLite doesn't really deal with foreign keys, we can't just |
| | 220 | # use an ON DELETE CASCADE in the table creation, so we have to delete |
| | 221 | # all entries manually |
| | 222 | def deletePlaylist(self, aPlaylist): |
| | 223 | query = """DELETE FROM audio_playlist_data WHERE audio_playlist_id = ?; |
| | 224 | DELETE FROM audio_playlist WHERE id = ?""" |
| | 225 | return self._iDB.execute(query, (aPlaylist, aPlaylist)) |
| | 226 | |
| | 227 | |
| | 228 | ## Add song to database |
| | 229 | def addSong(self, aPath, aTitle, aArtist, aAlbum, aGenre, aTrackNb=None, aTime=None): |
| | 230 | query = """ INSERT INTO audio_artist(name) VALUES (?) WHERE NOT EXISTS((SELECT name FROM audio_artist WHERE name=?))""" |
| | 231 | aArtist |
| | 232 | aAlbum, |
| | 233 | |
| | 234 | query = 'INSERT INTO audio_song(path, title, artist_id, album_id, genre_id, ' |
| | 235 | if aTrackNb: |
| | 236 | query += ', track_no' |
| | 237 | if aTime: |
| | 238 | query += ', time' |
| | 239 | query += ')' |
| | 240 | query += """VALUES( ?, ?, """ % (aPath, aTitle) |
| | 241 | query += """(SELECT id FROM audio_artist WHERE name=?), """ % (aArtist) |
| | 242 | query += """(SELECT id FROM audio_album WHERE title=?), """ % (aAlbum) |
| | 243 | query += """(SELECT id FROM audio_genre WHERE name=?), """ % (aGenre) |
| | 244 | if aTrackNb: |
| | 245 | query += """'%d', """ |
| | 246 | if aTime: |
| | 247 | query += """'%d')""" |
| | 248 | return self._iDB.execute(query) |