PageRenderTime 398ms CodeModel.GetById 132ms app.highlight 162ms RepoModel.GetById 103ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_TrackAttributes.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 74 lines | 49 code | 8 blank | 17 comment | 4 complexity | 3a31c7a3c2ac120ea39d6a9c7993ced1 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_TrackAttributes.h"
20#include "DatabaseImpl.h"
21#include "Source.h"
22
23using namespace Tomahawk;
24
25DatabaseCommand_TrackAttributes::DatabaseCommand_TrackAttributes( DatabaseCommand_SetTrackAttributes::AttributeType type, const QList< Tomahawk::QID > ids )
26    : DatabaseCommand()
27    , m_type( type )
28    , m_ids( ids )
29{
30}
31
32DatabaseCommand_TrackAttributes::DatabaseCommand_TrackAttributes( DatabaseCommand_SetTrackAttributes::AttributeType type )
33    : DatabaseCommand()
34    , m_type( type )
35{
36}
37
38void DatabaseCommand_TrackAttributes::exec( DatabaseImpl* lib )
39{
40    TomahawkSqlQuery query = lib->newquery();
41
42    QString k;
43    switch ( m_type )
44    {
45        case DatabaseCommand_SetTrackAttributes::EchonestCatalogId:
46            k = "echonestcatalogid";
47            break;
48    }
49
50    PairList results;
51    if ( !m_ids.isEmpty() )
52    {
53        foreach ( const QID id, m_ids )
54        {
55            query.prepare( "SELECT v FROM track_attributes WHERE id = ? AND k = ?" );
56            query.bindValue( 0, id );
57            query.bindValue( 1, k );
58            if ( query.exec() )
59                results.append( QPair< QID, QString >( id, query.value( 0 ).toString() ) );
60        }
61    }
62    else
63    {
64        query.prepare( "SELECT id, v FROM track_attributes WHERE k = ?" );
65        query.bindValue( 0, k );
66        query.exec();
67        while ( query.next() )
68        {
69            results.append( QPair< QID, QString >( query.value( 0 ).toString(), query.value( 1 ).toString() ) );
70        }
71    }
72
73    emit trackAttributes( results );
74}