-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathJSONConnector.js
339 lines (306 loc) · 10.1 KB
/
JSONConnector.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var debug = require('debug')('node-database-connectors:node-database-connectors');
const utils=require("./utils.js");
//connect
var fieldIdentifier_left = '`',
fieldIdentifier_right = '`';
exports.connectPool = function(json, cb) {
return connectPool(json, cb);
}
exports.connect = function(json, cb) {
return connect(json, cb);
}
function connectPool(json, cb) {
cb(null,json);
}
function connect(json, cb) {
cb(null,json);
}
//disconnect
exports.disconnect = function() {
return disconnect(arguments[0]);
}
function disconnect(connection) {
// connection.end();
return "";
}
//prepare query
exports.prepareQuery = function() {
return prepareQuery(arguments[0]);
}
function createInsertQuery(json) {
return query;
}
function createUpdateQuery(json) {
return query;
}
function createSelectQuery(json, selectAll) {
return query ;
}
function createDeleteQuery(json) {
return query;
}
function validateJson(json) {
const result=utils.validateJson(json);
if (!json.hasOwnProperty('join') && !json.hasOwnProperty('table')) {
throw new Error('table or join key missing');
}
if (json.hasOwnProperty('join') && json.hasOwnProperty('table')) {
throw new Error('Either join or table allowed at a time');
}
if (json.hasOwnProperty('insert') || json.hasOwnProperty('update') || json.hasOwnProperty('delete') ) {
throw new Error('insert,update and delete operations are not supported now');
}
return result;
}
function prepareQuery(json) {
var validate = validateJson(json);
if (validate !== '') {
return validate;
} else {
return json;
}
}
//Create select expression
function createSelect(arr, selectAll) {
return arr;
}
function createInsert(arr) {
return arr;
}
function replaceSingleQuote(aValue) {
if (aValue != undefined && typeof aValue === 'string') {
aValue = aValue.replace(/\'/ig, "\\\'");
return aValue;
} else {
return aValue;
}
}
function createUpdate(arr) {
return arr;
}
//Create select expression
function createAggregationFilter(obj) {
var tempHaving = [];
if (obj != null) {
tempHaving = createFilter(obj);
}
return tempHaving;
}
exports.createFilter = function(arr) {
return createFilter(arr);
}
//Create filter conditions set
function createFilter(arr) {
var tempArrFilter = [];
if (arr != null) {
if (arr.hasOwnProperty('and') || arr.hasOwnProperty('AND') || arr.hasOwnProperty('or') || arr.hasOwnProperty('OR')) { //multiple conditions
tempArrFilter = createMultipleConditions(arr);
} else { //single condition
var conditiontext = createSingleCondition(arr);
tempArrFilter.push(conditiontext);
}
}
return tempArrFilter;
}
function createMultipleConditions(obj) {
var tempArrFilters = [];
var conditionType = Object.keys(obj)[0]; //AND/OR/NONE
if (conditionType.toString().toLowerCase() != 'none') {
var listOfConditions = obj[conditionType]; //all conditions
for (var c = 0; c < listOfConditions.length; c++) {
var tempConditionType = Object.keys(listOfConditions[c])[0];
//console.log('*************' + tempConditionType + '*******************');
if (tempConditionType.toString().toLowerCase() == 'and' || tempConditionType.toString().toLowerCase() == 'or') {
tempArrFilters.push(createMultipleConditions(listOfConditions[c]));
} else if (tempConditionType.toString().toLowerCase() == 'none') {
var conditiontext = createSingleCondition(listOfConditions[c].none);
tempArrFilters.push(conditiontext);
} else {
var conditiontext = createSingleCondition(listOfConditions[c]);
tempArrFilters.push(conditiontext);
}
}
} else { //single condition
if (listOfConditions.length > 0) {
var conditiontext = createSingleCondition(listOfConditions.none);
tempArrFilters.push(conditiontext);
}
}
var tempConditionSet = '(' + tempArrFilters.join(' ' + conditionType + ' ') + ')';
tempArrFilters = [];
tempArrFilters.push(tempConditionSet);
return tempArrFilters;
}
function encloseField(a, flag) {
if (flag == undefined || (flag != undefined && flag == true))
return fieldIdentifier_left + a + fieldIdentifier_right;
else
return a;
}
function operatorSign(operator, value) {
var sign = '';
if (operator.toString().toLowerCase() == 'eq') {
if (Object.prototype.toString.call(value) === '[object Array]') {
sign = 'IN';
} else if (typeof value === 'undefined' || value == null) {
sign = 'IS';
} else if (typeof value == 'string') {
sign = '=';
} else {
sign = '=';
}
} else if (operator.toString().toLowerCase() == 'noteq') {
if (Object.prototype.toString.call(value) === '[object Array]') {
sign = 'NOT IN';
} else if (typeof value === 'undefined' || value == null) {
sign = 'IS NOT';
} else if (typeof value == 'string') {
sign = '<>';
} else {
sign = '<>';
}
} else if (operator.toString().toLowerCase() == 'match') {
sign = 'LIKE';
} else if (operator.toString().toLowerCase() == 'notmatch') {
sign = 'NOT LIKE';
} else if (operator.toString().toLowerCase() == 'gt') {
sign = '>';
} else if (operator.toString().toLowerCase() == 'lt') {
sign = '<';
} else if (operator.toString().toLowerCase() == 'gteq') {
sign = '>=';
} else if (operator.toString().toLowerCase() == 'lteq') {
sign = '<=';
} else {
throw Error("Unknow operator '%s'", operator);
}
return sign;
}
function createSingleCondition(obj) {
var field = obj.field,
table = obj.table ? obj.table : '',
aggregation = obj.aggregation ? obj.aggregation : null,
operator = obj.operator,
value = obj.value,
encloseFieldFlag = obj.encloseField;
if (encloseFieldFlag != undefined && typeof encloseFieldFlag != "boolean") {
encloseFieldFlag = encloseFieldFlag == "false" ? false : true;
}
var conditiontext = '';
if (aggregation != null) {
if (encloseFieldFlag == false) {
//CBT:this is for nested aggregation if aggregation key contains Array
if (Object.prototype.toString.call(aggregation).toLowerCase() === "[object array]") {
var aggregationText = "";
aggregation.forEach(function(d) {
aggregationText = aggregationText + d + "("
});
conditiontext = aggregationText + field;
aggregationText = "";
aggregation.forEach(function(d) {
aggregationText = aggregationText + ")"
});
conditiontext = conditiontext + aggregationText;
} else {
conditiontext = aggregation + '(' + field + ')';
}
} else {
if (Object.prototype.toString.call(aggregation).toLowerCase() === "[object array]") {
var aggregationText = "";
aggregation.forEach(function(d) {
aggregationText = aggregationText + d + "("
});
conditiontext = aggregationText + encloseField(table) + '.' + encloseField(field);
aggregationText = "";
aggregation.forEach(function(d) {
aggregationText = aggregationText + ")"
});
conditiontext = conditiontext + aggregationText;
} else {
conditiontext = aggregation + '(' + encloseField(table) + '.' + encloseField(field) + ')';
}
}
} else {
if (encloseFieldFlag == false) {
conditiontext = field;
} else {
conditiontext = '' + encloseField(table) + '.' + encloseField(field) + '';
}
}
if (operator != undefined) {
if(Array.isArray(value) && value.length ==1)
{
const updatedValue = value[0];
value = updatedValue;
}
var sign = operatorSign(operator, value);
if (sign.indexOf('IN') > -1) { //IN condition has different format
var tempValue = value.map(d => d != null ? d.toString().replace(/\'/ig, "\\\'") : d).join("','");
conditiontext += " " + sign + " ('" + tempValue + "')";
} else {
var tempValue = '';
if (typeof value === 'undefined' || value == null) {
tempValue = 'null';
} else if (typeof value === 'object') {
sign = operatorSign(operator, '');
if (value.hasOwnProperty('field')) {
var rTable = value.table ? value.table : '';
tempValue = encloseField(rTable) + '.' + encloseField(value.field);
}
} else {
tempValue = '\'' + replaceSingleQuote(value) + '\'';
}
conditiontext += ' ' + sign + ' ' + tempValue;
}
}
return conditiontext;
}
//create join conditions
function createJOIN(join) {
var joinText = '';
if (join != null) {
var fromTbl = join.table;
var fromTblAlias = join.alias;
var joinwith = join.joinwith;
// var strJoinConditions = '';
joinText += encloseField(fromTbl) + (fromTblAlias ? (' as ' + fromTblAlias) : '');
for (var j = 0; j < joinwith.length; j++) {
var table = joinwith[j].table,
tableAlias = joinwith[j].alias,
type = joinwith[j].type ? joinwith[j].type : 'INNER',
joincondition = joinwith[j].joincondition;
// for (var jc = 0; jc < joincondition.length; jc++) {
// strJoinConditions += joincondition[jc].on + ' ' + operatorSign(joincondition[jc].operator, joincondition[jc].value) + ' ' + joincondition[jc].value + ' ';
// }
joinText += ' ' + type.toString().toUpperCase() + ' JOIN ' + encloseField(table) + (tableAlias ? (' as ' + tableAlias) : '') + ' ON ' + createFilter(joincondition).join('');
}
}
return joinText;
}
//run query
exports.execQuery = function(query, connection, cb) {
return execQuery(query, connection, cb);
}
function execQuery(query, connection, cb) {
// var query = arguments[0][0];
// var connection = null;
// var format = null;
// if (arguments[0].length > 1) {
// format = arguments[0][2];
// }
// if (arguments[0].length > 0) {
// connection = arguments[0][1];
//Commenting pipe and returning full JSON;
//return connection.query(query).stream({ highWaterMark: 5 }).pipe(objectToCSV(format));
connection.query(query, function(err, result, fields) {
cb(err, result, fields);
});
// } else {
// return {
// status: false,
// content: {
// result: 'Connection not specified.'
// }
// };
// }
}