/*
* Copyright (C) 2013 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
#include
#include "logger/Logger.hpp"
#include "SqlQuery.hpp"
#include "Types.hpp"
namespace Database {
static SqlQuery generatePartialQuery(SearchFilter& filter, bool forceGenreInnerJoin = false)
{
bool genreJoin = forceGenreInnerJoin;
SqlQuery sqlQuery;
// Process like searches
BOOST_FOREACH(SearchFilter::FieldValues& likeMatch, filter.likeMatches)
{
WhereClause likeWhereClause;
// Artist
{
WhereClause whereClause;
BOOST_FOREACH(const std::string& name, likeMatch[SearchFilter::Field::Artist])
whereClause.Or( WhereClause("t.artist_name LIKE ?") ).bind("%%" + name + "%%");
likeWhereClause.Or( whereClause );
}
// Release
{
WhereClause whereClause;
BOOST_FOREACH(const std::string& name, likeMatch[SearchFilter::Field::Release])
whereClause.Or( WhereClause("t.release_name LIKE ?") ).bind("%%" + name + "%%");
likeWhereClause.Or( whereClause );
}
// Genre
{
WhereClause whereClause;
BOOST_FOREACH(const std::string& name, likeMatch[SearchFilter::Field::Genre])
{
whereClause.Or( WhereClause("g.name LIKE ?") ).bind("%%" + name + "%%");
genreJoin = true;
}
likeWhereClause.Or( whereClause );
}
// Track
{
WhereClause whereClause;
BOOST_FOREACH(const std::string& name, likeMatch[SearchFilter::Field::Track])
whereClause.Or( WhereClause("t.name LIKE ?") ).bind("%%" + name + "%%");
likeWhereClause.Or( whereClause );
}
sqlQuery.where().And( likeWhereClause );
}
// Add exact search constraints
// Artist
{
WhereClause whereClause;
BOOST_FOREACH(const std::string& name, filter.exactMatch[SearchFilter::Field::Artist])
whereClause.Or( WhereClause("t.artist_name = ?") ).bind(name);
sqlQuery.where().And( whereClause );
}
// Release
{
WhereClause whereClause;
BOOST_FOREACH(const std::string& name, filter.exactMatch[SearchFilter::Field::Release])
whereClause.Or( WhereClause("t.release_name = ?") ).bind(name);
sqlQuery.where().And( whereClause );
}
// Genre
{
WhereClause whereClause;
BOOST_FOREACH(const std::string& name, filter.exactMatch[SearchFilter::Field::Genre])
{
whereClause.Or( WhereClause("g.name = ?") ).bind(name);
genreJoin = true;
}
sqlQuery.where().And( whereClause );
}
if (genreJoin)
{
sqlQuery.innerJoin().And( InnerJoinClause("genre g ON g.id = t_g.genre_id"));
sqlQuery.innerJoin().And( InnerJoinClause("track_genre t_g ON t_g.track_id = t.id"));
}
return sqlQuery;
}
Track::Track(const boost::filesystem::path& p)
:
_trackNumber(0),
_discNumber(0),
_filePath( p.string() ),
_hasCover(false)
{
}
Wt::Dbo::collection< Track::pointer >
Track::getAll(Wt::Dbo::Session& session)
{
return session.find