PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_AllArtists.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 112 lines | 75 code | 20 blank | 17 comment | 4 complexity | fde1b874ca566f372f896d14aa01a211 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.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_AllArtists.h"
 20
 21#include "utils/TomahawkUtils.h"
 22#include "utils/Logger.h"
 23
 24#include "Artist.h"
 25#include "DatabaseImpl.h"
 26#include "PlaylistEntry.h"
 27#include "Source.h"
 28
 29#include <QSqlQuery>
 30
 31namespace Tomahawk
 32{
 33
 34DatabaseCommand_AllArtists::DatabaseCommand_AllArtists( const Tomahawk::collection_ptr& collection, QObject* parent )
 35    : DatabaseCommand( parent )
 36    , m_collection( collection.objectCast< DatabaseCollection >() )
 37    , m_amount( 0 )
 38    , m_sortOrder( DatabaseCommand_AllArtists::None )
 39    , m_sortDescending( false )
 40{
 41}
 42
 43
 44DatabaseCommand_AllArtists::~DatabaseCommand_AllArtists()
 45{
 46}
 47
 48
 49void
 50DatabaseCommand_AllArtists::exec( DatabaseImpl* dbi )
 51{
 52    TomahawkSqlQuery query = dbi->newquery();
 53    QString orderToken, sourceToken, filterToken, tables, joins;
 54
 55    switch ( m_sortOrder )
 56    {
 57        case 0:
 58            break;
 59
 60        case ModificationTime:
 61            orderToken = "file.mtime";
 62    }
 63
 64    if ( !m_collection.isNull() )
 65        sourceToken = QString( "AND file.source %1" ).arg( m_collection->source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) );
 66
 67    if ( !m_filter.isEmpty() )
 68    {
 69        QString filtersql;
 70        QStringList sl = m_filter.split( " ", QString::SkipEmptyParts );
 71        foreach( QString s, sl )
 72        {
 73            filtersql += QString( " AND ( artist.name LIKE '%%1%' OR album.name LIKE '%%1%' OR track.name LIKE '%%1%' )" ).arg( TomahawkSqlQuery::escape( s ) );
 74        }
 75
 76        filterToken = QString( "AND file_join.track = track.id %1" ).arg( filtersql );
 77        joins = "LEFT JOIN album ON album.id = file_join.album";
 78        tables = "artist, track, file, file_join";
 79    }
 80    else
 81        tables = "artist, file, file_join";
 82
 83    QString sql = QString(
 84            "SELECT DISTINCT artist.id, artist.name "
 85            "FROM %1 "
 86            "%2 "
 87            "WHERE file.id = file_join.file "
 88            "AND file_join.artist = artist.id "
 89            "%3 %4 %5 %6 %7"
 90            ).arg( tables )
 91             .arg( joins )
 92             .arg( sourceToken )
 93             .arg( filterToken )
 94             .arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
 95             .arg( m_sortDescending ? "DESC" : QString() )
 96             .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
 97
 98    query.prepare( sql );
 99    query.exec();
100
101    QList<Tomahawk::artist_ptr> al;
102    while ( query.next() )
103    {
104        Tomahawk::artist_ptr artist = Tomahawk::Artist::get( query.value( 0 ).toUInt(), query.value( 1 ).toString() );
105        al << artist;
106    }
107
108    emit artists( al );
109    emit done();
110}
111
112}