/*
* 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 "services/database/Cluster.hpp"
#include "services/database/Artist.hpp"
#include "services/database/Release.hpp"
#include "services/database/ScanSettings.hpp"
#include "services/database/Session.hpp"
#include "services/database/Track.hpp"
#include "IdTypeTraits.hpp"
#include "SqlQuery.hpp"
#include "Utils.hpp"
namespace Database {
Cluster::Cluster(ObjectPtr type, std::string_view name)
: _name {std::string {name, 0, _maxNameLength}},
_clusterType {getDboPtr(type)}
{
}
Cluster::pointer
Cluster::create(Session& session, ObjectPtr type, std::string_view name)
{
return session.getDboSession().add(std::unique_ptr {new Cluster {type, name}});
}
std::size_t
Cluster::getCount(Session& session)
{
session.checkSharedLocked();
return session.getDboSession().query("SELECT COUNT(*) FROM cluster");
}
RangeResults
Cluster::find(Session& session, Range range)
{
session.checkSharedLocked();
auto query {session.getDboSession().query("SELECT id FROM cluster")};
return Utils::execQuery(query, range);
}
RangeResults
Cluster::findOrphans(Session& session, Range range)
{
session.checkSharedLocked();
auto query {session.getDboSession().query("SELECT DISTINCT c.id FROM cluster c WHERE NOT EXISTS(SELECT 1 FROM track_cluster t_c WHERE t_c.cluster_id = c.id)")};
return Utils::execQuery(query, range);
}
Cluster::pointer
Cluster::find(Session& session, ClusterId id)
{
session.checkSharedLocked();
return session.getDboSession().find().where("id = ?").bind(id).resultValue();
}
void
Cluster::addTrack(ObjectPtr