/*
* 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/Track.hpp"
#include
#include "services/database/Artist.hpp"
#include "services/database/Cluster.hpp"
#include "services/database/Release.hpp"
#include "services/database/TrackArtistLink.hpp"
#include "services/database/TrackFeatures.hpp"
#include "services/database/Session.hpp"
#include "services/database/User.hpp"
#include "utils/Logger.hpp"
#include "IdTypeTraits.hpp"
#include "SqlQuery.hpp"
#include "StringViewTraits.hpp"
#include "Utils.hpp"
namespace Database {
static
Wt::Dbo::Query
createQuery(Session& session, const Track::FindParameters& params)
{
session.checkSharedLocked();
auto query {session.getDboSession().query("SELECT DISTINCT t.id from track t")};
for (std::string_view keyword : params.keywords)
query.where("t.name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'").bind("%" + Utils::escapeLikeKeyword(keyword) + "%");
if (params.writtenAfter.isValid())
query.where("t.file_last_write > ?").bind(params.writtenAfter);
if (params.starringUser.isValid())
{
assert(params.scrobbler);
query.join("starred_track s_t ON s_t.track_id = t.id")
.where("s_t.user_id = ?").bind(params.starringUser)
.where("s_t.scrobbler = ?").bind(*params.scrobbler)
.where("s_t.scrobbling_state <> ?").bind(ScrobblingState::PendingRemove);
}
if (!params.clusters.empty())
{
std::ostringstream oss;
oss << "t.id IN (SELECT DISTINCT t.id FROM track t"
" INNER JOIN track_cluster t_c ON t_c.track_id = t.id"
" INNER JOIN cluster c ON c.id = t_c.cluster_id";
WhereClause clusterClause;
for (const ClusterId clusterId : params.clusters)
{
clusterClause.Or(WhereClause("c.id = ?"));
query.bind(clusterId);
}
oss << " " << clusterClause.get();
oss << " GROUP BY t.id HAVING COUNT(*) = " << params.clusters.size() << ")";
query.where(oss.str());
}
if (params.artist.isValid())
{
query.join("artist a ON a.id = t_a_l.artist_id")
.join("track_artist_link t_a_l ON t_a_l.track_id = t.id")
.where("a.id = ?").bind(params.artist);
if (!params.trackArtistLinkTypes.empty())
{
std::ostringstream oss;
bool first {true};
for (TrackArtistLinkType linkType : params.trackArtistLinkTypes)
{
if (!first)
oss << " OR ";
oss << "t_a_l.type = ?";
query.bind(linkType);
first = false;
}
query.where(oss.str());
}
}
assert(!(params.nonRelease && params.release.isValid()));
if (params.nonRelease)
query.where("t.release_id IS NULL");
else if (params.release.isValid())
query.where("t.release_id = ?").bind(params.release);
if (params.trackList.isValid())
{
query.join("tracklist t_l ON t_l_e.tracklist_id = t_l.id");
query.join("tracklist_entry t_l_e ON t.id = t_l_e.track_id");
query.where("t_l.id = ?").bind(params.trackList);
}
switch (params.sortMethod)
{
case TrackSortMethod::None:
break;
case TrackSortMethod::LastWritten:
query.orderBy("t.file_last_write DESC");
break;
case TrackSortMethod::Random:
query.orderBy("RANDOM()");
break;
case TrackSortMethod::StarredDateDesc:
assert(params.starringUser.isValid());
query.orderBy("s_t.date_time DESC");
break;
case TrackSortMethod::Name:
query.orderBy("t.name COLLATE NOCASE");
break;
case TrackSortMethod::DateDescAndRelease:
query.orderBy("t.date DESC,t.release_id,t.disc_number,t.track_number");
break;
case TrackSortMethod::Release:
query.orderBy("t.disc_number,t.track_number");
break;
case TrackSortMethod::TrackList:
assert(params.trackList.isValid());
query.orderBy("t_l.id");
}
return query;
}
Track::Track(const std::filesystem::path& p)
: _filePath {p.string()}
{
}
Track::pointer
Track::create(Session& session, const std::filesystem::path& p)
{
return session.getDboSession().add(std::unique_ptr