mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
afec508980
also to close #172 - as multiple statements now also supported
137 lines
4.5 KiB
JavaScript
137 lines
4.5 KiB
JavaScript
/**
|
|
* Copyright 2013 IBM Corp.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
**/
|
|
|
|
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;
|
|
var node = this;
|
|
|
|
function doConnect() {
|
|
node.connecting = true;
|
|
node.connection = mysqldb.createConnection({
|
|
host : node.host,
|
|
port : node.port,
|
|
user : node.credentials.user,
|
|
password : node.credentials.password,
|
|
database : node.dbname,
|
|
timezone : node.tz,
|
|
insecureAuth: true,
|
|
multipleStatements: true
|
|
});
|
|
|
|
node.connection.connect(function(err) {
|
|
node.connecting = false;
|
|
if (err) {
|
|
node.error(err);
|
|
node.tick = setTimeout(doConnect, reconnect);
|
|
} else {
|
|
node.connected = true;
|
|
}
|
|
});
|
|
|
|
node.connection.on('error', function(err) {
|
|
node.connected = false;
|
|
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
|
|
doConnect(); // silently reconnect...
|
|
} else {
|
|
node.error(err);
|
|
doConnect();
|
|
}
|
|
});
|
|
}
|
|
|
|
this.connect = function() {
|
|
if (!this.connected && !this.connecting) {
|
|
doConnect();
|
|
}
|
|
}
|
|
|
|
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); }
|
|
done();
|
|
});
|
|
} else {
|
|
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;
|
|
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);
|
|
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 {
|
|
this.error("MySQL database not configured");
|
|
}
|
|
}
|
|
RED.nodes.registerType("mysql",MysqlDBNodeIn);
|
|
}
|