diff 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
line wrap: on
line diff
--- a/SqliteDBLink.cpp	Mon Oct 21 20:03:39 2013 +0200
+++ b/SqliteDBLink.cpp	Tue Oct 22 11:53:54 2013 +0200
@@ -114,23 +114,33 @@
 
 void SqliteDBLink::updateFile(const DBInfo& dbinfo, bool lazy)
 {
-  QSqlQuery query(db);
-  query.prepare("UPDATE files "
-		"SET size=:size, mtime=:mtime, checksum=:checksum "
-		"WHERE path=:path");
-  query.bindValue(":path", dbinfo.path());
-  query.bindValue(":size", dbinfo.size());
-  query.bindValue(":mtime", dbinfo.mtime());
-  query.bindValue(":checksum", dbinfo.checksum());
-  if (!query.exec()) {
-    throw SQLException(query);
+  if (lazy) {
+    operations.push_back(Operation(dbinfo, Update));
+  }
+  else {
+    QSqlQuery query(db);
+    query.prepare("UPDATE files "
+		  "SET size=:size, mtime=:mtime, checksum=:checksum "
+		  "WHERE path=:path");
+    query.bindValue(":path", dbinfo.path());
+    query.bindValue(":size", dbinfo.size());
+    query.bindValue(":mtime", dbinfo.mtime());
+    query.bindValue(":checksum", dbinfo.checksum());
+    if (!query.exec()) {
+      throw SQLException(query);
+    }
   }
 }
 
 void SqliteDBLink::addFile(const DBInfo& dbinfo, bool lazy)
 {
-  if (!tryAddFile(dbinfo)) {
-    abort(); //Should throw exception
+  if (lazy) {
+    operations.push_back(Operation(dbinfo, Add));
+  }
+  else {
+    if (!tryAddFile(dbinfo)) {
+      abort(); //Should throw exception
+    }
   }
 }
 
@@ -243,3 +253,20 @@
 
   return values;
 }
+
+bool SqliteDBLink::commit()
+{
+  int n = 0;
+  int max = operations.size();
+  foreach(const Operation& operation, operations) {
+    switch (operation.second) {
+    case Add:
+      addFile(operation.first);
+      break;
+    case Update:
+      updateFile(operation.first);
+    }
+    emit progressUpdate(++n, max);
+  }
+  return true;
+}