-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfirebird.js
86 lines (80 loc) · 2.74 KB
/
firebird.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
module.exports = function(RED) {
"use strict";
var reconnect = RED.settings.firebirdReconnectTime || 20000;
var firebirddb = require('node-firebird');
function FirebirdNode(n) {
RED.nodes.createNode(this,n);
this.host = n.host;
this.port = n.port;
this.dbname = n.db;
var node = this;
this.query=function(query, done){
var result=null;
firebirddb.attach({
host: node.host,
port: node.port,
database: node.dbname,
user: node.credentials.user,
password: node.credentials.password,
role: null,
pageSize: 4096
}, function(err,db) {
if (err){
node.error(err);
done(err,result);
return;
}
db.query(query, function(err,result) {
if (err) {
node.error(err);
done(err,result);
}
db.detach();
done(err,result);
})
});
}
this.on('close', function (done) {
done();
});
}
RED.nodes.registerType("Firebirddatabase",FirebirdNode, {
credentials: {
user: {type: "text"},
password: {type: "password"}
}
});
function FirebirdDBNodeIn(n) {
RED.nodes.createNode(this,n);
this.firebirddb = n.firebirddb;
this.firebirddbConfig = RED.nodes.getNode(this.firebirddb);
if (this.firebirddbConfig) {
var node = this;
node.on("input", function(msg) {
node.status({fill:"green",shape:"dot",text:"Query.."});
if (typeof msg.topic === 'string') {
//console.log("query:",msg.topic);
var bind = Array.isArray(msg.payload) ? msg.payload : [];
node.firebirddbConfig.query(msg.topic, function(err, rows) {
if (err) {
node.error(err,msg);
node.status({fill:"red",shape:"ring",text:"Error"});
}
else {
msg.payload = rows;
node.send(msg);
node.status({fill:"green",shape:"dot",text:"OK"});
}
});
}
else {
if (typeof msg.topic !== 'string') { node.error("msg.topic : the query is not defined as a string"); }
}
});
}
else {
this.error("Firebird database not configured");
}
}
RED.nodes.registerType("firebird",FirebirdDBNodeIn);
}