mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Catch "not connected" to database in Mysql node
also to close #172 - as multiple statements now also supported
This commit is contained in:
parent
f70cd1d640
commit
afec508980
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var reconnect = RED.settings.mysqlReconnectTime || 30000;
|
var reconnect = RED.settings.mysqlReconnectTime || 20000;
|
||||||
var mysqldb = require('mysql');
|
var mysqldb = require('mysql');
|
||||||
|
|
||||||
function MySQLNode(n) {
|
function MySQLNode(n) {
|
||||||
@ -73,6 +73,7 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
this.on('close', function (done) {
|
this.on('close', function (done) {
|
||||||
if (this.tick) { clearTimeout(this.tick); }
|
if (this.tick) { clearTimeout(this.tick); }
|
||||||
|
node.connected = false;
|
||||||
if (this.connection) {
|
if (this.connection) {
|
||||||
node.connection.end(function(err) {
|
node.connection.end(function(err) {
|
||||||
if (err) { node.error(err); }
|
if (err) { node.error(err); }
|
||||||
@ -100,19 +101,30 @@ module.exports = function(RED) {
|
|||||||
this.mydbConfig.connect();
|
this.mydbConfig.connect();
|
||||||
var node = this;
|
var node = this;
|
||||||
node.on("input", function(msg) {
|
node.on("input", function(msg) {
|
||||||
if (typeof msg.topic === 'string') {
|
if (node.mydbConfig.connected) {
|
||||||
//console.log("query:",msg.topic);
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
||||||
var bind = Array.isArray(msg.payload) ? msg.payload : [];
|
if (typeof msg.topic === 'string') {
|
||||||
node.mydbConfig.connection.query(msg.topic, bind, function(err, rows) {
|
//console.log("query:",msg.topic);
|
||||||
if (err) { node.error(err,msg); }
|
var bind = Array.isArray(msg.payload) ? msg.payload : [];
|
||||||
else {
|
node.mydbConfig.connection.query(msg.topic, bind, function(err, rows) {
|
||||||
msg.payload = rows;
|
if (err) {
|
||||||
node.send(msg);
|
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 {
|
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"});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name" : "node-red-node-mysql",
|
"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",
|
"description" : "A Node-RED node to read and write to a MySQL database",
|
||||||
"dependencies" : {
|
"dependencies" : {
|
||||||
"mysql" : "2.11.1"
|
"mysql" : "2.11.1"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user