comparison SqliteDBLink.cpp @ 31:bf3dce7fedcb

Remove all references to QDebug
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Thu, 06 Sep 2012 19:14:58 +0200
parents 06166d6c083b
children c978d4a6514d
comparison
equal deleted inserted replaced
30:1072257d2bab 31:bf3dce7fedcb
1 #include "SqliteDBLink.hpp" 1 #include "SqliteDBLink.hpp"
2 2
3 #include <QtCore/QStringList> 3 #include <QtCore/QStringList>
4 #include <QtCore/QDebug> 4 #include <QtCore/QVariant>
5
5 6
6 #include <QtSql/QSqlQuery> 7 #include <QtSql/QSqlQuery>
7 #include <QtSql/QSqlError> 8 #include <QtSql/QSqlError>
8 #include <QtSql/QSqlRecord> 9 #include <QtSql/QSqlRecord>
10
11 #include "Exception/SQLException.hpp"
9 12
10 #include <cassert> 13 #include <cassert>
11 14
12 SqliteDBLink::SqliteDBLink(const QString& dbPath) 15 SqliteDBLink::SqliteDBLink(const QString& dbPath)
13 { 16 {
18 QSqlQuery query(db); 21 QSqlQuery query(db);
19 if (!query.exec(QString("SELECT * FROM files;"))) { 22 if (!query.exec(QString("SELECT * FROM files;"))) {
20 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, size INTEGER, mtime TEXT, checksum TEXT);");
21 } 24 }
22 if (!query.exec(QString("SELECT * FROM files;"))) { 25 if (!query.exec(QString("SELECT * FROM files;"))) {
23 qDebug()<<"No database"; 26 throw SQLException("No database");
24 exit(1);
25 } 27 }
26 28
27 } 29 }
28 30
29 SqliteDBLink::~SqliteDBLink() 31 SqliteDBLink::~SqliteDBLink()
35 { 37 {
36 QSqlQuery query(db); 38 QSqlQuery query(db);
37 query.prepare("SELECT path FROM files WHERE path = :path;"); 39 query.prepare("SELECT path FROM files WHERE path = :path;");
38 query.bindValue(":path", path); 40 query.bindValue(":path", path);
39 if (!query.exec()) { 41 if (!query.exec()) {
40 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); 42 throw SQLException(query);
41 } 43 }
42 return query.last(); 44 return query.last();
43 } 45 }
44 46
45 FileDBLink::DBStatus SqliteDBLink::existsWithMtime(const QString& path, const QDateTime& mtime) 47 FileDBLink::DBStatus SqliteDBLink::existsWithMtime(const QString& path, const QDateTime& mtime)
46 { 48 {
47 QSqlQuery query(db); 49 QSqlQuery query(db);
48 query.prepare("SELECT mtime FROM files WHERE path = :path;"); 50 query.prepare("SELECT mtime FROM files WHERE path = :path;");
49 query.bindValue(":path", path); 51 query.bindValue(":path", path);
50 if (!query.exec()) { 52 if (!query.exec()) {
51 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); 53 throw SQLException(query);
52 } 54 }
53 if (query.next()) { 55 if (query.next()) {
54 int dateIndex = query.record().indexOf("mtime"); 56 int dateIndex = query.record().indexOf("mtime");
55 QDateTime mtimeEntry = query.value(dateIndex).toDateTime(); 57 QDateTime mtimeEntry = query.value(dateIndex).toDateTime();
56 if (mtimeEntry == mtime) 58 if (mtimeEntry == mtime)
75 query.bindValue(":path", dbinfo.path()); 77 query.bindValue(":path", dbinfo.path());
76 query.bindValue(":size", dbinfo.size()); 78 query.bindValue(":size", dbinfo.size());
77 query.bindValue(":mtime", dbinfo.mtime()); 79 query.bindValue(":mtime", dbinfo.mtime());
78 query.bindValue(":checksum", dbinfo.checksum()); 80 query.bindValue(":checksum", dbinfo.checksum());
79 if (!query.exec()) { 81 if (!query.exec()) {
80 qDebug() << dbinfo.path() << "::" << query.lastQuery() << "::" << query.lastError().text(); 82 throw SQLException(query);
81 } 83 }
82 return true; 84 return true;
83 } 85 }
84 86
85 void SqliteDBLink::updateFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash) 87 void SqliteDBLink::updateFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash)
94 query.bindValue(":path", dbinfo.path()); 96 query.bindValue(":path", dbinfo.path());
95 query.bindValue(":size", dbinfo.size()); 97 query.bindValue(":size", dbinfo.size());
96 query.bindValue(":mtime", dbinfo.mtime()); 98 query.bindValue(":mtime", dbinfo.mtime());
97 query.bindValue(":checksum", dbinfo.checksum()); 99 query.bindValue(":checksum", dbinfo.checksum());
98 if (!query.exec()) { 100 if (!query.exec()) {
99 qDebug() << query.lastError().text(); 101 throw SQLException(query);
100 } 102 }
101 } 103 }
102 104
103 void SqliteDBLink::addFile(const DBInfo& dbinfo) 105 void SqliteDBLink::addFile(const DBInfo& dbinfo)
104 { 106 {
133 else { 135 else {
134 query.prepare("SELECT * FROM files"); 136 query.prepare("SELECT * FROM files");
135 } 137 }
136 138
137 if (!query.exec()) { 139 if (!query.exec()) {
138 qDebug() << prefix << "::" << query.lastQuery() << "::" << query.lastError().text(); 140 throw SQLException(query);
139 abort();
140 } 141 }
141 142
142 int pathIndex = query.record().indexOf("path"); 143 int pathIndex = query.record().indexOf("path");
143 int sizeIndex = query.record().indexOf("size"); 144 int sizeIndex = query.record().indexOf("size");
144 int dateIndex = query.record().indexOf("mtime"); 145 int dateIndex = query.record().indexOf("mtime");
160 { 161 {
161 QSqlQuery query(db); 162 QSqlQuery query(db);
162 query.prepare("DELETE FROM files WHERE path = :path"); 163 query.prepare("DELETE FROM files WHERE path = :path");
163 query.bindValue(":path", path); 164 query.bindValue(":path", path);
164 if (!query.exec()) { 165 if (!query.exec()) {
165 qDebug() << path << "::" << query.lastQuery() << "::" << query.lastError().text(); 166 throw SQLException(query);
166 } 167 }
167 } 168 }
168 169
169 170
170 void SqliteDBLink::keepOnlyFromPrefix(const QString& prefix, const QStringList& files) 171 void SqliteDBLink::keepOnlyFromPrefix(const QString& prefix, const QStringList& files)