Reduce database include dependencies

This commit is contained in:
emeric
2018-05-16 13:47:00 +02:00
parent 7e6bf57e75
commit 9a44fb838d
43 changed files with 179 additions and 135 deletions
+6
View File
@@ -33,6 +33,12 @@
#include "utils/Logger.hpp"
#include "DbArtist.hpp"
#include "MediaDirectory.hpp"
#include "Playlist.hpp"
#include "Release.hpp"
#include "Track.hpp"
#include "DatabaseHandler.hpp"
namespace Database {
+3 -6
View File
@@ -17,8 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DATABASE_HANDLER_HPP
#define DATABASE_HANDLER_HPP
#pragma once
#include <boost/filesystem.hpp>
#include <memory>
@@ -30,12 +29,11 @@
#include <Wt/Auth/Login.h>
#include <Wt/Auth/PasswordService.h>
#include "Types.hpp"
#include "User.hpp"
namespace Database {
typedef Wt::Dbo::dbo_default_traits::IdType id_type;
typedef Wt::Auth::Dbo::UserDatabase<AuthInfo> UserDatabase;
using UserDatabase = Wt::Auth::Dbo::UserDatabase<AuthInfo>;
// Session living class handling the database and the login
class Handler
@@ -72,5 +70,4 @@ class Handler
} // namespace Database
#endif
+9 -6
View File
@@ -17,11 +17,14 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Types.hpp"
#include "SqlQuery.hpp"
#include "utils/Logger.hpp"
#include "Release.hpp"
#include "DbArtist.hpp"
#include "Track.hpp"
namespace Database
{
@@ -46,7 +49,7 @@ Artist::getByMBID(Wt::Dbo::Session& session, const std::string& mbid)
}
Artist::pointer
Artist::getById(Wt::Dbo::Session& session, Artist::id_type id)
Artist::getById(Wt::Dbo::Session& session, IdType id)
{
return session.find<Artist>().where("id = ?").bind(id);
}
@@ -75,7 +78,7 @@ Artist::getAllOrphans(Wt::Dbo::Session& session)
static
Wt::Dbo::Query<Artist::pointer>
getQuery(Wt::Dbo::Session& session,
const std::set<Artist::id_type>& clusterIds,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords)
{
WhereClause where;
@@ -116,7 +119,7 @@ getQuery(Wt::Dbo::Session& session,
std::vector<Artist::pointer>
Artist::getByFilter(Wt::Dbo::Session& session,
const std::set<id_type>& clusters,
const std::set<IdType>& clusters,
const std::vector<std::string> keywords,
int offset, int size, bool& moreResults)
{
@@ -138,7 +141,7 @@ Artist::getByFilter(Wt::Dbo::Session& session,
}
std::vector<Wt::Dbo::ptr<Release>>
Artist::getReleases(const std::set<id_type>& clusterIds) const
Artist::getReleases(const std::set<IdType>& clusterIds) const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits<Artist>::invalidId() );
@@ -212,7 +215,7 @@ Artist::getClusterGroups(std::vector<ClusterType::pointer> clusterTypes, std::si
Wt::Dbo::collection<Cluster::pointer> queryRes = query;
std::map<ClusterType::id_type, std::vector<Cluster::pointer>> clusters;
std::map<IdType, std::vector<Cluster::pointer>> clusters;
for (auto cluster : queryRes)
{
if (clusters[cluster->getType().id()].size() < size)
+6 -7
View File
@@ -17,8 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _DB_ARTIST_HPP_
#define _DB_ARTIST_HPP_
#pragma once
#include <string>
#include <vector>
@@ -26,6 +25,8 @@
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/QueryModel.h>
#include "Types.hpp"
namespace Database
{
@@ -39,17 +40,16 @@ class Artist : public Wt::Dbo::Dbo<Artist>
public:
typedef Wt::Dbo::ptr<Artist> pointer;
typedef Wt::Dbo::dbo_traits<Artist>::IdType id_type;
Artist() {}
Artist(const std::string& name, const std::string& MBID = "");
// Accessors
static pointer getByMBID(Wt::Dbo::Session& session, const std::string& MBID);
static pointer getById(Wt::Dbo::Session& session, id_type id);
static pointer getById(Wt::Dbo::Session& session, IdType id);
static std::vector<pointer> getByName(Wt::Dbo::Session& session, const std::string& name);
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session,
const std::set<id_type>& clusters, // at least one track that belongs to these clusters
const std::set<IdType>& clusters, // at least one track that belongs to these clusters
const std::vector<std::string> keywords, // name must match all of these keywords
int offset,
int size,
@@ -63,7 +63,7 @@ class Artist : public Wt::Dbo::Dbo<Artist>
std::string getMBID(void) const { return _MBID; }
// Get the releases that have at least one track for this artist that belongs to optional cluster filters
std::vector<Wt::Dbo::ptr<Release>> getReleases(const std::set<id_type>& clusterIds = std::set<id_type>()) const;
std::vector<Wt::Dbo::ptr<Release>> getReleases(const std::set<IdType>& clusterIds = std::set<IdType>()) const;
// Get the cluster of the tracks made by this artist
// Each clusters are grouped by cluster type, sorted by the number of occurence
@@ -97,4 +97,3 @@ class Artist : public Wt::Dbo::Dbo<Artist>
} // namespace Database
#endif
+1 -1
View File
@@ -19,7 +19,7 @@
#include "utils/Utils.hpp"
#include "Types.hpp"
#include "MediaDirectory.hpp"
namespace Database {
+1 -4
View File
@@ -17,8 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DATABASE_MEDIA_DIRECTORY_HPP
#define DATABASE_MEDIA_DIRECTORY_HPP
#pragma once
#include <vector>
@@ -59,5 +58,3 @@ class MediaDirectory
} // namespace Database
#endif
+5 -1
View File
@@ -16,8 +16,12 @@
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include "Types.hpp"
#include "User.hpp"
#include "Track.hpp"
#include "Playlist.hpp"
namespace Database {
+1 -3
View File
@@ -17,8 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DATABASE_PLAYLIST_HPP
#define DATABASE_PLAYLIST_HPP
#pragma once
#include <Wt/Dbo/Dbo.h>
@@ -108,4 +107,3 @@ class PlaylistEntry
} // namespace Database
#endif
+10 -6
View File
@@ -17,9 +17,13 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Types.hpp"
#include "SqlQuery.hpp"
#include "DbArtist.hpp"
#include "Track.hpp"
#include "Release.hpp"
namespace Database
{
@@ -44,7 +48,7 @@ Release::getByMBID(Wt::Dbo::Session& session, const std::string& mbid)
}
Release::pointer
Release::getById(Wt::Dbo::Session& session, Release::id_type id)
Release::getById(Wt::Dbo::Session& session, IdType id)
{
return session.find<Release>().where("id = ?").bind(id);
}
@@ -73,7 +77,7 @@ Release::getAllOrphans(Wt::Dbo::Session& session)
static
Wt::Dbo::Query<Release::pointer>
getQuery(Wt::Dbo::Session& session,
const std::set<Release::id_type>& clusterIds,
const std::set<IdType>& clusterIds,
const std::vector<std::string> keywords)
{
WhereClause where;
@@ -113,7 +117,7 @@ getQuery(Wt::Dbo::Session& session,
std::vector<Release::pointer>
Release::getByFilter(Wt::Dbo::Session& session,
const std::set<id_type>& clusterIds,
const std::set<IdType>& clusterIds,
const std::vector<std::string> keywords,
int offset, int size, bool& moreResults)
{
@@ -182,7 +186,7 @@ Release::hasVariousArtists() const
}
std::vector<Wt::Dbo::ptr<Track>>
Release::getTracks(const std::set<id_type>& clusterIds) const
Release::getTracks(const std::set<IdType>& clusterIds) const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits<Release>::invalidId() );
@@ -256,7 +260,7 @@ Release::getClusterGroups(std::vector<ClusterType::pointer> clusterTypes, std::s
Wt::Dbo::collection<Cluster::pointer> queryRes = query;
std::map<ClusterType::id_type, std::vector<Cluster::pointer>> clusters;
std::map<IdType, std::vector<Cluster::pointer>> clusters;
for (auto cluster : queryRes)
{
if (clusters[cluster->getType().id()].size() < size)
+7 -4
View File
@@ -23,19 +23,22 @@
#include <Wt/Dbo/Dbo.h>
#include "Types.hpp"
namespace Database
{
class Track;
class Release;
class Artist;
class Cluster;
class ClusterType;
class Release : public Wt::Dbo::Dbo<Release>
{
public:
typedef Wt::Dbo::ptr<Release> pointer;
typedef Wt::Dbo::dbo_traits<Release>::IdType id_type;
Release() {}
Release(const std::string& name, const std::string& MBID = "");
@@ -43,18 +46,18 @@ class Release : public Wt::Dbo::Dbo<Release>
// Accessors
static pointer getByMBID(Wt::Dbo::Session& session, const std::string& MBID);
static std::vector<pointer> getByName(Wt::Dbo::Session& session, const std::string& name);
static pointer getById(Wt::Dbo::Session& session, id_type id);
static pointer getById(Wt::Dbo::Session& session, IdType id);
static std::vector<pointer> getAllOrphans(Wt::Dbo::Session& session); // no track related
static std::vector<pointer> getAll(Wt::Dbo::Session& session, int offset, int size);
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session,
const std::set<id_type>& clusters, // at least one track that belongs to these clusters
const std::set<IdType>& clusters, // at least one track that belongs to these clusters
const std::vector<std::string> keywords, // name must match all of these keywords
int offset,
int size,
bool& moreExpected);
std::vector<Wt::Dbo::ptr<Track>> getTracks(const std::set<id_type>& clusters = std::set<id_type>()) const;
std::vector<Wt::Dbo::ptr<Track>> getTracks(const std::set<IdType>& clusters = std::set<IdType>()) const;
// Get the cluster of the tracks that belong to this release
// Each clusters are grouped by cluster type, sorted by the number of occurence
// size is the max number of cluster per cluster type
+1 -4
View File
@@ -17,8 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SQL_QUERY_HPP___
#define SQL_QUERY_HPP___
#pragma once
#include <list>
#include <string>
@@ -134,5 +133,3 @@ class SqlQuery
GroupByStatement _groupByStatement; // GROUP BY statement
};
#endif
+19 -10
View File
@@ -21,7 +21,10 @@
#include "SqlQuery.hpp"
#include "Types.hpp"
#include "DbArtist.hpp"
#include "Release.hpp"
#include "Track.hpp"
namespace Database {
@@ -37,12 +40,12 @@ Track::getAll(Wt::Dbo::Session& session)
return session.find<Track>();
}
std::vector<Track::id_type>
std::vector<IdType>
Track::getAllIds(Wt::Dbo::Session& session)
{
Wt::Dbo::Transaction transaction(session);
Wt::Dbo::collection<Track::id_type> res = session.query<Track::id_type>("SELECT id from track");
return std::vector<Track::id_type>(res.begin(), res.end());
Wt::Dbo::collection<IdType> res = session.query<IdType>("SELECT id from track");
return std::vector<IdType>(res.begin(), res.end());
}
Track::pointer
@@ -52,7 +55,7 @@ Track::getByPath(Wt::Dbo::Session& session, const boost::filesystem::path& p)
}
Track::pointer
Track::getById(Wt::Dbo::Session& session, id_type id)
Track::getById(Wt::Dbo::Session& session, IdType id)
{
return session.find<Track>().where("id = ?").bind(id);
}
@@ -102,7 +105,7 @@ Track::getClusters(void) const
static
Wt::Dbo::Query< Track::pointer >
getQuery(Wt::Dbo::Session& session,
const std::set<Cluster::id_type>& clusterIds,
const std::set<IdType>& clusterIds,
const std::vector<std::string> keywords)
{
WhereClause where;
@@ -142,7 +145,7 @@ getQuery(Wt::Dbo::Session& session,
std::vector<Track::pointer>
Track::getByFilter(Wt::Dbo::Session& session,
const std::set<id_type>& clusterIds,
const std::set<IdType>& clusterIds,
const std::vector<std::string> keywords,
int offset, int size, bool& moreResults)
{
@@ -165,7 +168,7 @@ Track::getByFilter(Wt::Dbo::Session& session,
std::vector<Track::pointer>
Track::getByFilter(Wt::Dbo::Session& session,
const std::set<id_type>& clusters)
const std::set<IdType>& clusters)
{
bool moreResults;
@@ -233,7 +236,7 @@ Cluster::getAll(Wt::Dbo::Session& session)
}
Cluster::pointer
Cluster::getById(Wt::Dbo::Session& session, id_type id)
Cluster::getById(Wt::Dbo::Session& session, IdType id)
{
return session.find<Cluster>().where("id = ?").bind(id);
}
@@ -288,7 +291,7 @@ std::vector<Cluster::pointer>
ClusterType::getClusters() const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits<Release>::invalidId() );
assert(self()->id() != Wt::Dbo::dbo_traits<ClusterType>::invalidId() );
assert(session());
Wt::Dbo::collection<Cluster::pointer> res = session()->find<Cluster>()
@@ -298,5 +301,11 @@ ClusterType::getClusters() const
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
void
Cluster::addTrack(Wt::Dbo::ptr<Track> track)
{
_tracks.insert(track);
}
} // namespace Database
+8 -9
View File
@@ -31,6 +31,8 @@
#include <Wt/WDateTime.h>
#include "Types.hpp"
namespace Database {
@@ -44,14 +46,13 @@ class Cluster : public Wt::Dbo::Dbo<Cluster>
{
public:
typedef Wt::Dbo::ptr<Cluster> pointer;
typedef Wt::Dbo::dbo_traits<Cluster>::IdType id_type;
Cluster();
Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name);
// Find utility
static std::vector<pointer> getAll(Wt::Dbo::Session& session);
static pointer getById(Wt::Dbo::Session& session, id_type id);
static pointer getById(Wt::Dbo::Session& session, IdType id);
// Create utility
static pointer create(Wt::Dbo::Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name);
@@ -61,7 +62,7 @@ class Cluster : public Wt::Dbo::Dbo<Cluster>
Wt::Dbo::ptr<ClusterType> getType() const { return _clusterType; }
const Wt::Dbo::collection<Wt::Dbo::ptr<Track>>& getTracks() const { return _tracks; }
void addTrack(Wt::Dbo::ptr<Track> track) { _tracks.insert(track); }
void addTrack(Wt::Dbo::ptr<Track> track);
template<class Action>
void persist(Action& a)
@@ -88,7 +89,6 @@ class ClusterType : public Wt::Dbo::Dbo<ClusterType>
public:
using pointer = Wt::Dbo::ptr<ClusterType>;
using id_type = Wt::Dbo::dbo_traits<ClusterType>::IdType;
ClusterType() {}
ClusterType(std::string name);
@@ -126,7 +126,6 @@ class Track
public:
typedef Wt::Dbo::ptr<Track> pointer;
typedef Wt::Dbo::dbo_traits<Track>::IdType id_type;
Track() {}
Track(const boost::filesystem::path& p);
@@ -139,19 +138,19 @@ class Track
// Find utility functions
static pointer getByPath(Wt::Dbo::Session& session, const boost::filesystem::path& p);
static pointer getById(Wt::Dbo::Session& session, id_type id);
static pointer getById(Wt::Dbo::Session& session, IdType id);
static pointer getByMBID(Wt::Dbo::Session& session, const std::string& MBID);
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session,
const std::set<id_type>& clusters); // tracks that belong to these clusters
const std::set<IdType>& clusters); // tracks that belong to these clusters
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session,
const std::set<id_type>& clusters, // tracks that belong to these clusters
const std::set<IdType>& clusters, // tracks that belong to these clusters
const std::vector<std::string> keywords, // name must match all of these keywords
int offset,
int size,
bool& moreExpected);
static Wt::Dbo::collection< pointer > getAll(Wt::Dbo::Session& session);
static std::vector<id_type> getAllIds(Wt::Dbo::Session& session); // nested transaction
static std::vector<IdType> getAllIds(Wt::Dbo::Session& session); // nested transaction
static std::vector<boost::filesystem::path> getAllPaths(Wt::Dbo::Session& session); // nested transaction
static std::vector<pointer> getMBIDDuplicates(Wt::Dbo::Session& session);
static std::vector<pointer> getChecksumDuplicates(Wt::Dbo::Session& session);
+6 -7
View File
@@ -17,12 +17,11 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
// header file aimed to ease database class declarations
#pragma once
#include "DbArtist.hpp"
#include "Track.hpp"
#include "Playlist.hpp"
#include "Release.hpp"
#include "MediaDirectory.hpp"
#include "User.hpp"
#include <Wt/Dbo/Dbo.h>
namespace Database {
using IdType = Wt::Dbo::dbo_default_traits::IdType;
}
+1 -1
View File
@@ -17,7 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Types.hpp"
#include "User.hpp"
namespace Database {
+2 -4
View File
@@ -17,8 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DATABASE_USER_HPP
#define DATABASE_USER_HPP
#pragma once
#include <vector>
@@ -28,7 +27,7 @@
namespace Database {
class User;
typedef Wt::Auth::Dbo::AuthInfo<User> AuthInfo;
using AuthInfo = Wt::Auth::Dbo::AuthInfo<User>;
class Playlist;
@@ -109,4 +108,3 @@ class User : public Wt::Dbo::Dbo<User>
} // namespace Databas'
#endif