Split Cluster and Track in order to reduce compile dependencies

This commit is contained in:
emeric
2018-05-18 13:50:05 +02:00
parent 8f459e03a2
commit 97a630ce7f
18 changed files with 369 additions and 310 deletions
+126
View File
@@ -0,0 +1,126 @@
/*
* Copyright (C) 2013-2016 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Cluster.hpp"
#include "DbArtist.hpp"
#include "Release.hpp"
#include "SqlQuery.hpp"
#include "Track.hpp"
namespace Database {
Cluster::Cluster()
{
}
Cluster::Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name)
: _name(std::string(name, 0, _maxNameLength)),
_clusterType(type)
{
}
Cluster::pointer
Cluster::create(Wt::Dbo::Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name)
{
return session.add(std::make_unique<Cluster>(type, name));
}
std::vector<Cluster::pointer>
Cluster::getAll(Wt::Dbo::Session& session)
{
Wt::Dbo::collection<Cluster::pointer> res = session.find<Cluster>();
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
Cluster::pointer
Cluster::getById(Wt::Dbo::Session& session, IdType id)
{
return session.find<Cluster>().where("id = ?").bind(id);
}
ClusterType::ClusterType(std::string name)
: _name(name)
{
}
std::vector<ClusterType::pointer>
ClusterType::getAllOrphans(Wt::Dbo::Session& session)
{
Wt::Dbo::collection<pointer> res = session.query<Wt::Dbo::ptr<ClusterType>>("select c_t from cluster_type c_t LEFT OUTER JOIN cluster c ON c_t.id = c.cluster_type_id WHERE c.id IS NULL");
return std::vector<pointer>(res.begin(), res.end());
}
ClusterType::pointer
ClusterType::getByName(Wt::Dbo::Session& session, std::string name)
{
return session.find<ClusterType>().where("name = ?").bind(name);
}
std::vector<ClusterType::pointer>
ClusterType::getAll(Wt::Dbo::Session& session)
{
Wt::Dbo::collection<pointer> res = session.find<ClusterType>();
return std::vector<pointer>(res.begin(), res.end());
}
ClusterType::pointer
ClusterType::create(Wt::Dbo::Session& session, std::string name)
{
return session.add(std::make_unique<ClusterType>(name));
}
Cluster::pointer
ClusterType::getCluster(std::string name) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
return session()->find<Cluster>()
.where("name = ?").bind(name)
.where("cluster_type_id = ?").bind(self()->id());
}
std::vector<Cluster::pointer>
ClusterType::getClusters() const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::collection<Cluster::pointer> res = session()->find<Cluster>()
.where("cluster_type_id = ?").bind(self()->id())
.orderBy("name");
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
void
Cluster::addTrack(Wt::Dbo::ptr<Track> track)
{
_tracks.insert(track);
}
} // namespace Database
+115
View File
@@ -0,0 +1,115 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <vector>
#include <Wt/Dbo/Dbo.h>
#include <Wt/WDateTime.h>
#include "Types.hpp"
namespace Database {
class Track;
class ClusterType;
class Cluster : public Wt::Dbo::Dbo<Cluster>
{
public:
typedef Wt::Dbo::ptr<Cluster> pointer;
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, IdType id);
// Create utility
static pointer create(Wt::Dbo::Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name);
// Accessors
const std::string& getName(void) const { return _name; }
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);
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::belongsTo(a, _clusterType, "cluster_type", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "track_cluster", "", Wt::Dbo::OnDeleteCascade);
}
private:
static const std::size_t _maxNameLength = 128;
std::string _name;
Wt::Dbo::ptr<ClusterType> _clusterType;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
};
class ClusterType : public Wt::Dbo::Dbo<ClusterType>
{
public:
using pointer = Wt::Dbo::ptr<ClusterType>;
ClusterType() {}
ClusterType(std::string name);
static std::vector<pointer> getAllOrphans(Wt::Dbo::Session& session);
static pointer getByName(Wt::Dbo::Session& session, std::string name);
static std::vector<pointer> getAll(Wt::Dbo::Session& session);
static pointer create(Wt::Dbo::Session& session, std::string name);
static void remove(Wt::Dbo::Session& session, std::string name);
// Accessors
const std::string& getName(void) const { return _name; }
std::vector<Cluster::pointer> getClusters() const;
Cluster::pointer getCluster(std::string name) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToOne, "cluster_type");
}
private:
static const std::size_t _maxNameLength = 128;
std::string _name;
Wt::Dbo::collection< Wt::Dbo::ptr<Cluster> > _clusters;
};
} // namespace Database
+1
View File
@@ -36,6 +36,7 @@
#include "utils/Logger.hpp"
#include "DbArtist.hpp"
#include "Cluster.hpp"
#include "MediaDirectory.hpp"
#include "Playlist.hpp"
#include "Release.hpp"
+1
View File
@@ -20,6 +20,7 @@
#include "utils/Logger.hpp"
#include "Cluster.hpp"
#include "Release.hpp"
#include "SqlQuery.hpp"
#include "Track.hpp"
-1
View File
@@ -23,7 +23,6 @@
#include <vector>
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/QueryModel.h>
#include "Types.hpp"
-1
View File
@@ -24,7 +24,6 @@
#include <boost/filesystem/path.hpp>
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/WtSqlTraits.h>
namespace Database {
+1
View File
@@ -19,6 +19,7 @@
#include "Release.hpp"
#include "Cluster.hpp"
#include "DbArtist.hpp"
#include "SqlQuery.hpp"
#include "Track.hpp"
+4 -97
View File
@@ -19,8 +19,11 @@
#include "Track.hpp"
#include <Wt/Dbo/WtSqlTraits.h>
#include "utils/Logger.hpp"
#include "Cluster.hpp"
#include "DbArtist.hpp"
#include "Release.hpp"
#include "SqlQuery.hpp"
@@ -93,7 +96,7 @@ Track::getChecksumDuplicates(Wt::Dbo::Session& session)
return std::vector<pointer>(res.begin(), res.end());
}
std::vector< Cluster::pointer >
std::vector<Cluster::pointer>
Track::getClusters(void) const
{
std::vector< Cluster::pointer > clusters;
@@ -254,101 +257,5 @@ Track::getClusterGroups(std::vector<ClusterType::pointer> clusterTypes, std::siz
return res;
}
Cluster::Cluster()
{
}
Cluster::Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name)
: _name(std::string(name, 0, _maxNameLength)),
_clusterType(type)
{
}
Cluster::pointer
Cluster::create(Wt::Dbo::Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name)
{
return session.add(std::make_unique<Cluster>(type, name));
}
std::vector<Cluster::pointer>
Cluster::getAll(Wt::Dbo::Session& session)
{
Wt::Dbo::collection<Cluster::pointer> res = session.find<Cluster>();
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
Cluster::pointer
Cluster::getById(Wt::Dbo::Session& session, IdType id)
{
return session.find<Cluster>().where("id = ?").bind(id);
}
ClusterType::ClusterType(std::string name)
: _name(name)
{
}
std::vector<ClusterType::pointer>
ClusterType::getAllOrphans(Wt::Dbo::Session& session)
{
Wt::Dbo::collection<pointer> res = session.query<Wt::Dbo::ptr<ClusterType>>("select c_t from cluster_type c_t LEFT OUTER JOIN cluster c ON c_t.id = c.cluster_type_id WHERE c.id IS NULL");
return std::vector<pointer>(res.begin(), res.end());
}
ClusterType::pointer
ClusterType::getByName(Wt::Dbo::Session& session, std::string name)
{
return session.find<ClusterType>().where("name = ?").bind(name);
}
std::vector<ClusterType::pointer>
ClusterType::getAll(Wt::Dbo::Session& session)
{
Wt::Dbo::collection<pointer> res = session.find<ClusterType>();
return std::vector<pointer>(res.begin(), res.end());
}
ClusterType::pointer
ClusterType::create(Wt::Dbo::Session& session, std::string name)
{
return session.add(std::make_unique<ClusterType>(name));
}
Cluster::pointer
ClusterType::getCluster(std::string name) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
return session()->find<Cluster>()
.where("name = ?").bind(name)
.where("cluster_type_id = ?").bind(self()->id());
}
std::vector<Cluster::pointer>
ClusterType::getClusters() const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::collection<Cluster::pointer> res = session()->find<Cluster>()
.where("cluster_type_id = ?").bind(self()->id())
.orderBy("name");
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
void
Cluster::addTrack(Wt::Dbo::ptr<Track> track)
{
_tracks.insert(track);
}
} // namespace Database
+2 -82
View File
@@ -27,7 +27,6 @@
#include <boost/optional.hpp>
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/WtSqlTraits.h>
#include <Wt/WDateTime.h>
@@ -35,91 +34,12 @@
namespace Database {
class Artist;
class Release;
class Track;
class PlaylistEntry;
class Cluster;
class ClusterType;
class Cluster : public Wt::Dbo::Dbo<Cluster>
{
public:
typedef Wt::Dbo::ptr<Cluster> pointer;
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, IdType id);
// Create utility
static pointer create(Wt::Dbo::Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name);
// Accessors
const std::string& getName(void) const { return _name; }
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);
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::belongsTo(a, _clusterType, "cluster_type", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "track_cluster", "", Wt::Dbo::OnDeleteCascade);
}
private:
static const std::size_t _maxNameLength = 128;
std::string _name;
Wt::Dbo::ptr<ClusterType> _clusterType;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
};
class ClusterType : public Wt::Dbo::Dbo<ClusterType>
{
public:
using pointer = Wt::Dbo::ptr<ClusterType>;
ClusterType() {}
ClusterType(std::string name);
static std::vector<pointer> getAllOrphans(Wt::Dbo::Session& session);
static pointer getByName(Wt::Dbo::Session& session, std::string name);
static std::vector<pointer> getAll(Wt::Dbo::Session& session);
static pointer create(Wt::Dbo::Session& session, std::string name);
static void remove(Wt::Dbo::Session& session, std::string name);
// Accessors
const std::string& getName(void) const { return _name; }
std::vector<Cluster::pointer> getClusters() const;
Cluster::pointer getCluster(std::string name) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToOne, "cluster_type");
}
private:
static const std::size_t _maxNameLength = 128;
std::string _name;
Wt::Dbo::collection< Wt::Dbo::ptr<Cluster> > _clusters;
};
class Track : public Wt::Dbo::Dbo<Track>
{
public:
@@ -194,7 +114,7 @@ class Track : public Wt::Dbo::Dbo<Track>
const std::string& getMBID(void) const { return _MBID; }
Wt::Dbo::ptr<Artist> getArtist(void) const { return _artist; }
Wt::Dbo::ptr<Release> getRelease(void) const { return _release; }
std::vector< Cluster::pointer > getClusters(void) const;
std::vector<Wt::Dbo::ptr<Cluster>> getClusters(void) const;
std::vector<std::vector<Wt::Dbo::ptr<Cluster>>> getClusterGroups(std::vector<Wt::Dbo::ptr<ClusterType>> clusterTypes, std::size_t size) const;