Generate album ids based on album+albumArtist. This way things like

"greatest hits" albums get sorted properly.
This commit is contained in:
casey langen 2017-05-03 15:25:46 -07:00
parent 6351c1ec78
commit 26edd36087
3 changed files with 44 additions and 3 deletions

View File

@ -49,7 +49,7 @@ using namespace musik::core;
using namespace musik::core::library;
using namespace musik::core::runtime;
#define DATABASE_VERSION 2
#define DATABASE_VERSION 3
#define VERBOSE_LOGGING 0
#define MESSAGE_QUERY_COMPLETED 5000
@ -280,6 +280,18 @@ static void upgradeV1toV2(db::Connection &db) {
update.Step();
}
static void upgradeV2ToV3(db::Connection& db) {
db.Execute("DROP TABLE IF EXISTS albums");
db.Execute("DELETE from tracks");
db.Execute(
"CREATE TABLE IF NOT EXISTS albums ("
"id INTEGER PRIMARY KEY,"
"name TEXT default '',"
"thumbnail_id INTEGER default 0,"
"sort_order INTEGER DEFAULT 0)");
}
static void setVersion(db::Connection& db, int version) {
db.Execute("DELETE FROM version");
@ -363,7 +375,7 @@ void LocalLibrary::CreateDatabase(db::Connection &db){
/* albums */
db.Execute(
"CREATE TABLE IF NOT EXISTS albums ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"id INTEGER PRIMARY KEY,"
"name TEXT default '',"
"thumbnail_id INTEGER default 0,"
"sort_order INTEGER DEFAULT 0)");
@ -458,6 +470,10 @@ void LocalLibrary::CreateDatabase(db::Connection &db){
upgradeV1toV2(db);
}
if (lastVersion >= 1 && lastVersion < 3) {
upgradeV2ToV3(db);
}
/* ensure our version is set correctly */
setVersion(db, DATABASE_VERSION);

View File

@ -484,6 +484,29 @@ void IndexerTrack::ProcessNonStandardMetadata(db::Connection& connection) {
}
}
uint64_t IndexerTrack::SaveAlbum(db::Connection& dbConnection) {
std::string album = this->GetValue("album");
std::string value = album + "-" + this->GetValue("album_artist");
uint64_t id = std::hash<std::string>()(value);
std::string cacheKey = "album-" + value;
if (metadataIdCache.find(cacheKey) != metadataIdCache.end()) {
return metadataIdCache[cacheKey];
}
else {
std::string insertStatement = "INSERT INTO albums (id, name) VALUES (?, ?)";
db::Statement insertValue(insertStatement.c_str(), dbConnection);
insertValue.BindUint64(0, id);
insertValue.BindText(1, album);
if (insertValue.Step() == db::Done) {
metadataIdCache[cacheKey] = id;
}
}
return id;
}
uint64_t IndexerTrack::SaveSingleValueField(
db::Connection& dbConnection,
const std::string& trackMetadataKeyName,
@ -611,7 +634,7 @@ bool IndexerTrack::Save(db::Connection &dbConnection, std::string libraryDirecto
this->id = writeToTracksTable(dbConnection, *this);
uint64_t albumId = this->SaveSingleValueField(dbConnection, "album", "albums");
uint64_t albumId = this->SaveAlbum(dbConnection);
uint64_t genreId = this->SaveGenre(dbConnection);
uint64_t artistId = this->SaveArtist(dbConnection);
uint64_t albumArtistId = this->SaveSingleValueField(dbConnection, "album_artist", "artists");

View File

@ -103,6 +103,8 @@ namespace musik { namespace core {
uint64_t SaveArtist(db::Connection& connection);
uint64_t SaveAlbum(db::Connection& connection);
uint64_t SaveSingleValueField(
db::Connection& connection,
const std::string& trackMetadataKeyName,