From 70bb54969ace8a60b4b14eed5ac01335e3c42575 Mon Sep 17 00:00:00 2001 From: Lukas Waslowski Date: Sat, 27 Apr 2024 08:12:22 +0000 Subject: [PATCH] PlaylistStatsDAO: Consolidate into a single DAO instance and table view --- src/library/dao/playliststatsdao.cpp | 53 ++++++++++---------- src/library/dao/playliststatsdao.h | 9 ++-- src/library/trackcollection.cpp | 5 -- src/library/trackcollection.h | 19 +------ src/library/trackset/baseplaylistfeature.cpp | 7 +-- src/library/trackset/playlistfeature.cpp | 6 +-- src/library/trackset/setlogfeature.cpp | 6 +-- 7 files changed, 38 insertions(+), 67 deletions(-) diff --git a/src/library/dao/playliststatsdao.cpp b/src/library/dao/playliststatsdao.cpp index a04e4fa5bfb..72e23ffaf29 100644 --- a/src/library/dao/playliststatsdao.cpp +++ b/src/library/dao/playliststatsdao.cpp @@ -10,10 +10,8 @@ #include "util/make_const_iterator.h" #include "util/math.h" -PlaylistStatsDAO::PlaylistStatsDAO(const QString& countsDurationTableName, - const PlaylistDAO::HiddenType playlistType) - : m_countsDurationTableName(countsDurationTableName), - m_hiddenType(playlistType) { +PlaylistStatsDAO::PlaylistStatsDAO() + : m_countsDurationTableName(QStringLiteral("PlaylistsCountsDurations")) { } void PlaylistStatsDAO::initialize(const QSqlDatabase& database) { @@ -22,38 +20,36 @@ void PlaylistStatsDAO::initialize(const QSqlDatabase& database) { } void PlaylistStatsDAO::preparePlaylistSummaryTable() { - // If true, deleted tracks should still be counted - // towards the track count and duration totals. - // If false, treat deleted tracks as if they weren't there. - bool includeDeleted = m_hiddenType == PlaylistDAO::PLHT_SET_LOG; - + // Deleted tracks should still be counted towards the track count + // and duration totals for playlists with hidden == PLHT_SET_LOG. + // Otherwise, we treat deleted tracks as if they weren't there. QString queryString = QStringLiteral( "CREATE TEMPORARY VIEW IF NOT EXISTS %1 " "AS SELECT " " Playlists.id AS id, " " Playlists.name AS name, " " Playlists.date_created AS date_created, " + " Playlists.hidden as hidden, " " LOWER(Playlists.name) AS sort_name, " - " IF(%3, " - " max(PlaylistTracks.position)," - " COUNT(case library.mixxx_deleted " - " when 0 then 1 else null end)) " - " AS count, " - " IF(%3, " - " SUM(library.duration), " - " SUM(case library.mixxx_deleted " - " when 0 then library.duration else 0 end)) " - " AS durationSeconds " + " (case Playlists.hidden " + " when %2 then max(PlaylistTracks.position) " + " else COUNT(case library.mixxx_deleted " + " when 0 then 1 else null end) " + " end) AS count, " + " (case Playlists.hidden " + " when %2 then SUM(library.duration), " + " else SUM(case library.mixxx_deleted " + " when 0 then library.duration else 0 end) " + " end) AS durationSeconds, " "FROM Playlists " "LEFT JOIN PlaylistTracks " " ON PlaylistTracks.playlist_id = Playlists.id " - "LEFT JOIN library " + "LEFT JOIN lim_countsDurationTableNamebrary " " ON PlaylistTracks.track_id = library.id " - " WHERE Playlists.hidden = %2 " - " GROUP BY Playlists.id") - .arg(m_countsDurationTableName, - QString::number(m_hiddenType), - includeDeleted ? "true" : "false"); + "WHERE " + "GROUP BY Playlists.id") + .arg("m_countsDurationTableName", + QString::number(PlaylistDAO::PLHT_SET_LOG)); queryString.append( mixxx::DbConnection::collateLexicographically( @@ -64,14 +60,17 @@ void PlaylistStatsDAO::preparePlaylistSummaryTable() { } } -QList PlaylistStatsDAO::getPlaylistSummaries() { +QList PlaylistStatsDAO::getPlaylistSummaries( + PlaylistDAO::HiddenType playlistType) { // Setup the sidebar playlist model QSqlTableModel playlistTableModel(this, m_database); playlistTableModel.setTable(m_countsDurationTableName); - if (m_hiddenType == PlaylistDAO::PLHT_SET_LOG) { + if (playlistType == PlaylistDAO::PLHT_SET_LOG) { // TODO: Can we remove this special handling for the SetLog? playlistTableModel.setSort(playlistTableModel.fieldIndex("id"), Qt::DescendingOrder); } + const QString filter = "hidden=" + QString::number(playlistType); + playlistTableModel.setFilter(filter); playlistTableModel.select(); while (playlistTableModel.canFetchMore()) { playlistTableModel.fetchMore(); diff --git a/src/library/dao/playliststatsdao.h b/src/library/dao/playliststatsdao.h index a0f911e9fcc..9eec9a0197d 100644 --- a/src/library/dao/playliststatsdao.h +++ b/src/library/dao/playliststatsdao.h @@ -12,8 +12,7 @@ class PlaylistStatsDAO : public QObject, public virtual DAO { Q_OBJECT public: - PlaylistStatsDAO(const QString& countsDurationTableName, - const PlaylistDAO::HiddenType playlistType); + PlaylistStatsDAO(); ~PlaylistStatsDAO() override = default; void initialize(const QSqlDatabase& database) override; @@ -54,14 +53,14 @@ class PlaylistStatsDAO : public QObject, public virtual DAO { /// Prepares the table view used by getPlaylistSummary. void preparePlaylistSummaryTable(); - /// Returns the name, track count and total duration of all playlists. - QList getPlaylistSummaries(); + /// Returns the name, track count and total duration + /// of all playlists of the given type. + QList getPlaylistSummaries(PlaylistDAO::HiddenType playlistType); /// Returns the name, track count and total duration of the given playlist. PlaylistSummary getPlaylistSummary(const int playlistId); private: QString m_countsDurationTableName; - PlaylistDAO::HiddenType m_hiddenType; DISALLOW_COPY_AND_ASSIGN(PlaylistStatsDAO); }; diff --git a/src/library/trackcollection.cpp b/src/library/trackcollection.cpp index 1813f6151da..093f65953f3 100644 --- a/src/library/trackcollection.cpp +++ b/src/library/trackcollection.cpp @@ -18,10 +18,6 @@ mixxx::Logger kLogger("TrackCollection"); TrackCollection::TrackCollection( QObject* parent, const UserSettingsPointer& pConfig) : QObject(parent), - m_playlistStatsDao(QStringLiteral("PlaylistsCountsDurations"), - PlaylistDAO::PLHT_NOT_HIDDEN), - m_setlogStatsDao(QStringLiteral("SetlogCountsDurations"), - PlaylistDAO::PLHT_SET_LOG), m_analysisDao(pConfig), m_trackDao(m_cueDao, m_playlistDao, @@ -85,7 +81,6 @@ void TrackCollection::connectDatabase(const QSqlDatabase& database) { m_trackDao.initialize(database); m_playlistDao.initialize(database); m_playlistStatsDao.initialize(database); - m_setlogStatsDao.initialize(database); m_cueDao.initialize(database); m_directoryDao.initialize(database); m_analysisDao.initialize(database); diff --git a/src/library/trackcollection.h b/src/library/trackcollection.h index 2290c5efd72..de5888d7977 100644 --- a/src/library/trackcollection.h +++ b/src/library/trackcollection.h @@ -61,23 +61,9 @@ class TrackCollection : public QObject, DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this); return m_playlistDao; } - PlaylistStatsDAO& getPlaylistStatsDAO(PlaylistDAO::HiddenType playlistType) { + PlaylistStatsDAO& getPlaylistStatsDAO() { DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this); - switch (playlistType) { - case PlaylistDAO::PLHT_NOT_HIDDEN: { - return m_playlistStatsDao; - } - case PlaylistDAO::PLHT_SET_LOG: { - return m_setlogStatsDao; - } - case PlaylistDAO::PLHT_AUTO_DJ: - case PlaylistDAO::PLHT_UNKNOWN: - default: { - // This can only ever happen due to a coding error - DEBUG_ASSERT(false); - return m_playlistStatsDao; - } - } + return m_playlistStatsDao; } const DirectoryDAO& getDirectoryDAO() const { DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this); @@ -189,7 +175,6 @@ class TrackCollection : public QObject, PlaylistDAO m_playlistDao; PlaylistStatsDAO m_playlistStatsDao; - PlaylistStatsDAO m_setlogStatsDao; CrateStorage m_crates; CueDAO m_cueDao; DirectoryDAO m_directoryDao; diff --git a/src/library/trackset/baseplaylistfeature.cpp b/src/library/trackset/baseplaylistfeature.cpp index e64fae9fee7..7f9c52e4eef 100644 --- a/src/library/trackset/baseplaylistfeature.cpp +++ b/src/library/trackset/baseplaylistfeature.cpp @@ -711,12 +711,9 @@ void BasePlaylistFeature::htmlLinkClicked(const QUrl& link) { } QString BasePlaylistFeature::fetchPlaylistLabel(int playlistId) { - // This queries the temporary id/count/duration table that was has been created - // by the features' createPlaylistLabels() (updated each time playlists are added/removed) + // Query the temporary id/count/duration table PlaylistStatsDAO& playlistStatsDao = - m_pLibrary->trackCollectionManager() - ->internalCollection() - ->getPlaylistStatsDAO(PlaylistDAO::PLHT_NOT_HIDDEN); + m_pLibrary->trackCollectionManager()->internalCollection()->getPlaylistStatsDAO(); auto playlistInfo = playlistStatsDao.getPlaylistSummary(playlistId); if (!playlistInfo.isEmpty()) { diff --git a/src/library/trackset/playlistfeature.cpp b/src/library/trackset/playlistfeature.cpp index 6852b34fa34..0bfce0b1452 100644 --- a/src/library/trackset/playlistfeature.cpp +++ b/src/library/trackset/playlistfeature.cpp @@ -121,12 +121,10 @@ bool PlaylistFeature::dragMoveAcceptChild(const QModelIndex& index, const QUrl& QList PlaylistFeature::createPlaylistLabels() { // Setup the sidebar playlist model PlaylistStatsDAO& playlistStatsDao = - m_pLibrary->trackCollectionManager() - ->internalCollection() - ->getPlaylistStatsDAO(PlaylistDAO::PLHT_NOT_HIDDEN); + m_pLibrary->trackCollectionManager()->internalCollection()->getPlaylistStatsDAO(); QList playlistLabels; - for (auto playlistInfo : playlistStatsDao.getPlaylistSummaries()) { + for (auto playlistInfo : playlistStatsDao.getPlaylistSummaries(PlaylistDAO::PLHT_NOT_HIDDEN)) { BasePlaylistFeature::IdAndLabel idAndLabel; idAndLabel.id = playlistInfo.playlistId; idAndLabel.label = createPlaylistLabel( diff --git a/src/library/trackset/setlogfeature.cpp b/src/library/trackset/setlogfeature.cpp index d6b51979640..bf0c3e639d2 100644 --- a/src/library/trackset/setlogfeature.cpp +++ b/src/library/trackset/setlogfeature.cpp @@ -231,9 +231,7 @@ QModelIndex SetlogFeature::constructChildModel(int selectedId) { // qDebug() << "SetlogFeature::constructChildModel() selected:" << selectedId; // Setup the sidebar playlist model PlaylistStatsDAO& playlistStatsDao = - m_pLibrary->trackCollectionManager() - ->internalCollection() - ->getPlaylistStatsDAO(PlaylistDAO::PLHT_SET_LOG); + m_pLibrary->trackCollectionManager()->internalCollection()->getPlaylistStatsDAO(); // Nice to have: restore previous expanded/collapsed state of YEAR items clearChildModel(); @@ -242,7 +240,7 @@ QModelIndex SetlogFeature::constructChildModel(int selectedId) { // Generous estimate (number of years the db is used ;)) itemList.reserve(kNumToplevelHistoryEntries + 15); int numEntries = 0; - for (auto playlistInfo : playlistStatsDao.getPlaylistSummaries()) { + for (auto playlistInfo : playlistStatsDao.getPlaylistSummaries(PlaylistDAO::PLHT_SET_LOG)) { int id = playlistInfo.playlistId; QString label = createPlaylistLabel( playlistInfo.name, playlistInfo.count, playlistInfo.duration);