void
DatabaseCommand_ModifyInboxEntry::exec( DatabaseImpl* dbi )
{
    TomahawkSqlQuery query = dbi->newquery();

    Q_ASSERT( !m_query.isNull() );

    if ( m_query->queryTrack()->track().isEmpty() || m_query->queryTrack()->artist().isEmpty() )
    {
        emit done();
        return;
    }

    query.prepare(
                "UPDATE social_attributes "
                "SET v = ? "
                "WHERE social_attributes.k = ? AND social_attributes.id = ( "
                    "SELECT id FROM track "
                    "WHERE track.name = ? AND track.artist = ( "
                        "SELECT id FROM artist WHERE artist.name = ? "
                    ") "
                ")" );
    query.addBindValue( m_newValue );
    query.addBindValue( "Inbox" );
    query.addBindValue( m_query->queryTrack()->track() );
    query.addBindValue( m_query->queryTrack()->artist() );

    query.exec();

    emit done();
}
void
DatabaseCommand_CreateDynamicPlaylist::exec( DatabaseImpl* lib )
{
    qDebug() << Q_FUNC_INFO;
    Q_ASSERT( !( m_playlist.isNull() && m_v.isNull() ) );
    Q_ASSERT( !source().isNull() );

    DatabaseCommand_CreatePlaylist::createPlaylist( lib, true );
    qDebug() << "Created normal playlist, now creating additional dynamic info!";

    qDebug() << "Create dynamic execing!" << m_playlist << m_v;
    TomahawkSqlQuery cre = lib->newquery();

    cre.prepare( "INSERT INTO dynamic_playlist( guid, pltype, plmode, autoload ) "
                 "VALUES( ?, ?, ?, ? )" );

    if( m_playlist.isNull() ) {
        QVariantMap m = m_v.toMap();
        cre.addBindValue( m.value( "guid" ) );
        cre.addBindValue( m.value( "type" ) );
        cre.addBindValue( m.value( "mode" ) );
    } else {
        cre.addBindValue( m_playlist->guid() );
        cre.addBindValue( m_playlist->type() );
        cre.addBindValue( m_playlist->mode() );
    }
    cre.addBindValue( m_autoLoad );
    cre.exec();
}
void
DatabaseCommand_loadOps::exec( DatabaseImpl* dbi )
{
    QList< dbop_ptr > ops;

    if ( !m_since.isEmpty() )
    {
        TomahawkSqlQuery query = dbi->newquery();
        query.prepare( QString( "SELECT id FROM oplog WHERE guid = ?" ) );
        query.addBindValue( m_since );
        query.exec();

        if ( !query.next() )
        {
            tLog() << "Unknown oplog guid, requested, not replying:" << m_since;
            Q_ASSERT( false );
            emit done( m_since, m_since, ops );
            return;
        }
    }

    TomahawkSqlQuery query = dbi->newquery();
    query.prepare( QString(
                   "SELECT guid, command, json, compressed, singleton "
                   "FROM oplog "
                   "WHERE source %1 "
                   "AND id > coalesce((SELECT id FROM oplog WHERE guid = ?),0) "
                   "ORDER BY id ASC"
                   ).arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) )
                  );
    query.addBindValue( m_since );
    query.exec();

    QString lastguid = m_since;
    while( query.next() )
    {
        dbop_ptr op( new DBOp );
        op->guid = query.value( 0 ).toString();
        op->command = query.value( 1 ).toString();
        op->payload = query.value( 2 ).toByteArray();
        op->compressed = query.value( 3 ).toBool();
        op->singleton = query.value( 4 ).toBool();

        lastguid = op->guid;
        ops << op;
    }

