/* * 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 . */ #include "Cluster.hpp" #include "Artist.hpp" #include "Release.hpp" #include "ScanSettings.hpp" #include "Session.hpp" #include "SqlQuery.hpp" #include "Track.hpp" namespace Database { Cluster::Cluster() { } Cluster::Cluster(Wt::Dbo::ptr type, std::string name) : _name(std::string(name, 0, _maxNameLength)), _clusterType(type) { } Cluster::pointer Cluster::create(Session& session, Wt::Dbo::ptr type, std::string name) { session.checkUniqueLocked(); Cluster::pointer res {session.getDboSession().add(std::make_unique(type, name))}; session.getDboSession().flush(); return res; } std::vector Cluster::getAll(Session& session) { session.checkSharedLocked(); Wt::Dbo::collection res {session.getDboSession().find()}; return std::vector(res.begin(), res.end()); } std::vector Cluster::getAllOrphans(Session& session) { session.checkSharedLocked(); Wt::Dbo::collection res {session.getDboSession().query("SELECT DISTINCT c FROM cluster c WHERE NOT EXISTS(SELECT 1 FROM track t INNER JOIN track_cluster t_c ON t.id = t_c.track_id)")}; return std::vector(res.begin(), res.end()); } Cluster::pointer Cluster::getById(Session& session, IdType id) { session.checkSharedLocked(); return session.getDboSession().find().where("id = ?").bind(id); } void Cluster::addTrack(Wt::Dbo::ptr track) { _tracks.insert(track); } std::vector> Cluster::getTracks(int offset, int limit) const { assert(session()); assert(IdIsValid(self()->id())); Wt::Dbo::collection res = session()->query("SELECT t FROM track t INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id") .where("c.id = ?").bind(self()->id()) .offset(offset) .limit(limit); return std::vector>(res.begin(), res.end()); } std::set Cluster::getTrackIds() const { assert(session()); assert(IdIsValid(self()->id())); Wt::Dbo::collection res = session()->query("SELECT t_c.track_id FROM track_cluster t_c INNER JOIN cluster c ON c.id = t_c.cluster_id") .where("c.id = ?").bind(self()->id()); return std::set(res.begin(), res.end()); } std::size_t Cluster::getReleasesCount() const { assert(session()); assert(IdIsValid(self()->id())); return session()->query("SELECT COUNT(DISTINCT r.id) FROM release r 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") .where("c.id = ?").bind(self()->id()); } ClusterType::ClusterType(std::string name) : _name(name) { } std::vector ClusterType::getAllOrphans(Session& session) { session.checkSharedLocked(); Wt::Dbo::collection res = session.getDboSession().query>("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(res.begin(), res.end()); } ClusterType::pointer ClusterType::getByName(Session& session, std::string name) { session.checkSharedLocked(); return session.getDboSession().find().where("name = ?").bind(name); } ClusterType::pointer ClusterType::getById(Session& session, IdType id) { session.checkSharedLocked(); return session.getDboSession().find().where("id= ?").bind(id); } std::vector ClusterType::getAll(Session& session) { session.checkSharedLocked(); Wt::Dbo::collection res = session.getDboSession().find(); return std::vector(res.begin(), res.end()); } ClusterType::pointer ClusterType::create(Session& session, std::string name) { session.checkUniqueLocked(); ClusterType::pointer res {session.getDboSession().add(std::make_unique(name))}; session.getDboSession().flush(); return res; } Cluster::pointer ClusterType::getCluster(std::string name) const { assert(self()); assert(IdIsValid(self()->id())); assert(session()); return session()->find() .where("name = ?").bind(name) .where("cluster_type_id = ?").bind(self()->id()); } std::vector ClusterType::getClusters() const { assert(self()); assert(IdIsValid(self()->id())); assert(session()); Wt::Dbo::collection res = session()->find() .where("cluster_type_id = ?").bind(self()->id()) .orderBy("name"); return std::vector(res.begin(), res.end()); } } // namespace Database