-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandDB.js
128 lines (108 loc) · 3.67 KB
/
commandDB.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
var config = require('./config.js');
var mysql = require('mysql');
const command = require('./command.js');
class commandDB {
host;
user;
password;
database;
con;
constructor(host, user, password, database) {
this.host = host;
this.user = user;
this.password = password;
this.database = database;
this.con = mysql.createConnection({
host: this.host,
user: this.user,
password: this.password,
database: this.database
});
}
openDB(action) {
var con = this.con;
switch (action) {
case true:
con.connect(function (err, result) {
if (err) console.log(err);
});
break;
case false:
con.end();
break;
}
this.con = con;
}
performQuery(sql) {
var con = this.con;
con.query(sql, function (err, result) {
if (err) console.log(err);
//console.log(result);
return result;
});
}
createTables() {
var sql = "CREATE TABLE if not exists commands" +
"(commandID BIGINT NOT NULL," +
"commandTrigger VARCHAR(64)," +
"commandText TEXT," +
"helpText TEXT," +
"authorID BIGINT," +
"PRIMARY KEY(commandID)" +
")";
var result = this.performQuery(sql);
return result;
}
updateCommand(selectedCommand) {
var sql = `INSERT INTO commands` +
`(commandID, commandTrigger, commandText, helpText, authorID)` +
`VALUES (${selectedCommand.id}, \"${selectedCommand.trigger}\", \"${selectedCommand.text}\",` +
`\"${selectedCommand.help}\",${selectedCommand.author})` +
`ON DUPLICATE KEY UPDATE commandTrigger=\"${selectedCommand.trigger}\", commandText=\"${selectedCommand.text}\", helpText=\"${selectedCommand.help}\"`;
var result = this.performQuery(sql);
return result;
}
commandObjectToArray(commandArray) {
var values = [];
commandArray.forEach(function (element) {
values.push([element.id, element.trigger, element.text, element.help, element.author]);
});
return values;
}
updateCommandsBatch(valueArray) {
var con = this.con;
var sql = `INSERT INTO commands` +
`(commandID, commandTrigger, commandText, helpText, authorID)` +
`VALUES ?` +
`ON DUPLICATE KEY UPDATE commandTrigger=VALUES(commandTrigger),` +
`commandText=VALUES(commandText), helpText=VALUES(helpText)`;
con.query(sql, [valueArray], function (err) {
if (err) console.log(err);
});
}
retrieveCommand(commandID) {
var sql = `SELECT * FROM commands` +
`WHERE commandID = ${commandID}` +
`LIMIT 1`;
var result = this.performQuery(sql);
Object.keys(result).forEach(function (key) {
var row = result[key];
console.loge(row.name);
});
return result[key];
}
retrieveAllCommands() {
var con = this.con;
var sql = "SELECT * FROM commands";
con.query(sql, function (err, result, fields) {
if (err) console.log(err);
Object.keys(result).forEach(function (key) {
var row = result[key];
//console.log(row);
config.allCommands.push(new command(row.commandID, row.commandTrigger, row.commandText, row.helpText, row.authorID));
});
return result;
});
}
}
module.exports = commandDB;