forked from WerewolvesRevamped/Werewolves-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.js
More file actions
69 lines (61 loc) · 1.97 KB
/
sql.js
File metadata and controls
69 lines (61 loc) · 1.97 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
/*
Module for using sql / interacting with the database
- Simplified sql access w/ automatic error logging
- Simplified access to stats
*/
module.exports = function() {
/* Variables */
this.connection = null;
this.mysql = require("mysql");
/* Create Connection */
this.sqlSetup = function() {
// Create connection
connection = mysql.createConnection({
host : config.db.host,
user : config.db.user,
password : config.db.password,
database : config.db.database,
charset: "utf8mb4"
});
// Connection connection
connection.connect(err => {
if(err) logO(err);
else getStats();
});
}
/* Does a sql query and calls one callback with result on success and logs an error and calls another callback on failure */
this.sql = function(q, rC, eC) {
sqlQuery(q, rC, eC, 0)
}
/* Does a sql query and calls one callback with result[0].value on success and logs an error and calls another callback on failure */
this.sqlValue = function(q, rC, eC) {
sqlQuery(q, rC, eC, 1)
}
/* Sets a stat in the stat database */
this.sqlSetStat = function(id, value, resCallback, errCallback) {
sql("UPDATE stats SET value = " + connection.escape(value) + " WHERE id = " + connection.escape(id), resCallback, errCallback);
}
/* Gets a stat from the stat database */
this.sqlGetStat = function(id, resCallback, errCallback) {
sqlValue("SELECT value,name FROM stats WHERE id = " + connection.escape(id), resCallback, errCallback);
}
/* Does SQL Queries */
this.sqlQuery = function(query, resCallback, errCallback, mode) {
// Do query
connection.query(query, function(err, result, fields) {
// Check success
if(!err && result) {
// Check which mode and return result accordingly
switch(mode) {
case 0: resCallback(result); break;
case 1: result[0] ? resCallback(result[0].value) : errCallback(); break;
default: resCallback(result); break;
}
} else {
// Handle error
logO(err);
errCallback();
}
});
}
}