PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_SetTrackAttributes.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 99 lines | 66 code | 15 blank | 18 comment | 6 complexity | 70d5a599bffae1f76768538e41295c79 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_SetTrackAttributes.h"
20#include "TomahawkSqlQuery.h"
21#include "DatabaseImpl.h"
22#include "Source.h"
23#include "utils/Logger.h"
24
25using namespace Tomahawk;
26
27DatabaseCommand_SetTrackAttributes::DatabaseCommand_SetTrackAttributes( DatabaseCommand_SetTrackAttributes::AttributeType type, QList< QPair< QID, QString > > ids, bool toDelete )
28    : DatabaseCommandLoggable()
29    , m_loggable( false )
30    , m_delete( toDelete )
31    , m_type( type )
32    , m_tracks( ids )
33{
34}
35
36DatabaseCommand_SetTrackAttributes::DatabaseCommand_SetTrackAttributes( DatabaseCommand_SetTrackAttributes::AttributeType type )
37    : DatabaseCommandLoggable()
38    , m_loggable( false )
39    , m_delete( true )
40    , m_type( type )
41{
42
43}
44
45
46void
47DatabaseCommand_SetTrackAttributes::exec( DatabaseImpl* dbi )
48{
49    TomahawkSqlQuery checkquery = dbi->newquery();
50    TomahawkSqlQuery delquery = dbi->newquery();
51    TomahawkSqlQuery insertquery = dbi->newquery();
52
53    QString k;
54    switch ( m_type )
55    {
56    case EchonestCatalogId:
57        k = "echonestcatalogid";
58        break;
59    }
60
61    if ( m_delete && m_tracks.isEmpty() )
62    {
63        //delete all
64        TomahawkSqlQuery delAll = dbi->newquery();
65        delAll.prepare( "DELETE FROM track_attributes WHERE k = ?" );
66        delAll.bindValue( 0, k );
67        delAll.exec();
68        return;
69    }
70
71    checkquery.prepare( "SELECT id, sortname FROM track WHERE id = ?" );
72    delquery.prepare( "DELETE FROM track_attributes WHERE id = ? AND k = ?" );
73    insertquery.prepare( "INSERT INTO track_attributes ( id, k, v ) VALUES( ?, ?, ? )" );
74
75    QPair< QID, QString > track;
76    foreach ( track, m_tracks )
77    {
78        checkquery.bindValue( 0, track.first );
79        if ( !checkquery.exec() )
80        {
81            tLog() << "No track in track table for set track attribute command...aborting:" << track.first;
82            continue;
83        }
84
85        delquery.bindValue( 0, track.first );
86        delquery.bindValue( 1, k );
87        delquery.exec();
88
89        if ( m_delete )
90            continue; // stop at deleting, don't insert
91
92        insertquery.bindValue( 0, track.first );
93        insertquery.bindValue( 1, k );
94        insertquery.bindValue( 2, track.second );
95        if ( !insertquery.exec() )
96            tLog() << "Failed to insert track attribute:" << k << track.first << track.second;
97
98    }
99}