2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine .
* Copyright 2010 , David Sansome < me @ davidsansome . com >
2021-03-20 21:14:47 +01:00
* Copyright 2018 - 2021 , Jonas Kvinge < jonas @ jkvinge . net >
2018-02-27 18:06:05 +01:00
*
* Strawberry 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 .
*
* Strawberry 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 Strawberry . If not , see < http : //www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
# ifndef COLLECTIONQUERY_H
# define COLLECTIONQUERY_H
# include "config.h"
2018-05-01 00:41:33 +02:00
# include <QMetaType>
2018-02-27 18:06:05 +01:00
# include <QVariant>
2018-05-01 00:41:33 +02:00
# include <QString>
2018-02-27 18:06:05 +01:00
# include <QStringList>
2021-09-09 21:45:46 +02:00
# include <QMap>
2018-05-01 00:41:33 +02:00
# include <QSqlDatabase>
# include <QSqlQuery>
2018-02-27 18:06:05 +01:00
class Song ;
// This structure let's you customize behaviour of any CollectionQuery.
struct QueryOptions {
// Modes of CollectionQuery:
// - use the all songs table
2018-05-01 00:41:33 +02:00
// - use the duplicated songs view; by duplicated we mean those songs for which the (artist, album, title) tuple is found more than once in the songs table
// - use the untagged songs view; by untagged we mean those for which at least one of the (artist, album, title) tags is empty
2021-04-10 03:20:25 +02:00
// Please note that additional filtering based on FTS table (the filter attribute) won't work in Duplicates and Untagged modes.
2018-02-27 18:06:05 +01:00
enum QueryMode {
QueryMode_All ,
QueryMode_Duplicates ,
QueryMode_Untagged
} ;
QueryOptions ( ) ;
bool Matches ( const Song & song ) const ;
QString filter ( ) const { return filter_ ; }
void set_filter ( const QString & filter ) {
2021-09-13 20:49:33 +02:00
filter_ = filter ;
query_mode_ = QueryMode_All ;
2018-02-27 18:06:05 +01:00
}
int max_age ( ) const { return max_age_ ; }
2021-09-13 20:49:33 +02:00
void set_max_age ( int max_age ) { max_age_ = max_age ; }
2018-02-27 18:06:05 +01:00
QueryMode query_mode ( ) const { return query_mode_ ; }
void set_query_mode ( QueryMode query_mode ) {
2021-09-13 20:49:33 +02:00
query_mode_ = query_mode ;
filter_ = QString ( ) ;
2018-02-27 18:06:05 +01:00
}
private :
QString filter_ ;
int max_age_ ;
QueryMode query_mode_ ;
} ;
2021-04-10 03:20:25 +02:00
class CollectionQuery : public QSqlQuery {
2018-02-27 18:06:05 +01:00
public :
2021-04-10 03:20:25 +02:00
explicit CollectionQuery ( const QSqlDatabase & db , const QString & songs_table , const QString & fts_table , const QueryOptions & options = QueryOptions ( ) ) ;
2018-02-27 18:06:05 +01:00
// Sets contents of SELECT clause on the query (list of columns to get).
void SetColumnSpec ( const QString & spec ) { column_spec_ = spec ; }
2021-04-10 03:20:25 +02:00
2018-02-27 18:06:05 +01:00
// Sets an ORDER BY clause on the query.
void SetOrderBy ( const QString & order_by ) { order_by_ = order_by ; }
2018-05-01 00:41:33 +02:00
// Adds a fragment of WHERE clause. When executed, this Query will connect all the fragments with AND operator.
2018-02-27 18:06:05 +01:00
// Please note that IN operator expects a QStringList as value.
void AddWhere ( const QString & column , const QVariant & value , const QString & op = " = " ) ;
2019-04-18 00:45:32 +02:00
void AddWhereArtist ( const QVariant & value ) ;
2018-02-27 18:06:05 +01:00
2021-04-10 03:20:25 +02:00
void SetWhereClauses ( const QStringList & where_clauses ) { where_clauses_ = where_clauses ; }
void SetBoundValues ( const QVariantList & bound_values ) { bound_values_ = bound_values ; }
void SetDuplicatesOnly ( const bool duplicates_only ) { duplicates_only_ = duplicates_only ; }
void SetIncludeUnavailable ( const bool include_unavailable ) { include_unavailable_ = include_unavailable ; }
void SetLimit ( const int limit ) { limit_ = limit ; }
void AddCompilationRequirement ( const bool compilation ) ;
2018-02-27 18:06:05 +01:00
2021-04-10 03:20:25 +02:00
bool Exec ( ) ;
2018-02-27 18:06:05 +01:00
bool Next ( ) ;
2021-04-10 03:20:25 +02:00
QVariant Value ( const int column ) const ;
2018-02-27 18:06:05 +01:00
2021-04-10 03:20:25 +02:00
QString column_spec ( ) const { return column_spec_ ; }
QString order_by ( ) const { return order_by_ ; }
QStringList where_clauses ( ) const { return where_clauses_ ; }
QVariantList bound_values ( ) const { return bound_values_ ; }
bool include_unavailable ( ) const { return include_unavailable_ ; }
bool join_with_fts ( ) const { return join_with_fts_ ; }
bool duplicates_only ( ) const { return duplicates_only_ ; }
int limit ( ) const { return limit_ ; }
2018-02-27 18:06:05 +01:00
private :
2021-06-22 13:45:29 +02:00
QString GetInnerQuery ( ) const ;
2018-02-27 18:06:05 +01:00
2021-04-10 03:20:25 +02:00
QSqlDatabase db_ ;
QString songs_table_ ;
QString fts_table_ ;
2018-02-27 18:06:05 +01:00
QString column_spec_ ;
QString order_by_ ;
QStringList where_clauses_ ;
QVariantList bound_values_ ;
2021-04-10 03:20:25 +02:00
bool include_unavailable_ ;
bool join_with_fts_ ;
bool duplicates_only_ ;
int limit_ ;
2018-02-27 18:06:05 +01:00
} ;
2022-03-22 21:09:05 +01:00
# endif // COLLECTIONQUERY_H