File indexing completed on 2024-05-19 04:49:44
0001 /**************************************************************************************** 0002 * Copyright (c) 2013 Konrad Zemek <konrad.zemek@gmail.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 "AmarokTrack.h" 0018 0019 #include "importers/ImporterSqlConnection.h" 0020 0021 #include <QStringList> 0022 0023 using namespace StatSyncing; 0024 0025 AmarokTrack::AmarokTrack( const qint64 urlId, const ImporterSqlConnectionPtr &connection, 0026 const Meta::FieldHash &metadata, const QSet<QString> &labels ) 0027 : SimpleWritableTrack( metadata, labels ) 0028 , m_connection( connection ) 0029 , m_urlId( urlId ) 0030 { 0031 } 0032 0033 AmarokTrack::~AmarokTrack() 0034 { 0035 } 0036 0037 void 0038 AmarokTrack::doCommit( const qint64 fields ) 0039 { 0040 bool ok = true; 0041 m_connection->transaction(); 0042 0043 QStringList updates; 0044 QVariantMap bindValues; 0045 if( fields & Meta::valFirstPlayed ) 0046 { 0047 updates << "createdate = :createdate"; 0048 bindValues.insert( ":createdate", m_statistics.value( Meta::valFirstPlayed ) ); 0049 } 0050 if( fields & Meta::valLastPlayed ) 0051 { 0052 updates << "accessdate = :accessdate"; 0053 bindValues.insert( ":accessdate", m_statistics.value( Meta::valLastPlayed ) ); 0054 } 0055 if( fields & Meta::valRating ) 0056 { 0057 updates << "rating = :rating"; 0058 bindValues.insert( ":rating", m_statistics.value( Meta::valRating ) ); 0059 } 0060 if( fields & Meta::valPlaycount ) 0061 { 0062 updates << "playcount = :playcount"; 0063 bindValues.insert( ":playcount", m_statistics.value( Meta::valPlaycount ) ); 0064 } 0065 0066 if( !updates.isEmpty() ) 0067 { 0068 const QString query = "UPDATE statistics SET " + updates.join(", ") + 0069 " WHERE url = :url"; 0070 0071 bindValues.insert( ":url", m_urlId ); 0072 m_connection->query( query, bindValues, &ok ); 0073 if( !ok ) 0074 { 0075 m_connection->rollback(); 0076 return; 0077 } 0078 } 0079 0080 if( fields & Meta::valLabel ) 0081 { 0082 // Try to insert all labels. Since the 'label' field's unique, nothing will happen 0083 // if the label already exists 0084 foreach( const QString &label, m_labels ) 0085 { 0086 QVariantMap bindValues; 0087 bindValues.insert( ":label", label ); 0088 m_connection->query( "INSERT IGNORE INTO labels (label) VALUES ( :label )", 0089 bindValues, &ok ); 0090 if( !ok ) 0091 { 0092 m_connection->rollback(); 0093 return; 0094 } 0095 } 0096 0097 // Drop all labels for the track 0098 { 0099 QVariantMap bindValues; 0100 bindValues.insert( ":url", m_urlId ); 0101 m_connection->query( "DELETE QUICK FROM urls_labels WHERE url = :url", bindValues, 0102 &ok ); 0103 if( !ok ) 0104 { 0105 m_connection->rollback(); 0106 return; 0107 } 0108 } 0109 0110 // Add labels 0111 foreach( const QString &label, m_labels ) 0112 { 0113 const QString query = "INSERT INTO urls_labels (url, label) VALUES ( :url, " 0114 "(SELECT id FROM labels WHERE label = :label ))"; 0115 0116 QVariantMap bindValues; 0117 bindValues.insert( ":url", m_urlId ); 0118 bindValues.insert( ":label", label ); 0119 0120 m_connection->query( query, bindValues, &ok ); 0121 if( !ok ) 0122 { 0123 m_connection->rollback(); 0124 return; 0125 } 0126 } 0127 } 0128 0129 m_connection->commit(); 0130 }