Mercurial > dedupe
comparison SqliteDBLink.cpp @ 2:2833b7f8884a
Sql backend is working.
Need to get more speed on comparisson.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Tue, 21 Aug 2012 14:25:33 +0200 |
| parents | |
| children | 5e4985407feb |
comparison
equal
deleted
inserted
replaced
| 1:aae83c0a771d | 2:2833b7f8884a |
|---|---|
| 1 #include "SqliteDBLink.hpp" | |
| 2 | |
| 3 | |
| 4 #include <sqlite3.h> | |
| 5 | |
| 6 #include <cassert> | |
| 7 | |
| 8 #include <QtCore/QStringList> | |
| 9 #include <QtCore/QDebug> | |
| 10 | |
| 11 #include <QtSql/QSqlQuery> | |
| 12 #include <QtSql/QSqlError> | |
| 13 #include <QtSql/QSqlRecord> | |
| 14 | |
| 15 SqliteDBLink::SqliteDBLink(const QString& dbPath) | |
| 16 { | |
| 17 db = QSqlDatabase::addDatabase("QSQLITE"); | |
| 18 db.setDatabaseName(dbPath); | |
| 19 bool ok = db.open(); | |
| 20 assert(ok); | |
| 21 QSqlQuery query; | |
| 22 if (!query.exec(QString("SELECT * FROM files;"))) { | |
| 23 query.exec("CREATE TABLE files(path VARCHAR PRIMARY KEY ASC, size INTEGER, mtime TEXT, checksum TEXT);"); | |
| 24 } | |
| 25 if (!query.exec(QString("SELECT * FROM files;"))) { | |
| 26 qDebug()<<"No database"; | |
| 27 exit(1); | |
| 28 } | |
| 29 | |
| 30 } | |
| 31 | |
| 32 SqliteDBLink::~SqliteDBLink() | |
| 33 { | |
| 34 db.close(); | |
| 35 } | |
| 36 | |
| 37 bool SqliteDBLink::exists(const QString& path) | |
| 38 { | |
| 39 QSqlQuery query; | |
| 40 query.prepare("SELECT path FROM files WHERE path = :path;"); | |
| 41 query.bindValue(":path", path); | |
| 42 if (!query.exec()) { | |
| 43 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); | |
| 44 } | |
| 45 return query.last(); | |
| 46 } | |
| 47 | |
| 48 FileDBLink::DBStatus SqliteDBLink::existsWithMtime(const QString& path, const QDateTime& mtime) | |
| 49 { | |
| 50 QSqlQuery query; | |
| 51 query.prepare("SELECT mtime FROM files WHERE path = :path;"); | |
| 52 query.bindValue(":path", path); | |
| 53 if (!query.exec()) { | |
| 54 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); | |
| 55 } | |
| 56 if (query.next()) { | |
| 57 int dateIndex = query.record().indexOf("mtime"); | |
| 58 QDateTime mtimeEntry = query.value(dateIndex).toDateTime(); | |
| 59 if (mtimeEntry == mtime) | |
| 60 return SAME; | |
| 61 return MTIME_DIFFERENT; | |
| 62 } | |
| 63 return NONE; | |
| 64 } | |
| 65 | |
| 66 void SqliteDBLink::addFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash) | |
| 67 { | |
| 68 addFile(DBInfo(path, size, dtime, hash)); | |
| 69 } | |
| 70 | |
| 71 void SqliteDBLink::updateFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash) | |
| 72 { | |
| 73 updateFile(DBInfo(path, size, dtime, hash)); | |
| 74 } | |
| 75 | |
| 76 bool SqliteDBLink::tryAddFile(const DBInfo& dbinfo) | |
| 77 { | |
| 78 if (exists(dbinfo.path())) | |
| 79 return false; | |
| 80 QSqlQuery query; | |
| 81 query.prepare("INSERT INTO files (path, size, mtime, checksum) " | |
| 82 "VALUES (:path, :size, :mtime, :checksum)"); | |
| 83 query.bindValue(":path", dbinfo.path()); | |
| 84 query.bindValue(":size", dbinfo.size()); | |
| 85 query.bindValue(":mtime", dbinfo.mtime()); | |
| 86 query.bindValue(":checksum", dbinfo.checksum()); | |
| 87 if (!query.exec()) { | |
| 88 qDebug() << dbinfo.path() << "::" << query.lastQuery() << "::" << query.lastError().text(); | |
| 89 } | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 void SqliteDBLink::updateFile(const DBInfo& dbinfo) | |
| 94 { | |
| 95 QSqlQuery query; | |
| 96 query.prepare("UPDATE files SET size=:size, mtime=:mtime, checksum=:checksum WHERE path=:path"); | |
| 97 query.bindValue(":path", dbinfo.path()); | |
| 98 query.bindValue(":size", dbinfo.size()); | |
| 99 query.bindValue(":mtime", dbinfo.mtime()); | |
| 100 query.bindValue(":checksum", dbinfo.checksum()); | |
| 101 if (!query.exec()) { | |
| 102 qDebug() << query.lastError().text(); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void SqliteDBLink::addFile(const DBInfo& dbinfo) | |
| 107 { | |
| 108 if (!tryAddFile(dbinfo)) { | |
| 109 abort(); //Should throw exception | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 | |
| 114 QStringList SqliteDBLink::toStringList() | |
| 115 { | |
| 116 abort(); | |
| 117 QStringList list; | |
| 118 /* | |
| 119 foreach(QSharedPointer<DBInfo> info, entries) { | |
| 120 list << info->serialize(); | |
| 121 } | |
| 122 */ | |
| 123 return list; | |
| 124 } | |
| 125 | |
| 126 const QList<QSharedPointer<FileDBLink::DBInfo> > SqliteDBLink::values(const QString& prefix) const | |
| 127 { | |
| 128 QList<QSharedPointer<FileDBLink::DBInfo> > values; | |
| 129 | |
| 130 QSqlQuery query; | |
| 131 | |
| 132 if (prefix.size() > 0) { | |
| 133 query.prepare("SELECT * FROM files WHERE path LIKE :prefix"); | |
| 134 query.bindValue(":prefix", QString("%1%").arg(prefix)); | |
| 135 } | |
| 136 else { | |
| 137 query.prepare("SELECT * FROM files"); | |
| 138 } | |
| 139 | |
| 140 if (!query.exec()) { | |
| 141 qDebug() << prefix << "::" << query.lastQuery() << "::" << query.lastError().text(); | |
| 142 abort(); | |
| 143 } | |
| 144 | |
| 145 int pathIndex = query.record().indexOf("path"); | |
| 146 int sizeIndex = query.record().indexOf("size"); | |
| 147 int dateIndex = query.record().indexOf("mtime"); | |
| 148 int checksumIndex = query.record().indexOf("checksum"); | |
| 149 while (query.next()) { | |
| 150 QString path = query.value(pathIndex).toString(); | |
| 151 qint64 size = query.value(sizeIndex).toInt(); | |
| 152 QDateTime mtime = query.value(dateIndex).toDateTime(); | |
| 153 QByteArray checksum = query.value(checksumIndex).toByteArray(); | |
| 154 | |
| 155 values << QSharedPointer<FileDBLink::DBInfo>(new FileDBLink::DBInfo(path, size, mtime, checksum)); | |
| 156 //qDebug() << path << size << mtime << checksum.toHex(); | |
| 157 } | |
| 158 | |
| 159 return values; | |
| 160 } |
