2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
#include "libraryquery.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "core/song.h"
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
#include <QDateTime>
|
2010-03-31 02:30:57 +02:00
|
|
|
#include <QSqlError>
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
QueryOptions::QueryOptions()
|
|
|
|
: max_age(-1)
|
2009-12-24 20:16:07 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-05-08 23:54:36 +02:00
|
|
|
|
2010-06-20 18:30:10 +02:00
|
|
|
LibraryQuery::LibraryQuery(const QueryOptions& options)
|
|
|
|
: join_with_fts_(false),
|
|
|
|
limit_(-1)
|
|
|
|
{
|
2009-12-24 20:16:07 +01:00
|
|
|
if (!options.filter.isEmpty()) {
|
2010-06-20 18:30:10 +02:00
|
|
|
// We need to munge the filter text a little bit to get it to work as
|
|
|
|
// expected with sqlite's FTS3:
|
|
|
|
// 1) Append * to all tokens.
|
|
|
|
// 2) Prefix "fts" to column names.
|
|
|
|
|
|
|
|
// Split on whitespace
|
|
|
|
QStringList tokens(options.filter.split(QRegExp("\\s+")));
|
|
|
|
QString query;
|
|
|
|
foreach (const QString& token, tokens) {
|
|
|
|
if (token.contains(':'))
|
|
|
|
query += "fts" + token + "* ";
|
|
|
|
else
|
|
|
|
query += token + "* ";
|
|
|
|
}
|
|
|
|
|
|
|
|
where_clauses_ << "fts.%fts_table MATCH ?";
|
|
|
|
bound_values_ << query;
|
|
|
|
join_with_fts_ = true;
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.max_age != -1) {
|
|
|
|
int cutoff = QDateTime::currentDateTime().toTime_t() - options.max_age;
|
|
|
|
|
|
|
|
where_clauses_ << "ctime > ?";
|
|
|
|
bound_values_ << cutoff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-12 00:35:41 +02:00
|
|
|
void LibraryQuery::AddWhere(const QString& column, const QVariant& value, const QString& op) {
|
2009-12-24 20:16:07 +01:00
|
|
|
// Do integers inline - sqlite seems to get confused when you pass integers
|
|
|
|
// to bound parameters
|
|
|
|
|
|
|
|
if (value.type() == QVariant::Int)
|
2010-06-12 00:35:41 +02:00
|
|
|
where_clauses_ << QString("%1 %2 %3").arg(column, op, value.toString());
|
2009-12-24 20:16:07 +01:00
|
|
|
else {
|
2010-06-12 00:35:41 +02:00
|
|
|
where_clauses_ << QString("%1 %2 ?").arg(column, op);
|
2009-12-24 20:16:07 +01:00
|
|
|
bound_values_ << value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-27 21:12:22 +01:00
|
|
|
void LibraryQuery::AddCompilationRequirement(bool compilation) {
|
2010-03-21 22:17:01 +01:00
|
|
|
where_clauses_ << QString("effective_compilation = %1").arg(compilation ? 1 : 0);
|
2010-02-27 21:12:22 +01:00
|
|
|
}
|
|
|
|
|
2010-06-20 18:30:10 +02:00
|
|
|
QSqlError LibraryQuery::Exec(QSqlDatabase db, const QString& songs_table,
|
|
|
|
const QString& fts_table) {
|
|
|
|
QString sql;
|
|
|
|
if (join_with_fts_) {
|
|
|
|
sql = QString("SELECT %1 FROM %2 INNER JOIN %3 AS fts ON %2.ROWID = fts.ROWID")
|
|
|
|
.arg(column_spec_, songs_table, fts_table);
|
|
|
|
} else {
|
|
|
|
sql = QString("SELECT %1 FROM %2")
|
|
|
|
.arg(column_spec_, songs_table);
|
|
|
|
}
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if (!where_clauses_.isEmpty())
|
|
|
|
sql += " WHERE " + where_clauses_.join(" AND ");
|
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
if (!order_by_.isEmpty())
|
|
|
|
sql += " ORDER BY " + order_by_;
|
|
|
|
|
2010-06-20 18:30:10 +02:00
|
|
|
if (limit_ != -1)
|
|
|
|
sql += " LIMIT " + QString::number(limit_);
|
|
|
|
|
|
|
|
sql.replace("%songs_table", songs_table);
|
|
|
|
sql.replace("%fts_table", fts_table);
|
2010-03-31 02:30:57 +02:00
|
|
|
query_ = QSqlQuery(sql, db);
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
// Bind values
|
|
|
|
foreach (const QVariant& value, bound_values_) {
|
2010-03-31 02:30:57 +02:00
|
|
|
query_.addBindValue(value);
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2010-03-31 02:30:57 +02:00
|
|
|
query_.exec();
|
|
|
|
return query_.lastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibraryQuery::Next() {
|
|
|
|
return query_.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant LibraryQuery::Value(int column) const {
|
|
|
|
return query_.value(column);
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
2010-01-15 22:43:57 +01:00
|
|
|
|
|
|
|
bool QueryOptions::Matches(const Song& song) const {
|
|
|
|
if (max_age != -1) {
|
|
|
|
const uint cutoff = QDateTime::currentDateTime().toTime_t() - max_age;
|
|
|
|
if (song.ctime() <= cutoff)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!filter.isNull()) {
|
|
|
|
return song.artist().contains(filter, Qt::CaseInsensitive) ||
|
|
|
|
song.album().contains(filter, Qt::CaseInsensitive) ||
|
|
|
|
song.title().contains(filter, Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|