/src/libtomahawk/database/DatabaseCommand_GenericSelect.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 151 lines · 113 code · 20 blank · 18 comment · 33 complexity · 829b8d3293d44fe0d5cf88c8fe9edf39 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_GenericSelect.h"
  19. #include "utils/Logger.h"
  20. #include "Album.h"
  21. #include "Artist.h"
  22. #include "DatabaseImpl.h"
  23. #include "Pipeline.h"
  24. #include "PlaylistEntry.h"
  25. #include "SourceList.h"
  26. using namespace Tomahawk;
  27. DatabaseCommand_GenericSelect::DatabaseCommand_GenericSelect( const QString& sqlSelect, QueryType type, int limit, QObject* parent )
  28. : DatabaseCommand( parent )
  29. , m_sqlSelect( sqlSelect )
  30. , m_queryType( type )
  31. , m_limit( limit )
  32. , m_raw( false )
  33. {
  34. }
  35. DatabaseCommand_GenericSelect::DatabaseCommand_GenericSelect( const QString& sqlSelect, QueryType type, bool rawData, QObject* parent )
  36. : DatabaseCommand( parent )
  37. , m_sqlSelect( sqlSelect )
  38. , m_queryType( type )
  39. , m_limit( -1 )
  40. , m_raw( rawData )
  41. {
  42. }
  43. void
  44. DatabaseCommand_GenericSelect::exec( DatabaseImpl* dbi )
  45. {
  46. TomahawkSqlQuery query = dbi->newquery();
  47. query.prepare( QString( "%1 %2;" ).arg( m_sqlSelect ).arg( m_limit > -1 ? QString( " LIMIT %1" ).arg( m_limit ) : QString() ) );
  48. query.exec();
  49. QList< query_ptr > queries;
  50. QList< artist_ptr > arts;
  51. QList< album_ptr > albs;
  52. if ( m_raw )
  53. {
  54. QList< QStringList > rawDataItems;
  55. while( query.next() )
  56. {
  57. QStringList rawRow;
  58. int count = 0;
  59. while ( query.value( count ).isValid() )
  60. {
  61. rawRow << query.value( count ).toString();
  62. ++count;
  63. }
  64. rawDataItems << rawRow;
  65. }
  66. emit rawData( rawDataItems );
  67. return;
  68. }
  69. // Expecting
  70. while ( query.next() )
  71. {
  72. query_ptr qry;
  73. artist_ptr artist;
  74. album_ptr album;
  75. if ( m_queryType == Track )
  76. {
  77. QString artist, track;
  78. track = query.value( 0 ).toString();
  79. artist = query.value( 1 ).toString();
  80. qry = Tomahawk::Query::get( artist, track, QString() );
  81. if ( qry.isNull() )
  82. continue;
  83. }
  84. else if ( m_queryType == Artist )
  85. {
  86. int artistId = query.value( 0 ).toInt();
  87. QString artistName = query.value( 1 ).toString();
  88. artist = Tomahawk::Artist::get( artistId, artistName );
  89. }
  90. else if ( m_queryType == Album )
  91. {
  92. int albumId = query.value( 0 ).toInt();
  93. QString albumName = query.value( 1 ).toString();
  94. int artistId = query.value( 2 ).toInt();
  95. QString artistName = query.value( 3 ).toString();
  96. artist = Tomahawk::Artist::get( artistId, artistName );
  97. album = Tomahawk::Album::get( albumId, albumName, artist );
  98. }
  99. QVariantList extraData;
  100. int count = 2;
  101. while ( query.value( count ).isValid() )
  102. {
  103. extraData << query.value( count );
  104. count++;
  105. }
  106. if ( m_queryType == Track )
  107. {
  108. if ( !extraData.isEmpty() )
  109. qry->setProperty( "data", extraData );
  110. queries << qry;
  111. }
  112. else if ( m_queryType == Artist )
  113. {
  114. if ( !extraData.isEmpty() )
  115. artist->setProperty( "data", extraData );
  116. arts << artist;
  117. }
  118. else if ( m_queryType == Album )
  119. {
  120. if ( !extraData.isEmpty() )
  121. album->setProperty( "data", extraData );
  122. albs << album;
  123. }
  124. }
  125. if ( m_queryType == Track )
  126. emit tracks( queries );
  127. else if ( m_queryType == Artist )
  128. emit artists( arts );
  129. else if ( m_queryType == Album )
  130. emit albums( albs );
  131. }