PageRenderTime 115ms CodeModel.GetById 73ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_AddSource.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 71 lines | 41 code | 13 blank | 17 comment | 1 complexity | ae71e6fffd0cede8601bc0fb879926ed 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_AddSource.h"
20
21#include <QSqlQuery>
22
23#include "DatabaseImpl.h"
24#include "utils/Logger.h"
25#include "Source.h"
26
27namespace Tomahawk
28{
29
30DatabaseCommand_addSource::DatabaseCommand_addSource( const QString& username, const QString& fname, QObject* parent )
31    : DatabaseCommand( parent )
32    , m_username( username )
33    , m_fname( fname )
34{
35}
36
37
38void
39DatabaseCommand_addSource::exec( DatabaseImpl* dbi )
40{
41    Q_ASSERT( !m_fname.isEmpty() );
42
43    TomahawkSqlQuery query = dbi->newquery();
44    query.prepare( "SELECT id FROM source WHERE name = ?" );
45    query.addBindValue( m_username );
46    query.exec();
47
48    if ( query.next() )
49    {
50        unsigned int id = query.value( 0 ).toInt();
51        query.prepare( "UPDATE source SET isonline = 'true', friendlyname = ? WHERE id = ?" );
52        query.addBindValue( m_fname );
53        query.addBindValue( id );
54        query.exec();
55        emit done( id, m_fname );
56        return;
57    }
58
59    query.prepare( "INSERT INTO source(name, friendlyname, isonline) VALUES(?,?,?)" );
60    query.addBindValue( m_username );
61    query.addBindValue( m_fname );
62    query.addBindValue( "true" );
63    query.exec();
64
65    unsigned int id = query.lastInsertId().toUInt();
66    tDebug() << "Inserted new source to DB, id:" << id << "friendlyname" << m_username;
67
68    emit done( id, m_fname );
69}
70
71}