Merge pull request #6450 from jonaski/compilations

Fix updating compilations
This commit is contained in:
John Maguire 2019-11-13 18:01:29 +00:00 committed by GitHub
commit 21848d62d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 59 deletions

View File

@ -769,34 +769,20 @@ void LibraryBackend::UpdateCompilations() {
if (album.isEmpty()) continue; if (album.isEmpty()) continue;
// Find the directory the song is in // Find the directory the song is in
int last_separator = filename.lastIndexOf('/'); QUrl url = QUrl::fromEncoded(filename.toUtf8());
if (last_separator == -1) continue; QString directory =
url.toString(QUrl::PreferLocalFile | QUrl::RemoveFilename);
CompilationInfo& info = compilation_info[album]; CompilationInfo& info = compilation_info[directory + album];
info.artists.insert(artist); info.urls << url;
info.directories.insert(filename.left(last_separator)); if (!info.artists.contains(artist)) info.artists << artist;
if (sampler) if (sampler)
info.has_samplers = true; ++info.has_samplers;
else else
info.has_not_samplers = true; ++info.has_not_samplers;
} }
// Now mark the songs that we think are in compilations // Now mark the songs that we think are in compilations
QSqlQuery update(db);
update.prepare(
QString(
"UPDATE %1"
" SET sampler = :sampler,"
" effective_compilation = ((compilation OR :sampler OR "
"forced_compilation_on) AND NOT forced_compilation_off) + 0"
" WHERE album = :album AND unavailable = 0").arg(songs_table_));
QSqlQuery find_songs(db);
find_songs.prepare(
QString(
"SELECT ROWID, " + Song::kColumnSpec +
" FROM %1"
" WHERE album = :album AND sampler = :sampler AND unavailable = 0")
.arg(songs_table_));
SongList deleted_songs; SongList deleted_songs;
SongList added_songs; SongList added_songs;
@ -807,20 +793,18 @@ void LibraryBackend::UpdateCompilations() {
compilation_info.constBegin(); compilation_info.constBegin();
for (; it != compilation_info.constEnd(); ++it) { for (; it != compilation_info.constEnd(); ++it) {
const CompilationInfo& info = it.value(); const CompilationInfo& info = it.value();
QString album(it.key());
// If there were more 'effective album artists' than there were directories // If there were more then one 'effective album artist' for this album
// for this album, // directory then it's a compilation
// then it's a compilation
if (info.artists.count() > info.directories.count()) { for (const QUrl& url : info.urls) {
if (info.has_not_samplers) if (info.artists.count() > 1) { // This directory+album is a compilation.
UpdateCompilations(find_songs, update, deleted_songs, added_songs, if (info.has_not_samplers > 0)
album, 1); UpdateCompilations(db, deleted_songs, added_songs, url, true);
} else { } else {
if (info.has_samplers) if (info.has_samplers > 0)
UpdateCompilations(find_songs, update, deleted_songs, added_songs, UpdateCompilations(db, deleted_songs, added_songs, url, false);
album, 0); }
} }
} }
@ -832,29 +816,43 @@ void LibraryBackend::UpdateCompilations() {
} }
} }
void LibraryBackend::UpdateCompilations(QSqlQuery& find_songs, void LibraryBackend::UpdateCompilations(const QSqlDatabase& db,
QSqlQuery& update,
SongList& deleted_songs, SongList& deleted_songs,
SongList& added_songs, SongList& added_songs, const QUrl& url,
const QString& album, int sampler) { const bool sampler) {
// Get songs that were already in that album, so we can tell the model QSqlQuery find_song(db);
// they've been updated find_song.prepare(QString("SELECT ROWID, " + Song::kColumnSpec +
find_songs.bindValue(":album", album); " FROM %1"
find_songs.bindValue(":sampler", int(!sampler)); " WHERE filename = :filename AND sampler = "
find_songs.exec(); ":sampler AND unavailable = 0")
while (find_songs.next()) { .arg(songs_table_));
QSqlQuery update_song(db);
update_song.prepare(
QString("UPDATE %1"
" SET sampler = :sampler,"
" effective_compilation = ((compilation OR :sampler OR "
"forced_compilation_on) AND NOT forced_compilation_off) + 0"
" WHERE filename = :filename AND unavailable = 0")
.arg(songs_table_));
// Get song, so we can tell the model its updated
find_song.bindValue(":filename", url.toEncoded());
find_song.bindValue(":sampler", int(!sampler));
find_song.exec();
while (find_song.next()) {
Song song; Song song;
song.InitFromQuery(find_songs, true); song.InitFromQuery(find_song, true);
deleted_songs << song; deleted_songs << song;
song.set_sampler(true); song.set_sampler(sampler);
added_songs << song; added_songs << song;
} }
// Mark this album // Update the song
update.bindValue(":sampler", sampler); update_song.bindValue(":sampler", int(sampler));
update.bindValue(":album", album); update_song.bindValue(":filename", url.toEncoded());
update.exec(); update_song.exec();
db_->CheckErrors(update); db_->CheckErrors(update_song);
} }
LibraryBackend::AlbumList LibraryBackend::GetAlbums(const QString& artist, LibraryBackend::AlbumList LibraryBackend::GetAlbums(const QString& artist,

View File

@ -237,20 +237,20 @@ signals:
private: private:
struct CompilationInfo { struct CompilationInfo {
CompilationInfo() : has_samplers(false), has_not_samplers(false) {} CompilationInfo() : has_samplers(0), has_not_samplers(0) {}
QSet<QString> artists; QList<QUrl> urls;
QSet<QString> directories; QStringList artists;
bool has_samplers; int has_samplers;
bool has_not_samplers; int has_not_samplers;
}; };
static const char* kNewScoreSql; static const char* kNewScoreSql;
void UpdateCompilations(QSqlQuery& find_songs, QSqlQuery& update, void UpdateCompilations(const QSqlDatabase& db, SongList& deleted_songs,
SongList& deleted_songs, SongList& added_songs, SongList& added_songs, const QUrl& url,
const QString& album, int sampler); const bool sampler);
AlbumList GetAlbums(const QString& artist, const QString& album_artist, AlbumList GetAlbums(const QString& artist, const QString& album_artist,
bool compilation = false, bool compilation = false,
const QueryOptions& opt = QueryOptions()); const QueryOptions& opt = QueryOptions());