Catch "not connected" to database in Mysql node

also to close #172 - as multiple statements now also supported
This commit is contained in:
Dave Conway-Jones 2016-09-14 22:44:53 +01:00
parent f70cd1d640
commit afec508980
2 changed files with 25 additions and 13 deletions

View File

@ -16,7 +16,7 @@
module.exports = function(RED) {
"use strict";
var reconnect = RED.settings.mysqlReconnectTime || 30000;
var reconnect = RED.settings.mysqlReconnectTime || 20000;
var mysqldb = require('mysql');
function MySQLNode(n) {
@ -73,6 +73,7 @@ module.exports = function(RED) {
this.on('close', function (done) {
if (this.tick) { clearTimeout(this.tick); }
node.connected = false;
if (this.connection) {
node.connection.end(function(err) {
if (err) { node.error(err); }
@ -100,19 +101,30 @@ module.exports = function(RED) {
this.mydbConfig.connect();
var node = this;
node.on("input", function(msg) {
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); }
else {
msg.payload = rows;
node.send(msg);
}
});
if (node.mydbConfig.connected) {
node.status({fill:"green",shape:"dot",text:"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"); }
}
}
else {
if (typeof msg.topic !== 'string') { node.error("msg.topic : the query is not defined as a string"); }
node.error("Database not connected",msg);
node.status({fill:"grey",shape:"ring",text:"Not connected"});
}
});
}

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-mysql",
"version" : "0.0.9",
"version" : "0.0.10",
"description" : "A Node-RED node to read and write to a MySQL database",
"dependencies" : {
"mysql" : "2.11.1"