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