Simplify in/out traces.
[dorian] / model / bookdb.cpp
1 #include <QFileInfo>
2
3 #include "bookdb.h"
4 #include "platform.h"
5 #include "trace.h"
6
7 BookDb *theInstance;
8
9 BookDb *BookDb::instance()
10 {
11     if (!theInstance) {
12         theInstance = new BookDb;
13     }
14     return theInstance;
15 }
16
17 void BookDb::close()
18 {
19     if (theInstance) {
20         delete theInstance;
21         theInstance = 0;
22     }
23 }
24
25 BookDb::BookDb()
26 {
27     TRACE;
28     bool shouldCreate = false;
29     QFileInfo info(Platform::dbPath());
30     if (!info.exists()) {
31         QDir dbDir;
32         if (!dbDir.mkpath(info.absolutePath())) {
33             qCritical() << "Could not create" << info.absolutePath();
34         }
35         shouldCreate = true;
36     }
37     db = QSqlDatabase::addDatabase("QSQLITE");
38     db.setDatabaseName(QDir::toNativeSeparators(Platform::dbPath()));
39     if (!db.open()) {
40         qCritical() << "Could not open" << Platform::dbPath() << ": Error"
41                 << db.lastError().text();
42     }
43     if (shouldCreate) {
44         create();
45     }
46 }
47
48 BookDb::~BookDb()
49 {
50     db.close();
51 }
52
53 void BookDb::create()
54 {
55     TRACE;
56     QSqlQuery query;
57     if (!query.exec("create table book "
58                     "(name text primary key, content blob)")) {
59         qCritical() << "Failed to create database:"
60                 << query.lastError().text();
61     }
62 }
63
64 QVariantHash BookDb::load(const QString &book)
65 {
66     TRACE;
67     qDebug() << book;
68     QVariantHash ret;
69     QByteArray bytes;
70     QSqlQuery query("select content from book where name = ?");
71     query.bindValue(0, book);
72     query.setForwardOnly(true);
73     if (!query.exec()) {
74         qCritical() << "Query failed:" << query.lastError().text();
75         return ret;
76     }
77     while (query.next()) {
78         bytes = query.value(0).toByteArray();
79         QDataStream in(bytes);
80         in >> ret;
81         break;
82     }
83     return ret;
84 }
85
86 void BookDb::save(const QString &book, const QVariantHash &data)
87 {
88     TRACE;
89     qDebug() << book;
90     QByteArray bytes;
91     QDataStream out(&bytes, QIODevice::WriteOnly);
92     out << data;
93     QSqlQuery query("insert or replace into book values (?, ?)");
94     query.bindValue(0, book);
95     query.bindValue(1, bytes);
96     if (!query.exec()) {
97         qCritical() << "Query failed:" << query.lastError().text();
98     }
99 }
100
101 void BookDb::remove(const QString &book)
102 {
103     TRACE;
104     qDebug() << book;
105     QSqlQuery query("delete from book where name = ?");
106     query.bindValue(0, book);
107     if (!query.exec()) {
108         qCritical() << "Query failed:" << query.lastError().text();
109     }
110 }
111
112 QStringList BookDb::books()
113 {
114     TRACE;
115     QStringList ret;
116     QSqlQuery query("select name from book");
117     query.setForwardOnly(true);
118     if (!query.exec()) {
119         qCritical() << "Query failed:" << query.lastError().text();
120         return ret;
121     }
122     while (query.next()) {
123         ret.append(query.value(0).toString());
124     }
125     qDebug() << ret;
126     return ret;
127 }
128
129 void BookDb::removeAll()
130 {
131     foreach (QString book, books()) {
132         remove(book);
133     }
134 }