File indexing completed on 2024-05-19 04:49:26

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "TagStatisticsStore.h"
0018 
0019 #include <core/storage/SqlStorage.h>
0020 #include "core/meta/Meta.h"
0021 #include "core-impl/storage/StorageManager.h"
0022 
0023 TagStatisticsStore::TagStatisticsStore( Meta::Track *track )
0024     : PersistentStatisticsStore( track )
0025     , m_name( track->name() )
0026     , m_artist( track->artist() ? track->artist()->name() : QString() )
0027     , m_album( track->album() ? track->album()->name() : QString() )
0028 {
0029     auto sql = StorageManager::instance()->sqlStorage();
0030 
0031     const QString query = "SELECT firstPlayed, lastPlayed, score, rating, playcount FROM "
0032                           "statistics_tag WHERE name = '%1' AND artist = '%2' AND album = '%3'";
0033     QStringList result = sql->query( query.arg( sql->escape( m_name ),
0034                                                 sql->escape( m_artist ),
0035                                                 sql->escape( m_album ) ) );
0036     if( !result.isEmpty() )
0037     {
0038         m_firstPlayed = QDateTime::fromString( result.value( 0 ), s_sqlDateFormat );
0039         m_lastPlayed = QDateTime::fromString( result.value( 1 ), s_sqlDateFormat );
0040         m_score = result.value( 2 ).toDouble();
0041         m_rating = result.value( 3 ).toInt();
0042         m_playCount = result.value( 4 ).toInt();
0043     }
0044 }
0045 
0046 void
0047 TagStatisticsStore::save()
0048 {
0049     auto sql = StorageManager::instance()->sqlStorage();
0050 
0051     const QString check = "SELECT COUNT(*) FROM statistics_tag WHERE name = '%1' "
0052                           "AND artist = '%2' AND album = '%3'";
0053     QStringList rsCheck = sql->query( check.arg( sql->escape( m_name ),
0054                                                  sql->escape( m_artist ),
0055                                                  sql->escape( m_album ) ) );
0056     if( !rsCheck.isEmpty() )
0057     {
0058         QString sqlString;
0059         if( rsCheck.first().toInt() )
0060         {
0061             sqlString = "UPDATE statistics_tag SET firstPlayed = '%1',lastPlayed = '%2',"
0062                         "score = %3,rating = %4,playcount=%5 WHERE name = '%6' "
0063                         "AND artist = '%7' AND album = '%8'";
0064         }
0065         else
0066         {
0067             sqlString = "INSERT INTO statistics_tag(firstPlayed,lastPlayed,score,"
0068                         "rating,playcount,name,artist,album) "
0069                         "VALUE ('%1','%2',%3,%4,%5,'%6','%7','%8')";
0070         }
0071         sqlString = sqlString.arg( m_firstPlayed.toString( s_sqlDateFormat ),
0072                                    m_lastPlayed.toString( s_sqlDateFormat ),
0073                                    QString::number( m_score ),
0074                                    QString::number( m_rating ),
0075                                    QString::number( m_playCount ),
0076                                    sql->escape( m_name ),
0077                                    sql->escape( m_artist ),
0078                                    sql->escape( m_album ) );
0079         sql->query( sqlString );
0080     }
0081 }