1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

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,20 +101,31 @@ module.exports = function(RED) {
this.mydbConfig.connect();
var node = this;
node.on("input", function(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); }
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 {
node.error("Database not connected",msg);
node.status({fill:"grey",shape:"ring",text:"Not connected"});
}
});
}
else {

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"