Mercurial > dedupe
comparison SqliteDBLink.cpp @ 64:b9515dc35fe4
Make sure no file has greater linewidth than 80.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Fri, 14 Sep 2012 22:50:45 +0200 |
| parents | 247adcbbaf8b |
| children | 69a30d9f126e |
comparison
equal
deleted
inserted
replaced
| 63:dd086ec3220d | 64:b9515dc35fe4 |
|---|---|
| 13 SqliteDBLink::SqliteDBLink(const QString& dbPath) | 13 SqliteDBLink::SqliteDBLink(const QString& dbPath) |
| 14 { | 14 { |
| 15 db = QSqlDatabase::addDatabase("QSQLITE", "SqliteDBLink"); | 15 db = QSqlDatabase::addDatabase("QSQLITE", "SqliteDBLink"); |
| 16 db.setDatabaseName(dbPath); | 16 db.setDatabaseName(dbPath); |
| 17 if (!db.open()) | 17 if (!db.open()) |
| 18 throw IOException(QString("Unable to open SQLite database with path '%1'").arg(dbPath)); | 18 throw |
| 19 IOException(QString("Unable to open SQLite database with path '%1'") | |
| 20 .arg(dbPath)); | |
| 19 QSqlQuery query(db); | 21 QSqlQuery query(db); |
| 20 if (!query.exec(QString("SELECT * FROM files;"))) { | 22 if (!query.exec(QString("SELECT * FROM files;"))) { |
| 21 query.exec("CREATE TABLE files(path VARCHAR PRIMARY KEY ASC, size INTEGER, mtime TEXT, checksum TEXT);"); | 23 query.exec("CREATE TABLE files(path VARCHAR PRIMARY KEY ASC," |
| 24 " size INTEGER, mtime TEXT, checksum TEXT);"); | |
| 22 } | 25 } |
| 23 if (!query.exec(QString("SELECT * FROM files;"))) { | 26 if (!query.exec(QString("SELECT * FROM files;"))) { |
| 24 throw SQLException("No database"); | 27 throw SQLException("No database"); |
| 25 } | 28 } |
| 26 | 29 |
| 40 throw SQLException(query); | 43 throw SQLException(query); |
| 41 } | 44 } |
| 42 return query.last(); | 45 return query.last(); |
| 43 } | 46 } |
| 44 | 47 |
| 45 FileDBLink::DBStatus SqliteDBLink::existsWithMtime(const QString& path, const QDateTime& mtime) | 48 FileDBLink::DBStatus SqliteDBLink::existsWithMtime(const QString& path, |
| 49 const QDateTime& mtime) | |
| 46 { | 50 { |
| 47 QSqlQuery query(db); | 51 QSqlQuery query(db); |
| 48 query.prepare("SELECT mtime FROM files WHERE path = :path;"); | 52 query.prepare("SELECT mtime FROM files WHERE path = :path;"); |
| 49 query.bindValue(":path", path); | 53 query.bindValue(":path", path); |
| 50 if (!query.exec()) { | 54 if (!query.exec()) { |
| 58 return MTIME_DIFFERENT; | 62 return MTIME_DIFFERENT; |
| 59 } | 63 } |
| 60 return NONE; | 64 return NONE; |
| 61 } | 65 } |
| 62 | 66 |
| 63 void SqliteDBLink::addFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash) | 67 void SqliteDBLink::addFile(const QString& path, qint64 size, |
| 68 const QDateTime& dtime, const QByteArray& hash) | |
| 64 { | 69 { |
| 65 addFile(DBInfo(path, size, dtime, hash)); | 70 addFile(DBInfo(path, size, dtime, hash)); |
| 66 } | 71 } |
| 67 | 72 |
| 68 bool SqliteDBLink::tryAddFile(const DBInfo& dbinfo) | 73 bool SqliteDBLink::tryAddFile(const DBInfo& dbinfo) |
| 80 throw SQLException(query); | 85 throw SQLException(query); |
| 81 } | 86 } |
| 82 return true; | 87 return true; |
| 83 } | 88 } |
| 84 | 89 |
| 85 void SqliteDBLink::updateFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash) | 90 void SqliteDBLink::updateFile(const QString& path, |
| 91 qint64 size, const QDateTime& dtime, | |
| 92 const QByteArray& hash) | |
| 86 { | 93 { |
| 87 updateFile(DBInfo(path, size, dtime, hash)); | 94 updateFile(DBInfo(path, size, dtime, hash)); |
| 88 } | 95 } |
| 89 | 96 |
| 90 void SqliteDBLink::updateFile(const DBInfo& dbinfo) | 97 void SqliteDBLink::updateFile(const DBInfo& dbinfo) |
| 91 { | 98 { |
| 92 QSqlQuery query(db); | 99 QSqlQuery query(db); |
| 93 query.prepare("UPDATE files SET size=:size, mtime=:mtime, checksum=:checksum WHERE path=:path"); | 100 query.prepare("UPDATE files " |
| 101 "SET size=:size, mtime=:mtime, checksum=:checksum " | |
| 102 "WHERE path=:path"); | |
| 94 query.bindValue(":path", dbinfo.path()); | 103 query.bindValue(":path", dbinfo.path()); |
| 95 query.bindValue(":size", dbinfo.size()); | 104 query.bindValue(":size", dbinfo.size()); |
| 96 query.bindValue(":mtime", dbinfo.mtime()); | 105 query.bindValue(":mtime", dbinfo.mtime()); |
| 97 query.bindValue(":checksum", dbinfo.checksum()); | 106 query.bindValue(":checksum", dbinfo.checksum()); |
| 98 if (!query.exec()) { | 107 if (!query.exec()) { |
| 111 QStringList SqliteDBLink::toStringList() | 120 QStringList SqliteDBLink::toStringList() |
| 112 { | 121 { |
| 113 abort(); | 122 abort(); |
| 114 QStringList list; | 123 QStringList list; |
| 115 /* | 124 /* |
| 116 foreach(QSharedPointer<DBInfo> info, entries) { | 125 foreach(dbinf_ptr_t info, entries) { |
| 117 list << info->serialize(); | 126 list << info->serialize(); |
| 118 } | 127 } |
| 119 */ | 128 */ |
| 120 return list; | 129 return list; |
| 121 } | 130 } |
| 122 | 131 |
| 123 const QList<QSharedPointer<FileDBLink::DBInfo> > SqliteDBLink::values(const QString& prefix) const | 132 const QList<FileDBLink::dbinf_ptr_t > |
| 133 SqliteDBLink::values(const QString& prefix) const | |
| 124 { | 134 { |
| 125 QList<QSharedPointer<FileDBLink::DBInfo> > values; | 135 QList<FileDBLink::dbinf_ptr_t > values; |
| 126 | 136 |
| 127 QSqlQuery query(db); | 137 QSqlQuery query(db); |
| 128 | 138 |
| 129 if (prefix.size() > 0) { | 139 if (prefix.size() > 0) { |
| 130 query.prepare("SELECT * FROM files WHERE path LIKE :prefix"); | 140 query.prepare("SELECT * FROM files WHERE path LIKE :prefix"); |
| 146 QString path = query.value(pathIndex).toString(); | 156 QString path = query.value(pathIndex).toString(); |
| 147 qint64 size = query.value(sizeIndex).toInt(); | 157 qint64 size = query.value(sizeIndex).toInt(); |
| 148 QDateTime mtime = query.value(dateIndex).toDateTime(); | 158 QDateTime mtime = query.value(dateIndex).toDateTime(); |
| 149 QByteArray checksum = query.value(checksumIndex).toByteArray(); | 159 QByteArray checksum = query.value(checksumIndex).toByteArray(); |
| 150 | 160 |
| 151 values << QSharedPointer<FileDBLink::DBInfo>(new FileDBLink::DBInfo(path, size, mtime, checksum)); | 161 values << |
| 152 //qDebug() << path << size << mtime << checksum.toHex(); | 162 FileDBLink::dbinf_ptr_t(new FileDBLink::DBInfo(path, |
| 163 size, mtime, checksum)); | |
| 153 } | 164 } |
| 154 | 165 |
| 155 return values; | 166 return values; |
| 156 } | 167 } |
| 157 | 168 |
| 164 throw SQLException(query); | 175 throw SQLException(query); |
| 165 } | 176 } |
| 166 } | 177 } |
| 167 | 178 |
| 168 | 179 |
| 169 void SqliteDBLink::keepOnlyFromPrefix(const QString& prefix, const QStringList& files) | 180 void SqliteDBLink::keepOnlyFromPrefix(const QString& prefix, |
| 181 const QStringList& files) | |
| 170 { | 182 { |
| 171 QStringList list; | 183 QStringList list; |
| 172 foreach(QSharedPointer<DBInfo> info, values(prefix)) { | 184 foreach(dbinf_ptr_t info, values(prefix)) { |
| 173 if (!files.contains(info->path())) { | 185 if (!files.contains(info->path())) { |
| 174 list << info->path(); | 186 list << info->path(); |
| 175 } | 187 } |
| 176 } | 188 } |
| 177 foreach(QString path, list) { | 189 foreach(QString path, list) { |
