diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SqliteDBLink.cpp	Tue Aug 21 14:25:33 2012 +0200
@@ -0,0 +1,160 @@
+#include "SqliteDBLink.hpp"
+
+
+#include <sqlite3.h>
+
+#include <cassert>
+
+#include <QtCore/QStringList>
+#include <QtCore/QDebug>
+
+#include <QtSql/QSqlQuery>
+#include <QtSql/QSqlError>
+#include <QtSql/QSqlRecord>
+
+SqliteDBLink::SqliteDBLink(const QString& dbPath)
+{
+  db = QSqlDatabase::addDatabase("QSQLITE");
+  db.setDatabaseName(dbPath);
+  bool ok = db.open();
+  assert(ok);
+  QSqlQuery query;
+  if (!query.exec(QString("SELECT * FROM files;"))) {
+    query.exec("CREATE TABLE files(path VARCHAR PRIMARY KEY ASC, size INTEGER, mtime TEXT, checksum TEXT);");
+  }
+  if (!query.exec(QString("SELECT * FROM files;"))) {
+    qDebug()<<"No database";
+    exit(1);
+  }
+
+}
+
+SqliteDBLink::~SqliteDBLink()
+{
+  db.close();
+}
+
+bool SqliteDBLink::exists(const QString& path)
+{
+  QSqlQuery query;
+  query.prepare("SELECT path FROM files WHERE path = :path;");
+  query.bindValue(":path", path);
+  if (!query.exec()) {
+    qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text();
+  }
+  return query.last();
+}
+
+FileDBLink::DBStatus SqliteDBLink::existsWithMtime(const QString& path, const QDateTime& mtime)
+{
+  QSqlQuery query;
+  query.prepare("SELECT mtime FROM files WHERE path = :path;");
+  query.bindValue(":path", path);
+  if (!query.exec()) {
+    qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text();
+  }
+  if (query.next()) {
+    int dateIndex = query.record().indexOf("mtime");
+    QDateTime mtimeEntry = query.value(dateIndex).toDateTime();
+    if (mtimeEntry == mtime)
+      return SAME;
+    return MTIME_DIFFERENT;
+  }
+  return NONE;
+}
+
+void SqliteDBLink::addFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash)
+{
+  addFile(DBInfo(path, size, dtime, hash));
+}
+
+void SqliteDBLink::updateFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash)
+{
+  updateFile(DBInfo(path, size, dtime, hash));
+}
+
+bool SqliteDBLink::tryAddFile(const DBInfo& dbinfo)
+{
+  if (exists(dbinfo.path()))
+    return false;
+  QSqlQuery query;
+  query.prepare("INSERT INTO files (path, size, mtime, checksum) "
+		"VALUES (:path, :size, :mtime, :checksum)");
+  query.bindValue(":path", dbinfo.path());
+  query.bindValue(":size", dbinfo.size());
+  query.bindValue(":mtime", dbinfo.mtime());
+  query.bindValue(":checksum", dbinfo.checksum());
+  if (!query.exec()) {
+    qDebug() << dbinfo.path() << "::" << query.lastQuery() << "::" << query.lastError().text();
+  }
+  return true;
+}
+
+void SqliteDBLink::updateFile(const DBInfo& dbinfo)
+{
+  QSqlQuery query;
+  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()) {
+    qDebug() << query.lastError().text();
+  }
+}
+
+void SqliteDBLink::addFile(const DBInfo& dbinfo)
+{
+  if (!tryAddFile(dbinfo)) {
+    abort(); //Should throw exception
+  }
+}
+
+
+QStringList SqliteDBLink::toStringList()
+{
+  abort();
+  QStringList list;
+  /*
+  foreach(QSharedPointer<DBInfo> info, entries) {
+    list << info->serialize();
+  }
+  */
+  return list;
+}
+
+const QList<QSharedPointer<FileDBLink::DBInfo> > SqliteDBLink::values(const QString& prefix) const
+{
+  QList<QSharedPointer<FileDBLink::DBInfo> > values;
+
+  QSqlQuery query;
+
+  if (prefix.size() > 0) {
+    query.prepare("SELECT * FROM files WHERE path LIKE :prefix");
+    query.bindValue(":prefix", QString("%1%").arg(prefix));
+  }
+  else {
+    query.prepare("SELECT * FROM files");
+  }
+
+  if (!query.exec()) {
+    qDebug() << prefix << "::" << query.lastQuery() << "::" << query.lastError().text();
+    abort();
+  }
+
+  int pathIndex = query.record().indexOf("path");
+  int sizeIndex = query.record().indexOf("size");
+  int dateIndex = query.record().indexOf("mtime");
+  int checksumIndex = query.record().indexOf("checksum");
+  while (query.next()) {
+    QString path = query.value(pathIndex).toString();
+    qint64 size = query.value(sizeIndex).toInt();
+    QDateTime mtime = query.value(dateIndex).toDateTime();
+    QByteArray checksum = query.value(checksumIndex).toByteArray();
+
+    values << QSharedPointer<FileDBLink::DBInfo>(new FileDBLink::DBInfo(path, size, mtime, checksum));
+    //qDebug() << path << size << mtime << checksum.toHex();
+  }
+
+  return values;
+}