/src/libtomahawk/database/DatabaseCommand_LoadOps.cpp

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