-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibmgr.cpp
More file actions
363 lines (304 loc) · 10.7 KB
/
libmgr.cpp
File metadata and controls
363 lines (304 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "libmgr.h"
void initDB(sqlite3*& DB, char*& messageError) {
int exit = 0;
exit = sqlite3_open("library.db", &DB);
QString sql_books = R"(
CREATE TABLE IF NOT EXISTS BOOKS(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
TITLE TINYTEXT NOT NULL,
YEAR INT UNSIGNED NOT NULL);
)";
QString sql_authors = R"(
CREATE TABLE IF NOT EXISTS AUTHORS(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
FORENAME TINYTEXT NOT NULL,
SURNAME TINYTEXT NOT NULL,
BIO TEXT,
BIRTH DATE NOT NULL,
DEATH DATE);
)";
QString sql_book_authors = R"(
CREATE TABLE IF NOT EXISTS BOOK_AUTHORS(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
BOOK_ID INTEGER NOT NULL,
AUTHOR_ID INTEGER NOT NULL,
FOREIGN KEY (BOOK_ID) REFERENCES BOOKS(ID) ON DELETE CASCADE,
FOREIGN KEY (AUTHOR_ID) REFERENCES AUTHORS(ID) ON DELETE CASCADE);
)";
QString sql_genres = R"(
CREATE TABLE IF NOT EXISTS GENRES(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
NAME TINYTEXT NOT NULL);
)";
QString sql_book_genres = R"(
CREATE TABLE IF NOT EXISTS BOOK_GENRES(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
BOOK_ID INTEGER NOT NULL,
GENRE_ID INTEGER NOT NULL,
FOREIGN KEY (BOOK_ID) REFERENCES BOOKS(ID) ON DELETE CASCADE,
FOREIGN KEY (GENRE_ID) REFERENCES GENRES(ID) ON DELETE CASCADE);
)";
QString sql_users = R"(
CREATE TABLE IF NOT EXISTS USERS(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
FORENAME TINYTEXT NOT NULL,
SURNAME TINYTEXT NOT NULL,
BIRTH DATE NOT NULL,
EMAIL TINYTEXT NOT NULL,
PHONE TINYTEXT);
)";
QString sql_loans = R"(
CREATE TABLE IF NOT EXISTS LOANS(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
USER_ID INTEGER NOT NULL,
BOOK_ID INTEGER NOT NULL,
START DATE NOT NULL,
END DATE NOT NULL,
FOREIGN KEY (USER_ID) REFERENCES USERS(ID) ON DELETE CASCADE,
FOREIGN KEY (BOOK_ID) REFERENCES BOOKS(ID) ON DELETE CASCADE);
)";
exit = sqlite3_exec(DB, sql_books.toUtf8().constData(), NULL, 0, &messageError);
if (exit != SQLITE_OK) {
qCritical() << "Error in create BOOKS." << "\n";
sqlite3_free(messageError);
}
exit = sqlite3_exec(DB, sql_authors.toUtf8().constData(), NULL, 0, &messageError);
if (exit != SQLITE_OK) {
qCritical() << "Error in create AUTHORS." << "\n";
sqlite3_free(messageError);
}
exit = sqlite3_exec(DB, sql_book_authors.toUtf8().constData(), NULL, 0, &messageError);
if (exit != SQLITE_OK) {
qCritical() << "Error in create BOOK_AUTHORS." << "\n";
sqlite3_free(messageError);
}
exit = sqlite3_exec(DB, sql_genres.toUtf8().constData(), NULL, 0, &messageError);
if (exit != SQLITE_OK) {
qCritical() << "Error in create GENRES." << "\n";
sqlite3_free(messageError);
}
exit = sqlite3_exec(DB, sql_book_genres.toUtf8().constData(), NULL, 0, &messageError);
if (exit != SQLITE_OK) {
qCritical() << "Error in create BOOK_GENRES" << "\n";
sqlite3_free(messageError);
}
exit = sqlite3_exec(DB, sql_users.toUtf8().constData(), NULL, 0, &messageError);
if (exit != SQLITE_OK) {
qCritical() << "Error in create USERS" << "\n";
sqlite3_free(messageError);
}
exit = sqlite3_exec(DB, sql_loans.toUtf8().constData(), NULL, 0, &messageError);
if (exit != SQLITE_OK) {
qCritical() << "Error in create LOANS" << "\n";
sqlite3_free(messageError);
}
}
void linkAuthorToBook(sqlite3*& DB, int author_id, int book_id) {
sqlite3_stmt* stmt;
const char* sql = "INSERT INTO BOOK_AUTHORS (BOOK_ID, AUTHOR_ID) VALUES (?, ?);";
int exit = 0;
exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
if (exit != SQLITE_OK) {
qCritical() << "Error preparing SQL statement: " << sqlite3_errmsg(DB) << "\n";
return;
}
sqlite3_bind_int(stmt, 1, book_id);
sqlite3_bind_int(stmt, 2, author_id);
exit = sqlite3_step(stmt);
if (exit != SQLITE_DONE) {
qCritical() << "Error executing SQL statement: " << sqlite3_errmsg(DB) << "\n";
}
else {
qDebug("Book<->Author relationship inserted successfully!");
}
sqlite3_finalize(stmt);
}
void linkGenreToBook(sqlite3*& DB, int genre_id, int book_id) {
sqlite3_stmt* stmt;
const char* sql = "INSERT INTO BOOK_GENRES (BOOK_ID, GENRE_ID) VALUES (?, ?);";
int exit = 0;
exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
if (exit != SQLITE_OK) {
qCritical() << "Error preparing SQL statement: " << sqlite3_errmsg(DB) << "\n";
return;
}
sqlite3_bind_int(stmt, 1, book_id);
sqlite3_bind_int(stmt, 2, genre_id);
exit = sqlite3_step(stmt);
if (exit != SQLITE_DONE) {
qCritical() << "Error executing SQL statement: " << sqlite3_errmsg(DB) << "\n";
}
else {
qDebug("Book<->Genre relationship inserted successfully!");
}
sqlite3_finalize(stmt);
}
bool unlinkAuthorFromBook(sqlite3*& DB, int author_id, int book_id) {
sqlite3_stmt* stmt;
const char* sql = R"(
DELETE FROM BOOK_AUTHORS
WHERE BOOK_ID = ? AND AUTHOR_ID = ?;
)";
int exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, nullptr);
if (exit != SQLITE_OK) {
qCritical() << "Error preparing statement: " << sqlite3_errmsg(DB) << "\n";
return false;
}
sqlite3_bind_int(stmt, 1, book_id);
sqlite3_bind_int(stmt, 2, author_id);
exit = sqlite3_step(stmt);
if (exit != SQLITE_DONE) {
qCritical() << "Error executing statement: " << sqlite3_errmsg(DB) << "\n";
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return true;
}
bool unlinkGenreFromBook(sqlite3*& DB, int genre_id, int book_id) {
sqlite3_stmt* stmt;
const char* sql = R"(
DELETE FROM BOOK_GENRES
WHERE BOOK_ID = ? AND GENRE_ID = ?;
)";
int exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, nullptr);
if (exit != SQLITE_OK) {
qCritical() << "Error preparing statement: " << sqlite3_errmsg(DB) << "\n";
return false;
}
sqlite3_bind_int(stmt, 1, book_id);
sqlite3_bind_int(stmt, 2, genre_id);
exit = sqlite3_step(stmt);
if (exit != SQLITE_DONE) {
qCritical() << "Error executing statement: " << sqlite3_errmsg(DB) << "\n";
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return true;
}
bool getAuthorsByBookID(sqlite3*& DB, int book_id, std::vector<Author>& authors) {
sqlite3_stmt* stmt;
const char* sql = R"(
SELECT BOOK_AUTHORS.AUTHOR_ID
FROM BOOK_AUTHORS
WHERE BOOK_AUTHORS.BOOK_ID = ?;
)";
int exit = 0;
exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
sqlite3_bind_int(stmt, 1, book_id);
std::vector<int> result;
authors.clear();
while ((exit = sqlite3_step(stmt)) == SQLITE_ROW) {
result.push_back(sqlite3_column_int(stmt, 0));
}
sqlite3_finalize(stmt);
for (int i = 0; i < result.size(); i++) {
Author author;
if (!getAuthorByID(DB, result[i], author)) return false;
authors.push_back(author);
}
return true;
}
bool getBooksByAuthorID(sqlite3*& DB, int author_id, std::vector<Book>& books) {
sqlite3_stmt* stmt;
const char* sql = R"(
SELECT BOOK_AUTHORS.BOOK_ID
FROM BOOK_AUTHORS
WHERE BOOK_AUTHORS.AUTHOR_ID = ?;
)";
int exit = 0;
exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
sqlite3_bind_int(stmt, 1, author_id);
std::vector<int> result;
while ((exit = sqlite3_step(stmt)) == SQLITE_ROW) {
result.push_back(sqlite3_column_int(stmt, 0));
}
sqlite3_finalize(stmt);
for (int i = 0; i < result.size(); i++) {
Book book;
if (!getBookByID(DB, result[i], book)) return false;
books.push_back(book);
}
return true;
}
bool getGenresByBookID(sqlite3*& DB, int book_id, std::vector<Genre>& genres) {
sqlite3_stmt* stmt;
const char* sql = R"(
SELECT BOOK_GENRES.GENRE_ID
FROM BOOK_GENRES
WHERE BOOK_GENRES.BOOK_ID = ?;
)";
int exit = 0;
exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
sqlite3_bind_int(stmt, 1, book_id);
std::vector<int> result;
while ((exit = sqlite3_step(stmt)) == SQLITE_ROW) {
result.push_back(sqlite3_column_int(stmt, 0));
}
sqlite3_finalize(stmt);
for (int i = 0; i < result.size(); i++) {
Genre genre;
if (!getGenreByID(DB, result[i], genre)) return false;
genres.push_back(genre);
}
return true;
}
bool getBooksByGenreID(sqlite3*& DB, int genre_id, std::vector<Book>& books) {
sqlite3_stmt* stmt;
const char* sql = R"(
SELECT BOOK_GENRES.BOOK_ID
FROM BOOK_GENRES
WHERE BOOK_GENRES.GENRE_ID = ?;
)";
int exit = 0;
exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
sqlite3_bind_int(stmt, 1, genre_id);
std::vector<int> result;
while ((exit = sqlite3_step(stmt)) == SQLITE_ROW) {
result.push_back(sqlite3_column_int(stmt, 0));
}
sqlite3_finalize(stmt);
for (int i = 0; i < result.size(); i++) {
Book book;
if (!getBookByID(DB, result[i], book)) return false;
books.push_back(book);
}
return true;
}
bool getGenresByAuthorID(sqlite3*& DB, int author_id, std::vector<Genre>& genres) {
genres.clear();
std::vector<Book> authorBooks;
if (!getBooksByAuthorID(DB, author_id, authorBooks)) return false;
for (int i = 0; i < authorBooks.size(); i++) {
std::vector<Genre> searchVec;
if (!getGenresByBookID(DB, authorBooks[i].id, searchVec)) return false;
for (int j = 0; j < searchVec.size(); j++) {
if (std::find(genres.begin(), genres.end(), searchVec[j]) == genres.end())
genres.push_back(searchVec[j]);
}
}
return true;
}
bool getBooksByUserID(sqlite3*& DB, int user_id, std::vector<Book>& books) {
sqlite3_stmt* stmt;
const char* sql = R"(
SELECT LOANS.BOOK_ID
FROM LOANS
WHERE LOANS.USER_ID = ?;
)";
int exit = 0;
exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
sqlite3_bind_int(stmt, 1, user_id);
std::vector<int> result;
while ((exit = sqlite3_step(stmt)) == SQLITE_ROW) {
result.push_back(sqlite3_column_int(stmt, 0));
}
sqlite3_finalize(stmt);
for (int i = 0; i < result.size(); i++) {
Book book;
if (!getBookByID(DB, result[i], book)) return false;
books.push_back(book);
}
return true;
}