-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlManager.js
More file actions
119 lines (114 loc) · 3.34 KB
/
sqlManager.js
File metadata and controls
119 lines (114 loc) · 3.34 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
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
const mysql = require('mysql');
const { db_host, profile_user, profile_password, profile_db, profile_table, db_user, db_password, db_name, db_table} = require('./config.json');
var connection;
var connectionArray = [];
module.exports = {
singleQuery: function(sqlQuery1, author){
connection = mysql.createConnection({
host: db_host,
user: db_user,
password: db_password,
database: db_name,
multipleStatements: true
});
if(connectionArray[author] != null){
connectionArray[author].end();
}
connectionArray[author] = connection;
return new Promise(function(resolve, reject){
connectionArray[author].query(sqlQuery1, function(error, result, fields){
//console.log("[DEBUG] Invoking Query 1");
if(error){
console.log("[SQL MANAGER] There has been an error in Single Query");
return reject(error);
}
resolve(result);
});
});
},
doubleQuery: function(sqlQuery1, sqlQuery2, author){
connection = mysql.createConnection({
host: db_host,
user: db_user,
password: db_password,
database: db_name,
multipleStatements: true
});
if(connectionArray[author] != null){
connectionArray[author].end();
}
connectionArray[author] = connection;
return new Promise(function(resolve, reject){
connectionArray[author].query(sqlQuery1, function(error, result, fields){
//console.log("[DEBUG] Invoking Query 1");
if(error){
console.log("[SQL MANAGER] There has been an error in Double Query Pt 1");
return reject(error);
}
if(result.length === 0){
//console.log(sqlQuery2);
//console.log("[DEBUG] No Results Found. Invoking Query 2");
connectionArray[author].query(sqlQuery2, function(error, result, fields){
if(error){
return reject(error);
console.log("[SQL MANAGER] There has been an error in Double Query Pt 2");
}
resolve(result);
});
}else{
resolve(result);
}
});
});
},
createProfileCard: function(author){
connection = mysql.createConnection({
host: db_host,
user: profile_user,
password: profile_password,
database: profile_db,
multipleStatements: true
});
if(connectionArray[author] != null){
connectionArray[author].end();
}
connectionArray[author] = connection;
return new Promise(function(resolve, reject){
var sql = "SELECT * FROM "+profile_table+" WHERE user_id = "+author;
connectionArray[author].query(sql, function(error, result, fields){
if(error){
console.log("[SQL MANAGER] There has been an error in Create Query");
return reject(error);
}
resolve(result);
});
});
},
queryProfileCard: function(query, author){
connection = mysql.createConnection({
host: db_host,
user: profile_user,
password: profile_password,
database: profile_db,
multipleStatements: true
});
if(connectionArray[author] != null){
connectionArray[author].end();
}
connectionArray[author] = connection;
return new Promise(function(resolve, reject){
connectionArray[author].query(query, function(error, result, fields){
if(error){
console.log("[SQL MANAGER] There has been an error in Profile Query");
return reject(error);
}
resolve(result);
});
});
},
endConnection: function(author){
console.log("[SQL MANAGER] Connection for "+author+" has ended.");
connectionArray[author].end();
connectionArray[author] = null;
}
}