Changeset View
Changeset View
Standalone View
Standalone View
src/qtquick/BookDatabase.cpp
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 51 | if (!db.open()) { | |||
---|---|---|---|---|---|
53 | return false; | 53 | return false; | ||
54 | } | 54 | } | ||
55 | 55 | | |||
56 | QStringList tables = db.tables(); | 56 | QStringList tables = db.tables(); | ||
57 | if (tables.contains("books", Qt::CaseInsensitive)) | 57 | if (tables.contains("books", Qt::CaseInsensitive)) | ||
58 | return true; | 58 | return true; | ||
59 | 59 | | |||
60 | QSqlQuery q; | 60 | QSqlQuery q; | ||
61 | if (!q.exec(QLatin1String("create table books(filename varchar primary key, filetitle varchar, title varchar, series varchar, author varchar, publisher varchar, created datetime, lastOpenedTime datetime, totalPages integer, currentPage integer, thumbnail varchar)"))) { | 61 | if (!q.exec(QLatin1String("create table books(filename varchar primary key, filetitle varchar, title varchar, series varchar, author varchar, publisher varchar, created datetime, lastOpenedTime datetime, totalPages integer, currentPage integer, thumbnail varchar, description varchar, comment varchar, tags varchar, rating integer)"))) { | ||
62 | qDebug() << "Database could not create the table books"; | 62 | qDebug() << "Database could not create the table books"; | ||
63 | return false; | 63 | return false; | ||
64 | } | 64 | } | ||
65 | 65 | | |||
66 | return true; | 66 | return true; | ||
67 | } | 67 | } | ||
68 | 68 | | |||
69 | void closeDb() { | 69 | void closeDb() { | ||
Show All 14 Lines | |||||
84 | 84 | | |||
85 | QList<BookEntry*> BookDatabase::loadEntries() | 85 | QList<BookEntry*> BookDatabase::loadEntries() | ||
86 | { | 86 | { | ||
87 | if(!d->prepareDb()) { | 87 | if(!d->prepareDb()) { | ||
88 | return QList<BookEntry*>(); | 88 | return QList<BookEntry*>(); | ||
89 | } | 89 | } | ||
90 | 90 | | |||
91 | QList<BookEntry*> entries; | 91 | QList<BookEntry*> entries; | ||
92 | QSqlQuery allEntries("SELECT filename, filetitle, title, series, author, publisher, created, lastOpenedTime, totalPages, currentPage, thumbnail FROM books"); | 92 | QSqlQuery allEntries("SELECT filename, filetitle, title, series, author, publisher, created, lastOpenedTime, totalPages, currentPage, thumbnail, description, comment, tags, rating FROM books"); | ||
93 | while(allEntries.next()) | 93 | while(allEntries.next()) | ||
94 | { | 94 | { | ||
95 | BookEntry* entry = new BookEntry(); | 95 | BookEntry* entry = new BookEntry(); | ||
96 | entry->filename = allEntries.value(0).toString(); | 96 | entry->filename = allEntries.value(0).toString(); | ||
97 | entry->filetitle = allEntries.value(1).toString(); | 97 | entry->filetitle = allEntries.value(1).toString(); | ||
98 | entry->title = allEntries.value(2).toString(); | 98 | entry->title = allEntries.value(2).toString(); | ||
99 | entry->series = allEntries.value(3).toString(); | 99 | entry->series = allEntries.value(3).toString().split(","); | ||
100 | entry->author = allEntries.value(4).toString(); | 100 | entry->author = allEntries.value(4).toString().split(","); | ||
101 | entry->publisher = allEntries.value(5).toString(); | 101 | entry->publisher = allEntries.value(5).toString(); | ||
102 | entry->created = allEntries.value(6).toDateTime(); | 102 | entry->created = allEntries.value(6).toDateTime(); | ||
103 | entry->lastOpenedTime = allEntries.value(7).toDateTime(); | 103 | entry->lastOpenedTime = allEntries.value(7).toDateTime(); | ||
104 | entry->totalPages = allEntries.value(8).toInt(); | 104 | entry->totalPages = allEntries.value(8).toInt(); | ||
105 | entry->currentPage = allEntries.value(9).toInt(); | 105 | entry->currentPage = allEntries.value(9).toInt(); | ||
106 | entry->thumbnail = allEntries.value(10).toString(); | 106 | entry->thumbnail = allEntries.value(10).toString(); | ||
107 | entry->description = allEntries.value(11).toString().split(","); | ||||
108 | entry->comment = allEntries.value(12).toString(); | ||||
109 | entry->tags = allEntries.value(13).toString().split(","); | ||||
110 | entry->rating = allEntries.value(14).toInt(); | ||||
107 | entries.append(entry); | 111 | entries.append(entry); | ||
108 | } | 112 | } | ||
109 | 113 | | |||
110 | d->closeDb(); | 114 | d->closeDb(); | ||
111 | return entries; | 115 | return entries; | ||
112 | } | 116 | } | ||
113 | 117 | | |||
114 | void BookDatabase::addEntry(BookEntry* entry) | 118 | void BookDatabase::addEntry(BookEntry* entry) | ||
115 | { | 119 | { | ||
116 | if(!d->prepareDb()) { | 120 | if(!d->prepareDb()) { | ||
117 | return; | 121 | return; | ||
118 | } | 122 | } | ||
119 | qDebug() << "Adding newly discovered book to the database" << entry->filename; | 123 | qDebug() << "Adding newly discovered book to the database" << entry->filename; | ||
120 | 124 | | |||
121 | QSqlQuery newEntry; | 125 | QSqlQuery newEntry; | ||
122 | newEntry.prepare("INSERT INTO books (filename, filetitle, title, series, author, publisher, created, lastOpenedTime, totalPages, currentPage, thumbnail) " | 126 | newEntry.prepare("INSERT INTO books (filename, filetitle, title, series, author, publisher, created, lastOpenedTime, totalPages, currentPage, thumbnail, description, comment, tags, rating) " | ||
123 | "VALUES (:filename, :filetitle, :title, :series, :author, :publisher, :created, :lastOpenedTime, :totalPages, :currentPage, :thumbnail)"); | 127 | "VALUES (:filename, :filetitle, :title, :series, :author, :publisher, :created, :lastOpenedTime, :totalPages, :currentPage, :thumbnail, :description, :comment, :tags, :rating)"); | ||
124 | newEntry.bindValue(":filename", entry->filename); | 128 | newEntry.bindValue(":filename", entry->filename); | ||
125 | newEntry.bindValue(":filetitle", entry->filetitle); | 129 | newEntry.bindValue(":filetitle", entry->filetitle); | ||
126 | newEntry.bindValue(":title", entry->title); | 130 | newEntry.bindValue(":title", entry->title); | ||
127 | newEntry.bindValue(":series", entry->series); | 131 | newEntry.bindValue(":series", entry->series.join(",")); | ||
128 | newEntry.bindValue(":author", entry->author); | 132 | newEntry.bindValue(":author", entry->author.join(",")); | ||
129 | newEntry.bindValue(":publisher", entry->publisher); | 133 | newEntry.bindValue(":publisher", entry->publisher); | ||
130 | newEntry.bindValue(":publisher", entry->publisher); | 134 | newEntry.bindValue(":publisher", entry->publisher); | ||
131 | newEntry.bindValue(":created", entry->created); | 135 | newEntry.bindValue(":created", entry->created); | ||
132 | newEntry.bindValue(":lastOpenedTime", entry->lastOpenedTime); | 136 | newEntry.bindValue(":lastOpenedTime", entry->lastOpenedTime); | ||
133 | newEntry.bindValue(":totalPages", entry->totalPages); | 137 | newEntry.bindValue(":totalPages", entry->totalPages); | ||
134 | newEntry.bindValue(":currentPage", entry->currentPage); | 138 | newEntry.bindValue(":currentPage", entry->currentPage); | ||
135 | newEntry.bindValue(":thumbnail", entry->thumbnail); | 139 | newEntry.bindValue(":thumbnail", entry->thumbnail); | ||
140 | newEntry.bindValue(":description", entry->description.join(",")); | ||||
141 | newEntry.bindValue(":comment", entry->comment); | ||||
142 | newEntry.bindValue(":tags", entry->tags.join(",")); | ||||
143 | newEntry.bindValue(":rating", entry->rating); | ||||
136 | newEntry.exec(); | 144 | newEntry.exec(); | ||
137 | 145 | | |||
138 | d->closeDb(); | 146 | d->closeDb(); | ||
139 | } | 147 | } |