-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_db.js
177 lines (169 loc) · 5.09 KB
/
init_db.js
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
var fs = require('fs');
var readline = require('readline');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('storage.db');
var rl = readline.createInterface({
input: fs.createReadStream('questions.txt')
});
db.serialize(function () {
// Quiz Game Schema
db.run(
"CREATE TABLE groups (\
gid INTEGER,\
displayName TEXT NOT NULL,\
PRIMARY KEY (gid))",
function (err) {
console.log("Table 'groups' created!");
});
db.run(
"CREATE TABLE users (\
uid INTEGER,\
gid INTEGER,\
username TEXT NOT NULL,\
password TEXT NOT NULL,\
displayName TEXT NOT NULL,\
role INTEGER NOT NULL DEFAULT 1,\
FOREIGN KEY (gid) REFERENCES groups(gid),\
PRIMARY KEY (uid))",
function (err) {
console.log("Table 'users' created!");
});
db.run(
"CREATE TABLE questions (\
qid INTEGER,\
question TEXT,\
choices TEXT,\
answer INTEGER,\
PRIMARY KEY (qid))",
function (err) {
console.log("Table 'questions' created!");
});
db.run(
"CREATE TABLE collections (\
cid INTEGER,\
collectionName TEXT,\
PRIMARY KEY (cid))",
function (err) {
console.log("Table 'collections' created!");
});
db.run(
"CREATE TABLE questions_collections (\
qid INTEGER,\
cid INTEGER,\
FOREIGN KEY (qid) REFERENCES questions(qid),\
FOREIGN KEY (cid) REFERENCES collections(cid))",
function (err) {
console.log("Table 'questions_collections' created!");
});
db.run(
"CREATE TABLE quizzes (\
qzid INTEGER,\
gid INTEGER,\
cid INTEGER,\
FOREIGN KEY (gid) REFERENCES groups(gid),\
FOREIGN KEY (cid) REFERENCES collections(cid),\
PRIMARY KEY (qzid))",
function (err) {
console.log("Table 'quizzes' created!");
});
db.run(
"CREATE TABLE quiz_sessions (\
qsid INTEGER,\
qzid INTEGER,\
qid INTEGER,\
start_time DATETIME DEFAULT (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')),\
end_time DATETIME,\
FOREIGN KEY (qzid) REFERENCES quizzes(qzid),\
FOREIGN KEY (qid) REFERENCES questions(qid),\
PRIMARY KEY (qsid))",
function (err) {
console.log("Table 'quiz_sessions' created!");
});
db.run(
"CREATE TABLE answered (\
aid INTEGER,\
qsid INTEGER,\
uid INTEGER,\
answer INTEGER,\
answered_time DATETIME DEFAULT (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')),\
FOREIGN KEY (qsid) REFERENCES quiz_sessions(qsid),\
FOREIGN KEY (uid) REFERENCES users(uid),\
PRIMARY KEY (aid))",
function (err) {
console.log("Table 'answered' created!");
});
// Fill Groups Table
var stmt = db.prepare("INSERT INTO groups (gid, displayName) VALUES (?, ?)");
stmt.run(1, "ผู้เข้าร่วมอบรม");
stmt.finalize(function (err) {
console.log("Groups table filled!");
});
// Fill Users Table
var stmt = db.prepare("INSERT INTO users (uid, gid, username, password, displayName, role) VALUES (?, ?, ?, ?, ?, ?)");
stmt.run(1, null, "admin", "Qz654321", "ผู้ดูแลระบบ", 0);
stmt.run(2, 1, "g1", "Qz123456", "กลุ่ม 1", 1);
stmt.run(3, 1, "g2", "Qz123456", "กลุ่ม 2", 1);
stmt.run(4, 1, "g3", "Qz123456", "กลุ่ม 3", 1);
stmt.run(5, 1, "g4", "Qz123456", "กลุ่ม 4", 1);
stmt.run(6, 1, "g5", "Qz123456", "กลุ่ม 5", 1);
stmt.run(7, 1, "g6", "Qz123456", "กลุ่ม 6", 1);
stmt.finalize(function (err) {
console.log("Users table filled!");
});
// Fill Collections Table
var stmt = db.prepare("INSERT INTO collections (cid, collectionName) VALUES (?, ?)");
stmt.run(1, "ชุดคำถามที่ 1");
stmt.finalize(function (err) {
console.log("Collections table filled!");
});
// Import Questions
var stmt = db.prepare("INSERT INTO questions (question, choices, answer) VALUES (?, ?, ?)");
var count = 0;
var question = "";
var choices = [];
var answer;
var saveToDB = function () {
if (question.length !== 0 && answer != null) {
stmt.run(question, JSON.stringify(choices), answer);
question = "";
choices = [];
answer = undefined;
}
};
rl.on('line', (line) => {
count++;
line = line.trim();
if (line.length === 0) {
saveToDB();
count = 0;
} else {
if (count == 1) { question = line; }
else if (count > 1) {
if (line.charAt(0) == '*') {
line = line.slice(1);
answer = count - 2;
}
choices.push(line);
}
}
});
rl.on('close', () => {
saveToDB();
stmt.finalize(function (err) {
if (err) { console.log(err); }
console.log("Questions imported!");
// Add all questions to default collection
var stmt = db.prepare("INSERT INTO questions_collections (qid, cid) VALUES (?, ?)");
db.each("SELECT qid FROM questions", (err, row) => {
stmt.run(row.qid, 1);
}, (err, num) => {
stmt.finalize(function (err) {
console.log("Added all questions to the default collection!");
});
});
});
});
});
db.close(function (err) {
console.log("Database initialization completed!");
});