2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QueryOptions::QueryOptions() : max_age_(-1), query_mode_(QueryMode_All) {}
|
2010-05-08 23:54:36 +02:00
|
|
|
|
2010-06-20 18:30:10 +02:00
|
|
|
LibraryQuery::LibraryQuery(const QueryOptions& options)
|
2014-02-07 16:34:20 +01:00
|
|
|
: include_unavailable_(false), join_with_fts_(false), limit_(-1) {
|
2011-02-14 18:29:56 +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.
|
2014-01-13 22:15:07 +01:00
|
|
|
// 3) Remove colons which don't correspond to column names.
|
2010-06-20 18:30:10 +02:00
|
|
|
|
|
|
|
// Split on whitespace
|
2014-02-07 16:34:20 +01:00
|
|
|
QStringList tokens(
|
|
|
|
options.filter().split(QRegExp("\\s+"), QString::SkipEmptyParts));
|
2010-06-20 18:30:10 +02:00
|
|
|
QString query;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QString token : tokens) {
|
2010-12-04 19:16:00 +01:00
|
|
|
token.remove('(');
|
|
|
|
token.remove(')');
|
2011-03-04 22:10:10 +01:00
|
|
|
token.remove('"');
|
2014-01-16 16:35:09 +01:00
|
|
|
token.replace('-', ' ');
|
2010-12-04 19:16:00 +01:00
|
|
|
|
2014-01-13 00:23:54 +01:00
|
|
|
if (token.contains(':')) {
|
|
|
|
// Only prefix fts if the token is a valid column name.
|
|
|
|
if (Song::kFtsColumns.contains("fts" + token.section(':', 0, 0),
|
|
|
|
Qt::CaseInsensitive)) {
|
|
|
|
// Account for multiple colons.
|
2014-02-07 16:34:20 +01:00
|
|
|
QString columntoken =
|
|
|
|
token.section(':', 0, 0, QString::SectionIncludeTrailingSep);
|
2014-01-13 00:23:54 +01:00
|
|
|
QString subtoken = token.section(':', 1, -1);
|
|
|
|
subtoken.replace(":", " ");
|
2014-01-13 22:15:07 +01:00
|
|
|
subtoken = subtoken.trimmed();
|
2014-01-13 00:23:54 +01:00
|
|
|
query += "fts" + columntoken + subtoken + "* ";
|
|
|
|
} else {
|
2014-01-13 22:15:07 +01:00
|
|
|
token.replace(":", " ");
|
|
|
|
token = token.trimmed();
|
2014-01-13 00:23:54 +01:00
|
|
|
query += token + "* ";
|
|
|
|
}
|
|
|
|
} else {
|
2010-06-20 18:30:10 +02:00
|
|
|
query += token + "* ";
|
2014-01-13 00:23:54 +01:00
|
|
|
}
|
2010-06-20 18:30:10 +02:00
|
|
|
}
|
|
|
|
|
2010-11-27 20:37:09 +01:00
|
|
|
where_clauses_ << "fts.%fts_table_noprefix MATCH ?";
|
2010-06-20 18:30:10 +02:00
|
|
|
bound_values_ << query;
|
|
|
|
join_with_fts_ = true;
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2011-02-14 18:29:56 +01:00
|
|
|
if (options.max_age() != -1) {
|
|
|
|
int cutoff = QDateTime::currentDateTime().toTime_t() - options.max_age();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
where_clauses_ << "ctime > ?";
|
|
|
|
bound_values_ << cutoff;
|
|
|
|
}
|
2011-01-30 22:00:49 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
// TODO: currently you cannot use any QueryMode other than All and fts at the
|
|
|
|
// same time.
|
|
|
|
// joining songs, duplicated_songs and songs_fts all together takes a huge
|
|
|
|
// amount of
|
2011-02-06 14:18:18 +01:00
|
|
|
// time. the query takes about 20 seconds on my machine then. why?
|
2014-02-07 16:34:20 +01:00
|
|
|
// untagged mode could work with additional filtering but I'm disabling it
|
|
|
|
// just to be
|
2011-02-06 14:18:18 +01:00
|
|
|
// consistent - this way filtering is available only in the All mode.
|
2014-02-07 16:34:20 +01:00
|
|
|
// remember though that when you fix the Duplicates + FTS cooperation, enable
|
|
|
|
// the
|
2011-02-06 14:18:18 +01:00
|
|
|
// filtering in both Duplicates and Untagged modes.
|
2011-02-14 18:29:56 +01:00
|
|
|
duplicates_only_ = options.query_mode() == QueryOptions::QueryMode_Duplicates;
|
2011-02-06 14:18:18 +01:00
|
|
|
|
2011-02-14 18:29:56 +01:00
|
|
|
if (options.query_mode() == QueryOptions::QueryMode_Untagged) {
|
2011-02-06 14:18:18 +01:00
|
|
|
where_clauses_ << "(artist = '' OR album = '' OR title ='')";
|
2011-01-30 22:00:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LibraryQuery::GetInnerQuery() {
|
|
|
|
return duplicates_only_
|
2014-02-07 16:34:20 +01:00
|
|
|
? QString(
|
|
|
|
" INNER JOIN (select * from duplicated_songs) dsongs "
|
|
|
|
"ON (%songs_table.artist = dsongs.dup_artist "
|
|
|
|
"AND %songs_table.album = dsongs.dup_album "
|
|
|
|
"AND %songs_table.title = dsongs.dup_title) ")
|
2011-01-30 22:00:49 +01:00
|
|
|
: QString();
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2011-01-30 22:00:49 +01:00
|
|
|
void LibraryQuery::AddWhere(const QString& column, const QVariant& value,
|
|
|
|
const QString& op) {
|
|
|
|
// ignore 'literal' for IN
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!op.compare("IN", Qt::CaseInsensitive)) {
|
2011-01-30 22:00:49 +01:00
|
|
|
QStringList final;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& single_value : value.toStringList()) {
|
2011-01-30 22:00:49 +01:00
|
|
|
final.append("?");
|
|
|
|
bound_values_ << single_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
where_clauses_ << QString("%1 IN (" + final.join(",") + ")").arg(column);
|
|
|
|
} else {
|
|
|
|
// Do integers inline - sqlite seems to get confused when you pass integers
|
|
|
|
// to bound parameters
|
2014-02-07 16:34:20 +01:00
|
|
|
if (value.type() == QVariant::Int) {
|
2011-01-30 22:00:49 +01:00
|
|
|
where_clauses_ << QString("%1 %2 %3").arg(column, op, value.toString());
|
2011-01-18 17:34:43 +01:00
|
|
|
} else {
|
|
|
|
where_clauses_ << QString("%1 %2 ?").arg(column, op);
|
|
|
|
bound_values_ << value;
|
|
|
|
}
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-27 21:12:22 +01:00
|
|
|
void LibraryQuery::AddCompilationRequirement(bool compilation) {
|
2014-04-22 12:25:59 +02:00
|
|
|
// The unary + is added to prevent sqlite from using the index
|
|
|
|
// idx_comp_artist. When joining with fts, sqlite 3.8 has a tendency
|
|
|
|
// to use this index and thereby nesting the tables in an order
|
|
|
|
// which gives very poor performance. See
|
|
|
|
// https://github.com/clementine-player/Clementine/pull/4285 for
|
|
|
|
// more details.
|
2014-04-19 14:23:50 +02:00
|
|
|
where_clauses_ << QString("+effective_compilation = %1")
|
2014-02-07 16:34:20 +01:00
|
|
|
.arg(compilation ? 1 : 0);
|
2010-02-27 21:12:22 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 14:43:04 +01:00
|
|
|
QSqlQuery LibraryQuery::Exec(QSqlDatabase db, const QString& songs_table,
|
2010-06-20 18:30:10 +02:00
|
|
|
const QString& fts_table) {
|
|
|
|
QString sql;
|
2011-01-18 17:34:43 +01:00
|
|
|
|
2010-06-20 18:30:10 +02:00
|
|
|
if (join_with_fts_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
sql = QString(
|
|
|
|
"SELECT %1 FROM %2 INNER JOIN %3 AS fts ON %2.ROWID = fts.ROWID")
|
|
|
|
.arg(column_spec_, songs_table, fts_table);
|
2010-06-20 18:30:10 +02:00
|
|
|
} else {
|
2011-01-30 22:00:49 +01:00
|
|
|
sql = QString("SELECT %1 FROM %2 %3")
|
2014-02-07 16:34:20 +01:00
|
|
|
.arg(column_spec_, songs_table, GetInnerQuery());
|
2010-06-20 18:30:10 +02:00
|
|
|
}
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2011-05-14 15:43:57 +02:00
|
|
|
QStringList where_clauses(where_clauses_);
|
|
|
|
if (!include_unavailable_) {
|
|
|
|
where_clauses << "unavailable = 0";
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!where_clauses.isEmpty()) sql += " WHERE " + where_clauses.join(" AND ");
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!order_by_.isEmpty()) sql += " ORDER BY " + order_by_;
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (limit_ != -1) sql += " LIMIT " + QString::number(limit_);
|
2010-06-20 18:30:10 +02:00
|
|
|
|
|
|
|
sql.replace("%songs_table", songs_table);
|
2010-11-27 20:37:09 +01:00
|
|
|
sql.replace("%fts_table_noprefix", fts_table.section('.', -1, -1));
|
2010-06-20 18:30:10 +02:00
|
|
|
sql.replace("%fts_table", fts_table);
|
2011-01-18 17:34:43 +01:00
|
|
|
|
2010-03-31 02:30:57 +02:00
|
|
|
query_ = QSqlQuery(sql, db);
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
// Bind values
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QVariant& value : bound_values_) {
|
|
|
|
query_.addBindValue(value);
|
|
|
|
}
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-03-31 02:30:57 +02:00
|
|
|
query_.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
return query_;
|
2010-03-31 02:30:57 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool LibraryQuery::Next() { return query_.next(); }
|
2010-03-31 02:30:57 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QVariant LibraryQuery::Value(int column) const { return query_.value(column); }
|
2010-01-15 22:43:57 +01:00
|
|
|
|
|
|
|
bool QueryOptions::Matches(const Song& song) const {
|
2011-02-14 18:29:56 +01:00
|
|
|
if (max_age_ != -1) {
|
|
|
|
const uint cutoff = QDateTime::currentDateTime().toTime_t() - max_age_;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (song.ctime() <= cutoff) return false;
|
2010-01-15 22:43:57 +01:00
|
|
|
}
|
|
|
|
|
2011-02-14 18:29:56 +01:00
|
|
|
if (!filter_.isNull()) {
|
|
|
|
return song.artist().contains(filter_, Qt::CaseInsensitive) ||
|
|
|
|
song.album().contains(filter_, Qt::CaseInsensitive) ||
|
|
|
|
song.title().contains(filter_, Qt::CaseInsensitive);
|
2010-01-15 22:43:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|