2010-10-29 20:41:49 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-10-29 20:41:49 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-11-18 21:19:33 +01:00
|
|
|
#include "search.h"
|
2011-04-24 21:07:01 +02:00
|
|
|
#include "core/logging.h"
|
2010-10-29 20:41:49 +02:00
|
|
|
#include "core/song.h"
|
|
|
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
2010-11-18 21:19:33 +01:00
|
|
|
namespace smart_playlists {
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
Search::Search() { Reset(); }
|
2010-10-29 20:41:49 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
Search::Search(SearchType type, TermList terms, SortType sort_type,
|
|
|
|
SearchTerm::Field sort_field, int limit)
|
|
|
|
: search_type_(type),
|
|
|
|
terms_(terms),
|
|
|
|
sort_type_(sort_type),
|
|
|
|
sort_field_(sort_field),
|
|
|
|
limit_(limit),
|
|
|
|
first_item_(0) {}
|
2010-11-17 21:21:04 +01:00
|
|
|
|
2010-11-18 21:19:33 +01:00
|
|
|
void Search::Reset() {
|
2010-11-03 21:58:33 +01:00
|
|
|
search_type_ = Type_And;
|
2010-10-29 20:41:49 +02:00
|
|
|
terms_.clear();
|
|
|
|
sort_type_ = Sort_Random;
|
2010-11-18 21:19:33 +01:00
|
|
|
sort_field_ = SearchTerm::Field_Title;
|
2010-10-29 20:41:49 +02:00
|
|
|
limit_ = -1;
|
2010-11-27 13:24:47 +01:00
|
|
|
first_item_ = 0;
|
2010-10-29 20:41:49 +02:00
|
|
|
}
|
|
|
|
|
2010-11-18 21:19:33 +01:00
|
|
|
QString Search::ToSql(const QString& songs_table) const {
|
2010-10-29 20:41:49 +02:00
|
|
|
QString sql = "SELECT ROWID," + Song::kColumnSpec + " FROM " + songs_table;
|
|
|
|
|
|
|
|
// Add search terms
|
2010-11-20 19:49:54 +01:00
|
|
|
QStringList where_clauses;
|
|
|
|
QStringList term_where_clauses;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const SearchTerm& term : terms_) {
|
2010-11-20 19:49:54 +01:00
|
|
|
term_where_clauses << term.ToSql();
|
2010-10-29 20:41:49 +02:00
|
|
|
}
|
2010-11-20 19:49:54 +01:00
|
|
|
|
2010-11-03 21:58:33 +01:00
|
|
|
if (!terms_.isEmpty() && search_type_ != Type_All) {
|
|
|
|
QString boolean_op = search_type_ == Type_And ? " AND " : " OR ";
|
2010-11-20 19:49:54 +01:00
|
|
|
where_clauses << "(" + term_where_clauses.join(boolean_op) + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restrict the IDs of songs if we're making a dynamic playlist
|
|
|
|
if (!id_not_in_.isEmpty()) {
|
|
|
|
QString numbers;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (int id : id_not_in_) {
|
2010-11-20 19:49:54 +01:00
|
|
|
numbers += (numbers.isEmpty() ? "" : ",") + QString::number(id);
|
|
|
|
}
|
|
|
|
where_clauses << "(ROWID NOT IN (" + numbers + "))";
|
|
|
|
}
|
|
|
|
|
2011-06-17 22:16:45 +02:00
|
|
|
// We never want to include songs that have been deleted, but are still kept
|
|
|
|
// in the database in case the directory containing them has just been
|
|
|
|
// unmounted.
|
|
|
|
where_clauses << "unavailable = 0";
|
|
|
|
|
2010-11-20 19:49:54 +01:00
|
|
|
if (!where_clauses.isEmpty()) {
|
|
|
|
sql += " WHERE " + where_clauses.join(" AND ");
|
2010-10-29 20:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add sort by
|
|
|
|
if (sort_type_ == Sort_Random) {
|
|
|
|
sql += " ORDER BY random()";
|
|
|
|
} else {
|
2014-02-07 16:34:20 +01:00
|
|
|
sql += " ORDER BY " + SearchTerm::FieldColumnName(sort_field_) +
|
|
|
|
(sort_type_ == Sort_FieldAsc ? " ASC" : " DESC");
|
2010-10-29 20:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add limit
|
2010-11-21 13:24:36 +01:00
|
|
|
if (first_item_) {
|
2010-11-27 13:24:47 +01:00
|
|
|
sql += QString(" LIMIT %1 OFFSET %2").arg(limit_).arg(first_item_);
|
2010-11-21 13:24:36 +01:00
|
|
|
} else if (limit_ != -1) {
|
2010-10-29 20:41:49 +02:00
|
|
|
sql += " LIMIT " + QString::number(limit_);
|
|
|
|
}
|
2011-04-24 21:07:01 +02:00
|
|
|
qLog(Debug) << sql;
|
2010-10-29 20:41:49 +02:00
|
|
|
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2010-11-18 21:19:33 +01:00
|
|
|
bool Search::is_valid() const {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (search_type_ == Type_All) return true;
|
2010-11-03 21:58:33 +01:00
|
|
|
return !terms_.isEmpty();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool Search::operator==(const Search& other) const {
|
|
|
|
return search_type_ == other.search_type_ && terms_ == other.terms_ &&
|
|
|
|
sort_type_ == other.sort_type_ && sort_field_ == other.sort_field_ &&
|
2010-11-20 13:20:26 +01:00
|
|
|
limit_ == other.limit_;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
} // namespace
|
2010-11-18 21:19:33 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QDataStream& operator<<(QDataStream& s, const smart_playlists::Search& search) {
|
2010-10-29 20:41:49 +02:00
|
|
|
s << search.terms_;
|
|
|
|
s << quint8(search.sort_type_);
|
|
|
|
s << quint8(search.sort_field_);
|
|
|
|
s << qint32(search.limit_);
|
2010-11-19 00:08:37 +01:00
|
|
|
s << quint8(search.search_type_);
|
2010-10-29 20:41:49 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QDataStream& operator>>(QDataStream& s, smart_playlists::Search& search) {
|
2010-11-19 00:08:37 +01:00
|
|
|
quint8 sort_type, sort_field, search_type;
|
2010-10-29 20:41:49 +02:00
|
|
|
qint32 limit;
|
|
|
|
|
2010-11-19 00:08:37 +01:00
|
|
|
s >> search.terms_ >> sort_type >> sort_field >> limit >> search_type;
|
2010-11-18 21:19:33 +01:00
|
|
|
search.sort_type_ = smart_playlists::Search::SortType(sort_type);
|
|
|
|
search.sort_field_ = smart_playlists::SearchTerm::Field(sort_field);
|
2010-10-29 20:41:49 +02:00
|
|
|
search.limit_ = limit;
|
2010-11-19 00:08:37 +01:00
|
|
|
search.search_type_ = smart_playlists::Search::SearchType(search_type);
|
2010-10-29 20:41:49 +02:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|