File indexing completed on 2025-03-09 04:24:20
0001 /* 0002 Copyright (C) 2009 Dan Meltzer <parallelgrapefruit@gmail.com> 0003 0004 This program is free software: you can redistribute it and/or modify 0005 it under the terms of the GNU General Public License as published by 0006 the Free Software Foundation, either version 2 of the License, or 0007 (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 GNU General Public License for more details. 0013 0014 You should have received a copy of the GNU General Public License 0015 along with this program. If not, see <http://www.gnu.org/licenses/>. 0016 0017 */ 0018 0019 #include "SqlWriteLabelCapability.h" 0020 0021 #include "core-impl/collections/support/CollectionManager.h" 0022 #include "core/support/Debug.h" 0023 #include <core/storage/SqlStorage.h> 0024 0025 namespace Capabilities 0026 { 0027 0028 SqlWriteLabelCapability::SqlWriteLabelCapability( Meta::SqlTrack* track, const QSharedPointer<SqlStorage> &storage ) 0029 : WriteLabelCapability() 0030 , m_track( track ) 0031 , m_storage( storage ) 0032 { 0033 } 0034 0035 void 0036 SqlWriteLabelCapability::setLabels( const QStringList &removedLabels, const QStringList &newlabels ) 0037 { 0038 0039 if( !m_storage ) 0040 { 0041 debug() << "Could not get SqlStorage, aborting" << Qt::endl; 0042 return; 0043 } 0044 0045 for ( int x = 0; x < newlabels.length(); x++) 0046 { 0047 //Check if all new labels are already in the Database 0048 const QString checkQuery = "SELECT label FROM labels WHERE label=\"%1\""; 0049 QStringList result = m_storage->query( checkQuery.arg( m_storage->escape( newlabels.at( x ) ) ) ); 0050 0051 if ( result.isEmpty() ) 0052 { 0053 const QString newQuery = "INSERT INTO labels (label) VALUE(\"%1\")"; 0054 m_storage->query( newQuery.arg( m_storage->escape( newlabels.at( x ) ) ) ); 0055 } 0056 0057 //Insert connection for every new label if not already there 0058 const QString checkNewQuery = "SELECT label from urls_labels WHERE label=(SELECT id FROM labels WHERE label=\"%1\") AND url=(SELECT id FROM urls WHERE uniqueid=\"%2\")"; 0059 result = m_storage->query( checkNewQuery.arg( m_storage->escape( newlabels.at( x ) ), m_storage->escape( m_track->uidUrl() ) ) ); 0060 0061 if ( result.isEmpty() ) 0062 { 0063 const QString insertQuery = "INSERT INTO urls_labels (label,url) VALUE((SELECT id FROM labels WHERE label=\"%1\"),(SELECT id FROM urls WHERE uniqueid=\"%2\"))"; 0064 m_storage->query( insertQuery.arg( m_storage->escape( newlabels.at( x ) ), m_storage->escape( m_track->uidUrl() ) ) ); 0065 } 0066 } 0067 0068 for ( int y = 0; y < removedLabels.length(); y++) 0069 { 0070 //Delete connections for every removed label 0071 const QString removeQuery = "DELETE FROM urls_labels WHERE url=(SELECT id FROM urls WHERE uniqueid=\"%1\") AND label=(SELECT id FROM labels WHERE label=\"%2\")"; 0072 m_storage->query( removeQuery.arg( m_storage->escape( m_track->uidUrl() ), m_storage->escape( removedLabels.at( y ) ) ) ); 0073 0074 //Check if label isn't used anymore 0075 const QString checkQuery = "SELECT label FROM urls_labels where label=(SELECT id FROM labels WHERE label=\"%1\")"; 0076 QStringList result = m_storage->query( checkQuery.arg( m_storage->escape( removedLabels.at( y ) ) ) ); 0077 0078 if ( result.isEmpty() ) 0079 { 0080 const QString labelRemoveQuery = "DELETE FROM labels WHERE label=\"%1\""; 0081 m_storage->query( labelRemoveQuery.arg( m_storage->escape( removedLabels.at( y ) ) ) ); 0082 } 0083 } 0084 } 0085 0086 }