-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.js
97 lines (89 loc) · 2.68 KB
/
sql.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
/**
Database Connection
**/
module.exports = function() {
/* Variables */
this.connection = null;
this.mysql = require("mysql");
/**
SQL Setup
Creates the connection to the database and then loads all the stats afterwards
*/
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",
supportBigNumbers : true
});
// Connection connection
connection.connect(err => {
if(err) console.log(err);
});
}
/**
SQL Query
Does a sql query and calls one callback with result on success and logs an error and calls another callback on failure
Basically a wrapper for sqlQuery with mode=0
**/
this.sql = function(q, rC, eC) {
sqlQuery(q, rC, eC, 0)
}
/**
SQL Query (Internal)
Does SQL Queries. Should only be called internally from other sql functions
The universal sql query function. Takes a query and two callbacks, and optionally a mode value.
Modes:
0: Default query, resolves the promise with the query's result
1: Either resolves with result[0].value if result[0] is set or runs the error callback
*/
this.sqlQuery = function(query, resCallback = ()=>{}, errCallback = ()=>{}, mode = 0) {
// 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
console.log(err);
errCallback();
}
});
}
/**
SQL Promise
Does a sql query as a promise
**/
this.sqlProm = function(query) {
return new Promise(res => {
sql(query, result => {
res(result);
});
});
}
this.sqlPromOne = function(query) {
return new Promise(res => {
sql(query, result => {
res(result[0] ?? null);
});
});
}
/**
SQL Promise (Escaped)
Does a sql query as a promise and appends an escaped value which was parsed unescaped as a second parameter
**/
this.sqlPromEsc = function(query, val) {
return sqlProm(query + connection.escape(val));
}
this.sqlPromOneEsc = function(query, val) {
return sqlPromOne(query + connection.escape(val));
}
}