File indexing completed on 2024-05-05 04:49:18

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0003  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #ifndef AMAROK_SERVICESQLQUERYMAKER_H
0019 #define AMAROK_SERVICESQLQUERYMAKER_H
0020 
0021 #include "ServiceMetaBase.h"
0022 #include "ServiceSqlRegistry.h"
0023 #include "core/collections/QueryMaker.h"
0024 
0025 #include <QObject>
0026 #include <ThreadWeaver/Job>
0027 #include <ThreadWeaver/ThreadWeaver>
0028 #include <ThreadWeaver/Queue>
0029 
0030 namespace Collections {
0031 
0032 class ServiceSqlCollection;
0033 
0034 class ServiceSqlQueryMaker : public QueryMaker
0035 {
0036     Q_OBJECT
0037 
0038     public:
0039        ServiceSqlQueryMaker( ServiceSqlCollection* collection, ServiceMetaFactory * metaFactory, ServiceSqlRegistry * registry );
0040         ~ServiceSqlQueryMaker() override;
0041 
0042         void abortQuery() override;
0043         void run() override;
0044 
0045         QueryMaker* setQueryType( QueryType type ) override;
0046 
0047         QueryMaker* addMatch( const Meta::TrackPtr &track ) override;
0048         QueryMaker* addMatch( const Meta::ArtistPtr &artist, ArtistMatchBehaviour behaviour = TrackArtists ) override;
0049         QueryMaker* addMatch( const Meta::AlbumPtr &album ) override;
0050         QueryMaker* addMatch( const Meta::ComposerPtr &composer ) override;
0051         QueryMaker* addMatch( const Meta::GenrePtr &genre ) override;
0052         QueryMaker* addMatch( const Meta::YearPtr &year ) override;
0053         QueryMaker* addMatch( const Meta::LabelPtr &label ) override;
0054 
0055         QueryMaker* addFilter( qint64 value, const QString &filter, bool matchBegin, bool matchEnd ) override;
0056         QueryMaker* excludeFilter( qint64 value, const QString &filter, bool matchBegin, bool matchEnd ) override;
0057 
0058         QueryMaker* addNumberFilter( qint64 value, qint64 filter, QueryMaker::NumberComparison compare ) override;
0059         QueryMaker* excludeNumberFilter( qint64 value, qint64 filter, QueryMaker::NumberComparison compare ) override;
0060 
0061         QueryMaker* addReturnValue( qint64 value ) override;
0062         QueryMaker* addReturnFunction( ReturnFunction function, qint64 value ) override;
0063         QueryMaker* orderBy( qint64 value, bool descending = false ) override;
0064 
0065         QueryMaker* limitMaxResultSize( int size ) override;
0066 
0067         QueryMaker* beginAnd() override;
0068         QueryMaker* beginOr() override;
0069         QueryMaker* endAndOr() override;
0070 
0071         QueryMaker* setAlbumQueryMode( AlbumQueryMode mode ) override;
0072 
0073         QString query();
0074         QStringList runQuery( const QString &query );
0075         void handleResult( const QStringList &result );
0076 
0077     protected:
0078         virtual QString escape( const QString &text ) const;
0079         virtual QString likeCondition( const QString &text, bool anyBegin, bool anyEnd ) const;
0080 
0081     public Q_SLOTS:
0082         void done( ThreadWeaver::JobPointer job );
0083 
0084     private:
0085         template<class PointerType, class ListType>
0086         void emitProperResult( const ListType& list );
0087 
0088         void linkTables();
0089         void buildQuery();
0090 
0091         bool isValidValue( qint64 value );
0092         QString nameForValue( qint64 value );
0093         QString andOr() const;
0094 
0095         void handleTracks( const QStringList &result );
0096         void handleArtists( const QStringList &result );
0097         void handleAlbums( const QStringList &result );
0098         void handleGenres( const QStringList &result );
0099         //void handleComposers( const QStringList &result );
0100         //void handleYears( const QStringList &result );
0101 
0102         ServiceSqlCollection *m_collection;
0103         ServiceSqlRegistry * m_registry;
0104         ServiceMetaFactory * m_metaFactory;
0105 
0106         struct Private;
0107         Private * const d;
0108 };
0109 
0110 } //namespace Collections
0111 
0112 #endif /* AMAROK_COLLECTION_SQLQUERYBUILDER_H */