comparison SqliteDBLink.cpp @ 93:308a718812ba

Small refactoring to allow lazy commits.
author Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no>
date Tue, 22 Oct 2013 11:53:54 +0200
parents f49023c61dac
children 93981e675d67
comparison
equal deleted inserted replaced
92:f49023c61dac 93:308a718812ba
112 updateFile(DBInfo(path, size, dtime, hash), lazy); 112 updateFile(DBInfo(path, size, dtime, hash), lazy);
113 } 113 }
114 114
115 void SqliteDBLink::updateFile(const DBInfo& dbinfo, bool lazy) 115 void SqliteDBLink::updateFile(const DBInfo& dbinfo, bool lazy)
116 { 116 {
117 QSqlQuery query(db); 117 if (lazy) {
118 query.prepare("UPDATE files " 118 operations.push_back(Operation(dbinfo, Update));
119 "SET size=:size, mtime=:mtime, checksum=:checksum " 119 }
120 "WHERE path=:path"); 120 else {
121 query.bindValue(":path", dbinfo.path()); 121 QSqlQuery query(db);
122 query.bindValue(":size", dbinfo.size()); 122 query.prepare("UPDATE files "
123 query.bindValue(":mtime", dbinfo.mtime()); 123 "SET size=:size, mtime=:mtime, checksum=:checksum "
124 query.bindValue(":checksum", dbinfo.checksum()); 124 "WHERE path=:path");
125 if (!query.exec()) { 125 query.bindValue(":path", dbinfo.path());
126 throw SQLException(query); 126 query.bindValue(":size", dbinfo.size());
127 query.bindValue(":mtime", dbinfo.mtime());
128 query.bindValue(":checksum", dbinfo.checksum());
129 if (!query.exec()) {
130 throw SQLException(query);
131 }
127 } 132 }
128 } 133 }
129 134
130 void SqliteDBLink::addFile(const DBInfo& dbinfo, bool lazy) 135 void SqliteDBLink::addFile(const DBInfo& dbinfo, bool lazy)
131 { 136 {
132 if (!tryAddFile(dbinfo)) { 137 if (lazy) {
133 abort(); //Should throw exception 138 operations.push_back(Operation(dbinfo, Add));
139 }
140 else {
141 if (!tryAddFile(dbinfo)) {
142 abort(); //Should throw exception
143 }
134 } 144 }
135 } 145 }
136 146
137 147
138 QStringList SqliteDBLink::toStringList() 148 QStringList SqliteDBLink::toStringList()
241 size, mtime, checksum)); 251 size, mtime, checksum));
242 } 252 }
243 253
244 return values; 254 return values;
245 } 255 }
256
257 bool SqliteDBLink::commit()
258 {
259 int n = 0;
260 int max = operations.size();
261 foreach(const Operation& operation, operations) {
262 switch (operation.second) {
263 case Add:
264 addFile(operation.first);
265 break;
266 case Update:
267 updateFile(operation.first);
268 }
269 emit progressUpdate(++n, max);
270 }
271 return true;
272 }