New UI: first step

This commit is contained in:
emeric
2018-01-28 14:15:50 +01:00
parent d8f8c77c07
commit 6591006fcc
98 changed files with 1602 additions and 8515 deletions
+1 -1
View File
@@ -187,7 +187,7 @@ Handler::createConnectionPool(boost::filesystem::path p)
connection->executeSql("pragma journal_mode=WAL");
// connection->setProperty("show-queries", "true");
connection->setProperty("show-queries", "true");
return new Wt::Dbo::FixedSqlConnectionPool(connection, 1);
}
+1
View File
@@ -33,6 +33,7 @@
namespace Database {
typedef Wt::Dbo::dbo_default_traits::IdType id_type;
typedef Wt::Auth::Dbo::UserDatabase<AuthInfo> UserDatabase;
// Session living class handling the database and the login
+202
View File
@@ -0,0 +1,202 @@
/*
* Copyright (C) 2015 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 "Types.hpp"
#include "SqlQuery.hpp"
#include "utils/Logger.hpp"
namespace Database
{
Artist::Artist(const std::string& name, const std::string& MBID)
: _name(std::string(name, 0 , _maxNameLength)),
_MBID(MBID)
{
}
std::vector<Artist::pointer>
Artist::getByName(Wt::Dbo::Session& session, const std::string& name)
{
Wt::Dbo::collection<Artist::pointer> res = session.find<Artist>().where("name = ?").bind( std::string(name, 0, _maxNameLength) );
return std::vector<Artist::pointer>(res.begin(), res.end());
}
Artist::pointer
Artist::getByMBID(Wt::Dbo::Session& session, const std::string& mbid)
{
return session.find<Artist>().where("mbid = ?").bind(mbid);
}
Artist::pointer
Artist::getById(Wt::Dbo::Session& session, Artist::id_type id)
{
return session.find<Artist>().where("id = ?").bind(id);
}
Artist::pointer
Artist::create(Wt::Dbo::Session& session, const std::string& name, const std::string& MBID)
{
return session.add(new Artist(name, MBID));
}
Artist::pointer
Artist::getNone(Wt::Dbo::Session& session)
{
std::vector<pointer> res = getByName(session, "<None>");
if (res.empty())
return create(session, "<None>");
return res.front();
}
bool
Artist::isNone() const
{
return _name == "<None>";
}
std::vector<Artist::pointer>
Artist::getAll(Wt::Dbo::Session& session, int offset, int size)
{
Wt::Dbo::collection<pointer> res = session.find<Artist>().orderBy("LOWER(name)").offset(offset).limit(size);
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Artist::pointer>
Artist::getAllOrphans(Wt::Dbo::Session& session)
{
Wt::Dbo::collection<Artist::pointer> res = session.query< Wt::Dbo::ptr<Artist> >("SELECT DISTINCT a FROM artist a LEFT OUTER JOIN Track t ON a.id = t.artist_id WHERE t.id IS NULL");
return std::vector<pointer>(res.begin(), res.end());
}
static
Wt::Dbo::Query<Artist::pointer>
getQuery(Wt::Dbo::Session& session,
const std::vector<id_type>& clusterIds,
const std::vector<std::string>& keywords)
{
WhereClause where;
std::ostringstream oss;
oss << "SELECT DISTINCT a FROM artist a";
for (auto keyword : keywords)
where.And(WhereClause("a.name LIKE ?")).bind("%%" + keyword + "%%");
if (!clusterIds.empty())
{
oss << " INNER JOIN track t ON t.artist_id = a.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id";
WhereClause clusterClause;
for (auto id : clusterIds)
clusterClause.Or(WhereClause("c.id = ?")).bind(std::to_string(id));
where.And(clusterClause);
}
oss << " " << where.get();
if (!clusterIds.empty())
oss << " GROUP BY t.id HAVING COUNT(*) = " << clusterIds.size();
oss << " ORDER BY a.name";
Wt::Dbo::Query<Artist::pointer> query = session.query<Artist::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
{
query.bind(bindArg);
}
return query;
}
std::vector<Artist::pointer>
Artist::getByFilter(Wt::Dbo::Session& session,
const std::vector<id_type>& clusters,
const std::vector<std::string> keywords,
int offset, int size, bool& moreResults)
{
Wt::Dbo::collection<Artist::pointer> collection = getQuery(session, clusters, keywords).limit(size).offset(offset);
auto res = std::vector<pointer>(collection.begin(), collection.end());
if (size != -1 && res.size() == static_cast<std::size_t>(size) + 1)
{
moreResults = true;
res.pop_back();
}
else
moreResults = false;
return res;
}
std::vector<Wt::Dbo::ptr<Release> >
Artist::getReleases(const std::vector<id_type>& clusterIds) const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits<Artist>::invalidId() );
assert(session());
WhereClause where;
std::ostringstream oss;
oss << "SELECT DISTINCT r FROM release r INNER JOIN artist a ON t.artist_id = a.id INNER JOIN track t ON t.release_id = r.id";
if (!clusterIds.empty())
{
oss << " INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id";
WhereClause clusterClause;
for (auto id : clusterIds)
clusterClause.Or(WhereClause("c.id = ?")).bind(std::to_string(id));
where.And(clusterClause);
}
where.And(WhereClause("a.id = ?")).bind(std::to_string(id()));
oss << " " << where.get();
if (!clusterIds.empty())
oss << " GROUP BY t.id HAVING COUNT(*) = " << clusterIds.size();
// TODO order
oss << " ORDER BY t.date,r.name";
Wt::Dbo::Query<Release::pointer> query = session()->query<Release::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
{
query.bind(bindArg);
}
Wt::Dbo::collection< Wt::Dbo::ptr<Release> > res = query;
return std::vector< Wt::Dbo::ptr<Release> > (res.begin(), res.end());
}
} // namespace Database
+98
View File
@@ -0,0 +1,98 @@
/*
* Copyright (C) 2015 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/>.
*/
#ifndef _DB_ARTIST_HPP_
#define _DB_ARTIST_HPP_
#include <string>
#include <vector>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/QueryModel>
#include "SearchFilter.hpp"
namespace Database
{
class Track;
class Cluster;
class Release;
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 getNone(Wt::Dbo::Session& session); // Special entry
static std::vector<pointer> getByName(Wt::Dbo::Session& session, const std::string& name);
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session,
const std::vector<id_type>& 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);
static std::vector<pointer> getAll(Wt::Dbo::Session& session, int offset = -1, int size = -1);
static std::vector<pointer> getAllOrphans(Wt::Dbo::Session& session); // No track related
// Accessors
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 + belongs to optonal cluster filters
std::vector<Wt::Dbo::ptr<Release>> getReleases(const std::vector<id_type>& clusterIds = std::vector<id_type>()) const;
void setMBID(std::string mbid) { _MBID = mbid; }
// Create
static pointer create(Wt::Dbo::Session& session, const std::string& name, const std::string& MBID = "");
bool isNone(void) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _MBID, "mbid");
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToOne, "artist");
}
private:
static const std::size_t _maxNameLength = 128;
std::string _name;
std::string _MBID; // Musicbrainz Identifier
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> _tracks; // Tracks of this artist
};
} // namespace Database
#endif
+87 -59
View File
@@ -82,68 +82,61 @@ Release::getAll(Wt::Dbo::Session& session, int offset, int size)
std::vector<Release::pointer>
Release::getAllOrphans(Wt::Dbo::Session& session)
{
Wt::Dbo::collection<Release::pointer> res = session.query< Wt::Dbo::ptr<Release> >("select r from release r LEFT OUTER JOIN Track t ON r.id = t.release_id WHERE t.id IS NULL");
Wt::Dbo::collection<Release::pointer> res = session.query<Wt::Dbo::ptr<Release>>("select r from release r LEFT OUTER JOIN Track t ON r.id = t.release_id WHERE t.id IS NULL");
return std::vector<pointer>(res.begin(), res.end());
}
static
Wt::Dbo::Query<Release::pointer>
Release::getQuery(Wt::Dbo::Session& session, SearchFilter filter)
getQuery(Wt::Dbo::Session& session,
const std::vector<id_type>& clusterIds,
const std::vector<std::string> keywords)
{
SqlQuery sqlQuery = generatePartialQuery(filter);
WhereClause where;
Wt::Dbo::Query<pointer> query
= session.query<pointer>("SELECT r FROM release r INNER JOIN artist a ON a.id = t.artist_id INNER JOIN track t ON t.release_id = r.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id " + sqlQuery.where().get()).groupBy("r.id").orderBy("r.name");
std::ostringstream oss;
oss << "SELECT DISTINCT r FROM release r";
for (const std::string& bindArg : sqlQuery.where().getBindArgs())
query.bind(bindArg);
for (auto keyword : keywords)
where.And(WhereClause("r.name LIKE ?")).bind("%%" + keyword + "%%");
return query;
}
Wt::Dbo::Query<Release::UIQueryResult>
Release::getUIQuery(Wt::Dbo::Session& session, SearchFilter filter)
{
SqlQuery sqlQuery = generatePartialQuery(filter);
// TODO DATE of RELEASE
Wt::Dbo::Query<UIQueryResult> query
= session.query<UIQueryResult>("SELECT r.id, r.name, t.date, COUNT(DISTINCT t.id) FROM release r INNER JOIN track t ON t.release_id = r.id INNER JOIN artist a ON a.id = t.artist_id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id " + sqlQuery.where().get()).groupBy("r.id").orderBy("r.name");
for (const std::string& bindArg : sqlQuery.where().getBindArgs())
query.bind(bindArg);
return query;
}
void
Release::updateUIQueryModel(Wt::Dbo::Session& session, Wt::Dbo::QueryModel<UIQueryResult>& model, SearchFilter filter, const std::vector<Wt::WString>& columnNames)
{
Wt::Dbo::Query<UIQueryResult> query = getUIQuery(session, filter);
model.setQuery(query, columnNames.empty() ? true : false);
// TODO do something better
if (columnNames.size() == 3)
if (!clusterIds.empty())
{
model.addColumn( "r.name", columnNames[0]);
model.addColumn( "t.date", columnNames[1]);
model.addColumn( "COUNT(DISTINCT t.id)", columnNames[2]);
oss << " INNER JOIN track t ON t.release_id = r.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id";
WhereClause clusterClause;
for (auto id : clusterIds)
clusterClause.And(WhereClause("c.id = ?")).bind(std::to_string(id));
where.And(clusterClause);
}
oss << " " << where.get();
if (!clusterIds.empty())
oss << " GROUP BY t.id HAVING COUNT(*) = " << clusterIds.size();
oss << " ORDER BY r.name";
Wt::Dbo::Query<Release::pointer> query = session.query<Release::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
query.bind(bindArg);
return query;
}
std::vector<Release::pointer>
Release::getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, int size)
Release::getByFilter(Wt::Dbo::Session& session,
const std::vector<id_type>& clusterIds,
const std::vector<std::string> keywords,
int offset, int size, bool& moreResults)
{
Wt::Dbo::collection<pointer> res = getQuery(session, filter).limit(size).offset(offset);
Wt::Dbo::collection<pointer> collection = getQuery(session, clusterIds, keywords).limit(size).offset(offset);
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Release::pointer>
Release::getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, int size, bool& moreResults)
{
auto res = getByFilter(session, filter, offset, size + 1);
auto res = std::vector<pointer>(collection.begin(), collection.end());
if (size != -1 && res.size() == static_cast<std::size_t>(size) + 1)
{
@@ -156,28 +149,63 @@ Release::getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset,
return res;
}
int
boost::optional<int>
Release::getReleaseYear(bool original) const
{
assert(session());
// TODO something better
auto tracks = Track::getByFilter(*session(), SearchFilter::ById(SearchFilter::Field::Release, this->id()), -1, 1);
Wt::Dbo::collection<boost::posix_time::ptime> times = session()->query<boost::posix_time::ptime>(
std::string("SELECT ") + (original ? "t.original_date" : "t.date") + " FROM track t INNER JOIN release r ON r.id = t.release_id")
.where("r.id = ?")
.groupBy("t.date")
.bind(this->id());
if (tracks.empty())
return 0;
/* various dates, no date */
if (times.empty() || times.size() > 1)
return boost::none;
boost::gregorian::date date;
if (original)
date = tracks.front()->getOriginalDate().date();
else
date = tracks.front()->getDate().date();
boost::gregorian::date date = times.front().date();
if (date.is_special())
return 0;
return boost::none;
return date.year();
return boost::make_optional<int>(date.year());
}
std::vector<Wt::Dbo::ptr<Artist>>
Release::getArtists() const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits<Release>::invalidId() );
assert(session());
Wt::Dbo::collection<Wt::Dbo::ptr<Artist>> res = session()->query<Wt::Dbo::ptr<Artist>>(
"SELECT DISTINCT a FROM artist a INNER JOIN release r ON t.artist_id = a.id INNER JOIN track t ON t.release_id = r.id")
.where("r.id = ?")
.bind(id());
return std::vector<Wt::Dbo::ptr<Artist>>(res.begin(), res.end());
}
bool
Release::hasVariousArtists() const
{
// TODO optimize
return getArtists().size() > 1;
}
std::vector<Wt::Dbo::ptr<Track>>
Release::getTracks() const
{
assert(self());
assert(session());
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> res = session()->query<Wt::Dbo::ptr<Track>>(
"SELECT t FROM track t INNER JOIN release r ON t.release_id = r.id")
.where("r.id = ?")
.orderBy("t.disc_number,t.track_number")
.bind(id());
return std::vector<Wt::Dbo::ptr<Track>>(res.begin(), res.end());
}
} // namespace Database
+20 -18
View File
@@ -17,11 +17,11 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _DB_RELEASE_HPP_
#define _DB_RELEASE_HPP_
#pragma once
#include <boost/optional.hpp>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/QueryModel>
#include "SearchFilter.hpp"
@@ -30,6 +30,7 @@ namespace Database
class Track;
class Release;
class Artist;
class Release : public Wt::Dbo::Dbo<Release>
{
@@ -46,30 +47,34 @@ class Release : public Wt::Dbo::Dbo<Release>
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 getNone(Wt::Dbo::Session& session); // Special entry
static std::vector<pointer> getAllOrphans(Wt::Dbo::Session& session);
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, SearchFilter filter, int offset = -1, int size = -1);
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, int size, bool& moreExpected);
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session,
const std::vector<id_type>& 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;
// Create
static pointer create(Wt::Dbo::Session& session, const std::string& name, const std::string& MBID = "");
// Utility functions
int getReleaseYear(bool originalDate = false) const; // 0 if unknown or various
boost::optional<int> getReleaseYear(bool originalDate = false) const; // 0 if unknown or various
// MVC models for the user interface
// ID, Release name, year, track counts
typedef boost::tuple<id_type, std::string, boost::posix_time::ptime, int> UIQueryResult;
static Wt::Dbo::Query<UIQueryResult> getUIQuery(Wt::Dbo::Session& session, SearchFilter filter);
static void updateUIQueryModel(Wt::Dbo::Session& session, Wt::Dbo::QueryModel< UIQueryResult >& model, SearchFilter filter, const std::vector<Wt::WString>& columnNames = std::vector<Wt::WString>());
// Accessosrs
// Accessors
std::string getName() const { return _name; }
std::string getMBID() const { return _MBID; }
bool isNone(void) const;
boost::posix_time::time_duration getDuration(void) const;
// Get the artists of this release
std::vector<Wt::Dbo::ptr<Artist> > getArtists() const;
bool hasVariousArtists() const;
void setMBID(std::string mbid) { _MBID = mbid; }
template<class Action>
@@ -82,8 +87,6 @@ class Release : public Wt::Dbo::Dbo<Release>
}
private:
static Wt::Dbo::Query<pointer> getQuery(Wt::Dbo::Session& session, SearchFilter filter);
static const std::size_t _maxNameLength = 128;
std::string _name;
@@ -94,5 +97,4 @@ class Release : public Wt::Dbo::Dbo<Release>
} // namespace Database
#endif
+4 -87
View File
@@ -24,98 +24,15 @@
namespace Database
{
static std::ostream& operator<<(std::ostream& ost, const std::vector< Wt::Dbo::dbo_default_traits::IdType>& ids)
void
SearchFilter::operator+=(const SearchFilter& filter)
{
const char *sep = "";
for (auto id : ids)
{
ost << sep << std::to_string(id);
sep = ",";
}
return ost;
}
SqlQuery generatePartialQuery(SearchFilter& filter)
SqlQuery
SearchFilter::generatePartialQuery()
{
SqlQuery sqlQuery;
// Process name like parameters
for (auto nameLikeMatches : filter.nameLikeMatch)
{
WhereClause likeWhereClause;
for (auto nameLikeMatch : nameLikeMatches)
{
switch (nameLikeMatch.first)
{
case SearchFilter::Field::Artist:
for (const std::string& name : nameLikeMatch.second)
likeWhereClause.Or( WhereClause("a.name LIKE ?") ).bind("%%" + name + "%%");
break;
case SearchFilter::Field::Release:
for (const std::string& name : nameLikeMatch.second)
likeWhereClause.Or( WhereClause("r.name LIKE ?") ).bind("%%" + name + "%%");
break;
case SearchFilter::Field::Cluster:
for (const std::string& name : nameLikeMatch.second)
likeWhereClause.Or( WhereClause("c.name LIKE ?") ).bind("%%" + name + "%%");
break;
case SearchFilter::Field::Track:
for (const std::string& name : nameLikeMatch.second)
likeWhereClause.Or( WhereClause("t.name LIKE ?") ).bind("%%" + name + "%%");
break;
}
}
sqlQuery.where().And( likeWhereClause );
}
// Process id exact match parameters
// Id list may be long, we do not bind the parameters
// Safe since we already known these are just ints
for (auto idMatch : filter.idMatch)
{
WhereClause idWhereClause;
switch (idMatch.first)
{
case SearchFilter::Field::Artist:
{
std::ostringstream oss;
oss << "a.id IN (" << idMatch.second << ")";
idWhereClause.Or(oss.str());
}
break;
case SearchFilter::Field::Release:
{
std::ostringstream oss;
oss << "r.id IN (" << idMatch.second << ")";
idWhereClause.Or(oss.str());
}
break;
case SearchFilter::Field::Cluster:
{
std::ostringstream oss;
oss << "c.id IN (" << idMatch.second << ")";
idWhereClause.Or(oss.str());
}
break;
case SearchFilter::Field::Track:
{
std::ostringstream oss;
oss << "t.id IN (" << idMatch.second << ")";
idWhereClause.Or(oss.str());
}
break;
}
sqlQuery.where().And( idWhereClause );
}
return sqlQuery;
}
+14 -48
View File
@@ -31,71 +31,37 @@
namespace Database
{
typedef Wt::Dbo::dbo_default_traits::IdType id_type;
class SearchFilter
{
public:
enum class Field {
Artist, // artist
Release, // release
Track, // track
Cluster, // cluster
};
typedef std::vector<std::map<Field, std::vector<std::string> > > NameLikeMatchType;
typedef std::map<Field, std::vector< Wt::Dbo::dbo_default_traits::IdType> > IdMatchType;
typedef int id_type;
SearchFilter() {}
// Helpers
static SearchFilter IdMatch( const IdMatchType& _idMatch )
{
return SearchFilter(_idMatch);
}
static SearchFilter Artist(std::string name) {return SearchFilter();}
static SearchFilter Artist(id_type id) {return SearchFilter();}
static SearchFilter NameLikeMatch( const NameLikeMatchType& _nameLikeMatch )
{
return SearchFilter(_nameLikeMatch);
}
static SearchFilter Release(std::string name) {return SearchFilter();}
static SearchFilter Release(id_type id) {return SearchFilter();}
// Single Field ID match
static SearchFilter ById(Field field, Wt::Dbo::dbo_default_traits::IdType id)
{
return SearchFilter({{field, {id} }});
}
static SearchFilter Track(std::string name) {return SearchFilter();}
// Single Field Name match OR
static SearchFilter ByNameOr(Field field, std::vector<std::string> keywords)
{
return NameLikeMatch({{{field, keywords}}});
}
static SearchFilter Cluster(id_type id) {return SearchFilter();}
// Single Field Name match AND
static SearchFilter ByNameAnd(Field field, std::vector<std::string> keywords)
{
NameLikeMatchType nameLikeMatch;
// Combine search filters by add operation
// Caution: multiple filters on different artist/release/track
// values may lead to empty results
void operator+=(const SearchFilter& filter);
for (auto keyword : keywords)
nameLikeMatch.push_back({{{field, {keyword}}}});
return NameLikeMatch(nameLikeMatch);
}
// The filter is a AND of the following conditions:
// ((Field1.name LIKE STR1-1 OR Field1.name LIKE STR1-2 ...) OR (Field2.name LIKE STR2-1 OR Field2.name LIKE STR2-2 ...) ...
NameLikeMatchType nameLikeMatch;
// (Field1.id IN (ID1-1,ID1-2 ... ) AND (Field2.id IN (ID2-1,ID2-2 ... ) ...
IdMatchType idMatch;
SqlQuery generatePartialQuery();
private:
SearchFilter(const NameLikeMatchType& _nameLikeMatch) : nameLikeMatch(_nameLikeMatch) {}
SearchFilter(const IdMatchType& _idMatch) : idMatch(_idMatch) {}
};
SqlQuery generatePartialQuery(SearchFilter& filter);
} // namespace Database
+62 -71
View File
@@ -108,29 +108,42 @@ Track::getClusters(void) const
return clusters;
}
static
Wt::Dbo::Query< Track::pointer >
Track::getQuery(Wt::Dbo::Session& session, SearchFilter filter)
getQuery(Wt::Dbo::Session& session,
const std::vector<id_type>& clusterIds,
const std::vector<std::string> keywords)
{
SqlQuery sqlQuery = generatePartialQuery(filter);
WhereClause where;
Wt::Dbo::Query<pointer> query
= session.query<pointer>( "SELECT t FROM track t INNER JOIN artist a ON t.artist_id = a.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN release r ON r.id = t.release_id " + sqlQuery.where().get()).groupBy("t.id").orderBy("a.name,t.date,r.name,t.disc_number,t.track_number");
std::ostringstream oss;
oss << "SELECT t FROM track t";
for (const std::string& bindArg : sqlQuery.where().getBindArgs())
query.bind(bindArg);
for (auto keyword : keywords)
where.And(WhereClause("t.name LIKE ?")).bind("%%" + keyword + "%%");
return query;
}
if (!clusterIds.empty())
{
oss << " INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id";
Wt::Dbo::Query< Track::UIQueryResult >
Track::getUIQuery(Wt::Dbo::Session& session, SearchFilter filter)
{
SqlQuery sqlQuery = generatePartialQuery(filter);
WhereClause clusterClause;
Wt::Dbo::Query<UIQueryResult> query
= session.query<UIQueryResult>( "SELECT t.id, a.name, r.name, t.disc_number, t.track_number, t.name, t.duration, t.date, t.original_date, t.genre_list FROM track t INNER JOIN artist a ON t.artist_id = a.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN release r ON r.id = t.release_id " + sqlQuery.where().get()).groupBy("t.id").orderBy("a.name,t.date,r.name,t.disc_number,t.track_number");
for (auto id : clusterIds)
clusterClause.And(WhereClause("c.id = ?")).bind(std::to_string(id));
for (const std::string& bindArg : sqlQuery.where().getBindArgs())
where.And(clusterClause);
}
oss << " " << where.get();
if (!clusterIds.empty())
oss << " GROUP BY t.id HAVING COUNT(*) = " << clusterIds.size();
oss << " ORDER BY t.name";
Wt::Dbo::Query<Track::pointer> query = session.query<Track::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
query.bind(bindArg);
return query;
@@ -139,7 +152,7 @@ Track::getUIQuery(Wt::Dbo::Session& session, SearchFilter filter)
Track::StatsQueryResult
Track::getStats(Wt::Dbo::Session& session, SearchFilter filter)
{
SqlQuery sqlQuery = generatePartialQuery(filter);
SqlQuery sqlQuery = filter.generatePartialQuery();
Wt::Dbo::Query<StatsQueryResult> query = session.query<StatsQueryResult>( "SELECT COUNT(\"id\"), SUM(\"dur\") FROM (SELECT t.id as \"id\", t.duration as \"dur\" FROM track t INNER JOIN artist a ON t.artist_id = a.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN release r ON r.id = t.release_id " + sqlQuery.where().get() + " GROUP BY t.id)");
@@ -151,17 +164,14 @@ Track::getStats(Wt::Dbo::Session& session, SearchFilter filter)
std::vector<Track::pointer>
Track::getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, int size)
Track::getByFilter(Wt::Dbo::Session& session,
const std::vector<id_type>& clusterIds,
const std::vector<std::string> keywords,
int offset, int size, bool& moreResults)
{
Wt::Dbo::collection<pointer> res = getQuery(session, filter).limit(size).offset(offset);
Wt::Dbo::collection<pointer> collection = getQuery(session, clusterIds, keywords).limit(size).offset(offset);
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Track::pointer>
Track::getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, int size, bool& moreResults)
{
auto res = getByFilter(session, filter, offset, size + 1);
auto res = std::vector<pointer>(collection.begin(), collection.end());
if (size != -1 && res.size() == static_cast<std::size_t>(size) + 1)
{
@@ -174,26 +184,28 @@ Track::getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, i
return res;
}
void
Track::updateUIQueryModel(Wt::Dbo::Session& session, Wt::Dbo::QueryModel< UIQueryResult >& model, SearchFilter filter, const std::vector<Wt::WString>& columnNames)
boost::optional<std::size_t>
Track::getTrackNumber(void) const
{
Wt::Dbo::Query< UIQueryResult > query = getUIQuery(session, filter);
model.setQuery(query, columnNames.empty() ? true : false);
return (_trackNumber > 0) ? boost::make_optional<std::size_t>(_trackNumber) : boost::none;
}
// TODO do something better
if (columnNames.size() == 9)
{
model.addColumn( "a.name", columnNames[0] );
model.addColumn( "r.name", columnNames[1] );
model.addColumn( "t.disc_number", columnNames[2] );
model.addColumn( "t.track_number", columnNames[3] );
model.addColumn( "t.name", columnNames[4] );
model.addColumn( "t.duration", columnNames[5] );
model.addColumn( "t.date", columnNames[6] );
model.addColumn( "t.original_date", columnNames[7] );
model.addColumn( "t.genre_list", columnNames[8] );
}
boost::optional<std::size_t>
Track::getTotalTrackNumber(void) const
{
return (_totalTrackNumber > 0) ? boost::make_optional<std::size_t>(_totalTrackNumber) : boost::none;
}
boost::optional<std::size_t>
Track::getDiscNumber(void) const
{
return (_discNumber > 0) ? boost::make_optional<std::size_t>(_discNumber) : boost::none;
}
boost::optional<std::size_t>
Track::getTotalDiscNumber(void) const
{
return (_totalDiscNumber > 0) ? boost::make_optional<std::size_t>(_totalDiscNumber) : boost::none;
}
@@ -221,6 +233,13 @@ Cluster::get(Wt::Dbo::Session& session, std::string type, std::string name)
return session.find<Cluster>().where("type = ?").where("name = ?").bind( std::string(type, 0, _maxTypeLength)).bind( std::string(name, 0, _maxNameLength));
}
std::vector<Cluster::pointer>
Cluster::getByType(Wt::Dbo::Session& session, std::string type)
{
Wt::Dbo::collection<pointer> res = session.find<Cluster>().where("type = ?").bind( std::string(type, 0, _maxTypeLength)).orderBy("name");
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
Cluster::pointer
Cluster::getNone(Wt::Dbo::Session& session)
{
@@ -252,7 +271,7 @@ Cluster::remove(Wt::Dbo::Session& session, std::string type)
Wt::Dbo::Query<Cluster::pointer>
Cluster::getQuery(Wt::Dbo::Session& session, SearchFilter filter)
{
SqlQuery sqlQuery = generatePartialQuery(filter);
SqlQuery sqlQuery = filter.generatePartialQuery();
Wt::Dbo::Query<pointer> query
= session.query<pointer>( "SELECT g FROM cluster c INNER JOIN track_cluster t_c ON t_c.cluster_id = c.id INNER JOIN artist a ON t.artist_id = a.id INNER JOIN release r ON r.id = t.release_id INNER JOIN track t ON t.id = t_c.track_id " + sqlQuery.where().get()).groupBy("c.name").orderBy("c.name");
@@ -263,34 +282,6 @@ Cluster::getQuery(Wt::Dbo::Session& session, SearchFilter filter)
return query;
}
Wt::Dbo::Query<Cluster::UIQueryResult>
Cluster::getUIQuery(Wt::Dbo::Session& session, SearchFilter filter)
{
SqlQuery sqlQuery = generatePartialQuery(filter);
Wt::Dbo::Query<UIQueryResult> query
= session.query<UIQueryResult>( "SELECT c.id, c.name, COUNT(DISTINCT t.id) FROM cluster c INNER JOIN track_cluster t_c ON t_c.cluster_id = c.id INNER JOIN artist a ON t.artist_id = a.id INNER JOIN release r ON r.id = t.release_id INNER JOIN track t ON t.id = t_c.track_id " + sqlQuery.where().get()).groupBy("c.name").orderBy("c.name");
for (const std::string& bindArg : sqlQuery.where().getBindArgs())
query.bind(bindArg);
return query;
}
void
Cluster::updateUIQueryModel(Wt::Dbo::Session& session, Wt::Dbo::QueryModel<UIQueryResult>& model, SearchFilter filter, const std::vector<Wt::WString>& columnNames)
{
Wt::Dbo::Query<UIQueryResult> query = getUIQuery(session, filter);
model.setQuery(query, columnNames.empty() ? true : false);
// TODO do something better
if (columnNames.size() == 2)
{
model.addColumn( "c.name", columnNames[0] );
model.addColumn( "COUNT(DISTINCT t.id)", columnNames[1] );
}
}
std::vector<Cluster::pointer>
Cluster::getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, int size)
{
+18 -30
View File
@@ -24,6 +24,7 @@
#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/optional.hpp>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/WtSqlTraits>
@@ -44,6 +45,12 @@ class Cluster
{
public:
enum class Type
{
Genre = 1,
Mood = 2,
};
typedef Wt::Dbo::ptr<Cluster> pointer;
typedef Wt::Dbo::dbo_traits<Cluster>::IdType id_type;
@@ -57,12 +64,6 @@ class Cluster
static Wt::Dbo::collection<pointer> getAll(Wt::Dbo::Session& session);
static std::vector<pointer> getByType(Wt::Dbo::Session& session, std::string type);
// MVC models for the user interface
// ClusterID, type, name, track count
typedef boost::tuple<id_type, std::string, int> UIQueryResult;
static Wt::Dbo::Query<UIQueryResult> getUIQuery(Wt::Dbo::Session& session, SearchFilter filter);
static void updateUIQueryModel(Wt::Dbo::Session& session, Wt::Dbo::QueryModel<UIQueryResult>& model, SearchFilter filter, const std::vector<Wt::WString>& columnNames = std::vector<Wt::WString>());
// Create utility
static pointer create(Wt::Dbo::Session& session, std::string type, std::string name);
@@ -118,8 +119,13 @@ class Track
static pointer getByPath(Wt::Dbo::Session& session, const boost::filesystem::path& p);
static pointer getById(Wt::Dbo::Session& session, id_type id);
static pointer getByMBID(Wt::Dbo::Session& session, const std::string& MBID);
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset = -1, int size = -1);
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session, SearchFilter filter, int offset, int size, bool &moreResults);
static std::vector<pointer> getByFilter(Wt::Dbo::Session& session,
const std::vector<id_type>& 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<boost::filesystem::path> getAllPaths(Wt::Dbo::Session& session); // nested transaction
@@ -127,22 +133,6 @@ class Track
static std::vector<pointer> getChecksumDuplicates(Wt::Dbo::Session& session);
// Utility fonctions
// MVC models for the user interface
// ID, Artist name, Release Name, DiscNumber, TrackNumber, Name, duration, date, original date, genre list
typedef boost::tuple<id_type, // ID
std::string, // Artist name
std::string, // Release Name
int, // Disc Number
int, // Track Number
std::string, // Name
boost::posix_time::time_duration, // Duration
boost::posix_time::ptime, // Date
boost::posix_time::ptime, // Original date
std::string> // genre list
UIQueryResult;
static Wt::Dbo::Query< UIQueryResult > getUIQuery(Wt::Dbo::Session& session, SearchFilter filter);
static void updateUIQueryModel(Wt::Dbo::Session& session, Wt::Dbo::QueryModel< UIQueryResult >& model, SearchFilter filter, const std::vector<Wt::WString>& columnNames = std::vector<Wt::WString>());
// Stats for a given search filter
typedef boost::tuple<
int, // Total tracks
@@ -174,10 +164,10 @@ class Track
void setArtist(Wt::Dbo::ptr<Artist> artist) { _artist = artist; }
void setRelease(Wt::Dbo::ptr<Release> release) { _release = release; }
int getTrackNumber(void) const { return _trackNumber; }
int getTotalTrackNumber(void) const { return _totalTrackNumber; }
int getDiscNumber(void) const { return _discNumber; }
int getTotalDiscNumber(void) const { return _totalDiscNumber; }
boost::optional<std::size_t> getTrackNumber(void) const;
boost::optional<std::size_t> getTotalTrackNumber(void) const;
boost::optional<std::size_t> getDiscNumber(void) const;
boost::optional<std::size_t> getTotalDiscNumber(void) const;
std::string getName(void) const { return _name; }
boost::filesystem::path getPath(void) const { return _filePath; }
boost::posix_time::time_duration getDuration(void) const { return _duration; }
@@ -218,8 +208,6 @@ class Track
private:
static Wt::Dbo::Query< pointer > getQuery(Wt::Dbo::Session& session, SearchFilter filter);
static const std::size_t _maxNameLength = 128;
int _trackNumber;
+1 -1
View File
@@ -19,7 +19,7 @@
// header file aimed to ease database class declarations
#include "Artist.hpp"
#include "DbArtist.hpp"
#include "Track.hpp"
#include "Playlist.hpp"
#include "Release.hpp"