PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 16ms RepoModel.GetById 24ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_GenericSelect.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 151 lines | 113 code | 20 blank | 18 comment | 32 complexity | 829b8d3293d44fe0d5cf88c8fe9edf39 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "DatabaseCommand_GenericSelect.h"
 20
 21#include "utils/Logger.h"
 22
 23#include "Album.h"
 24#include "Artist.h"
 25#include "DatabaseImpl.h"
 26#include "Pipeline.h"
 27#include "PlaylistEntry.h"
 28#include "SourceList.h"
 29
 30using namespace Tomahawk;
 31
 32
 33DatabaseCommand_GenericSelect::DatabaseCommand_GenericSelect( const QString& sqlSelect, QueryType type, int limit, QObject* parent )
 34    : DatabaseCommand( parent )
 35    , m_sqlSelect( sqlSelect )
 36    , m_queryType( type )
 37    , m_limit( limit )
 38    , m_raw( false )
 39{
 40}
 41
 42DatabaseCommand_GenericSelect::DatabaseCommand_GenericSelect( const QString& sqlSelect, QueryType type, bool rawData, QObject* parent )
 43    : DatabaseCommand( parent )
 44    , m_sqlSelect( sqlSelect )
 45    , m_queryType( type )
 46    , m_limit( -1 )
 47    , m_raw( rawData )
 48{
 49}
 50
 51void
 52DatabaseCommand_GenericSelect::exec( DatabaseImpl* dbi )
 53{
 54    TomahawkSqlQuery query = dbi->newquery();
 55
 56    query.prepare( QString( "%1 %2;" ).arg( m_sqlSelect ).arg( m_limit > -1 ? QString( " LIMIT %1" ).arg( m_limit ) : QString() ) );
 57    query.exec();
 58
 59    QList< query_ptr > queries;
 60    QList< artist_ptr > arts;
 61    QList< album_ptr > albs;
 62
 63    if ( m_raw )
 64    {
 65        QList< QStringList > rawDataItems;
 66
 67        while( query.next() )
 68        {
 69            QStringList rawRow;
 70            int count = 0;
 71            while ( query.value( count ).isValid() )
 72            {
 73                rawRow << query.value( count ).toString();
 74                ++count;
 75            }
 76            rawDataItems << rawRow;
 77        }
 78        emit rawData( rawDataItems );
 79        return;
 80    }
 81
 82    // Expecting
 83    while ( query.next() )
 84    {
 85        query_ptr qry;
 86        artist_ptr artist;
 87        album_ptr album;
 88
 89        if ( m_queryType == Track )
 90        {
 91            QString artist, track;
 92            track = query.value( 0 ).toString();
 93            artist = query.value( 1 ).toString();
 94
 95            qry = Tomahawk::Query::get( artist, track, QString() );
 96            if ( qry.isNull() )
 97                continue;
 98        }
 99        else if ( m_queryType == Artist )
100        {
101            int artistId = query.value( 0 ).toInt();
102            QString artistName = query.value( 1 ).toString();
103
104            artist = Tomahawk::Artist::get( artistId, artistName );
105        }
106        else if ( m_queryType == Album )
107        {
108            int albumId = query.value( 0 ).toInt();
109            QString albumName = query.value( 1 ).toString();
110            int artistId = query.value( 2 ).toInt();
111            QString artistName = query.value( 3 ).toString();
112
113            artist = Tomahawk::Artist::get( artistId, artistName );
114            album = Tomahawk::Album::get( albumId, albumName, artist );
115        }
116
117        QVariantList extraData;
118        int count = 2;
119        while ( query.value( count ).isValid() )
120        {
121            extraData << query.value( count );
122            count++;
123        }
124
125        if ( m_queryType == Track )
126        {
127            if ( !extraData.isEmpty() )
128                qry->setProperty( "data", extraData );
129            queries << qry;
130        }
131        else if ( m_queryType == Artist )
132        {
133            if ( !extraData.isEmpty() )
134                artist->setProperty( "data", extraData );
135            arts << artist;
136        }
137        else if ( m_queryType == Album )
138        {
139            if ( !extraData.isEmpty() )
140                album->setProperty( "data", extraData );
141            albs << album;
142        }
143    }
144
145    if ( m_queryType == Track )
146        emit tracks( queries );
147    else if ( m_queryType == Artist )
148        emit artists( arts );
149    else if ( m_queryType == Album )
150        emit albums( albs );
151}