/*
* 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 "Types.hpp"
#include "SearchFilter.hpp"
#include "SqlQuery.hpp"
namespace Database
{
Release::Release(const std::string& name, const std::string& MBID)
: _name(std::string(name, 0 , _maxNameLength)),
_MBID(MBID)
{
}
std::vector
Release::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());
}
Release::pointer
Release::getByMBID(Wt::Dbo::Session& session, const std::string& mbid)
{
return session.find().where("mbid = ?").bind(mbid);
}
Release::pointer
Release::getById(Wt::Dbo::Session& session, Release::id_type id)
{
return session.find().where("id = ?").bind(id);
}
Release::pointer
Release::create(Wt::Dbo::Session& session, const std::string& name, const std::string& MBID)
{
return session.add(new Release(name, MBID));
}
std::vector
Release::getAll(Wt::Dbo::Session& session, int offset, int size)
{
Wt::Dbo::collection res = session.find().offset(offset).limit(size);
return std::vector(res.begin(), res.end());
}
std::vector
Release::getAllOrphans(Wt::Dbo::Session& session)
{
Wt::Dbo::collection res = session.query>("select r from release r LEFT OUTER JOIN Track t ON r.id = t.release_id WHERE t.id IS NULL");
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 r FROM release r";
for (auto keyword : keywords)
where.And(WhereClause("r.name LIKE ?")).bind("%%" + keyword + "%%");
if (!clusterIds.empty())
{
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.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 r.name";
Wt::Dbo::Query query = session.query( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
query.bind(bindArg);
return query;
}
std::vector
Release::getByFilter(Wt::Dbo::Session& session,
const std::set& clusterIds,
const std::vector keywords,
int offset, int size, bool& moreResults)
{
Wt::Dbo::collection collection = getQuery(session, clusterIds, keywords)
.limit(size != -1 ? size + 1 : -1)
.offset(offset);
auto res = std::vector(collection.begin(), collection.end());
if (size != -1 && res.size() == static_cast(size) + 1)
{
moreResults = true;
res.pop_back();
}
else
moreResults = false;
return res;
}
boost::optional
Release::getReleaseYear(bool original) const
{
assert(session());
Wt::Dbo::collection times = session()->query(
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());
/* various dates, no date */
if (times.empty() || times.size() > 1)
return boost::none;
boost::gregorian::date date = times.front().date();
if (date.is_special())
return boost::none;
return boost::make_optional(date.year());
}
std::vector>
Release::getArtists() const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits::invalidId() );
assert(session());
Wt::Dbo::collection> res = session()->query>(
"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>(res.begin(), res.end());
}
bool
Release::hasVariousArtists() const
{
// TODO optimize
return getArtists().size() > 1;
}
std::vector>
Release::getTracks(const std::set& clusterIds) const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits::invalidId() );
assert(session());
WhereClause where;
std::ostringstream oss;
oss << "SELECT t FROM track t INNER JOIN release r 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("r.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.disc_number,t.track_number";
Wt::Dbo::Query query = session()->query( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
{
query.bind(bindArg);
}
Wt::Dbo::collection< Wt::Dbo::ptr