bool SyncJournalDb::postSyncCleanup(const QHash<QString, QString> &items ) { QMutexLocker locker(&_mutex); if( !checkConnect() ) { return false; } QSqlQuery query(_db); query.prepare("SELECT phash, path FROM metadata order by path"); if (!query.exec()) { QString err = query.lastError().text(); qDebug() << "Error creating prepared statement: " << query.lastQuery() << ", Error:" << err;; return false; } QStringList superfluousItems; while(query.next()) { const QString file = query.value(1).toString(); bool contained = items.contains(file); if( !contained ) { superfluousItems.append(query.value(0).toString()); } } if( superfluousItems.count() ) { QString sql = "DELETE FROM metadata WHERE phash in ("+ superfluousItems.join(",")+")"; qDebug() << "Sync Journal cleanup: " << sql; QSqlQuery delQuery(_db); delQuery.prepare(sql); if( !delQuery.exec() ) { QString err = delQuery.lastError().text(); qDebug() << "Error removing superfluous journal entries: " << delQuery.lastQuery() << ", Error:" << err;; return false; } } return true; }
/** * Removes all file references from the db * * @param filterId is the filter id */ void ServerDatabase::removeFiles(QString filterId) { m_dbSem.acquire(); QSqlQuery query(m_db); QSqlQuery delQuery(m_db); #ifdef _VERBOSE_DATABASE qDebug() << "removing files for " << filterId; #endif // retrieve file_ids for the given filterId if (!query.exec("SELECT file_id FROM files WHERE filter_id=" + filterId)) { qDebug() << QObject::tr("Failed to delete from files and attributes table in DB ") + DB_NAME + QObject::tr(" on host ") + DB_HOST + QObject::tr(" with usr/pwd ") + DB_USR + "/" + DB_PWD; qDebug() << QObject::tr("ERROR: ") + query.lastError().text(); } else { while (query.next()) { QString fileId = query.value(0).toString(); #ifdef _VERBOSE_DATABASE qDebug() << "removing file " << filterId << "/" << fileId; #endif if (!delQuery.exec("DELETE FROM files WHERE file_id=" + fileId)) { qDebug() << QObject::tr("Failed to delete from files and attributes table in DB ") + DB_NAME + QObject::tr(" on host ") + DB_HOST + QObject::tr(" with usr/pwd ") + DB_USR + "/" + DB_PWD; qDebug() << QObject::tr("ERROR: ") + delQuery.lastError().text(); } if (!delQuery.exec("DELETE FROM attributes WHERE file_id=" + fileId)) { qDebug() << QObject::tr("Failed to delete from files and attributes table in DB ") + DB_NAME + QObject::tr(" on host ") + DB_HOST + QObject::tr(" with usr/pwd ") + DB_USR + "/" + DB_PWD; qDebug() << QObject::tr("ERROR: ") + delQuery.lastError().text(); } } } m_dbSem.release(); }