forked from darshit-shah/node-database-connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClickhouseConnector.js
640 lines (593 loc) · 22.5 KB
/
ClickhouseConnector.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
var debug = require('debug')('node-database-connectors:clickhouse');
var ClickHouse = require('@apla/clickhouse');
// var PlainTextAuthProvider = db.auth.PlainTextAuthProvider;
//connect
var fieldIdentifier_left = '`',
fieldIdentifier_right = '`';
exports.connect = function(json, cb) {
connect(json, cb);
}
exports.connectPool = function(json, cb) {
connect(json, cb);
}
function connect(json, cb) {
debug("****************json----", json)
var ClickHouseJSON = {
host: json.host,
port: json.port,
dataObjects: 'FORMAT JSON',
queryOptions: json.database ? {
database: json.database
} : undefined
};
var ch = new ClickHouse(ClickHouseJSON);
cb(null, ch);
}
//disconnect
exports.disconnect = function() {
return disconnect(arguments[0]);
}
function disconnect(connection) {
//connection.end();
}
//prepare query
exports.prepareQuery = function() {
return prepareQuery(arguments[0]);
}
function createInsertQuery(json) {
var table = json.table ? json.table : null;
var vInsert = json.insert ? json.insert : null;
var arrInsert = [];
arrInsert = createInsert(vInsert);
var query = '';
if (!Array.isArray(vInsert)) {
if (!Array.isArray(vInsert.fValue[0])) {
query = 'INSERT INTO ' + table + '(' + arrInsert.fieldArr.join() + ') VALUES(' + arrInsert.valueArr.join() + ')';
} else {
query = 'INSERT INTO ' + table + '(' + arrInsert.fieldArr.join() + ') VALUES ' + arrInsert.valueArr.join() + '';
}
} else {
query = 'INSERT INTO ' + table + '(' + arrInsert.fieldArr.join() + ') VALUES(' + arrInsert.valueArr.join() + ')';
}
return query //+ ';';
}
function createUpdateQuery(json) {
var arrUpdate = [],
arrFilter = [],
strJOIN = '';
var table = json.table ? json.table : null;
var vUpdate = json.update ? json.update : null,
vFilter = json.filter ? json.filter : null,
join = json.join ? json.join : null;
if (vFilter != null)
arrFilter = createFilter(vFilter);
if (join != null) {
strJOIN = createJOIN(join);
if (strJOIN.length > 0) {
table = strJOIN;
} else {
table = encloseField(table) + (fromTblAlias ? (' as ' + fromTblAlias) : '');
}
}
arrUpdate = createUpdate(vUpdate);
query = 'UPDATE ' + table + ' SET ' + arrUpdate.join() + '';
if (arrFilter.length > 0) {
query += ' WHERE ' + arrFilter.join('');
}
return query //+ ';';
}
function createSelectQuery(json, selectAll) {
var arrSelect = [],
arrSortBy = [],
arrFilter = [],
arrGroupBy = [],
arrHaving = [],
strJOIN = '';
var table = json.table ? json.table : null;
var fromTblAlias = json.alias ? json.alias : null;
var sortby = json.sortby ? json.sortby : null,
limit = json.limit ? json.limit : null,
join = json.join ? json.join : null;
var vSelect = json.select ? json.select : null,
vFilter = json.filter ? json.filter : null,
vGroupby = json.groupby ? json.groupby : null,
vHaving = json.having ? json.having : null;
if (vHaving != null)
arrHaving = createAggregationFilter(vHaving);
if (vGroupby != null)
arrGroupBy = createSelect(vGroupby, false);
if (vFilter != null)
arrFilter = createFilter(vFilter);
arrSelect = createSelect(vSelect, true);
//from/join
strJOIN = createJOIN(join);
if (strJOIN.length > 0) {
table = strJOIN;
} else {
table = table + (fromTblAlias ? (' as ' + fromTblAlias) : '');
}
//order by
if (sortby != null) {
for (var s = 0; s < sortby.length; s++) {
var encloseFieldFlag = (sortby[s].encloseField != undefined) ? sortby[s].encloseField : true;
var sortField = encloseFieldFlag ? encloseField(sortby[s].field) : sortby[s].field;
var sortTable = sortby[s].table != undefined ? encloseField(sortby[s].table) : null;
var sortOrder = sortby[s].order ? sortby[s].order : 'ASC';
if (sortTable == null)
arrSortBy.push(sortField + ' ' + sortOrder);
else
arrSortBy.push(sortTable + '.' + sortField + ' ' + sortOrder);
}
}
var query = 'SELECT ' + arrSelect.join();
if (table != '') {
query += ' FROM ' + table + '';
}
if (arrFilter.length > 0) {
query += ' WHERE ' + arrFilter.join('');
}
if (arrGroupBy.length > 0) {
query += ' GROUP BY ' + arrGroupBy.join();
}
if (arrHaving.length > 0) {
query += ' HAVING ' + arrHaving.join('');
}
if (arrSortBy.length > 0) {
query += ' ORDER BY ' + arrSortBy.join();
}
if (limit != null) {
query += ' LIMIT ' + limit;
}
return query //+ ';';
}
function createDeleteQuery(json) {
var table = json.table ? json.table : null;
var arrFilter = [];
var vFilter = json.filter ? json.filter : null;;
if (vFilter != null) {
arrFilter = createFilter(vFilter);
}
var query = '';
if (arrFilter.length > 0) {
query = 'DELETE FROM ' + table + ' WHERE' + arrFilter.join('');
} else {
query = 'DELETE FROM ' + table + ' WHERE 1=1';
}
return query //+ ';';
}
function validateJson(json) {
if (!json.hasOwnProperty('insert') && !json.hasOwnProperty('update') && !json.hasOwnProperty('delete') && !json.hasOwnProperty('select')) {
return 'J2Q_INVALID_JSON';
}
if (json.hasOwnProperty('filter') && json.hasOwnProperty('insert')) {
return 'J2Q_INVALID_INSERTJOSN';
}
if (json.hasOwnProperty('limit') || json.hasOwnProperty('having') || json.hasOwnProperty('groupby')) {
if (!json.hasOwnProperty('select')) {
return 'J2Q_ONLYSELECT_JSON';
}
}
if (json.hasOwnProperty('insert') && (json.hasOwnProperty('update') || json.hasOwnProperty('delete') || json.hasOwnProperty('select'))) {
return 'J2Q_INSERT_UPDATE_MERGED';
}
if (json.hasOwnProperty('udpate') && (json.hasOwnProperty('insert') || json.hasOwnProperty('delete') || json.hasOwnProperty('select'))) {
return 'J2Q_INSERT_UPDATE_MERGED';
}
if (json.hasOwnProperty('delete') && (json.hasOwnProperty('update') || json.hasOwnProperty('insert') || json.hasOwnProperty('select'))) {
return 'J2Q_INSERT_UPDATE_MERGED';
}
if (json.hasOwnProperty('select') && (json.hasOwnProperty('update') || json.hasOwnProperty('delete') || json.hasOwnProperty('insert'))) {
return 'J2Q_INSERT_UPDATE_MERGED';
} else {
return '';
}
}
function prepareQuery(json) {
var validate = validateJson(json);
if (validate !== '') {
return validate;
} else {
var query = '';
vInsert = json.insert ? json.insert : null,
vSelect = json.select ? json.select : null,
vUpdate = json.update ? json.update : null,
vDelete = json.delete ? json.delete : null;
//INSERT
if (vInsert != null) {
return createInsertQuery(json);
}
//UPDATE
else if (vUpdate != null) {
return createUpdateQuery(json);
}
//DELETE
else if (vDelete != null) {
return createDeleteQuery(json);
}
//SELECT
else if (vSelect != null) {
return createSelectQuery(json);
}
}
}
//Create select expression
function createSelect(arr, selectAll) {
var tempArr = [];
if (arr != null) {
if (arr.length == 0 && selectAll == true) {
tempArr.push('*');
} else {
for (var s = 0; s < arr.length; s++) {
var obj = arr[s];
if (obj.encloseField != undefined && typeof obj.encloseField != "boolean") {
obj.encloseField = obj.encloseField == 'false' ? false : true;
}
var encloseFieldFlag = (obj.encloseField != undefined) ? obj.encloseField : true;
var field = encloseField(obj.field, encloseFieldFlag);
var table = encloseField((obj.table ? obj.table : ''));
var hasAlias = (obj.alias ? true : false);
var alias = encloseField((obj.alias ? obj.alias : obj.field));
var expression = obj.expression ? obj.expression : null;
var aggregation = obj.aggregation ? obj.aggregation : null;
var dataType = obj.dataType ? obj.dataType : null;
var format = obj.format ? obj.format : null;
var selectText = '';
if (expression != null) {
selectText = '(CASE ';
var cases = expression.cases;
var defaultCase = expression['default'];
var defaultValue = '';
for (var e = 0; e < cases.length; e++) {
var operator = cases[e].operator;
var value = cases[e].value;
var out = cases[e].out;
var outVal = '';
if (out.hasOwnProperty('value')) {
outVal = out.value;
} else {
outVal = encloseField(out.table) + '.' + encloseField(out.field);
}
var strOperatorSign = '';
strOperatorSign = operatorSign(operator, value);
if (strOperatorSign.indexOf('IN') > -1) { //IN condition has different format
selectText += ' WHEN ' + field + ' ' + strOperatorSign + " ('" + value.join("','") + "') THEN " + outVal;
} else {
selectText += ' WHEN ' + field + ' ' + strOperatorSign + " '" + value + "' THEN " + outVal;
}
}
if (defaultCase.hasOwnProperty('value')) {
defaultValue = defaultCase.value;
} else {
defaultValue = encloseField(defaultCase.field);
}
selectText += ' ELSE ' + defaultValue + ' END)';
} else {
if (dataType != null) {
if (dataType.toString().toLowerCase() == 'datetime') {
selectText = ' DATE_FORMAT(' + field + ',\'' + format + '\') ';
} else {
if (encloseFieldFlag == false || encloseFieldFlag == 'false')
selectText = field;
else
selectText = field;
}
} else {
if (encloseFieldFlag == false || encloseFieldFlag == 'false') {
selectText = field;
} else {
selectText = field;
}
}
}
if (aggregation != null) {
selectText = aggregation + '(' + selectText + ')';
}
if (hasAlias) selectText += ' as ' + alias;
tempArr.push(selectText);
selectText = null;
};
}
}
// else {
// tempArr.push('*');
// }
return tempArr;
}
function createInsert(arr) {
var tempJson = {
fieldArr: [],
valueArr: []
};
if (arr == null) {
console.error('createInsert -> blank arr found');
} else {
if (!Array.isArray(arr)) {
for (var y = 0; y < arr.field.length; y++) {
var obj = arr.field[y];
var field = encloseField(obj, encloseFieldFlag)
tempJson.fieldArr.push(field);
}
// arr.forEach(function(obj){
// var field = encloseField(obj, encloseFieldFlag)
// tempJson.fieldArr.push(field);
// });
for (var z = 0; z < arr.fValue.length; z++) {
var obj = arr.fValue[z];
if (Array.isArray(obj)) {
subValueArr = [];
for (var k = 0; k < obj.length; k++) {
var objSub = obj[k]
var fValue = objSub
fValue = (replaceSingleQuote(fValue));
subValueArr.push('\'' + fValue + '\'');
}
if (tempJson.valueArr !== []) {
tempJson.valueArr.push('(' + subValueArr.join() + ')');
} else {
tempJson.valueArr.push(', (' + subValueArr.join() + ')');
}
} else {
var fValue = (replaceSingleQuote(obj));
tempJson.valueArr.push('\'' + fValue + '\'');
}
}
} else {
for (var s = 0; s < arr.length; s++) {
var obj = arr[s];
if (obj.encloseField != undefined && typeof obj.encloseField != "boolean") {
obj.encloseField = obj.encloseField == "false" ? false : true;
}
var encloseFieldFlag = (obj.encloseField != undefined) ? obj.encloseField : true;
var field = encloseField(obj.field, encloseFieldFlag)
var table = encloseField(obj.table ? obj.table : '');
var fValue = obj.fValue ? obj.fValue : '';
fValue = (replaceSingleQuote(fValue));
tempJson.fieldArr.push(field);
tempJson.valueArr.push('\'' + fValue + '\'');
}
}
return tempJson;
}
}
function replaceSingleQuote(aValue) {
if (aValue != undefined && typeof aValue === 'string') {
aValue = aValue.replace(/\'/ig, "\\\'");
return aValue;
} else {
return aValue;
}
}
function createUpdate(arr) {
var tempArr = [];
if (arr != null) {
for (var s = 0; s < arr.length; s++) {
var obj = arr[s];
if (obj.encloseField != undefined && typeof obj.encloseField != "boolean") {
obj.encloseField = obj.encloseField == "false" ? false : true;
}
var encloseFieldFlag = (obj.encloseField != undefined) ? obj.encloseField : true;
var field = encloseField(obj.field, encloseFieldFlag)
var table = encloseField(obj.table ? obj.table : '');
var fValue = obj.fValue ? obj.fValue : '';
fValue = (replaceSingleQuote(fValue));
var selectText = '';
selectText = table + '.' + field + '=' + '\'' + fValue + '\'';
tempArr.push(selectText);
}
return tempArr;
}
}
//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 a;
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') {
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') {
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) {
conditiontext = aggregation + '(' + field + ')';
} else {
conditiontext = aggregation + '(' + field + ')';
}
} else {
if (encloseFieldFlag == false) {
conditiontext = field;
} else {
conditiontext = field;
}
}
if (operator != undefined) {
var sign = operatorSign(operator, value);
if (sign.indexOf('IN') > -1) { //IN condition has different format
if (typeof value[0] == 'string') {
conditiontext += ' ' + sign + " ('" + value.join("','") + "')";
} else {
conditiontext += ' ' + sign + ' (' + value.join(',') + ')';
}
} else {
var tempValue = '';
if (typeof value === 'undefined') {
tempValue = 'null';
} else if (typeof value === 'object') {
sign = operatorSign(operator, '');
if (value.hasOwnProperty('field')) {
tempValue = value.field;
}
} else {
if (typeof value == 'string') {
tempValue = '\'' + value + '\'';
} else {
tempValue = 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);
// });
var stream = connection.query(query);
var rows = [];
// stream.on('metadata', function(columns) {
// });
stream.on('data', function(row) {
rows.push(row);
});
stream.on('error', function(err) {
// debug(err);
cb(err, null, null);
});
stream.on('end', function() {
cb(null, rows, null);
});
// } else {
// return {
// status: false,
// content: {
// result: 'Connection not specified.'
// }
// };
// }
}