1 /* This file is part of Clementine.
2    Copyright 2010, David Sansome <me@davidsansome.com>
3 
4    Clementine is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    Clementine is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef LIBRARYQUERY_H
19 #define LIBRARYQUERY_H
20 
21 #include <QString>
22 #include <QVariant>
23 #include <QSqlQuery>
24 #include <QStringList>
25 #include <QVariantList>
26 
27 class Song;
28 class LibraryBackend;
29 
30 // This structure let's you customize behaviour of any LibraryQuery.
31 struct QueryOptions {
32   // Modes of LibraryQuery:
33   // - use the all songs table
34   // - use the duplicated songs view; by duplicated we mean those songs
35   //   for which the (artist, album, title) tuple is found more than once
36   //   in the songs table
37   // - use the untagged songs view; by untagged we mean those for which
38   //   at least one of the (artist, album, title) tags is empty
39   // Please note that additional filtering based on fts table (the filter
40   // attribute) won't work in Duplicates and Untagged modes.
41   enum QueryMode { QueryMode_All, QueryMode_Duplicates, QueryMode_Untagged };
42 
43   QueryOptions();
44 
45   bool Matches(const Song& song) const;
46 
filterQueryOptions47   QString filter() const { return filter_; }
set_filterQueryOptions48   void set_filter(const QString& filter) {
49     this->filter_ = filter;
50     this->query_mode_ = QueryMode_All;
51   }
52 
max_ageQueryOptions53   int max_age() const { return max_age_; }
set_max_ageQueryOptions54   void set_max_age(int max_age) { this->max_age_ = max_age; }
55 
query_modeQueryOptions56   QueryMode query_mode() const { return query_mode_; }
set_query_modeQueryOptions57   void set_query_mode(QueryMode query_mode) {
58     this->query_mode_ = query_mode;
59     this->filter_ = QString();
60   }
61 
62  private:
63   QString filter_;
64   int max_age_;
65   QueryMode query_mode_;
66 };
67 
68 class LibraryQuery {
69  public:
70   LibraryQuery(const QueryOptions& options = QueryOptions());
71 
72   // Sets contents of SELECT clause on the query (list of columns to get).
SetColumnSpec(const QString & spec)73   void SetColumnSpec(const QString& spec) { column_spec_ = spec; }
74   // Sets an ORDER BY clause on the query.
SetOrderBy(const QString & order_by)75   void SetOrderBy(const QString& order_by) { order_by_ = order_by; }
76 
77   // Adds a fragment of WHERE clause. When executed, this Query will connect all
78   // the fragments with AND operator.
79   // Please note that IN operator expects a QStringList as value.
80   void AddWhere(const QString& column, const QVariant& value,
81                 const QString& op = "=");
82 
83   void AddCompilationRequirement(bool compilation);
SetLimit(int limit)84   void SetLimit(int limit) { limit_ = limit; }
SetIncludeUnavailable(bool include_unavailable)85   void SetIncludeUnavailable(bool include_unavailable) {
86     include_unavailable_ = include_unavailable;
87   }
88 
89   QSqlQuery Exec(QSqlDatabase db, const QString& songs_table,
90                  const QString& fts_table);
91   bool Next();
92   QVariant Value(int column) const;
93 
94   operator const QSqlQuery&() const { return query_; }
95 
96   static const QStringList kNumericCompOperators;
97 
98  private:
99   QString GetInnerQuery();
100 
101   bool include_unavailable_;
102   bool join_with_fts_;
103   QString column_spec_;
104   QString order_by_;
105   QStringList where_clauses_;
106   QVariantList bound_values_;
107   int limit_;
108   bool duplicates_only_;
109 
110   QSqlQuery query_;
111 };
112 
113 #endif  // LIBRARYQUERY_H
114