Actually delete books from database. Delete all books when "Clear
[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 t("BookDb::BookDb");
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 t("BookDb::create");
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 t("BookDb::load");
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     qDebug() << ret;
84     return ret;
85 }
86
87 void BookDb::save(const QString &book, const QVariantHash &data)
88 {
89     Trace t("BookDb::save");
90     qDebug() << book;
91     qDebug() << data;
92     QByteArray bytes;
93     QDataStream out(&bytes, QIODevice::WriteOnly);
94     out << data;
95     QSqlQuery query("insert or replace into book values (?, ?)");
96     query.bindValue(0, book);
97     query.bindValue(1, bytes);
98     if (!query.exec()) {
99         qCritical() << "Query failed:" << query.lastError().text();
100     }
101 }
102
103 void BookDb::remove(const QString &book)
104 {
105     Trace t("BookDb::remove");
106     qDebug() << book;
107     QSqlQuery query("delete from book where name = ?");
108     query.bindValue(0, book);
109     if (!query.exec()) {
110         qCritical() << "Query failed:" << query.lastError().text();
111     }
112 }
113
114 QStringList BookDb::books()
115 {
116     Trace t("BookDb::books");
117     QStringList ret;
118     QSqlQuery query("select name from book");
119     query.setForwardOnly(true);
120     if (!query.exec()) {
121         qCritical() << "Query failed:" << query.lastError().text();
122         return ret;
123     }
124     while (query.next()) {
125         ret.append(query.value(0).toString());
126     }
127     qDebug() << ret;
128     return ret;
129 }
130
131 void BookDb::removeAll()
132 {
133     foreach (QString book, books()) {
134         remove(book);
135     }
136 }