//    qDebug() << "Loaded" << ops.length() << "ops from db";
    emit done( m_since, lastguid, ops );
}
void
DatabaseCommand_SocialAction::exec( DatabaseImpl* dbi )
{
    qDebug() << Q_FUNC_INFO;
    Q_ASSERT( !source().isNull() );

    TomahawkSqlQuery query = dbi->newquery();

    QVariant srcid = source()->isLocal() ? QVariant( QVariant::Int ) : source()->id();

    if ( m_artist.isNull() || m_track.isEmpty() )
        return;

    int artid = dbi->artistId( m_artist, true );
    if ( artid < 1 )
        return;
    int trkid = dbi->trackId( artid, m_track, true );
    if ( trkid < 1 )
        return;

    // update if it already exists
    TomahawkSqlQuery find = dbi->newquery();
    find.prepare( QString( "SELECT id, k, v FROM social_attributes WHERE social_attributes.id = ? AND social_attributes.source %1 AND social_attributes.k = ?" ).arg( source()->isLocal() ? "IS NULL" : QString( "=%1" ).arg( source()->id() ) ) );
    find.addBindValue( trkid );
    find.addBindValue( m_action );
    if ( find.exec() && find.next() )
    {
        // update
        query.prepare( QString( "UPDATE social_attributes SET v = '%1', timestamp = %2 WHERE social_attributes.id = %3 AND social_attributes.source %4 AND social_attributes.k = '%5'" )
                               .arg( m_comment )
                               .arg( m_timestamp )
                               .arg( trkid )
                               .arg( source()->isLocal() ? "IS NULL" : QString( "=%1" ).arg( source()->id() ) )
                               .arg( m_action ) );
    }
    else
    {
        query.prepare( "INSERT INTO social_attributes(id, source, k, v, timestamp) "
                       "VALUES (?, ?, ?, ?, ?)" );

        query.bindValue( 0, trkid );
        query.bindValue( 1, srcid );
        query.bindValue( 2, m_action );
        query.bindValue( 3, m_comment );
        query.bindValue( 4, m_timestamp );
    }

    query.exec();
}
void
DatabaseCommand_LoadDynamicPlaylistEntries::exec( DatabaseImpl* dbi )
{
//    qDebug() << "Loading dynamic playlist guid" << guid();
    // load the entries first
    generateEntries( dbi );

    // now load the controls etc

    TomahawkSqlQuery controlsQuery = dbi->newquery();
    controlsQuery.prepare("SELECT playlist_revision.playlist, controls, plmode, pltype "
                          "FROM dynamic_playlist_revision, playlist_revision "
                          "WHERE dynamic_playlist_revision.guid = ? AND playlist_revision.guid = dynamic_playlist_revision.guid");
    controlsQuery.addBindValue( revisionGuid() );
    controlsQuery.exec();

    QString type;
    GeneratorMode mode;

    QList< QVariantMap > controls;
    QString playlist_guid;
//    qDebug() << "Loading controls..." << revisionGuid();
//    qDebug() << "SELECT playlist_revision.playlist, controls, plmode, pltype "
//    "FROM dynamic_playlist_revision, playlist_revision "
//    "WHERE dynamic_playlist_revision.guid = "<< revisionGuid() << " AND playlist_revision.guid = dynamic_playlist_revision.guid";

    if( controlsQuery.first() )
    {
        playlist_guid = controlsQuery.value( 0 ).toString();
        QJson::Parser parser;
        bool ok;
        QVariant v = parser.parse( controlsQuery.value(1).toByteArray(), &ok );
        Q_ASSERT( ok && v.type() == QVariant::List ); //TODO


        type = controlsQuery.value( 3 ).toString();
        mode = static_cast<GeneratorMode>( controlsQuery.value( 2 ).toInt() );

        QStringList controlIds = v.toStringList();
//        qDebug() << "Got controls in dynamic playlist, loading:" << controlIds << controlsQuery.value(1);
        foreach( const QString& controlId, controlIds )
        {
            TomahawkSqlQuery controlQuery = dbi->newquery();
            controlQuery.prepare( "SELECT selectedType, match, input "
                                  "FROM dynamic_playlist_controls "
                                  "WHERE id = :id" );
            controlQuery.bindValue( ":id", controlId );
            controlQuery.exec();
            if( controlQuery.next() )
            {
                QVariantMap c;
                c[ "type" ] = type;
                c[ "id" ] = controlId;
                c[ "selectedType" ] = controlQuery.value( 0 ).toString();
                c[ "match" ] = controlQuery.value( 1 ).toString();
                c[ "input" ] = controlQuery.value( 2 ).toString();
                controls << c;
            }
        }
void
DatabaseCommand_SetPlaylistRevision::exec( DatabaseImpl* lib )
{
    QString currentRevision;
    // get the current revision for this playlist
    // this also serves to check the playlist exists.
    TomahawkSqlQuery chkq = lib->newquery();
    chkq.prepare( "SELECT currentrevision FROM playlist WHERE guid = ?" );
    chkq.addBindValue( m_playlistguid );
    if ( chkq.exec() && chkq.next() )
    {
        currentRevision = chkq.value( 0 ).toString();
        tDebug() << Q_FUNC_INFO << "pl guid" << m_playlistguid << "- curr rev" << currentRevision << source()->friendlyName() << source()->id();
    }
    else
    {
        tDebug() << "ERROR: No such playlist:" << m_playlistguid << currentRevision << source()->friendlyName() << source()->id();
//        Q_ASSERT_X( false, "DatabaseCommand_SetPlaylistRevision::exec", "No such playlist, WTF?" );
        m_failed = true;
        return;
    }

    QVariantList vlist = m_orderedguids;
    const QByteArray entries = TomahawkUtils::toJson( vlist );

    // add any new items:
    TomahawkSqlQuery adde = lib->newquery();
    if ( m_localOnly )
    {
        QString sql = "UPDATE playlist_item SET result_hint = ? WHERE guid = ?";
        adde.prepare( sql );

        foreach( const plentry_ptr& e, m_entries )
        {
            if ( !e->isValid() )
                continue;
            if ( !e->query()->numResults() )
                continue;

            adde.bindValue( 0, e->resultHint() );
            adde.bindValue( 1, e->guid() );
            adde.exec();
        }

        return;
    }
    else if ( m_metadataUpdate )
void
DatabaseCommand_SetPlaylistRevision::exec( DatabaseImpl* lib )
{
    using namespace Tomahawk;

    // get the current revision for this playlist
    // this also serves to check the playlist exists.
    TomahawkSqlQuery chkq = lib->newquery();
    chkq.prepare( "SELECT currentrevision FROM playlist WHERE guid = ?" );
    chkq.addBindValue( m_playlistguid );
    if( chkq.exec() && chkq.next() )
    {
        m_currentRevision = chkq.value( 0 ).toString();
        qDebug() << Q_FUNC_INFO << "pl guid" << m_playlistguid << " curr rev" << m_currentRevision;
    }
    else
    {
        throw "No such playlist, WTF?";
        return;
    }

    QVariantList vlist = m_orderedguids;
    QJson::Serializer ser;
    const QByteArray entries = ser.serialize( vlist );

    // add any new items:
    TomahawkSqlQuery adde = lib->newquery();
    if ( m_localOnly )
    {
        QString sql = "UPDATE playlist_item SET result_hint = ? WHERE guid = ?";
        adde.prepare( sql );

        foreach( const plentry_ptr& e, m_entries )
        {
            if ( e->query()->results().isEmpty() )
                continue;

            adde.bindValue( 0, e->query()->results().first()->url() );
            adde.bindValue( 1, e->guid() );
            adde.exec();
        }

        return;
    }
    else
    {
void DatabaseCommand_AddClientAuth::exec(DatabaseImpl* lib)
{
    TomahawkSqlQuery q = lib->newquery();
    q.prepare( "INSERT INTO http_client_auth (token, website, name, ua, mtime, permissions) VALUES (?, ?, ?, ?, ?, ?)" );
    q.addBindValue( m_clientToken );
    q.addBindValue( m_website );
    q.addBindValue( m_name );
    q.addBindValue( m_userAgent );
    q.addBindValue( 0 );
    q.addBindValue( "*" );
    
    if( !q.exec() ) {
        qWarning() << "Failed to insert http client into auth table!";
    }
}
void DatabaseCommand_ClientAuthValid::exec( DatabaseImpl* lib )
{
    TomahawkSqlQuery q = lib->newquery();
    q.prepare( "SELECT name FROM http_client_auth WHERE token = ?" );
    q.addBindValue( m_clientToken );

    if ( q.exec() )
    {
        if ( q.next() )
        {
            QString name = q.value( 0 ).toString();
            emit authValid( m_clientToken, name, true );
        }
        else
        {
            emit authValid( m_clientToken, QString(), false );
        }
    }
    else
    {
        qWarning() << "Failed to query http auth table for client:" << m_clientToken;
    }
}
Example #10
0
void
DatabaseWorker::doWork()
{
    /*
        Run the dbcmd. Only inside a transaction if the cmd does mutates.

        If the cmd is modifying local content (ie source->isLocal()) then
        log to the database oplog for replication to peers.

     */

#ifdef DEBUG_TIMING
    QTime timer;
    timer.start();
#endif

    QList< QSharedPointer<DatabaseCommand> > cmdGroup;
    QSharedPointer<DatabaseCommand> cmd;
    {
        QMutexLocker lock( &m_mut );
        cmd = m_commands.takeFirst();
    }

    if ( cmd->doesMutates() )
    {
        bool transok = m_dbimpl->database().transaction();
        Q_ASSERT( transok );
        Q_UNUSED( transok );
    }

    unsigned int completed = 0;
    try
    {
        bool finished = false;
        {
            while ( !finished )
            {
                completed++;
                cmd->_exec( m_dbimpl ); // runs actual SQL stuff

                if ( cmd->loggable() )
                {
                    // We only save our own ops to the oplog, since incoming ops from peers
                    // are applied immediately.
                    //
                    // Crazy idea: if peers had keypairs and could sign ops/msgs, in theory it
                    // would be safe to sync ops for friend A from friend B's cache, if he saved them,
                    // which would mean you could get updates even if a peer was offline.
                    if ( cmd->source()->isLocal() && !cmd->localOnly() )
                    {
                        // save to op-log
                        DatabaseCommandLoggable* command = (DatabaseCommandLoggable*)cmd.data();
                        logOp( command );
                    }
                    else
                    {
                        // Make a note of the last guid we applied for this source
                        // so we can always request just the newer ops in future.
                        //
                        if ( !cmd->singletonCmd() )
                        {
                            TomahawkSqlQuery query = m_dbimpl->newquery();
                            query.prepare( "UPDATE source SET lastop = ? WHERE id = ?" );
                            query.addBindValue( cmd->guid() );
                            query.addBindValue( cmd->source()->id() );

                            if ( !query.exec() )
                            {
                                throw "Failed to set lastop";
                            }
                        }
                    }
                }

                cmdGroup << cmd;
                if ( cmd->groupable() && !m_commands.isEmpty() )
                {
                    QMutexLocker lock( &m_mut );
                    if ( m_commands.first()->groupable() )
                    {
                        cmd = m_commands.takeFirst();
                    }
                    else
                    {
                        finished = true;
                    }
                }
                else
                    finished = true;
            }

            if ( cmd->doesMutates() )
            {
                qDebug() << "Committing" << cmd->commandname() << cmd->guid();
                if ( !m_dbimpl->database().commit() )
                {
                    tDebug() << "FAILED TO COMMIT TRANSACTION*";
                    throw "commit failed";
                }
            }

#ifdef DEBUG_TIMING
            uint duration = timer.elapsed();
            tDebug() << "DBCmd Duration:" << duration << "ms, now running postcommit for" << cmd->commandname();
#endif

            foreach ( QSharedPointer<DatabaseCommand> c, cmdGroup )
                c->postCommit();

#ifdef DEBUG_TIMING
            tDebug() << "Post commit finished in" << timer.elapsed() - duration << "ms for" << cmd->commandname();
#endif
        }
    }
    catch( const char * msg )
    {
        tLog() << endl
                 << "*ERROR* processing databasecommand:"
                 << cmd->commandname()
                 << msg
                 << m_dbimpl->database().lastError().databaseText()
                 << m_dbimpl->database().lastError().driverText()
                 << endl;

        if ( cmd->doesMutates() )
            m_dbimpl->database().rollback();

        Q_ASSERT( false );
    }
    catch(...)
    {
        qDebug() << "Uncaught exception processing dbcmd";
        if ( cmd->doesMutates() )
            m_dbimpl->database().rollback();

        Q_ASSERT( false );
        throw;
    }

    foreach ( QSharedPointer<DatabaseCommand> c, cmdGroup )
        c->emitFinished();

    QMutexLocker lock( &m_mut );
    m_outstanding -= completed;
    if ( m_outstanding > 0 )
        QTimer::singleShot( 0, this, SLOT( doWork() ) );
}