![]() |
Mixxx
|
00001 00002 #include <QtSql> 00003 #include <QString> 00004 #include <QtDebug> 00005 #include <QVariant> 00006 #include <QThread> 00007 00008 #include "libraryhashdao.h" 00009 00010 LibraryHashDAO::LibraryHashDAO(QSqlDatabase& database) 00011 : m_database(database) { 00012 00013 } 00014 00015 LibraryHashDAO::~LibraryHashDAO() 00016 { 00017 } 00018 00019 void LibraryHashDAO::initialize() 00020 { 00021 qDebug() << "LibraryHashDAO::initialize" << QThread::currentThread() << m_database.connectionName(); 00022 } 00023 00024 int LibraryHashDAO::getDirectoryHash(QString dirPath) 00025 { 00026 //qDebug() << "LibraryHashDAO::getDirectoryHash" << QThread::currentThread() << m_database.connectionName(); 00027 int hash = -1; 00028 00029 QSqlQuery query(m_database); 00030 query.prepare("SELECT hash FROM LibraryHashes " 00031 "WHERE directory_path=:directory_path"); 00032 query.bindValue(":directory_path", dirPath); 00033 00034 if (!query.exec()) { 00035 qDebug() << "SELECT hash failed:" << query.lastError(); 00036 } 00037 //Grab a hash for this directory from the database, from the last time it was scanned. 00038 if (query.next()) { 00039 hash = query.value(query.record().indexOf("hash")).toInt(); 00040 //qDebug() << "prev hash exists" << hash << dirPath; 00041 } 00042 else { 00043 //qDebug() << "prev hash does not exist" << dirPath; 00044 } 00045 00046 return hash; 00047 } 00048 00049 void LibraryHashDAO::saveDirectoryHash(QString dirPath, int hash) 00050 { 00051 //qDebug() << "LibraryHashDAO::saveDirectoryHash" << QThread::currentThread() << m_database.connectionName(); 00052 QSqlQuery query(m_database); 00053 query.prepare("INSERT INTO LibraryHashes (directory_path, hash, directory_deleted) " 00054 "VALUES (:directory_path, :hash, :directory_deleted)"); 00055 query.bindValue(":directory_path", dirPath); 00056 query.bindValue(":hash", hash); 00057 query.bindValue(":directory_deleted", 0); 00058 00059 00060 if (!query.exec()) { 00061 qDebug() << "Creating new dirhash failed:" << query.lastError(); 00062 } 00063 //qDebug() << "created new hash" << hash; 00064 } 00065 00066 void LibraryHashDAO::updateDirectoryHash(QString dirPath, int newHash, int dir_deleted) 00067 { 00068 //qDebug() << "LibraryHashDAO::updateDirectoryHash" << QThread::currentThread() << m_database.connectionName(); 00069 QSqlQuery query(m_database); 00070 query.prepare("UPDATE LibraryHashes " 00071 "SET hash=:hash, directory_deleted=:directory_deleted " 00072 "WHERE directory_path=:directory_path"); 00073 query.bindValue(":hash", newHash); 00074 query.bindValue(":directory_deleted", dir_deleted); 00075 query.bindValue(":directory_path", dirPath); 00076 00077 if (!query.exec()) { 00078 qDebug() << "Updating existing dirhash failed:" << query.lastError(); 00079 } 00080 //qDebug() << "updated old existing hash" << newHash << dirPath << dir_deleted; 00081 00082 //DEBUG: Print out the directory hash we just saved to verify... 00083 //qDebug() << getDirectoryHash(dirPath); 00084 } 00085 00086 void LibraryHashDAO::updateDirectoryStatus(QString dirPath, bool deleted, bool verified) { 00087 //qDebug() << "LibraryHashDAO::updateDirectoryStatus" << QThread::currentThread() << m_database.connectionName(); 00088 QSqlQuery query(m_database); 00089 query.prepare("UPDATE LibraryHashes " 00090 "SET directory_deleted=:directory_deleted, " 00091 "needs_verification=:needs_verification " 00092 "WHERE directory_path=:directory_path"); 00093 query.bindValue(":directory_deleted", deleted); 00094 query.bindValue(":needs_verification", !verified); 00095 query.bindValue(":directory_path", dirPath); 00096 if (!query.exec()) { 00097 qDebug() << "Updating directory status failed: " << query.lastError(); 00098 } 00099 } 00100 00101 void LibraryHashDAO::markAsExisting(QString dirPath) 00102 { 00103 //qDebug() << "LibraryHashDAO::markExisting" << QThread::currentThread() << m_database.connectionName(); 00104 QSqlQuery query(m_database); 00105 query.prepare("UPDATE LibraryHashes " 00106 "SET directory_deleted=:directory_deleted " 00107 "WHERE directory_path=:directory_path"); 00108 query.bindValue(":directory_deleted", 0); 00109 query.bindValue(":directory_path", dirPath); 00110 if (!query.exec()) { 00111 qDebug() << "Updating dirhash to mark as existing failed:" << query.lastError(); 00112 } 00113 } 00114 00115 void LibraryHashDAO::markAsVerified(QString dirPath) 00116 { 00117 //qDebug() << "LibraryHashDAO::markExisting" << QThread::currentThread() << m_database.connectionName(); 00118 QSqlQuery query(m_database); 00119 query.prepare("UPDATE LibraryHashes " 00120 "SET needs_verification=0 " 00121 "WHERE directory_path=:directory_path"); 00122 // query.bindValue(":directory_deleted", 0); 00123 query.bindValue(":directory_path", dirPath); 00124 if (!query.exec()) { 00125 qDebug() << "Updating dirhash to mark as verified failed:" << query.lastError(); 00126 } 00127 } 00128 00129 void LibraryHashDAO::invalidateAllDirectories() 00130 { 00131 //qDebug() << "LibraryHashDAO::invalidateAllDirectories" 00132 //<< QThread::currentThread() << m_database.connectionName(); 00133 QSqlQuery query(m_database); 00134 query.prepare("UPDATE LibraryHashes " 00135 "SET needs_verification=:needs_verification"); 00136 query.bindValue(":needs_verification", 1); 00137 if (!query.exec()) { 00138 qDebug() << query.lastError(); 00139 } 00140 } 00141 00142 void LibraryHashDAO::markUnverifiedDirectoriesAsDeleted() 00143 { 00144 //qDebug() << "LibraryHashDAO::markUnverifiedDirectoriesAsDeleted" 00145 //<< QThread::currentThread() << m_database.connectionName(); 00146 QSqlQuery query(m_database); 00147 query.prepare("UPDATE LibraryHashes " 00148 "SET directory_deleted=:directory_deleted " 00149 "WHERE needs_verification=1"); 00150 query.bindValue(":directory_deleted", 1); 00151 if (!query.exec()) { 00152 qDebug() << query.lastError(); 00153 } 00154 } 00155 00156 void LibraryHashDAO::removeDeletedDirectoryHashes() 00157 { 00158 QSqlQuery query(m_database); 00159 query.prepare("DELETE FROM LibraryHashes WHERE " 00160 "directory_deleted=:directory_deleted"); 00161 query.bindValue(":directory_deleted", 1); 00162 Q_ASSERT(query.exec()); 00163 }