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 "UrlStatisticsStore.h"
0018 
0019 #include <core/storage/SqlStorage.h>
0020 #include "core/meta/Meta.h"
0021 #include "core/support/Debug.h"
0022 #include "core-impl/storage/StorageManager.h"
0023 
0024 UrlStatisticsStore::UrlStatisticsStore( Meta::Track *track, const QString &permanentUrl )
0025     : PersistentStatisticsStore( track )
0026     , m_permanentUrl( permanentUrl )
0027 {
0028     if( m_permanentUrl.isEmpty() )
0029         m_permanentUrl = track->uidUrl();
0030     auto sql = StorageManager::instance()->sqlStorage();
0031     if( !sql )
0032     {
0033         warning() << __PRETTY_FUNCTION__ << "could not get SqlStorage, aborting";
0034         return;
0035     }
0036 
0037 
0038     const QString query = "SELECT firstplayed, lastplayed, score, rating, playcount FROM "
0039                           "statistics_permanent WHERE url = '%1'";
0040     QStringList result = sql->query( query.arg( sql->escape( m_permanentUrl ) ) );
0041     if( !result.isEmpty() )
0042     {
0043         m_firstPlayed = QDateTime::fromString( result.value( 0 ), s_sqlDateFormat );
0044         m_lastPlayed = QDateTime::fromString( result.value( 1 ), s_sqlDateFormat );
0045         m_score = result.value( 2 ).toDouble();
0046         m_rating = result.value( 3 ).toInt();
0047         m_playCount = result.value( 4 ).toInt();
0048     }
0049 }
0050 
0051 void
0052 UrlStatisticsStore::save()
0053 {
0054     auto sql = StorageManager::instance()->sqlStorage();
0055     if( !sql )
0056     {
0057         warning() << __PRETTY_FUNCTION__ << "could not get SqlStorage, aborting";
0058         return;
0059     }
0060 
0061     const QString check = QStringLiteral("SELECT COUNT(*) FROM statistics_permanent WHERE url = '%1'");
0062     QStringList rsCheck = sql->query( check.arg( sql->escape( m_permanentUrl ) ) );
0063     if( !rsCheck.isEmpty() )
0064     {
0065         QString sqlString;
0066         if( rsCheck.first().toInt() )
0067         {
0068             sqlString = "UPDATE statistics_permanent SET firstplayed = '%1',lastplayed = '%2',"
0069                         "score = %3,rating = %4,playcount=%5 WHERE url = '%6'";
0070         }
0071         else
0072         {
0073             sqlString = "INSERT INTO statistics_permanent(firstplayed,lastplayed,score,"
0074                         "rating,playcount,url) VALUE ('%1','%2',%3,%4,%5,'%6')";
0075         }
0076         sqlString = sqlString.arg( m_firstPlayed.toString( s_sqlDateFormat ),
0077                                    m_lastPlayed.toString( s_sqlDateFormat ),
0078                                    QString::number( m_score ),
0079                                    QString::number( m_rating ),
0080                                    QString::number( m_playCount ),
0081                                    sql->escape( m_permanentUrl ) );
0082         sql->query( sqlString );
0083     }
0084 }