forked from node-red/node-red-nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path68-mysql.js
More file actions
169 lines (155 loc) · 6.25 KB
/
68-mysql.js
File metadata and controls
169 lines (155 loc) · 6.25 KB
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
module.exports = function(RED) {
"use strict";
var reconnect = RED.settings.mysqlReconnectTime || 20000;
var mysqldb = require('mysql');
function MySQLNode(n) {
RED.nodes.createNode(this,n);
this.host = n.host;
this.port = n.port;
this.tz = n.tz || "local";
this.connected = false;
this.connecting = false;
this.dbname = n.db;
this.setMaxListeners(0);
var node = this;
function checkVer() {
node.connection.query("SELECT version();", [], function(err, rows) {
node.connection.release();
if (err) {
node.error(err);
node.status({fill:"red",shape:"ring",text:"Bad Ping"});
doConnect();
}
});
}
function doConnect() {
node.connecting = true;
node.emit("state","connecting");
if (!node.pool) {
node.pool = mysqldb.createPool({
host : node.host,
port : node.port,
user : node.credentials.user,
password : node.credentials.password,
database : node.dbname,
timezone : node.tz,
insecureAuth: true,
multipleStatements: true,
connectionLimit: 25
});
}
node.pool.getConnection(function(err, connection) {
node.connecting = false;
if (err) {
node.emit("state",err.code);
node.error(err);
node.tick = setTimeout(doConnect, reconnect);
}
else {
node.connection = connection;
node.connected = true;
node.emit("state","connected");
node.connection.on('error', function(err) {
node.connected = false;
node.connection.release();
node.emit("state",err.code);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
doConnect(); // silently reconnect...
}
else if (err.code === 'ECONNRESET') {
doConnect(); // silently reconnect...
}
else {
node.error(err);
doConnect();
}
});
if (!node.check) { node.check = setInterval(checkVer, 290000); }
}
});
}
this.connect = function() {
if (!this.connected && !this.connecting) {
doConnect();
}
}
this.on('close', function (done) {
if (this.tick) { clearTimeout(this.tick); }
if (this.check) { clearInterval(this.check); }
node.connected = false;
node.connection.release();
node.emit("state"," ");
node.pool.end(function (err) { done(); });
});
}
RED.nodes.registerType("MySQLdatabase",MySQLNode, {
credentials: {
user: {type: "text"},
password: {type: "password"}
}
});
function MysqlDBNodeIn(n) {
RED.nodes.createNode(this,n);
this.mydb = n.mydb;
this.mydbConfig = RED.nodes.getNode(this.mydb);
if (this.mydbConfig) {
this.mydbConfig.connect();
var node = this;
var busy = false;
var status = {};
node.mydbConfig.on("state", function(info) {
if (info === "connecting") { node.status({fill:"grey",shape:"ring",text:info}); }
else if (info === "connected") { node.status({fill:"green",shape:"dot",text:info}); }
else {
if (info === "ECONNREFUSED") { info = "connection refused"; }
if (info === "PROTOCOL_CONNECTION_LOST") { info = "connection lost"; }
node.status({fill:"red",shape:"ring",text:info});
}
});
node.on("input", function(msg) {
if (node.mydbConfig.connected) {
if (typeof msg.topic === 'string') {
var bind = Array.isArray(msg.payload) ? msg.payload : [];
node.mydbConfig.connection.query(msg.topic, bind, function(err, rows) {
if (err) {
status = {fill:"red",shape:"ring",text:"Error: "+err.code};
node.status(status);
node.error(err,msg);
}
else {
if (rows.constructor.name === "OkPacket") {
msg.payload = JSON.parse(JSON.stringify(rows));
}
else { msg.payload = rows; }
node.send(msg);
status = {fill:"green",shape:"dot",text:"OK"};
node.status(status);
}
});
}
else {
if (typeof msg.topic !== 'string') { node.error("msg.topic : the query is not defined as a string"); }
}
}
else {
node.error("Database not connected",msg);
status = {fill:"red",shape:"ring",text:"not yet connected"};
}
if (!busy) {
busy = true;
node.status(status);
node.tout = setTimeout(function() { busy = false; node.status(status); },500);
}
});
node.on('close', function () {
if (node.tout) { clearTimeout(node.tout); }
node.mydbConfig.removeAllListeners();
node.status({});
});
}
else {
this.error("MySQL database not configured");
}
}
RED.nodes.registerType("mysql",MysqlDBNodeIn);
}