-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlhelper.cpp
82 lines (73 loc) · 2.66 KB
/
sqlhelper.cpp
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
#include "sqlhelper.h"
#include <string>
using namespace std;
void SqlHelper::openDatabase() {
EXEC SQL CONNECT TO '[email protected]' USER csdb3 IDENTIFIED BY csdb3;
EXEC SQL WHENEVER SQLERROR SQLPRINT;
EXEC SQL WHENEVER SQLWARNING SQLPRINT;
}
void SqlHelper::insertQuestions(Question *q) {
EXEC SQL BEGIN DECLARE SECTION;
int category = q->category;
const char* question = q->question.c_str();
const char* correct = q->correct.c_str();
const char* wrong1 = q->wrong1.c_str();
const char* wrong2 = q->wrong2.c_str();
const char* wrong3 = q->wrong3.c_str();
EXEC SQL END DECLARE SECTION;
EXEC SQL INSERT INTO "quiz" VALUES (:category, :question, :correct, :wrong1, :wrong2, :wrong3);
EXEC SQL COMMIT;
}
Question* SqlHelper::getQuestion(int questionId) {
EXEC SQL BEGIN DECLARE SECTION;
int category;
int qid = questionId;
char question[64];
char correct[64];
char wrong1[64];
char wrong2[64];
char wrong3[64];
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT qid, category, question, correct, wrong1, wrong2, wrong3 INTO :qid, :category, :question, :correct, :wrong1, :wrong2, :wrong3 FROM quiz WHERE qid = :qid;
return new Question(category, string(question), string(correct), string(wrong1), string(wrong2), string(wrong3), qid);
}
void SqlHelper::deleteQuestion(int questionId) {
EXEC SQL BEGIN DECLARE SECTION;
int qid = questionId;
EXEC SQL END DECLARE SECTION;
EXEC SQL DELETE FROM quiz WHERE qid = :qid;
EXEC SQl COMMIT;
}
void SqlHelper::updateQuestion(Question *q) {
EXEC SQL BEGIN DECLARE SECTION;
int category = q->category;
int qid = q->qid;
char question[64];
char correct[64];
char wrong1[64];
char wrong2[64];
char wrong3[64];
EXEC SQL END DECLARE SECTION;
strcpy(question, q->question.c_str());
strcpy(correct, q->correct.c_str());
strcpy(wrong1, q->wrong1.c_str());
strcpy(wrong2, q->wrong2.c_str());
strcpy(wrong3, q->wrong3.c_str());
EXEC SQL UPDATE quiz SET correct = :correct, question = :question, wrong1 = :wrong1, wrong2 = :wrong2, wrong3 = :wrong3, category = :category WHERE qid = :qid;
EXEC SQl COMMIT;
}
void SqlHelper::insertCategory(Category *c) {
EXEC SQL BEGIN DECLARE SECTION;
const char* name = c->name.c_str();
EXEC SQL END DECLARE SECTION;
EXEC SQL INSERT INTO "category" ("name") VALUES (:name);
EXEC SQL COMMIT;
}
bool SqlHelper::existCategory(int cid) {
EXEC SQL BEGIN DECLARE SECTION;
int cid2 = cid;
int foundCid;
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT cid INTO :foundCid FROM category WHERE cid = :cid2;
return cid == foundCid;
}