File indexing completed on 2025-01-19 04:24:25
0001 /**************************************************************************************** 0002 * Copyright (c) 2009 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 #include "SqlQueryMakerInternal.h" 0018 0019 #include <core/storage/SqlStorage.h> 0020 #include "core/support/Debug.h" 0021 #include "SqlCollection.h" 0022 #include "SqlMeta.h" 0023 #include "SqlRegistry.h" 0024 0025 #include <QStringList> 0026 0027 using namespace Collections; 0028 0029 SqlQueryMakerInternal::SqlQueryMakerInternal( SqlCollection *collection ) 0030 : QObject() 0031 , m_collection( collection ) 0032 , m_queryType( QueryMaker::None ) 0033 { 0034 } 0035 0036 SqlQueryMakerInternal::~ SqlQueryMakerInternal() 0037 { 0038 disconnect(); 0039 } 0040 0041 void 0042 SqlQueryMakerInternal::run() 0043 { 0044 Q_ASSERT( !m_query.isEmpty() ); 0045 if( !m_collection.isNull() ) 0046 { 0047 QStringList result = m_collection->sqlStorage()->query( m_query ); 0048 handleResult( result ); 0049 } 0050 else 0051 { 0052 deleteLater(); 0053 } 0054 0055 } 0056 0057 void 0058 SqlQueryMakerInternal::setQuery( const QString &query ) 0059 { 0060 //qDebug() << query; 0061 m_query = query; 0062 } 0063 0064 void 0065 SqlQueryMakerInternal::setQueryType( QueryMaker::QueryType type ) 0066 { 0067 m_queryType = type; 0068 } 0069 0070 void 0071 SqlQueryMakerInternal::handleResult( const QStringList &result ) 0072 { 0073 if( !result.isEmpty() ) 0074 { 0075 switch( m_queryType ) { 0076 case QueryMaker::Custom: 0077 Q_EMIT newResultReady( result ); 0078 break; 0079 case QueryMaker::Track: 0080 handleTracks( result ); 0081 break; 0082 case QueryMaker::Artist: 0083 case QueryMaker::AlbumArtist: 0084 handleArtists( result ); 0085 break; 0086 case QueryMaker::Album: 0087 handleAlbums( result ); 0088 break; 0089 case QueryMaker::Genre: 0090 handleGenres( result ); 0091 break; 0092 case QueryMaker::Composer: 0093 handleComposers( result ); 0094 break; 0095 case QueryMaker::Year: 0096 handleYears( result ); 0097 break; 0098 case QueryMaker::Label: 0099 handleLabels( result ); 0100 break; 0101 0102 case QueryMaker::None: 0103 debug() << "Warning: queryResult with queryType == NONE"; 0104 } 0105 } 0106 else 0107 { 0108 switch( m_queryType ) { 0109 case QueryMaker::Custom: 0110 Q_EMIT newResultReady( QStringList() ); 0111 break; 0112 case QueryMaker::Track: 0113 Q_EMIT newTracksReady( Meta::TrackList() ); 0114 break; 0115 case QueryMaker::Artist: 0116 case QueryMaker::AlbumArtist: 0117 Q_EMIT newArtistsReady( Meta::ArtistList() ); 0118 break; 0119 case QueryMaker::Album: 0120 Q_EMIT newAlbumsReady( Meta::AlbumList() ); 0121 break; 0122 case QueryMaker::Genre: 0123 Q_EMIT newGenresReady( Meta::GenreList() ); 0124 break; 0125 case QueryMaker::Composer: 0126 Q_EMIT newComposersReady( Meta::ComposerList() ); 0127 break; 0128 case QueryMaker::Year: 0129 Q_EMIT newYearsReady( Meta::YearList() ); 0130 break; 0131 case QueryMaker::Label: 0132 Q_EMIT newLabelsReady( Meta::LabelList() ); 0133 break; 0134 0135 case QueryMaker::None: 0136 debug() << "Warning: queryResult with queryType == NONE"; 0137 } 0138 } 0139 0140 //queryDone will be emitted in done(Job*) 0141 } 0142 0143 void 0144 SqlQueryMakerInternal::handleTracks( const QStringList &result ) 0145 { 0146 Meta::TrackList tracks; 0147 SqlRegistry* reg = m_collection->registry(); 0148 int returnCount = Meta::SqlTrack::getTrackReturnValueCount(); 0149 int resultRows = result.size() / returnCount; 0150 for( int i = 0; i < resultRows; i++ ) 0151 { 0152 QStringList row = result.mid( i*returnCount, returnCount ); 0153 tracks.append( reg->getTrack( row[Meta::SqlTrack::returnIndex_trackId].toInt(), row ) ); 0154 } 0155 Q_EMIT newTracksReady( tracks ); 0156 } 0157 0158 void 0159 SqlQueryMakerInternal::handleArtists( const QStringList &result ) 0160 { 0161 Meta::ArtistList artists; 0162 SqlRegistry* reg = m_collection->registry(); 0163 for( QStringListIterator iter( result ); iter.hasNext(); ) 0164 { 0165 QString name = iter.next(); 0166 QString id = iter.next(); 0167 if( id.toInt() > 0 ) 0168 artists.append( reg->getArtist( id.toInt(), name ) ); 0169 } 0170 Q_EMIT newArtistsReady( artists ); 0171 } 0172 0173 void 0174 SqlQueryMakerInternal::handleAlbums( const QStringList &result ) 0175 { 0176 Meta::AlbumList albums; 0177 SqlRegistry* reg = m_collection->registry(); 0178 for( QStringListIterator iter( result ); iter.hasNext(); ) 0179 { 0180 QString name = iter.next(); 0181 QString id = iter.next(); 0182 QString artist = iter.next(); 0183 albums.append( reg->getAlbum( id.toInt(), name, artist.toInt() ) ); 0184 } 0185 Q_EMIT newAlbumsReady( albums ); 0186 } 0187 0188 void 0189 SqlQueryMakerInternal::handleGenres( const QStringList &result ) 0190 { 0191 Meta::GenreList genres; 0192 SqlRegistry* reg = m_collection->registry(); 0193 for( QStringListIterator iter( result ); iter.hasNext(); ) 0194 { 0195 QString name = iter.next(); 0196 QString id = iter.next(); 0197 genres.append( reg->getGenre( id.toInt(), name ) ); 0198 } 0199 Q_EMIT newGenresReady( genres ); 0200 } 0201 0202 void 0203 SqlQueryMakerInternal::handleComposers( const QStringList &result ) 0204 { 0205 Meta::ComposerList composers; 0206 SqlRegistry* reg = m_collection->registry(); 0207 for( QStringListIterator iter( result ); iter.hasNext(); ) 0208 { 0209 QString name = iter.next(); 0210 QString id = iter.next(); 0211 composers.append( reg->getComposer( id.toInt(), name ) ); 0212 } 0213 Q_EMIT newComposersReady( composers ); 0214 } 0215 0216 void 0217 SqlQueryMakerInternal::handleYears( const QStringList &result ) 0218 { 0219 Meta::YearList years; 0220 SqlRegistry* reg = m_collection->registry(); 0221 for( QStringListIterator iter( result ); iter.hasNext(); ) 0222 { 0223 QString name = iter.next(); 0224 QString id = iter.next(); 0225 years.append( reg->getYear( id.toInt(), name.toInt() ) ); 0226 } 0227 Q_EMIT newYearsReady( years ); 0228 } 0229 0230 void 0231 SqlQueryMakerInternal::handleLabels( const QStringList &result ) 0232 { 0233 Meta::LabelList labels; 0234 SqlRegistry *reg = m_collection->registry(); 0235 for( QStringListIterator iter( result ); iter.hasNext(); ) 0236 { 0237 QString label = iter.next(); 0238 QString id = iter.next(); 0239 labels.append( reg->getLabel( id.toInt(), label ) ); 0240 } 0241 0242 Q_EMIT newLabelsReady( labels ); 0243 } 0244