diff --git a/src/database-updater/DatabaseUpdater.cpp b/src/database-updater/DatabaseUpdater.cpp index dd4d0683..464b8d54 100644 --- a/src/database-updater/DatabaseUpdater.cpp +++ b/src/database-updater/DatabaseUpdater.cpp @@ -630,19 +630,15 @@ void Updater::checkAudioFiles( Stats& stats ) { - LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Checking audio files..."; + LMS_LOG(MOD_DBUPDATER, SEV_INFO) << "Checking audio files..."; Wt::Dbo::Transaction transaction(_db.getSession()); std::vector rootDirs = getRootDirectoriesByType(_db.getSession(), Database::MediaDirectory::Audio); LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Checking tracks..."; - typedef Wt::Dbo::collection< Wt::Dbo::ptr > Tracks; - Tracks tracks = Track::getAll(_db.getSession()); - - for (Tracks::iterator it = tracks.begin(); it != tracks.end(); ++it) + auto tracks = Track::getAll(_db.getSession()); + for (auto track : tracks) { - Track::pointer track = (*it); - if (!checkFile(track->getPath(), rootDirs, _audioExtensions)) { track.remove(); @@ -651,20 +647,34 @@ Updater::checkAudioFiles( Stats& stats ) } // Now process orphan Genre (no track) -/* LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Checking Genres..."; - typedef Wt::Dbo::collection< Wt::Dbo::ptr > Genres; - Genres genres = Genre::getAll(_db.getSession()); - - for (Genres::iterator it = genres.begin(); it != genres.end(); ++it) + LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Checking Genres..."; + auto genres = Genre::getAll(_db.getSession()); + for (auto genre : genres) { - Genre::pointer genre = (*it); - if (genre->getTracks().size() == 0) + { + LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Removing orphan genre '" << genre->getName() << "'"; genre.remove(); + } } -*/ - LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Check audio files done!"; + LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Checking artists..."; + auto artists = Artist::getAllOrphans(_db.getSession()); + for (auto artist : artists) + { + LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Removing orphan artist '" << artist->getName() << "'"; + artist.remove(); + } + + LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Checking releases..."; + auto releases = Release::getAllOrphans(_db.getSession()); + for (auto release : releases) + { + LMS_LOG(MOD_DBUPDATER, SEV_DEBUG) << "Removing orphan release '" << release->getName() << "'"; + release.remove(); + } + + LMS_LOG(MOD_DBUPDATER, SEV_INFO) << "Check audio files done!"; } void diff --git a/src/database/Artist.cpp b/src/database/Artist.cpp index ffff62b7..01a39ae7 100644 --- a/src/database/Artist.cpp +++ b/src/database/Artist.cpp @@ -84,6 +84,18 @@ Artist::getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, return std::vector(res.begin(), res.end()); } +std::vector > +Artist::getReleases() +{ + assert(self()); + assert(self()->id() != Wt::Dbo::dbo_traits::invalidId() ); + assert(session()); + + Wt::Dbo::collection< Wt::Dbo::ptr > res = session()->query >("SELECT r FROM release r INNER JOIN artist a ON t.artist_id = a.id INNER JOIN track t ON t.release_id = r.id").where("a.id = ?").bind(id()); + + return std::vector< Wt::Dbo::ptr > (res.begin(), res.end()); +} + Wt::Dbo::Query Artist::getQuery(Wt::Dbo::Session& session, SearchFilter filter) { diff --git a/src/database/Artist.hpp b/src/database/Artist.hpp index 8c59ac07..fe46880c 100644 --- a/src/database/Artist.hpp +++ b/src/database/Artist.hpp @@ -33,8 +33,9 @@ namespace Database class Track; class Genre; +class Release; -class Artist +class Artist : public Wt::Dbo::Dbo { public: @@ -58,6 +59,9 @@ class Artist std::string getName(void) const { return _name; } std::string getMBID(void) const { return _MBID; } + // Get the releases that have at least one track for this artist + std::vector > getReleases(); + void setMBID(std::string mbid) { _MBID = mbid; } // Create diff --git a/src/database/Release.cpp b/src/database/Release.cpp index d2bd4f72..b5b7b490 100644 --- a/src/database/Release.cpp +++ b/src/database/Release.cpp @@ -71,7 +71,7 @@ Release::getAll(Wt::Dbo::Session& session, int offset, int size) std::vector Release::getAllOrphans(Wt::Dbo::Session& session) { - Wt::Dbo::collection res = session.query< Wt::Dbo::ptr >("select a from artist a LEFT OUTER JOIN Track t ON a.id = t.artist_id WHERE t.id IS NULL"); + Wt::Dbo::collection res = session.query< Wt::Dbo::ptr >("select r from release r LEFT OUTER JOIN Track t ON r.id = t.release_id WHERE t.id IS NULL"); return std::vector(res.begin(), res.end()); } @@ -129,4 +129,16 @@ Release::getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, return std::vector(res.begin(), res.end()); } +std::vector< Wt::Dbo::ptr > +Release::getArtists() +{ + assert(self()); + assert(self()->id() != Wt::Dbo::dbo_traits::invalidId() ); + assert(session()); + + Wt::Dbo::collection< Wt::Dbo::ptr > res = session()->query >("SELECT a FROM artist a INNER JOIN release r ON r.id = t.release_id INNER JOIN track t ON t.release_id = r.id").where("r.id = ?").bind(id()); + + return std::vector< Wt::Dbo::ptr > (res.begin(), res.end()); +} + } // namespace Database diff --git a/src/database/Release.hpp b/src/database/Release.hpp index 479870a7..607496c4 100644 --- a/src/database/Release.hpp +++ b/src/database/Release.hpp @@ -29,8 +29,9 @@ namespace Database { class Track; +class Release; -class Release +class Release : public Wt::Dbo::Dbo { public: @@ -64,6 +65,8 @@ class Release std::string getMBID() const { return _MBID; } bool isNone(void) const; boost::posix_time::time_duration getDuration(void) const; + std::vector > getArtists(); // Get the artists of this release + std::vector > getTracks(); // Get the tracks of this release void setMBID(std::string mbid) { _MBID = mbid; } diff --git a/src/database/Track.cpp b/src/database/Track.cpp index 850e72c6..062ea370 100644 --- a/src/database/Track.cpp +++ b/src/database/Track.cpp @@ -152,11 +152,10 @@ Genre::Genre(const std::string& name) { } -std::vector -Genre::getAll(Wt::Dbo::Session& session, int offset, int size) +Wt::Dbo::collection +Genre::getAll(Wt::Dbo::Session& session) { - Wt::Dbo::collection res = session.find().offset(offset).limit(size); - return std::vector(res.begin(), res.end()); + return session.find(); } Genre::pointer diff --git a/src/database/Track.hpp b/src/database/Track.hpp index 3d0e7fe5..cad39c44 100644 --- a/src/database/Track.hpp +++ b/src/database/Track.hpp @@ -52,7 +52,7 @@ class Genre static pointer getByName(Wt::Dbo::Session& session, const std::string& name); static pointer getNone(Wt::Dbo::Session& session); static std::vector getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset = -1, int size = -1); - static std::vector getAll(Wt::Dbo::Session& session, int offset = -1, int size = -1); + static Wt::Dbo::collection getAll(Wt::Dbo::Session& session); // MVC models for the user interface // Genre ID, name, track count