Skip to content

Commit

Permalink
add TrackDAO::getTrackDurationsTotal(QList<TrackId>)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Jan 7, 2024
1 parent 2cfee78 commit af5ca37
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/library/dao/trackdao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,38 @@ TrackId TrackDAO::getTrackIdByLocation(const QString& location) const {
return trackId;
}

double TrackDAO::getTrackDurationsTotal(const QList<TrackId>& trackIds) const {
double duration = 0.0;
if (trackIds.isEmpty()) {
return duration;
}

// Create a deduplicated joint string for the query
const QString idListJoined = joinTrackIdList(QSet<TrackId>::fromList(trackIds));

FwdSqlQuery query(m_database, QString("SELECT id, duration "
"FROM library "
"WHERE library.id in (%1)")
.arg(idListJoined));
if (query.hasError() || !query.execPrepared()) {
return duration;
}

QHash<TrackId, double> idDurationMap;
const int idIndex = query.fieldIndex("id");
const int durationIndex = query.fieldIndex("duration");
while (query.next()) {
idDurationMap.insert(TrackId(query.fieldValue(idIndex)),
query.fieldValue(durationIndex).toDouble());
}

for (const TrackId id : trackIds) {
duration += idDurationMap.value(id);
}

return duration;
}

QList<TrackId> TrackDAO::resolveTrackIds(
const QList<mixxx::FileInfo>& fileInfos,
ResolveTrackIdFlags flags) {
Expand Down
4 changes: 4 additions & 0 deletions src/library/dao/trackdao.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class TrackDAO : public QObject, public virtual DAO, public virtual GlobalTrackC
QSet<QString> getAllTrackLocations() const;
QString getTrackLocation(TrackId trackId) const;

// Returns the total of all track's durations.
// Considers duplicates so it can be used for playlists.
double getTrackDurationsTotal(const QList<TrackId>& trackIds) const;

// Only used by friend class LibraryScanner, but public for testing!
bool detectMovedTracks(
QList<RelocatedTrack>* pRelocatedTracks,
Expand Down

0 comments on commit af5ca37

Please sign in to comment.