/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. #include "DatabaseCommand_AddSource.h"
  19. #include <QSqlQuery>
  20. #include "DatabaseImpl.h"
  21. #include "utils/Logger.h"
  22. #include "Source.h"
  23. namespace Tomahawk
  24. {
  25. DatabaseCommand_addSource::DatabaseCommand_addSource( const QString& username, const QString& fname, QObject* parent )
  26. : DatabaseCommand( parent )
  27. , m_username( username )
  28. , m_fname( fname )
  29. {
  30. }
  31. void
  32. DatabaseCommand_addSource::exec( DatabaseImpl* dbi )
  33. {
  34. Q_ASSERT( !m_fname.isEmpty() );
  35. TomahawkSqlQuery query = dbi->newquery();
  36. query.prepare( "SELECT id FROM source WHERE name = ?" );
  37. query.addBindValue( m_username );
  38. query.exec();
  39. if ( query.next() )
  40. {
  41. unsigned int id = query.value( 0 ).toInt();
  42. query.prepare( "UPDATE source SET isonline = 'true', friendlyname = ? WHERE id = ?" );
  43. query.addBindValue( m_fname );
  44. query.addBindValue( id );
  45. query.exec();
  46. emit done( id, m_fname );
  47. return;
  48. }
  49. query.prepare( "INSERT INTO source(name, friendlyname, isonline) VALUES(?,?,?)" );
  50. query.addBindValue( m_username );
  51. query.addBindValue( m_fname );
  52. query.addBindValue( "true" );
  53. query.exec();
  54. unsigned int id = query.lastInsertId().toUInt();
  55. tDebug() << "Inserted new source to DB, id:" << id << "friendlyname" << m_username;
  56. emit done( id, m_fname );
  57. }
  58. }