-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_query.js
113 lines (81 loc) · 2.42 KB
/
run_query.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
module.exports = function(RED) {
"use strict";
var hanaClient = require('@sap/hana-client');
var async = require('async');
function sqlQueryIn(n) {
//console.dir(n);
RED.nodes.createNode(this, n);
this.mydbConf = n.mydb;
this.hanaConfig = RED.nodes.getNode(this.mydbConf);
var node = this;
node.on("input", function(msg, send, done) {
node.warn("Topic : " + msg.topic);
node.warn("Payload : " + JSON.stringify(msg.payload));
if (msg.topic === 'SQL' || msg.topic === 'sql' ){
if(typeof msg.payload === 'string' || msg.payload instanceof String){
msg.payload = [ msg.payload ];
}
var conn = hanaClient.createConnection();
node.conn_params = {
host : node.hanaConfig.host,
port : node.hanaConfig.port,
user : node.hanaConfig.user,
password : node.hanaConfig.password
};
conn.connect(node.conn_params, function(err, result) {
if(err) {
node.warn("Connection Params : " + JSON.stringify(node.conn_params));
node.warn(err);
var errMess = "Connection failed for host " + node.hanaConfig.host + " with user " + node.hanaConfig.user;
node.warn(errMess);
var errMsg = {
"topic" : "ERROR",
"payload" : err
};
send(errMsg);
done();
}
else
{
var fnExec = [];
for(var i = 0; i < msg.payload.length; i++){
let sql = msg.payload[i];
node.warn("SQL loop : " + sql);
var fn = function(callback){
conn.exec(sql, [], function (err2, result2) {
if (err2) {
node.warn(err2);
callback(null, err2);
}
else{
callback(null, result2);
}
});
};
fnExec.push(fn);
}
async.series(
fnExec,
function(err, results) {
node.warn("Async results : " + JSON.stringify(results) );
var msg = {
"topic" : "RESULTS_IN_PAYLOAD",
"payload" : results
}
send(msg);
done();
conn.disconnect();
}
);
}
});
}
else {
if (typeof msg.topic !== 'string') {
node.error("msg.topic : the query is not defined as a string");
}
}
});
}
RED.nodes.registerType("run_query", sqlQueryIn);
}