-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.js
111 lines (95 loc) · 2.38 KB
/
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
"use strict";
// db.js handles db stuff
(function() {
var db = require('sqlite-sync');
var conf = require('./conf.json');
const SCHEMA_VERSION = 2;
function getSchemaVer() {
var res = db.run("PRAGMA user_version");
return res[0]["user_version"];
}
function setSchemaVer(ver) {
var res = db.run("PRAGMA user_version=" + ver);
return (res.error);
}
function updateSchema(from) {
switch(from + 1) {
case 1:
var res = db.run(`
CREATE TABLE messages(
id INTEGER PRIMARY KEY AUTOINCREMENT,
user TEXT NOT NULL, userID TEXT NOT NULL,
channelID TEXT NOT NULL, message TEXT NOT NULL,
evt TEXT NOT NULL,
ts DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
if(res.error)
throw res.error;
setSchemaVer(1);
break;
case 2:
var res = db.run(`
CREATE TABLE suggest_enabled(
id INTEGER PRIMARY KEY AUTOINCREMENT,
enabled INTEGER NOT NULL, message_id INTEGER NOT NULL,
FOREIGN KEY(message_id) REFERENCES messages(id)
)
`);
if(res.error)
throw res.error;
setSchemaVer(2);
break;
}
}
function _logMessage(msg) {
delete msg["bot"];
console.log("log - (" + msg["user"] + ") " + msg["message"]);
var res = db.insert("messages", msg);
console.log("insert res: " + res);
return res;
}
function _getSuggestEnabled(msg) {
var res = db.run(`
SELECT enabled FROM suggest_enabled AS se
LEFT JOIN messages AS m ON m.id = se.message_id
WHERE m.channelID = ` + msg["channelID"] + `
ORDER BY m.ts DESC
LIMIT 1
`);
if(res.error)
throw res.error;
console.log("res: " + JSON.stringify(res));
if(res.length != 1)
return false;
var boolstr = res[0]["values"][0][0];
if(boolstr == "0")
return false;
if(boolstr == "1")
return true;
return null;
}
module.exports.connect = () => {
var dburi = conf["db"]["path"] + conf["db"]["name"];
db.connect(dburi);
}
module.exports.updateSchema = () => {
var ver = getSchemaVer();
while(ver < SCHEMA_VERSION) {
console.log("upgrading db schema ver: " + ver + " -> " + (ver + 1));
updateSchema(ver);
ver++;
}
}
module.exports.logMessage = (msg) => {
return _logMessage(msg);
}
module.exports.getSuggestEnabled = (msg) => {
return _getSuggestEnabled(msg);
}
module.exports.setSuggestEnabled = (msg, bool) => {
var msg_id = _logMessage(msg);
var boolstr = (bool) ? "1" : "0";
var res = db.insert("suggest_enabled", { enabled: boolstr, message_id: msg_id });
}
})();