-
Notifications
You must be signed in to change notification settings - Fork 4
/
odbc.js
98 lines (95 loc) · 2.96 KB
/
odbc.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
module.exports = function(RED) {
"use strict";
var mustache = require("mustache");
var Pool = require("odbc").Pool
function connection(config) {
RED.nodes.createNode(this, config);
this.cn = "";
if (config.driver!=""){
this.cn = this.cn + "DRIVER="+config.driver + ";"
}
if (config.server!=""){
this.cn = this.cn + "SERVER="+config.server + ";"
}
if (this.credentials.username!=""){
this.cn = this.cn + "USER="+ this.credentials.username + ";"
}
if (this.credentials.password != ""){
this.cn = this.cn + "PASSWORD="+ this.credentials.password + ";"
}
this.cn = this.cn + config.other;
this.pool = new Pool();
var node = this;
/* node.on('close',function(){
node.pool.close(function(){});
})
*/ }
RED.nodes.registerType("ODBC-CN", connection, {
credentials: {
username: {type:"text"},
password: {type:"password"}
}
});
function odbc(config) {
RED.nodes.createNode(this, config);
var odbcCN = RED.nodes.getNode(config.odbcCN);
this.query = config.query;
this.pool = odbcCN.pool;
this.cn = odbcCN.cn;
this.outField = config.outField;
var node = this;
var b = node.outField.split(".");
var i = 0;
var r = null;
var m = null;
var rec = function(obj) {
i += 1;
if ((i < b.length) && (typeof obj[b[i-1]] === "object")) {
rec(obj[b[i-1]]); // not there yet - carry on digging
}
else {
if (i === b.length) { // we've finished so assign the value
obj[b[i-1]] = r;
node.send(m);
node.status({});
}
else {
obj[b[i-1]] = {}; // needs to be a new object so create it
rec(obj[b[i-1]]); // and carry on digging
}
}
}
/* node.on('input',function(msg){
node.pool.open(node.cn, function(err, db){
if (err){
node.error(err);
node.status({fill:"red",shape:"ring",text:"Error"});
return;
}*/
node.pool.open(node.cn, function(err, db){
if (err) {
node.error(err);
node.status({fill:"red",shape:"ring",text:"Error"});
db.close(function(){});
return;
}
node.on('input', function(msg) {
node.status({fill:"blue",shape:"dot",text:"requesting"});
var query = mustache.render(node.query,msg);
db.query(query, function (err, rows, moreResultSets){
if (err) {
node.error(err);
node.status({fill:"red",shape:"ring",text:"Error"});
db.close(function(){});
return;
}
i = 0;
r = rows;
m = msg;
rec(msg);
});
});
});
}
RED.nodes.registerType("ODBC", odbc);
}