2013-11-03 22:29:20 +01:00
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2016-09-14 23:44:53 +02:00
|
|
|
var reconnect = RED.settings.mysqlReconnectTime || 20000;
|
2014-06-29 00:37:19 +02:00
|
|
|
var mysqldb = require('mysql');
|
2014-02-23 19:32:49 +01:00
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
function MySQLNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.host = n.host;
|
|
|
|
this.port = n.port;
|
2014-07-04 18:26:20 +02:00
|
|
|
this.tz = n.tz || "local";
|
2014-06-29 00:37:19 +02:00
|
|
|
|
|
|
|
this.connected = false;
|
|
|
|
this.connecting = false;
|
|
|
|
|
|
|
|
this.dbname = n.db;
|
2016-11-27 18:03:03 +01:00
|
|
|
this.setMaxListeners(0);
|
2014-06-29 00:37:19 +02:00
|
|
|
var node = this;
|
|
|
|
|
|
|
|
function doConnect() {
|
|
|
|
node.connecting = true;
|
2016-11-27 18:03:03 +01:00
|
|
|
node.emit("state","connecting");
|
2014-06-29 00:37:19 +02:00
|
|
|
node.connection = mysqldb.createConnection({
|
|
|
|
host : node.host,
|
|
|
|
port : node.port,
|
2015-01-19 10:46:29 +01:00
|
|
|
user : node.credentials.user,
|
|
|
|
password : node.credentials.password,
|
2014-06-29 00:37:19 +02:00
|
|
|
database : node.dbname,
|
2014-07-04 18:26:20 +02:00
|
|
|
timezone : node.tz,
|
2016-07-20 21:11:50 +02:00
|
|
|
insecureAuth: true,
|
|
|
|
multipleStatements: true
|
2013-11-22 21:49:31 +01:00
|
|
|
});
|
2013-11-03 22:29:20 +01:00
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
node.connection.connect(function(err) {
|
|
|
|
node.connecting = false;
|
|
|
|
if (err) {
|
2015-11-01 16:04:42 +01:00
|
|
|
node.error(err);
|
2016-11-27 18:03:03 +01:00
|
|
|
node.emit("state",err.code);
|
2014-06-29 00:37:19 +02:00
|
|
|
node.tick = setTimeout(doConnect, reconnect);
|
|
|
|
} else {
|
|
|
|
node.connected = true;
|
2016-11-27 18:03:03 +01:00
|
|
|
node.emit("state","connected");
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
});
|
2013-11-03 22:29:20 +01:00
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
node.connection.on('error', function(err) {
|
|
|
|
node.connected = false;
|
2016-11-27 18:03:03 +01:00
|
|
|
node.emit("state",err.code);
|
2014-06-29 00:37:19 +02:00
|
|
|
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
|
|
|
|
doConnect(); // silently reconnect...
|
|
|
|
} else {
|
|
|
|
node.error(err);
|
|
|
|
doConnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-11-03 22:29:20 +01:00
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
this.connect = function() {
|
2016-11-27 20:21:57 +01:00
|
|
|
if (!this.connected && !this.connecting) { doConnect(); }
|
|
|
|
if (this.connected) { node.emit("state","connected"); }
|
|
|
|
else { node.emit("state","connecting"); }
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
|
2015-01-19 10:46:29 +01:00
|
|
|
this.on('close', function (done) {
|
2014-06-29 00:37:19 +02:00
|
|
|
if (this.tick) { clearTimeout(this.tick); }
|
2016-09-14 23:44:53 +02:00
|
|
|
node.connected = false;
|
2016-11-27 18:03:03 +01:00
|
|
|
node.emit("state"," ");
|
2014-06-29 00:37:19 +02:00
|
|
|
if (this.connection) {
|
|
|
|
node.connection.end(function(err) {
|
|
|
|
if (err) { node.error(err); }
|
2015-01-19 10:46:29 +01:00
|
|
|
done();
|
2014-06-29 00:37:19 +02:00
|
|
|
});
|
2015-01-19 10:46:29 +01:00
|
|
|
} else {
|
|
|
|
done();
|
2013-11-03 22:29:20 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-01-19 10:46:29 +01:00
|
|
|
RED.nodes.registerType("MySQLdatabase",MySQLNode, {
|
|
|
|
credentials: {
|
|
|
|
user: {type: "text"},
|
|
|
|
password: {type: "password"}
|
|
|
|
}
|
|
|
|
});
|
2014-06-29 00:37:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
2016-11-27 18:03:03 +01:00
|
|
|
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});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
node.on("input", function(msg) {
|
2016-09-14 23:44:53 +02:00
|
|
|
if (node.mydbConfig.connected) {
|
|
|
|
if (typeof msg.topic === 'string') {
|
|
|
|
//console.log("query:",msg.topic);
|
|
|
|
var bind = Array.isArray(msg.payload) ? msg.payload : [];
|
|
|
|
node.mydbConfig.connection.query(msg.topic, bind, 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"); }
|
|
|
|
}
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-09-14 23:44:53 +02:00
|
|
|
node.error("Database not connected",msg);
|
2016-11-27 18:03:03 +01:00
|
|
|
node.status({fill:"red",shape:"ring",text:"not yet connected"});
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
});
|
2016-11-27 20:21:57 +01:00
|
|
|
|
|
|
|
node.on('close', function () {
|
|
|
|
node.mydbConfig.removeAllListeners();
|
|
|
|
node.status({});
|
|
|
|
});
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.error("MySQL database not configured");
|
|
|
|
}
|
2013-11-03 22:29:20 +01:00
|
|
|
}
|
2014-06-29 00:37:19 +02:00
|
|
|
RED.nodes.registerType("mysql",MysqlDBNodeIn);
|
|
|
|
}
|