-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthor.cpp
More file actions
260 lines (200 loc) · 8.42 KB
/
author.cpp
File metadata and controls
260 lines (200 loc) · 8.42 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
#include "author.h"
bool operator==(const Author& author, const Author& other) {
return author.id == other.id &&
author.forename == other.forename &&
author.surname == other.surname &&
author.bio == other.bio &&
author.birth == other.birth &&
author.death == other.death;
}
int addAuthor(sqlite3*& DB, QString forename, QString surname, QString bio, QDate q_birth, QDate q_death) {
sqlite3_stmt* stmt;
const char* sql = "INSERT INTO AUTHORS (FORENAME, SURNAME, BIO, BIRTH, DEATH) VALUES (?, ?, ?, ?, ?);";
int exit = 0;
QString birth = QDateToString(q_birth);
QString death = QDateToString(q_death);
exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
if (exit != SQLITE_OK) {
qCritical() << "Error preparing SQL statement1: " << sqlite3_errmsg(DB) << "\n";
return -1;
}
sqlite3_bind_text(stmt, 1, forename.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, surname.toUtf8().constData(), -1, SQLITE_TRANSIENT);
if (bio.isEmpty()) sqlite3_bind_null(stmt, 3);
else sqlite3_bind_text(stmt, 3, bio.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 4, birth.toUtf8().constData(), -1, SQLITE_TRANSIENT);
if (death.isEmpty()) sqlite3_bind_null(stmt, 5);
else sqlite3_bind_text(stmt, 5, death.toUtf8().constData(), -1, SQLITE_STATIC);
exit = sqlite3_step(stmt);
if (exit != SQLITE_DONE) {
qCritical() << "Error executing SQL statement2: " << sqlite3_errmsg(DB) << "\n";
}
else {
qDebug() << "Author inserted successfully!";
}
sqlite3_finalize(stmt);
return static_cast<int>(sqlite3_last_insert_rowid(DB));
}
bool getAuthorByID(sqlite3*& DB, int author_id, Author& author) {
sqlite3_stmt* stmt;
const char* sql = "SELECT FORENAME, SURNAME, BIO, BIRTH, DEATH FROM AUTHORS WHERE ID = ?;";
int exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
sqlite3_bind_int(stmt, 1, author_id);
if (sqlite3_step(stmt) == SQLITE_ROW) {
const char* forename = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
const char* surname = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
const char* value = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
QString bio = (value == nullptr) ? "" : QString(value);
const char* birth = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
value = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4));
QString death = (value == nullptr) ? "" : QString(value);
author = Author(author_id, forename, surname, bio, stringToQDate(birth), stringToQDate(death));
sqlite3_finalize(stmt);
return true;
}
sqlite3_finalize(stmt);
return false;
}
std::vector<Author> getAuthors(sqlite3*& DB) {
std::vector<Author> authors;
sqlite3_stmt* stmt;
const char* sql = "SELECT ID, FORENAME, SURNAME, BIO, BIRTH, DEATH FROM AUTHORS;";
int exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
while (sqlite3_step(stmt) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const char* forename = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
const char* surname = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
const char* value = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
QString bio = (value == nullptr) ? "" : QString(value);
const char* b_date = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4));
value = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 5));
QString d_date = (value == nullptr) ? "" : QString(value);
Author author(id, forename, surname, bio, stringToQDate(b_date), stringToQDate(d_date));
authors.push_back(author);
}
sqlite3_finalize(stmt);
return authors;
}
bool setAuthorForename(sqlite3*& DB, int author_id, QString newForename) {
sqlite3_stmt* stmt;
const char* sql = "UPDATE AUTHORS SET FORENAME = ? WHERE ID = ?;";
int exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
if (exit != SQLITE_OK) {
return false;
}
sqlite3_bind_text(stmt, 1, newForename.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, author_id);
exit = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return exit == SQLITE_DONE;
}
bool setAuthorSurname(sqlite3*& DB, int author_id, QString newSurname) {
sqlite3_stmt* stmt;
const char* sql = "UPDATE AUTHORS SET SURNAME = ? WHERE ID = ?;";
int exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
if (exit != SQLITE_OK) {
return false;
}
sqlite3_bind_text(stmt, 1, newSurname.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, author_id);
exit = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return exit == SQLITE_DONE;
}
bool setAuthorBio(sqlite3*& DB, int author_id, QString newBio) {
sqlite3_stmt* stmt;
const char* sql = "UPDATE AUTHORS SET BIO = ? WHERE ID = ?;";
int exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
if (exit != SQLITE_OK) {
return false;
}
if (newBio.isEmpty()) sqlite3_bind_null(stmt, 1);
else sqlite3_bind_text(stmt, 1, newBio.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, author_id);
exit = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return exit == SQLITE_DONE;
}
bool setAuthorBirthDate(sqlite3*& DB, int author_id, QDate q_newBirthDate) {
sqlite3_stmt* stmt;
const char* sql = "UPDATE AUTHORS SET BIRTH_DATE = ? WHERE ID = ?;";
int exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
if (exit != SQLITE_OK) {
return false;
}
QString newBirthDate = QDateToString(q_newBirthDate);
if (newBirthDate.isEmpty()) sqlite3_bind_null(stmt, 1);
else sqlite3_bind_text(stmt, 1, newBirthDate.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, author_id);
exit = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return exit == SQLITE_DONE;
}
bool setAuthorDeathDate(sqlite3*& DB, int author_id, QDate q_newDeathDate) {
sqlite3_stmt* stmt;
const char* sql = "UPDATE AUTHORS SET DEATH_DATE = ? WHERE ID = ?;";
int exit = sqlite3_prepare_v2(DB, sql, -1, &stmt, 0);
if (exit != SQLITE_OK) {
return false;
}
QString newDeathDate = QDateToString(q_newDeathDate);
if (newDeathDate.isEmpty()) sqlite3_bind_null(stmt, 1);
else sqlite3_bind_text(stmt, 1, newDeathDate.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, author_id);
exit = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return exit == SQLITE_DONE;
}
void deleteAuthorByID(sqlite3*& DB, int author_id) {
sqlite3_stmt* stmt;
const char* sql = "DELETE FROM AUTHORS WHERE ID = ?;";
int 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, author_id);
exit = sqlite3_step(stmt);
if (exit != SQLITE_DONE) {
qCritical() << "Error executing SQL statement: " << sqlite3_errmsg(DB) << "\n";
}
sqlite3_finalize(stmt);
}
void sortAuthors(std::vector<Author>& authors, Value value, bool bLess) {
switch (value) {
case ID: {
std::sort(authors.begin(), authors.end(), [bLess](const Author& a, const Author& b) {
return bLess ? a.id < b.id : a.id > b.id;
});
break;
}
case FORENAME: {
std::sort(authors.begin(), authors.end(), [bLess](const Author& a, const Author& b) {
return bLess ? a.forename < b.forename : a.forename > b.forename;
});
break;
}
case SURNAME: {
std::sort(authors.begin(), authors.end(), [bLess](const Author& a, const Author& b) {
return bLess ? a.surname < b.surname : a.surname > b.surname;
});
break;
}
case BIRTH: {
std::sort(authors.begin(), authors.end(), [bLess](const Author& a, const Author& b) {
return bLess ? a.birth < b.birth : a.birth > b.birth;
});
break;
}
case DEATH: {
std::sort(authors.begin(), authors.end(), [bLess](const Author& a, const Author& b) {
return bLess ? a.death < b.death : a.death > b.death;
});
break;
}
default: {
qDebug("WRONG VALUE FIELD");
break;
}
}
}