Added durations to SearchTrackListQuery

This commit is contained in:
casey langen 2021-01-26 00:04:37 -08:00
parent 46118ab6fe
commit fe752a0d6f
3 changed files with 29 additions and 11 deletions

View File

@ -129,7 +129,7 @@ size_t CategoryTrackListQuery::GetQueryHash() noexcept {
return this->hash;
}
CategoryTrackListQuery::Durations CategoryTrackListQuery::GetDurations() noexcept {
CategoryTrackListQuery::Durations CategoryTrackListQuery::GetDurations() noexcept {
return this->durations;
}
@ -175,15 +175,14 @@ void CategoryTrackListQuery::RegularQuery(musik::core::db::Connection &db) {
void CategoryTrackListQuery::ProcessResult(musik::core::db::Statement& trackQuery) {
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);
runningDuration += trackQuery.ColumnInt32(1);
std::string album = trackQuery.ColumnText(2);
runningDuration += trackQuery.ColumnInt32(1);
if (this->parseHeaders && album != lastAlbum) {
if (!headers->empty()) {
(*durations)[lastHeaderIndex] = runningDuration;

View File

@ -82,6 +82,7 @@ SearchTrackListQuery::SearchTrackListQuery(
this->orderBy = kTrackListSortOrderBy.find(sort)->second;
this->result = std::make_shared<TrackList>(library);
this->headers = std::make_shared<std::set<size_t>>();
this->durations = std::make_shared<std::map<size_t, size_t>>();
this->hash = 0;
}
@ -93,6 +94,10 @@ SearchTrackListQuery::Headers SearchTrackListQuery::GetHeaders() noexcept {
return this->headers;
}
SearchTrackListQuery::Durations SearchTrackListQuery::GetDurations() noexcept {
return this->durations;
}
size_t SearchTrackListQuery::GetQueryHash() noexcept {
this->hash = std::hash<std::string>()(this->filter);
return this->hash;
@ -106,17 +111,16 @@ bool SearchTrackListQuery::OnRun(Connection& db) {
if (result) {
this->result = std::make_shared<TrackList>(library);
this->headers = std::make_shared<std::set<size_t>>();
this->durations = std::make_shared<std::map<size_t, size_t>>();
}
const bool useRegex = (matchType == MatchType::Regex);
const bool hasFilter = (this->filter.size() > 0);
std::string lastAlbum;
size_t index = 0;
std::string query;
if (hasFilter) {
query =
"SELECT DISTINCT tracks.id, al.name "
"SELECT DISTINCT tracks.id, tracks.duration, al.name "
"FROM tracks, albums al, artists ar, genres gn "
"WHERE "
" tracks.visible=1 AND "
@ -129,7 +133,7 @@ bool SearchTrackListQuery::OnRun(Connection& db) {
}
else {
query =
"SELECT DISTINCT tracks.id, al.name "
"SELECT DISTINCT tracks.id, tracks.duration, al.name "
"FROM tracks, albums al, artists ar, genres gn "
"WHERE "
" tracks.visible=1 AND "
@ -152,15 +156,27 @@ bool SearchTrackListQuery::OnRun(Connection& db) {
trackQuery.BindText(3, patternToMatch);
}
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);
runningDuration += trackQuery.ColumnInt32(1);
std::string album = trackQuery.ColumnText(2);
if (!album.size()) {
album = _TSTR("tracklist_unknown_album");
}
if (this->parseHeaders && album != lastAlbum) {
if (!headers->empty()) {
(*durations)[lastHeaderIndex] = runningDuration;
lastHeaderIndex = index;
runningDuration = 0;
}
headers->insert(index);
lastAlbum = album;
}
@ -169,6 +185,10 @@ bool SearchTrackListQuery::OnRun(Connection& db) {
++index;
}
if (this->parseHeaders && !headers->empty()) {
(*durations)[lastHeaderIndex] = runningDuration;
}
return true;
}

View File

@ -60,9 +60,7 @@ namespace musik { namespace core { namespace library { namespace query {
Result GetResult() noexcept override;
Headers GetHeaders() noexcept override;
size_t GetQueryHash() noexcept override;
Durations GetDurations() noexcept override {
return std::make_shared<std::map<size_t, size_t>>();
}
Durations GetDurations() noexcept override;
/* ISerializableQuery */
std::string SerializeQuery() override;
@ -91,6 +89,7 @@ namespace musik { namespace core { namespace library { namespace query {
/* serialized result fields */
Result result;
Headers headers;
Durations durations;
};
} } } }