Skip to content

Commit

Permalink
Merge pull request #10553 from DaveTBlake/ManyAlbumsInFolder
Browse files Browse the repository at this point in the history
Setting album user rating using album ID not GetAlbumIdByPath
  • Loading branch information
DaveTBlake committed Sep 30, 2016
2 parents a96422a + 0fc46e8 commit 4e1c581
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 37 deletions.
50 changes: 16 additions & 34 deletions xbmc/music/MusicDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2167,12 +2167,18 @@ int CMusicDatabase::GetAlbumIdByPath(const std::string& strPath)
{
try
{
std::string strSQL=PrepareSQL("select distinct idAlbum from song join path on song.idPath = path.idPath where path.strPath='%s'", strPath.c_str());
m_pDS->query(strSQL);
if (m_pDS->eof())
return -1;
if (NULL == m_pDB.get()) return false;
if (NULL == m_pDS.get()) return false;

std::string strSQL = PrepareSQL("SELECT DISTINCT idAlbum FROM song JOIN path ON song.idPath = path.idPath WHERE path.strPath='%s'", strPath.c_str());
// run query
if (!m_pDS->query(strSQL)) return false;
int iRowsFound = m_pDS->num_rows();

int idAlbum = m_pDS->fv(0).get_asInt();
int idAlbum = -1; // If no album is found, or more than one album is found then -1 is returned
if (iRowsFound == 1)
idAlbum = m_pDS->fv(0).get_asInt();

m_pDS->close();

return idAlbum;
Expand All @@ -2182,7 +2188,7 @@ int CMusicDatabase::GetAlbumIdByPath(const std::string& strPath)
CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strPath.c_str());
}

return false;
return -1;
}

int CMusicDatabase::GetSongByArtistAndAlbumAndTitle(const std::string& strArtist, const std::string& strAlbum, const std::string& strTitle)
Expand Down Expand Up @@ -5497,24 +5503,22 @@ bool CMusicDatabase::SetSongUserrating(const std::string &filePath, int userrati
return false;
}

bool CMusicDatabase::SetAlbumUserrating(const std::string &filePath, int userrating)
bool CMusicDatabase::SetAlbumUserrating(const int idAlbum, int userrating)
{
try
{
if (filePath.empty()) return false;
if (NULL == m_pDB.get()) return false;
if (NULL == m_pDS.get()) return false;

int albumID = GetAlbumIdByPath(filePath);
if (-1 == albumID) return false;
if (-1 == idAlbum) return false;

std::string sql = PrepareSQL("UPDATE album SET iUserrating='%i' WHERE idAlbum = %i", userrating, albumID);
std::string sql = PrepareSQL("UPDATE album SET iUserrating='%i' WHERE idAlbum = %i", userrating, idAlbum);
m_pDS->exec(sql);
return true;
}
catch (...)
{
CLog::Log(LOGERROR, "%s (%s,%i) failed", __FUNCTION__, filePath.c_str(), userrating);
CLog::Log(LOGERROR, "%s (%i,%i) failed", __FUNCTION__, idAlbum, userrating);
}
return false;
}
Expand All @@ -5541,28 +5545,6 @@ bool CMusicDatabase::SetSongVotes(const std::string &filePath, int votes)
return false;
}

bool CMusicDatabase::SetAlbumVotes(const std::string &filePath, int votes)
{
try
{
if (filePath.empty()) return false;
if (NULL == m_pDB.get()) return false;
if (NULL == m_pDS.get()) return false;

int albumID = GetAlbumIdByPath(filePath);
if (-1 == albumID) return false;

std::string sql = PrepareSQL("UPDATE album SET iVotes='%i' WHERE idAlbum = %i", votes, albumID);
m_pDS->exec(sql);
return true;
}
catch (...)
{
CLog::Log(LOGERROR, "%s (%s,%i) failed", __FUNCTION__, filePath.c_str(), votes);
}
return false;
}

int CMusicDatabase::GetSongIDFromPath(const std::string &filePath)
{
// grab the where string to identify the song id
Expand Down
3 changes: 1 addition & 2 deletions xbmc/music/MusicDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ class CMusicDatabase : public CDatabase
bool Search(const std::string& search, CFileItemList &items);
bool RemoveSongsFromPath(const std::string &path, MAPSONGS& songs, bool exact=true);
bool SetSongUserrating(const std::string &filePath, int userrating);
bool SetAlbumUserrating(const std::string &filePath, int userrating);
bool SetSongVotes(const std::string &filePath, int votes);
bool SetAlbumVotes(const std::string &filePath, int votes);
int GetSongByArtistAndAlbumAndTitle(const std::string& strArtist, const std::string& strAlbum, const std::string& strTitle);

/////////////////////////////////////////////////
Expand Down Expand Up @@ -250,6 +248,7 @@ class CMusicDatabase : public CDatabase
int GetAlbumByName(const std::string& strAlbum, const std::string& strArtist="");
int GetAlbumByName(const std::string& strAlbum, const std::vector<std::string>& artist);
std::string GetAlbumById(int id);
bool SetAlbumUserrating(const int idAlbum, int userrating);

/////////////////////////////////////////////////
// Artist CRUD
Expand Down
2 changes: 1 addition & 1 deletion xbmc/music/dialogs/GUIDialogMusicInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool CGUIDialogMusicInfo::OnMessage(CGUIMessage& message)
if (db.Open())
{
m_needsUpdate = true;
db.SetAlbumUserrating(m_albumItem->GetPath(), m_albumItem->GetMusicInfoTag()->GetUserrating());
db.SetAlbumUserrating(m_albumItem->GetMusicInfoTag()->GetAlbumId(), m_albumItem->GetMusicInfoTag()->GetUserrating());
db.Close();
}
}
Expand Down

0 comments on commit 4e1c581

Please sign in to comment.