/* * 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 . */ #include "Artist.hpp" #include #include "utils/Logger.hpp" #include "Cluster.hpp" #include "Release.hpp" #include "SqlQuery.hpp" #include "Track.hpp" namespace Database { Artist::Artist(const std::string& name, const std::string& MBID) : _name{std::string(name, 0 , _maxNameLength)}, _MBID{MBID} { } std::vector Artist::getByName(Wt::Dbo::Session& session, const std::string& name) { Wt::Dbo::collection res = session.find().where("name = ?").bind( std::string{name, 0, _maxNameLength} ); return std::vector(res.begin(), res.end()); } Artist::pointer Artist::getByMBID(Wt::Dbo::Session& session, const std::string& mbid) { return session.find().where("mbid = ?").bind(mbid); } Artist::pointer Artist::getById(Wt::Dbo::Session& session, IdType id) { return session.find().where("id = ?").bind(id); } Artist::pointer Artist::create(Wt::Dbo::Session& session, const std::string& name, const std::string& MBID) { return session.add(std::make_unique(name, MBID)); } std::vector Artist::getAll(Wt::Dbo::Session& session, boost::optional offset, boost::optional size) { Wt::Dbo::collection res = session.find() .offset(offset ? static_cast(*offset) : -1) .limit(size ? static_cast(*size) : -1) .orderBy("name COLLATE NOCASE"); return std::vector(res.begin(), res.end()); } std::vector Artist::getAllOrphans(Wt::Dbo::Session& session) { Wt::Dbo::collection res = session.query< Wt::Dbo::ptr >("SELECT DISTINCT a FROM artist a WHERE NOT EXISTS(SELECT 1 FROM track t INNER JOIN track_artist t_a ON t_a.artist_id = a.id WHERE t.id = t_a.track_id)"); return std::vector(res.begin(), res.end()); } static Wt::Dbo::Query getQuery(Wt::Dbo::Session& session, const std::set& clusterIds, const std::vector& 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_a.track_id = t.id INNER JOIN track_artist t_a ON t_a.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 COLLATE NOCASE"; Wt::Dbo::Query query = session.query( oss.str() ); for (const std::string& bindArg : where.getBindArgs()) { query.bind(bindArg); } return query; } std::vector Artist::getByFilter(Wt::Dbo::Session& session, const std::set& clusters, const std::vector keywords, boost::optional offset, boost::optional size, bool& moreResults) { Wt::Dbo::collection collection = getQuery(session, clusters, keywords) .limit(size ? static_cast(*size) + 1 : -1) .offset(offset ? static_cast(*offset) : -1); auto res {std::vector(collection.begin(), collection.end())}; if (size && res.size() == static_cast(*size) + 1) { moreResults = true; res.pop_back(); } else moreResults = false; return res; } std::vector Artist::getLastAdded(Wt::Dbo::Session& session, Wt::WDateTime after, boost::optional limit) { Wt::Dbo::collection res = session.query("SELECT a from artist a INNER JOIN track_artist t_a ON t_a.artist_id = a.id INNER JOIN track t ON t.id = t_a.track_id") .where("t.file_added > ?").bind(after) .groupBy("a.id") .orderBy("t.file_added DESC") .limit(limit ? static_cast(*limit) : -1); return std::vector(res.begin(), res.end()); } std::vector> Artist::getReleases(const std::set& clusterIds) const { assert(self()); assert(IdIsValid(self()->id())); assert(session()); WhereClause where; std::ostringstream oss; oss << "SELECT DISTINCT r FROM release r INNER JOIN artist a ON t_a.artist_id = a.id INNER JOIN track_artist t_a ON t_a.track_id = t.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(); oss << " ORDER BY t.year,r.name"; Wt::Dbo::Query query = session()->query( oss.str() ); for (const std::string& bindArg : where.getBindArgs()) { query.bind(bindArg); } Wt::Dbo::collection< Wt::Dbo::ptr > res = query; return std::vector< Wt::Dbo::ptr > (res.begin(), res.end()); } std::vector> Artist::getTracks() const { return std::vector>(_tracks.begin(), _tracks.end()); } std::vector>> Artist::getClusterGroups(std::vector clusterTypes, std::size_t size) const { assert(self()); assert(IdIsValid(self()->id())); assert(session()); WhereClause where; std::ostringstream oss; oss << "SELECT c from cluster c INNER JOIN track t ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN cluster_type c_type ON c.cluster_type_id = c_type.id INNER JOIN artist a ON t_a.artist_id = a.id INNER JOIN track_artist t_a ON t_a.track_id = t.id"; where.And(WhereClause("a.id = ?")).bind(std::to_string(self()->id())); { WhereClause clusterClause; for (auto clusterType : clusterTypes) clusterClause.Or(WhereClause("c_type.id = ?")).bind(std::to_string(clusterType.id())); where.And(clusterClause); } oss << " " << where.get(); oss << "GROUP BY c.id ORDER BY COUNT(c.id) DESC"; Wt::Dbo::Query query = session()->query( oss.str() ); for (const std::string& bindArg : where.getBindArgs()) query.bind(bindArg); Wt::Dbo::collection queryRes = query; std::map> clusters; for (auto cluster : queryRes) { if (clusters[cluster->getType().id()].size() < size) clusters[cluster->getType().id()].push_back(cluster); } std::vector> res; for (auto cluster_list : clusters) res.push_back(cluster_list.second); return res; } } // namespace Database