Mercurial > dedupe
comparison SqliteDBLink.cpp @ 16:06166d6c083b
Add configuration processing.
Cache DB values
Add a custom RBTree to save space.
Track multiple DB connections properly.
More testing.
Add ValueExistsException.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Tue, 28 Aug 2012 18:58:02 +0200 |
| parents | b5943e4bf676 |
| children | bf3dce7fedcb |
comparison
equal
deleted
inserted
replaced
| 15:199fc63c60c1 | 16:06166d6c083b |
|---|---|
| 9 | 9 |
| 10 #include <cassert> | 10 #include <cassert> |
| 11 | 11 |
| 12 SqliteDBLink::SqliteDBLink(const QString& dbPath) | 12 SqliteDBLink::SqliteDBLink(const QString& dbPath) |
| 13 { | 13 { |
| 14 db = QSqlDatabase::addDatabase("QSQLITE"); | 14 db = QSqlDatabase::addDatabase("QSQLITE", "SqliteDBLink"); |
| 15 db.setDatabaseName(dbPath); | 15 db.setDatabaseName(dbPath); |
| 16 bool ok = db.open(); | 16 bool ok = db.open(); |
| 17 assert(ok); | 17 assert(ok); |
| 18 QSqlQuery query; | 18 QSqlQuery query(db); |
| 19 if (!query.exec(QString("SELECT * FROM files;"))) { | 19 if (!query.exec(QString("SELECT * FROM files;"))) { |
| 20 query.exec("CREATE TABLE files(path VARCHAR PRIMARY KEY ASC, size INTEGER, mtime TEXT, checksum TEXT);"); | 20 query.exec("CREATE TABLE files(path VARCHAR PRIMARY KEY ASC, size INTEGER, mtime TEXT, checksum TEXT);"); |
| 21 } | 21 } |
| 22 if (!query.exec(QString("SELECT * FROM files;"))) { | 22 if (!query.exec(QString("SELECT * FROM files;"))) { |
| 23 qDebug()<<"No database"; | 23 qDebug()<<"No database"; |
| 31 db.close(); | 31 db.close(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 bool SqliteDBLink::exists(const QString& path) | 34 bool SqliteDBLink::exists(const QString& path) |
| 35 { | 35 { |
| 36 QSqlQuery query; | 36 QSqlQuery query(db); |
| 37 query.prepare("SELECT path FROM files WHERE path = :path;"); | 37 query.prepare("SELECT path FROM files WHERE path = :path;"); |
| 38 query.bindValue(":path", path); | 38 query.bindValue(":path", path); |
| 39 if (!query.exec()) { | 39 if (!query.exec()) { |
| 40 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); | 40 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); |
| 41 } | 41 } |
| 42 return query.last(); | 42 return query.last(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 FileDBLink::DBStatus SqliteDBLink::existsWithMtime(const QString& path, const QDateTime& mtime) | 45 FileDBLink::DBStatus SqliteDBLink::existsWithMtime(const QString& path, const QDateTime& mtime) |
| 46 { | 46 { |
| 47 QSqlQuery query; | 47 QSqlQuery query(db); |
| 48 query.prepare("SELECT mtime FROM files WHERE path = :path;"); | 48 query.prepare("SELECT mtime FROM files WHERE path = :path;"); |
| 49 query.bindValue(":path", path); | 49 query.bindValue(":path", path); |
| 50 if (!query.exec()) { | 50 if (!query.exec()) { |
| 51 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); | 51 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); |
| 52 } | 52 } |
| 67 | 67 |
| 68 bool SqliteDBLink::tryAddFile(const DBInfo& dbinfo) | 68 bool SqliteDBLink::tryAddFile(const DBInfo& dbinfo) |
| 69 { | 69 { |
| 70 if (exists(dbinfo.path())) | 70 if (exists(dbinfo.path())) |
| 71 return false; | 71 return false; |
| 72 QSqlQuery query; | 72 QSqlQuery query(db); |
| 73 query.prepare("INSERT INTO files (path, size, mtime, checksum) " | 73 query.prepare("INSERT INTO files (path, size, mtime, checksum) " |
| 74 "VALUES (:path, :size, :mtime, :checksum)"); | 74 "VALUES (:path, :size, :mtime, :checksum)"); |
| 75 query.bindValue(":path", dbinfo.path()); | 75 query.bindValue(":path", dbinfo.path()); |
| 76 query.bindValue(":size", dbinfo.size()); | 76 query.bindValue(":size", dbinfo.size()); |
| 77 query.bindValue(":mtime", dbinfo.mtime()); | 77 query.bindValue(":mtime", dbinfo.mtime()); |
| 87 updateFile(DBInfo(path, size, dtime, hash)); | 87 updateFile(DBInfo(path, size, dtime, hash)); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void SqliteDBLink::updateFile(const DBInfo& dbinfo) | 90 void SqliteDBLink::updateFile(const DBInfo& dbinfo) |
| 91 { | 91 { |
| 92 QSqlQuery query; | 92 QSqlQuery query(db); |
| 93 query.prepare("UPDATE files SET size=:size, mtime=:mtime, checksum=:checksum WHERE path=:path"); | 93 query.prepare("UPDATE files SET size=:size, mtime=:mtime, checksum=:checksum WHERE path=:path"); |
| 94 query.bindValue(":path", dbinfo.path()); | 94 query.bindValue(":path", dbinfo.path()); |
| 95 query.bindValue(":size", dbinfo.size()); | 95 query.bindValue(":size", dbinfo.size()); |
| 96 query.bindValue(":mtime", dbinfo.mtime()); | 96 query.bindValue(":mtime", dbinfo.mtime()); |
| 97 query.bindValue(":checksum", dbinfo.checksum()); | 97 query.bindValue(":checksum", dbinfo.checksum()); |
| 122 | 122 |
| 123 const QList<QSharedPointer<FileDBLink::DBInfo> > SqliteDBLink::values(const QString& prefix) const | 123 const QList<QSharedPointer<FileDBLink::DBInfo> > SqliteDBLink::values(const QString& prefix) const |
| 124 { | 124 { |
| 125 QList<QSharedPointer<FileDBLink::DBInfo> > values; | 125 QList<QSharedPointer<FileDBLink::DBInfo> > values; |
| 126 | 126 |
| 127 QSqlQuery query; | 127 QSqlQuery query(db); |
| 128 | 128 |
| 129 if (prefix.size() > 0) { | 129 if (prefix.size() > 0) { |
| 130 query.prepare("SELECT * FROM files WHERE path LIKE :prefix"); | 130 query.prepare("SELECT * FROM files WHERE path LIKE :prefix"); |
| 131 query.bindValue(":prefix", QString("%1%").arg(prefix)); | 131 query.bindValue(":prefix", QString("%1%").arg(prefix)); |
| 132 } | 132 } |
| 156 return values; | 156 return values; |
| 157 } | 157 } |
| 158 | 158 |
| 159 void SqliteDBLink::deleteFileFromDB(const QString& path) | 159 void SqliteDBLink::deleteFileFromDB(const QString& path) |
| 160 { | 160 { |
| 161 QSqlQuery query; | 161 QSqlQuery query(db); |
| 162 query.prepare("DELETE FROM files WHERE path = :path"); | 162 query.prepare("DELETE FROM files WHERE path = :path"); |
| 163 query.bindValue(":path", path); | 163 query.bindValue(":path", path); |
| 164 if (!query.exec()) { | 164 if (!query.exec()) { |
| 165 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); | 165 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); |
| 166 } | 166 } |
| 167 } | 167 } |
| 168 | 168 |
| 169 | 169 |
| 170 void SqliteDBLink::keepOnlyFromPrefix(const QString& prefix, const QStringList& files) | 170 void SqliteDBLink::keepOnlyFromPrefix(const QString& prefix, const QStringList& files) |
| 171 { | 171 { |
