File indexing completed on 2025-03-09 04:24:19

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef AMAROK_COLLECTION_SQLQUERYMAKER_H
0018 #define AMAROK_COLLECTION_SQLQUERYMAKER_H
0019 
0020 #include "core/collections/QueryMaker.h"
0021 
0022 #include "amarok_sqlcollection_export.h"
0023 
0024 #include <ThreadWeaver/Job>
0025 
0026 namespace Collections {
0027 
0028 class SqlCollection;
0029 
0030 class AMAROK_SQLCOLLECTION_EXPORT SqlQueryMaker : public QueryMaker
0031 {
0032     Q_OBJECT
0033 
0034     public:
0035         explicit SqlQueryMaker( SqlCollection* collection );
0036         ~SqlQueryMaker() override;
0037 
0038         void abortQuery() override;
0039         void run() override;
0040 
0041         QueryMaker* setQueryType( QueryType type ) override;
0042 
0043         QueryMaker* addMatch( const Meta::TrackPtr &track ) override;
0044         QueryMaker* addMatch( const Meta::ArtistPtr &artist, ArtistMatchBehaviour behaviour = TrackArtists ) override;
0045         QueryMaker* addMatch( const Meta::AlbumPtr &album ) override;
0046         QueryMaker* addMatch( const Meta::ComposerPtr &composer ) override;
0047         QueryMaker* addMatch( const Meta::GenrePtr &genre ) override;
0048         QueryMaker* addMatch( const Meta::YearPtr &year ) override;
0049         QueryMaker* addMatch( const Meta::LabelPtr &label ) override;
0050 
0051         QueryMaker* addFilter( qint64 value, const QString &filter, bool matchBegin, bool matchEnd ) override;
0052         QueryMaker* excludeFilter( qint64 value, const QString &filter, bool matchBegin, bool matchEnd ) override;
0053 
0054         QueryMaker* addNumberFilter( qint64 value, qint64 filter, NumberComparison compare ) override;
0055         QueryMaker* excludeNumberFilter( qint64 value, qint64 filter, NumberComparison compare ) override;
0056 
0057         QueryMaker* addReturnValue( qint64 value ) override;
0058         QueryMaker* addReturnFunction( ReturnFunction function, qint64 value ) override;
0059         QueryMaker* orderBy( qint64 value, bool descending = false ) override;
0060 
0061         QueryMaker* limitMaxResultSize( int size ) override;
0062 
0063         QueryMaker* setAlbumQueryMode( AlbumQueryMode mode ) override;
0064         QueryMaker* setLabelQueryMode( LabelQueryMode mode ) override;
0065 
0066         QueryMaker* beginAnd() override;
0067         QueryMaker* beginOr() override;
0068         QueryMaker* endAndOr() override;
0069 
0070         QString query();
0071         QStringList runQuery( const QString &query );
0072         void handleResult( const QStringList &result );
0073 
0074         // for using it blocking (only for collection internal use)
0075 
0076         void setBlocking( bool enabled );
0077 
0078         QStringList collectionIds() const;
0079 
0080         Meta::TrackList tracks() const;
0081         Meta::AlbumList albums() const;
0082         Meta::ArtistList artists() const;
0083         Meta::GenreList genres() const;
0084         Meta::ComposerList composers() const;
0085         Meta::YearList years() const;
0086         QStringList customData() const;
0087         Meta::LabelList labels() const;
0088 
0089     protected:
0090         virtual QString escape( const QString &text ) const;
0091 
0092         /**
0093          * returns a pattern for LIKE operator that will match given text with given options
0094          * @param text the text to match (should not be escape()'d, function does it itself)
0095          * @param anyBegin wildcard match the beginning of @p text (*text)
0096          * @param anyEnd wildcard match the end of @p text (text*)
0097          */
0098         virtual QString likeCondition( const QString &text, bool anyBegin, bool anyEnd ) const;
0099 
0100     public Q_SLOTS:
0101         void done( ThreadWeaver::JobPointer job );
0102         void blockingNewTracksReady( const Meta::TrackList& );
0103         void blockingNewArtistsReady( const Meta::ArtistList& );
0104         void blockingNewAlbumsReady( const Meta::AlbumList& );
0105         void blockingNewGenresReady( const Meta::GenreList& );
0106         void blockingNewComposersReady( const Meta::ComposerList& );
0107         void blockingNewYearsReady( const Meta::YearList& );
0108         void blockingNewResultReady( const QStringList& );
0109         void blockingNewLabelsReady( const Meta::LabelList& );
0110 
0111     private:
0112 
0113         void linkTables();
0114         void buildQuery();
0115 
0116         QString nameForValue( qint64 value );
0117         QString andOr() const;
0118 
0119         SqlCollection *m_collection;
0120 
0121         struct Private;
0122         Private * const d;
0123 
0124 };
0125 
0126 class SqlQueryMakerFactory
0127 {
0128 public:
0129     virtual SqlQueryMaker* createQueryMaker() const = 0;
0130     virtual ~SqlQueryMakerFactory() {}
0131 };
0132 
0133 } //namespace Collections
0134 
0135 #endif /* AMAROK_COLLECTION_SQLQUERYMAKER_H */