From d0bd242d630946bbea9db8fd2e5aa29f64be710d Mon Sep 17 00:00:00 2001 From: Jonathan Marshall Date: Sun, 1 Dec 2013 22:19:26 +1300 Subject: [musicdb] adds UpdateAlbum and utilize this in the scanner, dropping SetAlbumInfo --- xbmc/interfaces/json-rpc/AudioLibrary.cpp | 2 +- xbmc/music/MusicDatabase.cpp | 207 ++++++++++++++++++---------- xbmc/music/MusicDatabase.h | 22 ++- xbmc/music/infoscanner/MusicInfoScanner.cpp | 7 +- 4 files changed, 159 insertions(+), 79 deletions(-) diff --git a/xbmc/interfaces/json-rpc/AudioLibrary.cpp b/xbmc/interfaces/json-rpc/AudioLibrary.cpp index 7acd916474..f668209131 100644 --- a/xbmc/interfaces/json-rpc/AudioLibrary.cpp +++ b/xbmc/interfaces/json-rpc/AudioLibrary.cpp @@ -465,7 +465,7 @@ JSONRPC_STATUS CAudioLibrary::SetAlbumDetails(const CStdString &method, ITranspo if (ParameterNotNull(parameterObject, "year")) album.iYear = (int)parameterObject["year"].asInteger(); - if (musicdatabase.SetAlbumInfo(id, album, album.infoSongs) <= 0) + if (musicdatabase.UpdateAlbum(album) <= 0) return InternalError; CJSONRPCUtils::NotifyItemUpdated(); diff --git a/xbmc/music/MusicDatabase.cpp b/xbmc/music/MusicDatabase.cpp index 30ba90b9e4..e8008214fd 100644 --- a/xbmc/music/MusicDatabase.cpp +++ b/xbmc/music/MusicDatabase.cpp @@ -460,6 +460,75 @@ bool CMusicDatabase::AddAlbum(CAlbum& album) return true; } +bool CMusicDatabase::UpdateAlbum(CAlbum& album) +{ + BeginTransaction(); + + UpdateAlbum(album.idAlbum, + album.strAlbum, album.strMusicBrainzAlbumID, + album.GetArtistString(), album.GetGenreString(), + StringUtils::Join(album.moods, g_advancedSettings.m_musicItemSeparator).c_str(), + StringUtils::Join(album.styles, g_advancedSettings.m_musicItemSeparator).c_str(), + StringUtils::Join(album.themes, g_advancedSettings.m_musicItemSeparator).c_str(), + album.strReview, + album.thumbURL.m_xml.c_str(), + album.strLabel, album.strType, + album.iRating, album.iYear, album.bCompilation); + + // Add the album artists + DeleteAlbumArtistsByAlbum(album.idAlbum); + for (VECARTISTCREDITS::iterator artistCredit = album.artistCredits.begin(); artistCredit != album.artistCredits.end(); ++artistCredit) + { + artistCredit->idArtist = AddArtist(artistCredit->GetArtist(), + artistCredit->GetMusicBrainzArtistID()); + AddAlbumArtist(artistCredit->idArtist, + album.idAlbum, + artistCredit->GetJoinPhrase(), + artistCredit == album.artistCredits.begin() ? false : true, + std::distance(album.artistCredits.begin(), artistCredit)); + } + + for (VECSONGS::iterator song = album.songs.begin(); song != album.songs.end(); ++song) + { + UpdateSong(song->idSong, + song->strTitle, + song->strMusicBrainzTrackID, + song->strFileName, + song->strComment, + song->strThumb, + StringUtils::Join(song->artist, g_advancedSettings.m_musicItemSeparator), + song->genre, + song->iTrack, + song->iDuration, + song->iYear, + song->iTimesPlayed, + song->iStartOffset, + song->iEndOffset, + song->lastPlayed, + song->rating, + song->iKaraokeNumber); + DeleteSongArtistsBySong(song->idSong); + for (VECARTISTCREDITS::iterator artistCredit = song->artistCredits.begin(); artistCredit != song->artistCredits.end(); ++artistCredit) + { + artistCredit->idArtist = AddArtist(artistCredit->GetArtist(), + artistCredit->GetMusicBrainzArtistID()); + AddSongArtist(artistCredit->idArtist, + song->idSong, + artistCredit->GetJoinPhrase(), + artistCredit == song->artistCredits.begin() ? false : true, + std::distance(song->artistCredits.begin(), artistCredit)); + } + } + for (VECSONGS::const_iterator infoSong = album.infoSongs.begin(); infoSong != album.infoSongs.end(); ++infoSong) + AddAlbumInfoSong(album.idAlbum, *infoSong); + + if (!album.art.empty()) + SetArtForItem(album.idAlbum, "album", album.art); + + CommitTransaction(); + return true; +} + int CMusicDatabase::AddSong(const int idAlbum, const CStdString& strTitle, const CStdString& strMusicBrainzTrackID, const CStdString& strPathAndFileName, const CStdString& strComment, const CStdString& strThumb, @@ -729,6 +798,44 @@ int CMusicDatabase::AddAlbum(const CStdString& strAlbum, const CStdString& strMu return -1; } +int CMusicDatabase::UpdateAlbum(int idAlbum, + const CStdString& strAlbum, const CStdString& strMusicBrainzAlbumID, + const CStdString& strArtist, const CStdString& strGenre, + const CStdString& strMoods, const CStdString& strStyles, + const CStdString& strThemes, const CStdString& strReview, + const CStdString& strImage, const CStdString& strLabel, + const CStdString& strType, + int iRating, int iYear, bool bCompilation) +{ + if (idAlbum < 0) + return -1; + + CStdString strSQL; + strSQL = PrepareSQL("UPDATE album SET " + " strAlbum = '%s', strArtists = '%s', strGenres = '%s', " + " strMoods = '%s', strStyles = '%s', strThemes = '%s', " + " strReview = '%s', strImage = '%s', strLabel = '%s', " + " strType = '%s'," + " iYear = %i, bCompilation = %i, lastScraped = '%s'", + strAlbum.c_str(), strArtist.c_str(), strGenre.c_str(), + strMoods.c_str(), strStyles.c_str(), strThemes.c_str(), + strReview.c_str(), strImage.c_str(), strLabel.c_str(), + strType.c_str(), + iYear, bCompilation, + CDateTime::GetCurrentDateTime().GetAsDBDateTime().c_str()); + if (strMusicBrainzAlbumID.empty()) + strSQL += PrepareSQL(", strMusicBrainzAlbumID = NULL"); + else + strSQL += PrepareSQL(", strMusicBrainzAlbumID = '%s'", strMusicBrainzAlbumID.c_str()); + + strSQL += PrepareSQL(" WHERE idAlbum = %i", idAlbum); + + bool status = ExecuteQuery(strSQL); + if (status) + AnnounceUpdate("album", idAlbum); + return idAlbum; +} + bool CMusicDatabase::GetAlbum(int idAlbum, CAlbum& album, bool getSongs /* = true */) { try @@ -983,6 +1090,11 @@ bool CMusicDatabase::AddSongArtist(int idArtist, int idSong, std::string joinPhr return ExecuteQuery(strSQL); }; +bool CMusicDatabase::DeleteSongArtistsBySong(int idSong) +{ + return ExecuteQuery(PrepareSQL("DELETE FROM song_artist WHERE idSong = %i", idSong)); +} + bool CMusicDatabase::AddAlbumArtist(int idArtist, int idAlbum, std::string joinPhrase, bool featured, int iOrder) { CStdString strSQL; @@ -991,6 +1103,11 @@ bool CMusicDatabase::AddAlbumArtist(int idArtist, int idAlbum, std::string joinP return ExecuteQuery(strSQL); }; +bool CMusicDatabase::DeleteAlbumArtistsByAlbum(int idAlbum) +{ + return ExecuteQuery(PrepareSQL("DELETE FROM album_artist WHERE idAlbum = %i", idAlbum)); +} + bool CMusicDatabase::AddSongGenre(int idGenre, int idSong, int iOrder) { if (idGenre == -1 || idSong == -1) @@ -1002,6 +1119,11 @@ bool CMusicDatabase::AddSongGenre(int idGenre, int idSong, int iOrder) return ExecuteQuery(strSQL); }; +bool CMusicDatabase::DeleteSongGenresBySong(int idSong) +{ + return ExecuteQuery(PrepareSQL("DELETE FROM song_genre WHERE idSong = %i", idSong)); +} + bool CMusicDatabase::AddAlbumGenre(int idGenre, int idAlbum, int iOrder) { if (idGenre == -1 || idAlbum == -1) @@ -1013,6 +1135,11 @@ bool CMusicDatabase::AddAlbumGenre(int idGenre, int idAlbum, int iOrder) return ExecuteQuery(strSQL); }; +bool CMusicDatabase::DeleteAlbumGenresByAlbum(int idAlbum) +{ + return ExecuteQuery(PrepareSQL("DELETE FROM album_genre WHERE idAlbum = %i", idAlbum)); +} + bool CMusicDatabase::GetAlbumsByArtist(int idArtist, bool includeFeatured, std::vector &albums) { try @@ -2059,71 +2186,6 @@ bool CMusicDatabase::SearchAlbums(const CStdString& search, CFileItemList &album return false; } -int CMusicDatabase::SetAlbumInfo(int idAlbum, const CAlbum& album, const VECSONGS& songs, bool bTransaction) -{ - CStdString strSQL; - try - { - if (NULL == m_pDB.get()) return -1; - if (NULL == m_pDS.get()) return -1; - - if (bTransaction) - BeginTransaction(); - - // update our data - strSQL=PrepareSQL("UPDATE album SET" - " strMoods='%s'," - " strStyles='%s'," - " strThemes='%s'," - " strReview='%s'," - " strImage='%s'," - " strLabel='%s'," - " strType='%s'," - " iRating=%i," - " iYear=%i," - " lastScraped='%s' WHERE idAlbum=%i", - StringUtils::Join(album.moods, g_advancedSettings.m_musicItemSeparator).c_str(), - StringUtils::Join(album.styles, g_advancedSettings.m_musicItemSeparator).c_str(), - StringUtils::Join(album.themes, g_advancedSettings.m_musicItemSeparator).c_str(), - album.strReview.c_str(), - album.thumbURL.m_xml.c_str(), - album.strLabel.c_str(), - album.strType.c_str(), - album.iRating, - album.iYear, - CDateTime::GetCurrentDateTime().GetAsDBDateTime().c_str(), - idAlbum); - m_pDS->exec(strSQL.c_str()); - - strSQL=PrepareSQL("delete from albuminfosong where idAlbumInfo=%i", idAlbum); - m_pDS->exec(strSQL.c_str()); - - for (int i = 0; i < (int)songs.size(); i++) - { - CSong song = songs[i]; - strSQL=PrepareSQL("insert into albuminfosong (idAlbumInfoSong,idAlbumInfo,iTrack,strTitle,iDuration) values(NULL,%i,%i,'%s',%i)", - idAlbum, - song.iTrack, - song.strTitle.c_str(), - song.iDuration); - m_pDS->exec(strSQL.c_str()); - } - if (bTransaction) - CommitTransaction(); - - return idAlbum; - } - catch (...) - { - CLog::Log(LOGERROR, "%s failed with query (%s)", __FUNCTION__, strSQL.c_str()); - } - - if (bTransaction) - RollbackTransaction(); - - return -1; -} - int CMusicDatabase::SetArtistInfo(int idArtist, const CArtist& artist) { CStdString strSQL; @@ -4957,12 +5019,17 @@ void CMusicDatabase::ImportFromXML(const CStdString &xmlFile) } else if (strnicmp(entry->Value(), "album", 5) == 0) { - CAlbum album; - album.Load(entry); - strTitle = album.strAlbum; - int idAlbum = GetAlbumByName(album.strAlbum,album.artist); + CAlbum importedAlbum; + importedAlbum.Load(entry); + strTitle = importedAlbum.strAlbum; + int idAlbum = GetAlbumByName(importedAlbum.strAlbum, importedAlbum.artist); if (idAlbum > -1) - SetAlbumInfo(idAlbum,album,album.infoSongs,false); + { + CAlbum album; + GetAlbum(idAlbum, album, true); + album.MergeScrapedAlbum(importedAlbum, true); + UpdateAlbum(album); + } current++; } diff --git a/xbmc/music/MusicDatabase.h b/xbmc/music/MusicDatabase.h index e86cc2e191..cce6001d8c 100644 --- a/xbmc/music/MusicDatabase.h +++ b/xbmc/music/MusicDatabase.h @@ -193,6 +193,11 @@ public: // Album ///////////////////////////////////////////////// bool AddAlbum(CAlbum& album); + /*! \brief Update an album and all its nested entities (artists, songs, infoSongs, etc) + \param album the album to update + \return true or false + */ + bool UpdateAlbum(CAlbum& album); /*! \brief Add an album and all its songs to the database \param album the album to add @@ -210,6 +215,14 @@ public: */ bool GetAlbum(int idAlbum, CAlbum& album, bool getSongs = true); int UpdateAlbum(int idAlbum, const CAlbum &album); + int UpdateAlbum(int idAlbum, + const CStdString& strAlbum, const CStdString& strMusicBrainzAlbumID, + const CStdString& strArtist, const CStdString& strGenre, + const CStdString& strMoods, const CStdString& strStyles, + const CStdString& strThemes, const CStdString& strReview, + const CStdString& strImage, const CStdString& strLabel, + const CStdString& strType, + int iRating, int iYear, bool bCompilation); bool DeleteAlbum(int idAlbum); bool ClearAlbumLastScrapedTime(int idAlbum); bool HasAlbumBeenScraped(int idAlbum); @@ -256,11 +269,6 @@ public: CStdString GetGenreById(int id); int GetGenreByName(const CStdString& strGenre); - ///////////////////////////////////////////////// - // AlbumInfo - ///////////////////////////////////////////////// - int SetAlbumInfo(int idAlbum, const CAlbum& album, const VECSONGS& songs, bool bTransaction=true); - ///////////////////////////////////////////////// // ArtistInfo ///////////////////////////////////////////////// @@ -279,16 +287,20 @@ public: bool AddAlbumArtist(int idArtist, int idAlbum, std::string joinPhrase, bool featured, int iOrder); bool GetAlbumsByArtist(int idArtist, bool includeFeatured, std::vector& albums); bool GetArtistsByAlbum(int idAlbum, bool includeFeatured, std::vector& artists); + bool DeleteAlbumArtistsByAlbum(int idAlbum); bool AddSongArtist(int idArtist, int idSong, std::string joinPhrase, bool featured, int iOrder); bool GetSongsByArtist(int idArtist, bool includeFeatured, std::vector& songs); bool GetArtistsBySong(int idSong, bool includeFeatured, std::vector& artists); + bool DeleteSongArtistsBySong(int idSong); bool AddSongGenre(int idGenre, int idSong, int iOrder); bool GetGenresBySong(int idSong, std::vector& genres); + bool DeleteSongGenresBySong(int idSong); bool AddAlbumGenre(int idGenre, int idAlbum, int iOrder); bool GetGenresByAlbum(int idAlbum, std::vector& genres); + bool DeleteAlbumGenresByAlbum(int idAlbum); ///////////////////////////////////////////////// // Top 100 diff --git a/xbmc/music/infoscanner/MusicInfoScanner.cpp b/xbmc/music/infoscanner/MusicInfoScanner.cpp index 75171fa138..baf8aa2025 100644 --- a/xbmc/music/infoscanner/MusicInfoScanner.cpp +++ b/xbmc/music/infoscanner/MusicInfoScanner.cpp @@ -1033,11 +1033,12 @@ loop: } else if (albumDownloadStatus == INFO_ADDED) { + album.MergeScrapedAlbum(albumInfo.GetAlbum()); m_musicDatabase.Open(); - m_musicDatabase.SetAlbumInfo(params.GetAlbumId(), albumInfo.GetAlbum(), albumInfo.GetAlbum().infoSongs); - GetAlbumArtwork(params.GetAlbumId(), albumInfo.GetAlbum()); - albumInfo.SetLoaded(true); + m_musicDatabase.UpdateAlbum(album); + GetAlbumArtwork(album.idAlbum, album); m_musicDatabase.Close(); + albumInfo.SetLoaded(true); } return albumDownloadStatus; } -- cgit v1.2.3