diff --git a/src/musikcore/library/query/CategoryTrackListQuery.cpp b/src/musikcore/library/query/CategoryTrackListQuery.cpp index 06ed0dc1f..78c3dfa75 100755 --- a/src/musikcore/library/query/CategoryTrackListQuery.cpp +++ b/src/musikcore/library/query/CategoryTrackListQuery.cpp @@ -97,6 +97,7 @@ CategoryTrackListQuery::CategoryTrackListQuery( this->library = library; this->result = std::make_shared(library); this->headers = std::make_shared>(); + this->durations = std::make_shared>(); this->hash = category::Hash(predicates); this->sortType = sortType; @@ -128,6 +129,10 @@ size_t CategoryTrackListQuery::GetQueryHash() noexcept { return this->hash; } + CategoryTrackListQuery::Durations CategoryTrackListQuery::GetDurations() noexcept { + return this->durations; +} + void CategoryTrackListQuery::PlaylistQuery(musik::core::db::Connection &db) { /* playlists are a special case. we already have a query for this, so delegate to that. */ @@ -171,18 +176,33 @@ void CategoryTrackListQuery::ProcessResult(musik::core::db::Statement& trackQuer std::string lastAlbum; size_t index = 0; + size_t lastHeaderIndex = 0; + size_t runningDuration = 0; + while (trackQuery.Step() == Row) { const int64_t id = trackQuery.ColumnInt64(0); - std::string album = trackQuery.ColumnText(1); + std::string album = trackQuery.ColumnText(2); + runningDuration += trackQuery.ColumnInt32(1); if (this->parseHeaders && album != lastAlbum) { headers->insert(index); + + if (!headers->empty()) { + (*durations)[lastHeaderIndex] = runningDuration; + lastHeaderIndex = index; + runningDuration = 0; + } + lastAlbum = album; } result->Add(id); ++index; } + + if (this->parseHeaders && !headers->empty()) { + (*durations)[lastHeaderIndex] = runningDuration; + } } bool CategoryTrackListQuery::OnRun(Connection& db) { diff --git a/src/musikcore/library/query/CategoryTrackListQuery.h b/src/musikcore/library/query/CategoryTrackListQuery.h index 6158393c1..b1d7c3b84 100755 --- a/src/musikcore/library/query/CategoryTrackListQuery.h +++ b/src/musikcore/library/query/CategoryTrackListQuery.h @@ -81,10 +81,8 @@ namespace musik { namespace core { namespace library { namespace query { /* TrackListQueryBase */ Result GetResult() noexcept override; Headers GetHeaders() noexcept override; + Durations GetDurations() noexcept override; size_t GetQueryHash() noexcept override; - Durations GetDurations() noexcept override { - return std::make_shared>(); - } /* ISerializableQuery */ std::string SerializeQuery() override; @@ -116,6 +114,7 @@ namespace musik { namespace core { namespace library { namespace query { /* serialized result fields */ Result result; Headers headers; + Durations durations; /* serialized query fields */ std::string filter; diff --git a/src/musikcore/library/query/util/CategoryQueryUtil.h b/src/musikcore/library/query/util/CategoryQueryUtil.h index e41bd95ba..5b78fd774 100644 --- a/src/musikcore/library/query/util/CategoryQueryUtil.h +++ b/src/musikcore/library/query/util/CategoryQueryUtil.h @@ -175,7 +175,7 @@ namespace musik { namespace core { namespace library { namespace query { /* note: al.name needs to be the second column selected to ensure proper grouping by album in the UI layer! */ static const std::string CATEGORY_TRACKLIST_QUERY = - "SELECT DISTINCT tracks.id, al.name, tracks.date_added, tracks.date_updated, tracks.last_played, tracks.play_count, tracks.rating " + "SELECT DISTINCT tracks.id, tracks.duration, al.name " "FROM tracks, albums al, artists ar, genres gn " "{{extended_predicates}} " "WHERE "