-
Notifications
You must be signed in to change notification settings - Fork 0
/
adodb.js
177 lines (145 loc) · 6.29 KB
/
adodb.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
module.exports = function(RED) {
"use strict";
var reconnect = RED.settings.sqliteReconnectTime || 20000;
// var sqlite3 = require('sqlite3');
const ADODB = require('node-adodb');
function AdodbNodeDB(n) {
RED.nodes.createNode(this,n);
this.dbname = n.db;
var node = this;
node.doConnect = function() {
if (node.db) { return; }
node.db = ADODB.open(node.dbname);
}
node.on('close', function (done) {
done()
});
}
RED.nodes.registerType("adodb_db",AdodbNodeDB);
function AdodbNodeIn(n) {
RED.nodes.createNode(this,n);
this.mydb = n.mydb;
this.sqlquery = n.sqlquery||"msg.topic";
this.sql = n.sql;
this.mydbConfig = RED.nodes.getNode(this.mydb);
var node = this;
node.status({});
if (node.mydbConfig) {
node.mydbConfig.doConnect();
node.status({fill:"green",shape:"dot",text:this.mydbConfig.mod});
var bind = [];
var doQuery = function(msg) {
bind = []
if (node.sqlquery == "viaMsgTopicWithResponse") {
if (typeof msg.topic === 'string') {
if (msg.topic.length > 0) {
async function query() {
console.log(msg.topic)
try {
const response = await node.mydbConfig.db.query(msg.topic);
msg.payload = response;
node.send(msg);
} catch (error) {
node.error(error);
node.error(error.process)
console.error(error);
}
}
query();
}
}
else {
node.error("msg.topic : the query is not defined as a string",msg);
node.status({fill:"red",shape:"dot",text:"msg.topic error"});
}
}
if (node.sqlquery == "viaMsgTopicWithoutResponse") {
if (typeof msg.topic === 'string') {
if (msg.topic.length > 0) {
async function query() {
console.log(msg.topic)
try {
const response = await node.mydbConfig.db.execute(msg.topic);
msg.payload = response;
node.send(msg);
} catch (error) {
node.error(error);
node.error(error.process)
console.error(error);
}
}
query();
}
}
else {
node.error("msg.topic : the query is not defined as a string",msg);
node.status({fill:"red",shape:"dot",text:"msg.topic error"});
}
}
if (node.sqlquery == "queryWithResponse") {
if (typeof node.sql === 'string') {
if (node.sql.length > 0) {
// some removed
async function query() {
try {
const response = await node.mydbConfig.db.query(node.sql);
msg.payload = response;
node.send(msg);
} catch (error) {
node.error(error)
node.error(error.process);
console.error(error);
}
}
query();
}
}
else {
if (node.sql === null || node.sql == "") {
node.error("SQL statement config not set up",msg);
node.status({fill:"red",shape:"dot",text:"SQL config not set up"});
}
}
}
if (node.sqlquery == "queryWithoutResponse") {
if (typeof node.sql === 'string') {
if (node.sql.length > 0) {
// some removed
async function query() {
try {
const response = await node.mydbConfig.db.execute(node.sql);
// msg.payload = response;
node.send(msg);
} catch (error) {
node.error(error)
node.error(error.process);
console.error(error);
}
}
query();
}
}
else {
if (node.sql === null || node.sql == "") {
node.error("SQL statement config not set up",msg);
node.status({fill:"red",shape:"dot",text:"SQL config not set up"});
}
}
}
}
node.on("input", function(msg) {
if (msg.hasOwnProperty("extension")) {
node.mydbConfig.db.loadExtension(msg.extension, function(err) {
if (err) { node.error(err,msg); }
else { doQuery(msg); }
});
}
else { doQuery(msg); }
});
}
else {
node.error("Sqlite database not configured");
}
}
RED.nodes.registerType("adodb",AdodbNodeIn);
}