-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbconnection.js
131 lines (110 loc) · 4.74 KB
/
dbconnection.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
129
130
131
class DBConnection {
constructor() {
this.mysql = require('mysql');
}
#connect() {
return this.mysql.createConnection({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
multipleStatements: false
});
}
queryDB(query) {
var conn = this.#connect();
conn.connect();
return new Promise((resolve, reject) => {
conn.query(query, function(err, result) {
if (result === undefined) {
conn.end();
reject(new Error(err.message));
} else {
conn.end();
resolve(result);
}
});
});
}
select(select, from, where = null, whereCondition = null, orderBy = null, orderByOrder = null) {
if (select.match(/^[-_A-Za-z()*%', ]+$/) && from.match(/^[A-Za-z]+$/)) {
var query = `SELECT ${select} FROM ${from}`;
if (where != null && whereCondition != null && where.match(/^[-_A-Za-z()*%', ]+$/) && whereCondition.match(/^[A-Za-z0-9()]+$/))
query += ` WHERE ${where} = '${whereCondition}'`;
if (orderBy != null && orderByOrder != null && orderBy.match(/^[A-Za-z]+$/) && orderByOrder.match(/^[A-Za-z]+$/))
query += ` ORDER BY ${orderBy} ${orderByOrder}`;
return this.queryDB(query);
} else {
return new Promise((resolve, reject) => {
reject(new Error("Invalid command entered"));
})
}
}
selectGroup(select, from, group, where = null, whereCondition = null, orderBy = null, limit = false) {
if (select.match(/^[-_A-Za-z()*%', ]+$/) && from.match(/^[A-Za-z]+$/) && group.match(/^[A-Za-z]+$/)) {
var query = `SELECT ${select} FROM ${from}`;
if (where != null && whereCondition != null && where.match(/^[-_A-Za-z()*%', ]+$/) && whereCondition.match(/^[A-Za-z0-9()]+$/))
query += ` WHERE ${where} = '${whereCondition}'`
query += ' GROUP BY ' + group;
if (orderBy != null && orderBy.match(/^[A-Za-z]+$/))
query += ' ORDER BY ' + orderBy + ' DESC';
if (limit)
query += ' LIMIT 1';
return this.queryDB(query);
} else {
return new Promise((resolve, reject) => {
reject(new Error("Invalid command entered"));
});
}
}
insert(into, fields, values) {
if (fields.length != values.length) {
return new Promise((resolve, reject) => {
reject(new Error("The number of fields and values do not match."));
});
}
if (into.match(/^[A-Za-z0-9*]+$/)) {
var fieldString = "";
fields.forEach((i) => {
if (i.match(/^[A-Za-z]+$/))
fieldString += i + ", ";
});
fieldString = fieldString.substring(0, fieldString.length - 2);
var valuesString = "";
values.forEach((i) => {
if (i.match(/^[A-Za-z0-9 ]+$/))
valuesString += "'" + i + "', ";
});
valuesString = valuesString.substring(0, valuesString.length - 2);
return this.queryDB("INSERT INTO " + into + " (" + fieldString + ") VALUES (" + valuesString + ")");
} else {
return new Promise((resolve, reject) => {
reject(new Error("Invalid command entered"));
});
}
}
update(table, fields, values, where, whereCondition) {
if (fields.length != values.length) {
return new Promise((resolve, reject) => {
reject(new Error("The number of fields and values do not match."));
});
}
var whereContent = "";
var updateContent = "";
if (table.match(/^[A-Za-z0-9]+$/)) {
for (var i = 0; i < fields.length; i++) {
if (fields[i].match(/^[A-Za-z0-9]+$/) && values[i].match(/^[A-Za-z0-9]+$/))
updateContent += `${fields[i]} = '${values[i]}', `
}
updateContent = updateContent.substring(0, updateContent.length - 2);
if (where.match(/^[A-Za-z0-9]+$/) && whereCondition.match(/^[A-Za-z0-9]+$/))
whereContent = `WHERE ${where} = '${whereCondition}'`
return this.queryDB(`UPDATE ${table} SET ${updateContent} ${whereContent}`);
} else {
return new Promise((resolve, reject) => {
reject(new Error("Invalid command entered"));
})
}
}
}
module.exports = DBConnection;