-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
305 lines (260 loc) · 8.99 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
///////////////////////////////////////////////////////////
// Include
var MySQL = require("mysql");
var Helper = require("./helperfunctions.js");
///////////////////////////////////////////////////////////
// Static vars
var ActiveConnections = [];
///////////////////////////////////////////////////////////
// Connection prototype
function SimSaveConnection(pOptions){
var priv = {
connection : null, // Mysql Connection object
options : pOptions, // Connection options for reference
tableList : {}, // List of available tables
procedureList : {}, // List of available procedures
connectionIndex : -1, // Index of connection in ActiveConnections
connected : false // Used to check if connection is active
}, self = this; // Reference for use in callbacks etc.
///////////////////////////////////////////////////////////
// Public functions
self.Connect = function(pCallBackError, pCallBackSuccess) {
priv.connection = MySQL.createConnection(priv.options);
priv.connection.connect(function(pError){
if(pError){
pCallBackError("Connection error: " + pError);
}else{
var vTablesDone = false, vProcsDone = false;
Query("SHOW Tables;", false, null, function(pResultSet){
pResultSet.Rows.forEach(function(pRow, pRowIndex){
priv.tableList[pRow[pResultSet.Columns[0]]] = {
name: pRow[pResultSet.Columns[0]] //Column name will be like "Tables_in_tablename"
};
});
if(vProcsDone) ConnectionSuccess(pCallBackSuccess);
else vTablesDone = true;
});
Query("SHOW PROCEDURE STATUS;", false, null, function(pResultSet){
pResultSet.Rows.forEach(function(pRow, pRowIndex){
priv.procedureList[pRow.Name] = {
name: pRow.Name
};
});
if(vTablesDone) ConnectionSuccess(pCallBackSuccess);
else vProcsDone = true;
});
}
});
};
self.Select = function(pTable, pColumns, pFilter, pCallBackError, pCallBackSuccess){
GetColumList(pTable, pCallBackError, function(vTable){
var vFilter = "";
if(pFilter){
try {
vFilter = " WHERE " + FormatFilter(vTable, pFilter);
} catch (e) {
return pCallBackError(e);
}
}
if(pColumns === "*"){
Query("SELECT * FROM " + vTable.name + vFilter, true, pCallBackError, pCallBackSuccess);
}else {
if(typeof pColumns === "string") {
pColumns = [pColumns];
}
var vColumns = [];
pColumns.forEach(function(pColumn){
var vColumn = Helper.VerifyField(pColumn, vTable.columns);
if(!vColumn) return pCallBackError("Invalid column!");
vColumns[vColumns.length] = vColumn;
});
Query("SELECT " + vColumns.join(", ") + " FROM " + vTable.name + vFilter, true, pCallBackError, pCallBackSuccess);
}
});
};
self.Insert = function(pTable, pData, pCallBackError, pCallBackSuccess){
GetColumList(pTable, pCallBackError, function(vTable){
var vKeys = Object.keys(pData);
var vColumns = [];
vKeys.forEach(function(pColumn){
var vColumn = Helper.VerifyField(pColumn, vTable.columns);
if(!vColumn) return pCallBackError("Invalid column!");
vColumns[vColumns.length] = vColumn;
});
var vData = [];
vColumns.forEach(function(pColumn){
vData[vData.length] = priv.connection.escape(pData[pColumn]);
});
var sql = "INSERT INTO " + vTable.name + "(" + vColumns.join(", ") + ")" +
"VALUES('" + vData.join("', '") + "')";
Query(sql, false, pCallBackError, pCallBackSuccess);
});
};
self.Update = function(pTable, pData, pFilter, pCallBackError, pCallBackSuccess){
GetColumList(pTable, pCallBackError, function(vTable){
var vFilter = "";
if(pFilter){
try {
vFilter = " WHERE " + FormatFilter(vTable, pFilter);
} catch (e) {
return pCallBackError(e);
}
}else {
return pCallBackError("Please specify a where clause when updating");
}
var vSQL = "UPDATE " + vTable.name + " SET ";
var vKeys = Object.keys(pData);
var vColumns = [];
for(var i = 0; i < vKeys.length; i++){
var vColumn = Helper.VerifyField(vKeys[i], vTable.columns);
if(!vColumn) return pCallBackError("Invalid column!");
vSQL += vColumn + " = " + priv.connection.escape(pData[vColumn]);
if(i < vKeys.length-1) vSQL += ", ";
}
vSQL += vFilter;
Query(vSQL, false, pCallBackError, pCallBackSuccess);
});
};
self.Delete = function(pTable, pFilter, pCallBackError, pCallBackSuccess){
GetColumList(pTable, pCallBackError, function(vTable){
var vFilter = "";
if(pFilter){
try {
vFilter = " WHERE " + FormatFilter(vTable, pFilter);
} catch (e) {
return pCallBackError(e);
}
}else {
return pCallBackError("Please specify a where clause when deleting");
}
var vSQL = "DELETE FROM " + vTable.name + vFilter;
Query(vSQL, false, pCallBackError, pCallBackSuccess);
});
};
self.Execute = function(pProcedureName, pParameters, pCallBackError, pCallBackSuccess){
GetProcParams(pProcedureName, pCallBackError, function(vProcedure){
var vKeys = Object.keys(pParameters);
var vParameters = [];
vKeys.forEach(function(pParamName){
var vParam = Helper.VerifyField(pParamName, vProcedure.parameters);
if(!vParam) return pCallBackError("Invalid parameter!");
vParameters[vParameters.length] = vParam;
});
var vData = [];
vParameters.forEach(function(pColumn){
vData[vData.length] = priv.connection.escape(pParameters[pColumn]);
});
var sql = "CALL " + vProcedure.name + "(" + vData.join(", ") + ");";
Query(sql, true, pCallBackError, pCallBackSuccess);
});
};
self.Close = function(){
// TODO: Do something about closing all connections/not filling up extreme amounts of ActiveConnections
if(priv.connection){
priv.connection.end();
priv.connection = null;
priv.connected = false;
}
};
self.IsConnected = function(){
return priv.connected;
};
///////////////////////////////////////////////////////////
// Private functions
function ConnectionSuccess(pCallBack){
priv.connectionIndex = ActiveConnections.length;
ActiveConnections[priv.connectionIndex] = self;
priv.connected = true;
pCallBack("Connected");
}
function GetColumList(pTableName, pCallBackError, pCallBackSuccess){
vTable = priv.tableList[pTableName];
if(!vTable) return pCallBackError("Invalid table name!");
if(vTable.columns){
pCallBackSuccess(vTable);
}else {
Query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" +
priv.options.database + "' AND `TABLE_NAME`='" + vTable.name + "';", false, null, function(pResultSet){
vTable.columns = [];
pResultSet.Rows.forEach(function(pRow, pRowIndex){
vTable.columns[vTable.columns.length] = pRow.COLUMN_NAME;
});
pCallBackSuccess(vTable);
});
}
}
function GetProcParams(pProcName, pCallBackError, pCallBackSuccess){
var vProc = priv.procedureList[pProcName];
if(!vProc) return pCallBackError("Invalid proc name!");
if(vProc.parameters){
pCallBackSuccess(vProc);
}else {
Query("SELECT PARAMETER_NAME FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_SCHEMA = '" +
priv.options.database + "' AND `SPECIFIC_NAME`='" + vProc.name + "';", false, null, function(pResultSet){
vProc.parameters = [];
pResultSet.Rows.forEach(function(pRow, pRowIndex){
vProc.parameters[vProc.parameters.length] = pRow.PARAMETER_NAME;
});
pCallBackSuccess(vProc);
});
}
}
function Query(pQuery, pIsProcedure, pCallBackError, pCallBackSuccess){
priv.connection.query(pQuery, function(pError, pRows, pFields){
if(pError){
if(pCallBackError)pCallBackError("Query error: " + pError);
else console.log(pError);
}else{
if(pIsProcedure){
pCallBackSuccess(pRows, pFields);
}else {
pCallBackSuccess(CreateResultSet(pRows, pFields));
}
}
});
}
function CreateResultSet(pRows, pFields){
var vRS = {
Rows: pRows,
Columns: []
};
if(pFields){
pFields.forEach(function(pColumn, pColumnIndex){
vRS.Columns[vRS.Columns.length] = pColumn?pColumn.name:"no-no-field";
});
}
return vRS;
}
function FormatFilter(pTable, pFilter, pRecurse){
if(pFilter.Column){
vColumn = Helper.VerifyField(pFilter.Column, pTable.columns);
if(!vColumn) throw new Error("Invalid column!");
vOperator = Helper.VerifyOperator(pFilter.Operator);
if(!vOperator) throw new Error("Invalid Operator!");
// Build string
return vColumn + " " + vOperator + " " + priv.connection.escape(pFilter.Value);
}else if(pFilter.Combo){
if(!pRecurse) pRecurse = 0;
if(pRecurse > 100) throw new Error("Max amount of filter recursing reaced!");
pRecurse ++;
vCombo = Helper.VerifyCombo(pFilter.Combo);
// Recurse each
var vFilterString = "(";
for(var i = 0; i < pFilter.Items.length; i++){
vFilterString += FormatFilter(pTable, pFilter.Items[i], pRecurse);
if(i < pFilter.Items.length-1) vFilterString += " " + vCombo + " ";
}
return vFilterString + ")";
}else{
throw new Error("Invalid filter!");
}
}
}
module.exports = {
Connection : SimSaveConnection,
CloseAll : function(){
for(var i = 0; i < ActiveConnections.length; i++){
ActiveConnections[i].Close();
}
}
};