-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbManager.js
More file actions
341 lines (267 loc) · 10.1 KB
/
dbManager.js
File metadata and controls
341 lines (267 loc) · 10.1 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
/***********************************************
Database insertion functions
-functions needed for question insertion into database
************************************************/
const { app } = require("electron");
const path = require("path");
const getDBPath = (filename) => {
let base = app.getAppPath()
if (app.isPackaged) {
base = base.replace('\app.asar', '')
}
//console.log(path.resolve(base, `./databases/${filename}`));
return path.resolve(base, `./databases/${filename}`);
};
var knex = require("knex")({
client: "sqlite3",
connection: {
filename: getDBPath('InQuizIt.db')
},
useNullAsDefault: true
});
/************************************************
*
* Database Merge/Replace Functions
*
***********************************************/
//given the path to another inquizit database file, merge all non redundant
//data into the main db
async function mergeDatabases(externDbPath) {
console.log(externDbPath);
// Connect second external database
const knex2 = require("knex")({
client: 'sqlite3',
connection: {
filename: externDbPath,
},
useNullAsDefault: true,
});
// Get a list of tables in the first database
const tables = await knex2('sqlite_master')
.where('type', 'table')
.select('name');
// For each table
for (const table of tables) {
try {
// Get all rows from the table in the first database
const rows = await knex2(table.name).select();
// Insert all rows into the corresponding table in the second database
await knex(table.name).insert(rows).onConflict().ignore();
} catch (err) {
console.error(`Error inserting data into table ${table.name}:`, err);
}
}
console.log("Succefully Merged External Database in Main");
// Close the connection
await knex2.destroy();
}
//fully replace exisitng main db with specified file path
//also creates backup of original file
async function replaceDatabase(externDbPath){
const fs = require('fs');
const path = require('path');
//close current knex connection
//await knex.destroy();
//create backup of original DB file
fs.copyFile(getDBPath('InQuizIt.db'), getDBPath('InQuizIt-old.db'), err => {
if (err) throw err;
console.log('Backup File copied and renamed successfully!');
});
//copies specified file as new main DB
const sourceName = externDbPath;
const sourceFile = path.resolve(externDbPath);
console.log(sourceFile);
const targetFile = getDBPath('InQuizIt.db');
fs.copyFile(sourceFile, targetFile, err => {
if (err) throw err;
console.log('New DB File copied and renamed successfully!');
});
//reopen new main knex connection
var knex = require("knex")({
client: "sqlite3",
connection: {
filename: getDBPath('InQuizIt.db')
},
useNullAsDefault: true
});
console.log("Main DB successfully reconnected");
}
//copys the db file to the desktop
async function exportDatabase()
{
const fs = require('fs');
const { app } = require('electron');
const sourceFile = getDBPath('InQuizIt.db');
const desktopPath = app.getPath('desktop');
const targetFile = `${desktopPath}/exportedLogicola.db`;
fs.copyFile(sourceFile, targetFile, err => {
if (err) throw err;
console.log('File copied to desktop successfully!');
});
}
/*************************************************
*
* Database Insertion Functions
* Written by: Connor Marshall
*
*************************************************/
//knex insert given row object and table name
async function insertToTable(row,table) {
try {
res = await knex(table).insert(row);
console.log(`Successfully inserted row into ${table} table!`);
return res;
} catch (err) {
console.error(`Error inserting row into ${table} table: ${err}`);
}
};
//Create a new QuestionSet
async function newQuestionSet(Category, Name, Options, TentativeScore = 100,Package = 'Logicola') {
const row = {PackageName: Package, SetCategory: Category, SetName: Name, SetOptions: Options, CompletionScore: TentativeScore};
const table = "QuestionSets_T";
res = await insertToTable(row,table);
return res;
};
//Add a new question to an existing questionset
async function newQuestion(Category, Name, Options, Type, Question, Package = 'Logicola') {
const row = {PackageName: Package, SetCategory: Category, SetName: Name, SetOptions: Options, QuestionType: Type, QuestionContent: Question};
const table = "Questions_T";
res = await insertToTable(row,table);
return res;
};
//Adds a new answer to a specific questionID question
async function newAnswer(Category,Name,Options,Type,Question,Ans, Package = 'Logicola') {
const row = {PackageName: Package, SetCategory: Category, SetName: Name, SetOptions: Options, QuestionType: Type, QuestionContent: Question, Answer: Ans};
const table = "Answers_T";
res = await insertToTable(row,table);
return res;
};
async function newUser(FirstName, LastName) {
const row = {FirstName: FirstName, LastName: LastName};
const table = "Users_T";
const res = await insertToTable(row, table);
return res;
}
/*************************************************
*
* Database Retrieval Functions
* Written by: Eddie Stillman & Connor Marshall
*
*************************************************/
async function getAllUsers() {
const res = await knex.select("*").from("Users_T");
return res;
}
//returns all available question sets
async function getAllQuestionSets() {
const res = await knex.select("*").from("QuestionSets_T");
return res;
};
//returns all available question sets
async function getAllCategories() {
const res = await knex.select(knex.raw('distinct "SetCategory"')).from("QuestionSets_T");
return res;
};
//return one question set given the (category, name, option)
async function getQuestionSet(setCategory, setName, setOptions) {
const res = await knex.select("*").from("QuestionSets_T").where({SetName : setName,
SetOptions : setOptions,
SetCategory : setCategory});
return res;
};
async function getAllQuestions(setCategory, setName, setOptions) {
const res = await knex.select("*").from("Questions_T").where({SetName : setName,
SetOptions : setOptions,
SetCategory : setCategory});
return res;
};
async function getAllAnswers(setCategory, setName, setOptions, questionContent, questionType) {
const res = await knex.select("*").from("Answers_T").where({SetName : setName,
SetOptions : setOptions,
SetCategory : setCategory,
QuestionType : questionType,
QuestionContent : questionContent});
return res;
};
/*************************************************
*
* Database Deletion Functions
* Written by: Connor Marshall
*
*
*************************************************/
async function deleteQuestion(setCategory, setName, setOptions, questionContent, questionType) {
const res = await knex.del('*').from("Answers_T")
.where({SetName : setName,
SetOptions : setOptions,
SetCategory : setCategory,
QuestionType : questionType,
QuestionContent : questionContent});
const res2 = await knex.del('*').from("Questions_T")
.where({SetName : setName,
SetOptions : setOptions,
SetCategory : setCategory,
QuestionType : questionType,
QuestionContent : questionContent});
return res2;
};
async function deleteQuestionSet(setCategory, setName, setOptions) {
const res = await knex.del('*').from("Questions_T")
.where({SetName : setName,
SetOptions : setOptions,
SetCategory : setCategory});
const res2 = await knex.del('*').from("QuestionSets_T")
.where({SetName : setName,
SetOptions : setOptions,
SetCategory : setCategory});
return res2;
};
async function deleteUser(firstName, lastName) {
const res = await knex.del('*').from("Scores_T")
.where({FirstName : firstName,
LastName : lastName});
const res2 = await knex.del('*').from("Users_T")
.where({FirstName : firstName,
LastName : lastName});
return res2;
};
/*************************************************
*
* Database Score Functions
* Written by: Connor Marshall
*
*
*************************************************/
//retrieve all scores for a specific user
async function getAllScores(firstName,lastName) {
const res = await knex.select("*")
.from("Scores_T")
.where({FirstName: firstName, LastName: lastName});
return res;
};
//update or create a score value if not already present
async function updateScore(firstName,lastName, setCategory, setName, setOptions, scoreVal){
const res = await knex("Scores_T")
.insert({ FirstName: firstName, LastName: lastName, PackageName: "Logicola", SetCategory: setCategory, SetName: setName, SetOptions: setOptions, CurrentScore: scoreVal})
.onConflict(["FirstName", "LastName", "PackageName", "SetCategory", "SetName", "SetOptions"]).merge();
return res;
}
//EXPORTS
exports.mergeDatabases = mergeDatabases;
exports.replaceDatabase = replaceDatabase;
exports.exportDatabase = exportDatabase;
exports.newQuestionSet = newQuestionSet;
exports.newQuestion = newQuestion;
exports.newAnswer = newAnswer;
exports.newUser = newUser;
exports.getAllUsers = getAllUsers;
exports.getAllQuestionSets = getAllQuestionSets;
exports.getAllCategories = getAllCategories;
exports.getQuestionSet = getQuestionSet;
exports.getAllQuestions = getAllQuestions;
exports.getAllAnswers = getAllAnswers;
exports.deleteQuestion = deleteQuestion;
exports.deleteQuestionSet = deleteQuestionSet;
exports.deleteUser = deleteUser;
exports.getAllScores = getAllScores;
exports.updateScore = updateScore;