[DB] Corrected searching by genre and added searching by multi keywords

This commit is contained in:
emeric
2014-12-19 22:56:03 +01:00
parent 730ad3b201
commit 30c5858fa2
20 changed files with 446 additions and 530 deletions
+4 -4
View File
@@ -29,7 +29,6 @@
#include "logger/Logger.hpp"
#include "TableFilter.hpp"
#include "TableFilterGenre.hpp"
#include "KeywordSearchFilter.hpp"
#include "Audio.hpp"
@@ -49,15 +48,15 @@ _playQueue(nullptr)
// Filters
Wt::WHBoxLayout *filterLayout = new Wt::WHBoxLayout();
TableFilterGenre *filterGenre = new TableFilterGenre(_db);
TableFilter *filterGenre = new TableFilter(_db, Database::SearchFilter::Field::Genre, { "Genre", "Tracks"} );
filterLayout->addWidget(filterGenre);
_filterChain.addFilter(filterGenre);
TableFilter *filterArtist = new TableFilter(_db, "track", "artist_name", "Artist");
TableFilter *filterArtist = new TableFilter(_db, Database::SearchFilter::Field::Artist, {"Artist", "Tracks"} );
filterLayout->addWidget(filterArtist);
_filterChain.addFilter(filterArtist);
TableFilter *filterRelease = new TableFilter(_db, "track", "release_name", "Release");
TableFilter *filterRelease = new TableFilter(_db, Database::SearchFilter::Field::Release, {"Release", "Tracks"});
filterLayout->addWidget(filterRelease);
_filterChain.addFilter(filterRelease);
@@ -95,6 +94,7 @@ _playQueue(nullptr)
Database::User::pointer user = _db.getCurrentUser();
Wt::WVBoxLayout* playQueueLayout = new Wt::WVBoxLayout();
playQueueLayout->setContentsMargins(5,5,5,5);
_mediaPlayer = new AudioMediaPlayer();
playQueueLayout->addWidget(_mediaPlayer);
+7 -4
View File
@@ -22,7 +22,7 @@
#include <Wt/WSignal>
#include "database/SqlQuery.hpp"
#include "database/AudioTypes.hpp"
namespace UserInterface {
@@ -31,17 +31,20 @@ class Filter
public:
struct Constraint {
WhereClause where; // WHERE SQL clause
std::vector<std::string> search;
typedef std::map<std::string, std::vector<std::string> > ColumnValues;
ColumnValues columnValues;
};
Filter() {}
virtual ~Filter() {}
// Refresh filter using constraints created by parent filters
virtual void refresh(const Constraint& constraint) = 0;
virtual void refresh(Database::SearchFilter& filter) = 0;
// Update constraints for next filters
virtual void getConstraint(Constraint& constraint) = 0;
virtual void getConstraint(Database::SearchFilter& filter) = 0;
// Emitted when a constraint has changed
Wt::Signal<void>& update() { return _update; };
+3 -3
View File
@@ -50,7 +50,7 @@ FilterChain::updateFilters(std::size_t startIdx)
_refreshingFilters = true;
Filter::Constraint currentConstraint;
Database::SearchFilter searchFilter;
for (std::size_t idFilter = 0; idFilter < _filters.size(); ++idFilter)
{
@@ -58,12 +58,12 @@ FilterChain::updateFilters(std::size_t startIdx)
// Apply contraints created by previous filters
if (idFilter > startIdx) {
filter->refresh(currentConstraint);
filter->refresh(searchFilter);
}
// Get constraints generated by this filter
// (Note: adding accross successive calls)
filter->getConstraint(currentConstraint);
filter->getConstraint(searchFilter);
}
_refreshingFilters = false;
+20 -5
View File
@@ -17,6 +17,10 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include "KeywordSearchFilter.hpp"
namespace UserInterface {
@@ -34,15 +38,26 @@ KeywordSearchFilter::setText(const std::string& text)
// Get constraints created by this filter
void
KeywordSearchFilter::getConstraint(Constraint& constraint)
KeywordSearchFilter::getConstraint(Database::SearchFilter& filter)
{
// No active search means no constaint!
if (!_lastEmittedText.empty()) {
const std::string bindText ("%%" + _lastEmittedText + "%%");
constraint.where.And( WhereClause("(track.name like ? or track.release_name like ? or track.artist_name like ? or track.genre_list like ?)").bind(bindText).bind(bindText).bind(bindText).bind(bindText));
}
std::vector<std::string> values;
boost::algorithm::split(values, _lastEmittedText, boost::is_any_of(" "), boost::token_compress_on);
// else no constraint!
// For each part, do a global search on all searchable fields
BOOST_FOREACH(std::string value, values)
{
Database::SearchFilter::FieldValues likeMatch;
likeMatch[Database::SearchFilter::Field::Artist].push_back(value);
likeMatch[Database::SearchFilter::Field::Release].push_back(value);
likeMatch[Database::SearchFilter::Field::Genre].push_back(value);
likeMatch[Database::SearchFilter::Field::Track].push_back(value);
filter.likeMatches.push_back(likeMatch);
}
}
}
} // namespace UserInterface
+2 -2
View File
@@ -34,10 +34,10 @@ class KeywordSearchFilter : public Filter
void setText(const std::string& text);
// Set constraint on this filter
void refresh(const Constraint& constraint) {}
void refresh(Database::SearchFilter& filter) {}
// Get constraints created by this filter
void getConstraint(Constraint& constraint);
void getConstraint(Database::SearchFilter& filter);
private:
+39 -43
View File
@@ -19,25 +19,41 @@
#include <boost/foreach.hpp>
#include "database/AudioTypes.hpp"
#include "logger/Logger.hpp"
#include "TableFilter.hpp"
namespace UserInterface {
TableFilter::TableFilter(Database::Handler& db, std::string table, std::string field, const Wt::WString& displayName, Wt::WContainerWidget* parent)
using namespace Database;
TableFilter::TableFilter(Database::Handler& db, Database::SearchFilter::Field field, std::vector<Wt::WString> columnNames, Wt::WContainerWidget* parent)
: Wt::WTableView( parent ),
Filter(),
_db(db),
_table(table),
_field(field)
{
_queryModel.setQuery( _db.getSession().query< ResultType >("select track." + _field + ", COUNT(DISTINCT track.id) from track GROUP BY track." + _field).orderBy("track." + _field));
_queryModel.addColumn( "track." + _field, displayName);
_queryModel.addColumn( "COUNT(DISTINCT track.id)", "Tracks");
SearchFilter filter;
switch (field)
{
case Database::SearchFilter::Field::Artist:
Track::updateArtistQueryModel(_db.getSession(), _queryModel, filter, columnNames);
break;
case Database::SearchFilter::Field::Release:
Track::updateReleaseQueryModel(_db.getSession(), _queryModel, filter, columnNames);
break;
case Database::SearchFilter::Field::Genre:
Genre::updateGenreQueryModel(_db.getSession(), _queryModel, filter, columnNames);
break;
default:
break;
}
this->setSelectionMode(Wt::ExtendedSelection);
this->setSortingEnabled(false);
this->setSortingEnabled(true);
this->setAlternatingRowColors(true);
this->setModel(&_queryModel);
@@ -68,53 +84,37 @@ _field(field)
void
TableFilter::layoutSizeChanged (int width, int height)
{
LMS_LOG(MOD_UI, SEV_DEBUG) << "LAYOUT CHANGED, new width = " << width;
LMS_LOG(MOD_UI, SEV_DEBUG) << "Before: sizes = " << this->columnWidth(0).toPixels() << ", " << this->columnWidth(1).toPixels();
std::size_t trackColumnSize = this->columnWidth(1).toPixels();
// Set the remaining size for the name column
this->setColumnWidth(0, width - trackColumnSize - (7 * 2) - 2);
LMS_LOG(MOD_UI, SEV_DEBUG) << "After: sizes = " << this->columnWidth(0).toPixels() << ", " << this->columnWidth(1).toPixels();
}
// Set constraints on this filter
void
TableFilter::refresh(const Constraint& constraint)
TableFilter::refresh(SearchFilter& filter)
{
SqlQuery sqlQuery;
sqlQuery.select("track." + _field + ", COUNT(DISTINCT track.id)");
sqlQuery.from().And( FromClause("track")) ;
sqlQuery.where().And(constraint.where); // Add constraint made by other filters
sqlQuery.groupBy().And( "track." + _field); // Add constraint made by other filters
LMS_LOG(MOD_UI, SEV_DEBUG) << _table << ", generated query = '" << sqlQuery.get() << "'";
Wt::Dbo::Query<ResultType> query = _db.getSession().query<ResultType>( sqlQuery.get() );
query.orderBy(_table + "." + _field);
BOOST_FOREACH(const std::string& bindArg, sqlQuery.where().getBindArgs()) {
LMS_LOG(MOD_UI, SEV_DEBUG) << "Binding value '" << bindArg << "'";
query.bind(bindArg);
switch (_field)
{
case Database::SearchFilter::Field::Artist:
Track::updateArtistQueryModel(_db.getSession(), _queryModel, filter);
break;
case Database::SearchFilter::Field::Release:
Track::updateReleaseQueryModel(_db.getSession(), _queryModel, filter);
break;
case Database::SearchFilter::Field::Genre:
Genre::updateGenreQueryModel(_db.getSession(), _queryModel, filter);
break;
default:
break;
}
_queryModel.setQuery( query, true );
LMS_LOG(MOD_UI, SEV_DEBUG) << "Finish !";
}
// Get constraint created by this filter
void
TableFilter::getConstraint(Constraint& constraint)
TableFilter::getConstraint(SearchFilter& filter)
{
Wt::WModelIndexSet indexSet = this->selectedIndexes();
// WHERE statement
WhereClause clause;
BOOST_FOREACH(Wt::WModelIndex index, indexSet) {
if (!index.isValid())
@@ -122,14 +122,10 @@ TableFilter::getConstraint(Constraint& constraint)
const ResultType& result = _queryModel.resultRow( index.row() );
// Get the track part
std::string name(result.get<0>());
std::string name = result.get<0>();
clause.Or(_table + "." + _field + " = ?").bind(name);
filter.exactMatch[_field].push_back(name);
}
// Adding our WHERE clause
constraint.where.And( clause );
}
} // namespace UserInterface
+5 -6
View File
@@ -33,13 +33,13 @@ class TableFilter : public Wt::WTableView, public Filter
{
public:
TableFilter(Database::Handler& db, std::string table, std::string field, const Wt::WString& displayName, Wt::WContainerWidget* parent = 0);
TableFilter(Database::Handler& db, Database::SearchFilter::Field field, std::vector<Wt::WString> displayName, Wt::WContainerWidget* parent = 0);
// Set constraints on this filter
void refresh(const Constraint& constraint);
void refresh(Database::SearchFilter& filter);
// Get constraints created by this filter
void getConstraint(Constraint& constraint);
void getConstraint(Database::SearchFilter& filter);
void layoutSizeChanged (int width, int height);
@@ -52,10 +52,9 @@ class TableFilter : public Wt::WTableView, public Filter
SigDoubleClicked _sigDoubleClicked;
Database::Handler& _db;
const std::string _table;
const std::string _field;
Database::SearchFilter::Field _field;
// Name, track count, special value that means 'all' if set to 1
// Name, track count
typedef boost::tuple<std::string, int> ResultType;
Wt::Dbo::QueryModel< ResultType > _queryModel;
-133
View File
@@ -1,133 +0,0 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <boost/foreach.hpp>
#include "logger/Logger.hpp"
#include "TableFilterGenre.hpp"
namespace UserInterface {
TableFilterGenre::TableFilterGenre(Database::Handler& db, Wt::WContainerWidget* parent)
: Wt::WTableView( parent ),
Filter(),
_db(db)
{
_queryModel.setQuery( _db.getSession().query< ResultType >("select genre.name, COUNT(DISTINCT track.id) from genre INNER JOIN track_genre ON track_genre.genre_id = genre.id INNER JOIN track ON track.id = track_genre.track_id").orderBy("genre.name").groupBy("genre.name").orderBy("genre.name"));
_queryModel.addColumn("genre.name", "Genre");
_queryModel.addColumn("COUNT(DISTINCT track.id)", "tracks");
this->setSelectionMode(Wt::ExtendedSelection);
this->setSortingEnabled(false);
this->setAlternatingRowColors(true);
this->setModel(&_queryModel);
this->selectionChanged().connect(this, &TableFilterGenre::emitUpdate);
setLayoutSizeAware(true);
_queryModel.setBatchSize(100);
// If an item is double clicked, select and emit signal
this->doubleClicked().connect( std::bind([=] (Wt::WModelIndex idx, Wt::WMouseEvent evt)
{
if (!idx.isValid())
return;
Wt::WModelIndexSet indexSet;
indexSet.insert(idx);
this->setSelectedIndexes( indexSet );
_sigDoubleClicked.emit( );
}, std::placeholders::_1, std::placeholders::_2));
}
void
TableFilterGenre::layoutSizeChanged (int width, int height)
{
LMS_LOG(MOD_UI, SEV_DEBUG) << "LAYOUT CHANGED!";
/* TODO
std::size_t trackColumnSize = this->columnWidth(1).toPixels() + 30 ;
// Set the remaining size for the name column
this->setColumnWidth(0, width - 7 - trackColumnSize);
*/
}
// Set constraints on this filter
void
TableFilterGenre::refresh(const Constraint& constraint)
{
SqlQuery sqlQuery;
sqlQuery.select("genre.name, COUNT(DISTINCT track.id)");
sqlQuery.from().And( std::string("genre INNER JOIN track_genre ON track_genre.genre_id = genre.id INNER JOIN track ON track.id = track_genre.track_id") );
sqlQuery.where().And(constraint.where); // Add constraint made by other filters
sqlQuery.groupBy().And( std::string("genre.name") );
LMS_LOG(MOD_UI, SEV_DEBUG) << "genre, generated query = '" << sqlQuery.get() << "'";
Wt::Dbo::Query<ResultType> query = _db.getSession().query<ResultType>( sqlQuery.get() );
query.orderBy("genre.name");
BOOST_FOREACH(const std::string& bindArg, sqlQuery.where().getBindArgs()) {
LMS_LOG(MOD_UI, SEV_DEBUG) << "Binding value '" << bindArg << "'";
query.bind(bindArg);
}
_queryModel.setQuery( query, true );
}
// Get constraint created by this filter
void
TableFilterGenre::getConstraint(Constraint& constraint)
{
Wt::WModelIndexSet indexSet = this->selectedIndexes();
// WHERE statement
WhereClause clause;
BOOST_FOREACH(Wt::WModelIndex index, indexSet) {
if (!index.isValid())
continue;
const ResultType& result = _queryModel.resultRow( index.row() );
// Get the track part
std::string name(result.get<0>());
if (name == "<None>")
clause.Or( std::string("track.genre_list = ?") ).bind("");
else
clause.Or( std::string("track.genre_list LIKE ?") ).bind("%%" + name + "%%");
}
// Adding our WHERE clause
constraint.where.And( clause );
}
} // namespace UserInterface
-65
View File
@@ -1,65 +0,0 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef TABLE_FILTER_GENRE_HPP
#define TABLE_FILTER_GENRE_HPP
#include <Wt/Dbo/QueryModel>
#include <Wt/WTableView>
#include "Filter.hpp"
#include "database/DatabaseHandler.hpp"
namespace UserInterface {
class TableFilterGenre : public Wt::WTableView, public Filter
{
public:
TableFilterGenre(Database::Handler& db, Wt::WContainerWidget* parent = 0);
// Set constraints on this filter
void refresh(const Constraint& constraint);
// Get constraints created by this filter
void getConstraint(Constraint& constraint);
void layoutSizeChanged (int width, int height);
typedef Wt::Signal<void> SigDoubleClicked;
SigDoubleClicked& sigDoubleClicked() { return _sigDoubleClicked; }
protected:
SigDoubleClicked _sigDoubleClicked;
Database::Handler& _db;
// genre_name, count
typedef boost::tuple<std::string, int> ResultType;
Wt::Dbo::QueryModel< ResultType > _queryModel;
};
} // namespace UserInterface
#endif
+22 -41
View File
@@ -33,18 +33,25 @@ TrackView::TrackView( Database::Handler& db, Wt::WContainerWidget* parent)
: Wt::WTableView( parent ),
_db(db)
{
_queryModel.setQuery(_db.getSession().query<ResultType>("select track from track").orderBy("track.artist_name,track.date,track.release_name,track.disc_number,track.track_number"));
_queryModel.addColumn( "track.artist_name", "Artist" );
_queryModel.addColumn( "track.release_name", "Album" );
_queryModel.addColumn( "track.disc_number", "Disc #" );
_queryModel.addColumn( "track.track_number", "Track #" );
_queryModel.addColumn( "track.name", "Track" );
_queryModel.addColumn( "track.duration", "Duration" );
_queryModel.addColumn( "track.date", "Date" );
_queryModel.addColumn( "track.original_date", "Original Date" );
_queryModel.addColumn( "track.genre_list", "Genres" );
_queryModel.setBatchSize(500);
static const std::vector<Wt::WString> columnNames =
{
"Artist",
"Album",
"Disc #",
"Track #",
"Track",
"Duration",
"Date",
"Original Date",
"Genres",
};
Database::SearchFilter filter;
Database::Track::updateTracksQueryModel(_db.getSession(), _queryModel, filter, columnNames);
_queryModel.setBatchSize(300);
this->setSortingEnabled(true);
this->setSelectionMode(Wt::ExtendedSelection);
@@ -60,7 +67,6 @@ _db(db)
this->setColumnWidth(6, 70); // Date
this->setColumnWidth(7, 70); // Original Date
this->setColumnWidth(8, 180); // Genres
// this->setOverflow(Wt::WContainerWidget::OverflowScroll, Wt::Vertical);
// Duration display
@@ -102,28 +108,9 @@ _db(db)
// Set constraints created by parent filters
void
TrackView::refresh(const Constraint& constraint)
TrackView::refresh(Database::SearchFilter& filter)
{
SqlQuery sqlQuery;
sqlQuery.select( "track" );
sqlQuery.from().And( FromClause("track"));
sqlQuery.where().And(constraint.where);
LMS_LOG(MOD_UI, SEV_DEBUG) << "TRACK REQ = '" << sqlQuery.get() << "'";
Wt::Dbo::Query<ResultType> query = _db.getSession().query<ResultType>( sqlQuery.get() );
query.groupBy("track").orderBy("track.artist_name,track.date,track.release_name,track.disc_number,track.track_number");
BOOST_FOREACH(const std::string& bindArg, sqlQuery.where().getBindArgs()) {
LMS_LOG(MOD_UI, SEV_DEBUG) << "Binding value '" << bindArg << "'";
query.bind(bindArg);
}
_queryModel.setQuery( query, true );
Database::Track::updateTracksQueryModel(_db.getSession(), _queryModel, filter);
}
void
@@ -138,10 +125,7 @@ TrackView::getSelectedTracks(std::vector<Database::Track::id_type>& track_ids)
if (!index.isValid())
continue;
const ResultType& result = _queryModel.resultRow( index.row() );
// Get the track part
Wt::Dbo::ptr<Database::Track> track ( result.get<0>() );
Database::Track::pointer track = _queryModel.resultRow( index.row() );
track_ids.push_back(track.id());
}
@@ -177,10 +161,7 @@ TrackView::getTracks(std::vector<Database::Track::id_type>& trackIds)
for (int i = 0; i < _queryModel.rowCount(); ++i)
{
const ResultType& result = _queryModel.resultRow( i );
// Get the track part
Wt::Dbo::ptr<Database::Track> track ( result.get<0>() );
Database::Track::pointer track = _queryModel.resultRow(i);
trackIds.push_back(track.id());
}
+3 -3
View File
@@ -38,10 +38,10 @@ class TrackView : public Wt::WTableView, public Filter
// Filter interface
// Set constraints created by parent filters
void refresh(const Constraint& constraint);
void refresh(Database::SearchFilter& filter);
// Create constraints for child filters (N/A)
void getConstraint(Constraint& constraint) {}
void getConstraint(Database::SearchFilter& filter) {}
// Get all the tracks that are currently selected
void getSelectedTracks(std::vector<Database::Track::id_type>& trackIds);
@@ -64,7 +64,7 @@ class TrackView : public Wt::WTableView, public Filter
Database::Handler& _db;
typedef boost::tuple<Database::Track::pointer> ResultType;
typedef Database::Track::pointer ResultType;
Wt::Dbo::QueryModel< ResultType > _queryModel;
Wt::WTableView* _tableView;