1 /****************************************************************************************
2  * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #ifndef AMAROK_COLLECTION_SQLQUERYMAKER_H
18 #define AMAROK_COLLECTION_SQLQUERYMAKER_H
19 
20 #include "core/collections/QueryMaker.h"
21 
22 #include "amarok_sqlcollection_export.h"
23 
24 #include <ThreadWeaver/Job>
25 
26 namespace Collections {
27 
28 class SqlCollection;
29 
30 class AMAROK_SQLCOLLECTION_EXPORT SqlQueryMaker : public QueryMaker
31 {
32     Q_OBJECT
33 
34     public:
35         explicit SqlQueryMaker( SqlCollection* collection );
36         ~SqlQueryMaker() override;
37 
38         void abortQuery() override;
39         void run() override;
40 
41         QueryMaker* setQueryType( QueryType type ) override;
42 
43         QueryMaker* addMatch( const Meta::TrackPtr &track ) override;
44         QueryMaker* addMatch( const Meta::ArtistPtr &artist, ArtistMatchBehaviour behaviour = TrackArtists ) override;
45         QueryMaker* addMatch( const Meta::AlbumPtr &album ) override;
46         QueryMaker* addMatch( const Meta::ComposerPtr &composer ) override;
47         QueryMaker* addMatch( const Meta::GenrePtr &genre ) override;
48         QueryMaker* addMatch( const Meta::YearPtr &year ) override;
49         QueryMaker* addMatch( const Meta::LabelPtr &label ) override;
50 
51         QueryMaker* addFilter( qint64 value, const QString &filter, bool matchBegin, bool matchEnd ) override;
52         QueryMaker* excludeFilter( qint64 value, const QString &filter, bool matchBegin, bool matchEnd ) override;
53 
54         QueryMaker* addNumberFilter( qint64 value, qint64 filter, NumberComparison compare ) override;
55         QueryMaker* excludeNumberFilter( qint64 value, qint64 filter, NumberComparison compare ) override;
56 
57         QueryMaker* addReturnValue( qint64 value ) override;
58         QueryMaker* addReturnFunction( ReturnFunction function, qint64 value ) override;
59         QueryMaker* orderBy( qint64 value, bool descending = false ) override;
60 
61         QueryMaker* limitMaxResultSize( int size ) override;
62 
63         QueryMaker* setAlbumQueryMode( AlbumQueryMode mode ) override;
64         QueryMaker* setLabelQueryMode( LabelQueryMode mode ) override;
65 
66         QueryMaker* beginAnd() override;
67         QueryMaker* beginOr() override;
68         QueryMaker* endAndOr() override;
69 
70         QString query();
71         QStringList runQuery( const QString &query );
72         void handleResult( const QStringList &result );
73 
74         // for using it blocking (only for collection internal use)
75 
76         void setBlocking( bool enabled );
77 
78         QStringList collectionIds() const;
79 
80         Meta::TrackList tracks() const;
81         Meta::AlbumList albums() const;
82         Meta::ArtistList artists() const;
83         Meta::GenreList genres() const;
84         Meta::ComposerList composers() const;
85         Meta::YearList years() const;
86         QStringList customData() const;
87         Meta::LabelList labels() const;
88 
89     protected:
90         virtual QString escape( const QString &text ) const;
91 
92         /**
93          * returns a pattern for LIKE operator that will match given text with given options
94          * @param text the text to match (should not be escape()'d, function does it itself)
95          * @param anyBegin wildcard match the beginning of @p text (*text)
96          * @param anyEnd wildcard match the end of @p text (text*)
97          */
98         virtual QString likeCondition( const QString &text, bool anyBegin, bool anyEnd ) const;
99 
100     public Q_SLOTS:
101         void done( ThreadWeaver::JobPointer job );
102         void blockingNewTracksReady( const Meta::TrackList& );
103         void blockingNewArtistsReady( const Meta::ArtistList& );
104         void blockingNewAlbumsReady( const Meta::AlbumList& );
105         void blockingNewGenresReady( const Meta::GenreList& );
106         void blockingNewComposersReady( const Meta::ComposerList& );
107         void blockingNewYearsReady( const Meta::YearList& );
108         void blockingNewResultReady( const QStringList& );
109         void blockingNewLabelsReady( const Meta::LabelList& );
110 
111     private:
112 
113         void linkTables();
114         void buildQuery();
115 
116         QString nameForValue( qint64 value );
117         QString andOr() const;
118 
119         SqlCollection *m_collection;
120 
121         struct Private;
122         Private * const d;
123 
124 };
125 
126 class SqlQueryMakerFactory
127 {
128 public:
129     virtual SqlQueryMaker* createQueryMaker() const = 0;
~SqlQueryMakerFactory()130     virtual ~SqlQueryMakerFactory() {}
131 };
132 
133 } //namespace Collections
134 
135 #endif /* AMAROK_COLLECTION_SQLQUERYMAKER_H */
136