PageRenderTime 243ms CodeModel.GetById 60ms app.highlight 7ms RepoModel.GetById 115ms app.codeStats 1ms

/src/libtomahawk/database/DatabaseCommand_LoadOps.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 80 lines | 51 code | 11 blank | 18 comment | 2 complexity | dcf571d07d2f0a7cc260361dd37e4b9e 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_LoadOps.h"
20
21#include "DatabaseImpl.h"
22#include "TomahawkSqlQuery.h"
23#include "Source.h"
24#include "utils/Logger.h"
25
26namespace Tomahawk
27{
28
29void
30DatabaseCommand_loadOps::exec( DatabaseImpl* dbi )
31{
32    QList< dbop_ptr > ops;
33
34    if ( !m_since.isEmpty() )
35    {
36        TomahawkSqlQuery query = dbi->newquery();
37        query.prepare( QString( "SELECT id FROM oplog WHERE guid = ?" ) );
38        query.addBindValue( m_since );
39        query.exec();
40
41        if ( !query.next() )
42        {
43            tLog() << "Unknown oplog guid, requested, not replying:" << m_since;
44            Q_ASSERT( false );
45            emit done( m_since, m_since, ops );
46            return;
47        }
48    }
49
50    TomahawkSqlQuery query = dbi->newquery();
51    query.prepare( QString(
52                   "SELECT guid, command, json, compressed, singleton "
53                   "FROM oplog "
54                   "WHERE source %1 "
55                   "AND id > coalesce((SELECT id FROM oplog WHERE guid = ?),0) "
56                   "ORDER BY id ASC"
57                   ).arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) )
58                  );
59    query.addBindValue( m_since );
60    query.exec();
61
62    QString lastguid = m_since;
63    while( query.next() )
64    {
65        dbop_ptr op( new DBOp );
66        op->guid = query.value( 0 ).toString();
67        op->command = query.value( 1 ).toString();
68        op->payload = query.value( 2 ).toByteArray();
69        op->compressed = query.value( 3 ).toBool();
70        op->singleton = query.value( 4 ).toBool();
71
72        lastguid = op->guid;
73        ops << op;
74    }
75
76//    qDebug() << "Loaded" << ops.length() << "ops from db";
77    emit done( m_since, lastguid, ops );
78}
79
80}