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;
|
2022-01-05 11:56:14 +01:00
|
|
|
var mysqldb = require('mysql2');
|
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";
|
2020-04-18 19:03:59 +02:00
|
|
|
this.charset = (n.charset || "UTF8_GENERAL_CI").toUpperCase();
|
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;
|
|
|
|
|
2017-02-26 11:18:07 +01:00
|
|
|
function checkVer() {
|
2021-03-12 22:01:54 +01:00
|
|
|
node.pool.query("SELECT version();", [], function(err, rows, fields) {
|
2017-02-26 11:18:07 +01:00
|
|
|
if (err) {
|
2017-03-17 14:45:22 +01:00
|
|
|
node.error(err);
|
2021-10-27 14:42:27 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:RED._("mysql.status.badping")});
|
2017-02-26 11:18:07 +01:00
|
|
|
doConnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
function doConnect() {
|
|
|
|
node.connecting = true;
|
2016-11-27 18:03:03 +01:00
|
|
|
node.emit("state","connecting");
|
2017-02-26 11:18:07 +01:00
|
|
|
if (!node.pool) {
|
|
|
|
node.pool = mysqldb.createPool({
|
|
|
|
host : node.host,
|
|
|
|
port : node.port,
|
|
|
|
user : node.credentials.user,
|
|
|
|
password : node.credentials.password,
|
|
|
|
database : node.dbname,
|
|
|
|
timezone : node.tz,
|
|
|
|
insecureAuth: true,
|
|
|
|
multipleStatements: true,
|
2022-01-05 11:56:14 +01:00
|
|
|
connectionLimit: RED.settings.mysqlConnectionLimit || 50,
|
2021-03-16 09:24:06 +01:00
|
|
|
connectTimeout: 30000,
|
2022-01-05 11:56:14 +01:00
|
|
|
charset: node.charset,
|
|
|
|
decimalNumbers: true
|
2017-02-26 11:18:07 +01:00
|
|
|
});
|
|
|
|
}
|
2013-11-03 22:29:20 +01:00
|
|
|
|
2021-05-18 10:09:33 +02:00
|
|
|
// connection test
|
2017-02-26 11:18:07 +01:00
|
|
|
node.pool.getConnection(function(err, connection) {
|
2014-06-29 00:37:19 +02:00
|
|
|
node.connecting = false;
|
|
|
|
if (err) {
|
2016-11-27 18:03:03 +01:00
|
|
|
node.emit("state",err.code);
|
2017-02-26 11:18:07 +01:00
|
|
|
node.error(err);
|
2014-06-29 00:37:19 +02:00
|
|
|
node.tick = setTimeout(doConnect, reconnect);
|
2017-02-26 11:18:07 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-06-29 00:37:19 +02:00
|
|
|
node.connected = true;
|
2016-11-27 18:03:03 +01:00
|
|
|
node.emit("state","connected");
|
2017-02-26 11:18:07 +01:00
|
|
|
if (!node.check) { node.check = setInterval(checkVer, 290000); }
|
2021-05-18 10:09:33 +02:00
|
|
|
connection.release();
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-11-03 22:29:20 +01:00
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
this.connect = function() {
|
2017-02-26 11:18:07 +01:00
|
|
|
if (!this.connected && !this.connecting) {
|
|
|
|
doConnect();
|
|
|
|
}
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 22:01:54 +01:00
|
|
|
this.on('close', function(done) {
|
2014-06-29 00:37:19 +02:00
|
|
|
if (this.tick) { clearTimeout(this.tick); }
|
2017-02-26 11:18:07 +01:00
|
|
|
if (this.check) { clearInterval(this.check); }
|
2016-09-14 23:44:53 +02:00
|
|
|
node.connected = false;
|
2021-03-13 14:59:00 +01:00
|
|
|
// node.connection.release();
|
2016-11-27 18:03:03 +01:00
|
|
|
node.emit("state"," ");
|
2021-03-12 22:01:54 +01:00
|
|
|
node.pool.end(function(err) { 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);
|
2020-04-05 13:35:15 +02:00
|
|
|
this.status({});
|
2014-06-29 00:37:19 +02:00
|
|
|
|
|
|
|
if (this.mydbConfig) {
|
|
|
|
this.mydbConfig.connect();
|
|
|
|
var node = this;
|
2019-02-01 23:15:22 +01:00
|
|
|
var busy = false;
|
|
|
|
var status = {};
|
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 {
|
|
|
|
node.status({fill:"red",shape:"ring",text:info});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-08 10:11:59 +02:00
|
|
|
node.on("input", function(msg, send, done) {
|
2021-04-08 18:07:23 +02:00
|
|
|
send = send || function() { node.send.apply(node,arguments) };
|
2016-09-14 23:44:53 +02:00
|
|
|
if (node.mydbConfig.connected) {
|
|
|
|
if (typeof msg.topic === 'string') {
|
2020-04-10 23:53:17 +02:00
|
|
|
//console.log("query:",msg.topic);
|
2022-01-05 11:56:14 +01:00
|
|
|
node.mydbConfig.pool.getConnection(function (err, conn) {
|
2016-09-14 23:44:53 +02:00
|
|
|
if (err) {
|
2022-02-21 21:08:33 +01:00
|
|
|
if (conn) {
|
|
|
|
conn.release()
|
|
|
|
}
|
2022-01-05 11:56:14 +01:00
|
|
|
status = { fill: "red", shape: "ring", text: RED._("mysql.status.error") + ": " + err.code };
|
2020-04-05 12:44:27 +02:00
|
|
|
node.status(status);
|
2022-01-05 11:56:14 +01:00
|
|
|
node.error(err, msg);
|
|
|
|
if (done) { done(); }
|
|
|
|
return
|
2016-09-14 23:44:53 +02:00
|
|
|
}
|
2022-01-05 11:56:14 +01:00
|
|
|
|
|
|
|
var bind = [];
|
|
|
|
if (Array.isArray(msg.payload)) {
|
|
|
|
bind = msg.payload;
|
|
|
|
}
|
|
|
|
else if (typeof msg.payload === 'object' && msg.payload !== null) {
|
|
|
|
bind = msg.payload;
|
2016-09-14 23:44:53 +02:00
|
|
|
}
|
2022-01-05 11:56:14 +01:00
|
|
|
conn.config.queryFormat = Array.isArray(msg.payload) ? null : customQueryFormat
|
|
|
|
conn.query(msg.topic, bind, function (err, rows) {
|
|
|
|
conn.release()
|
|
|
|
if (err) {
|
|
|
|
status = { fill: "red", shape: "ring", text: RED._("mysql.status.error") + ": " + err.code };
|
|
|
|
node.status(status);
|
|
|
|
node.error(err, msg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg.payload = rows;
|
|
|
|
send(msg);
|
|
|
|
status = { fill: "green", shape: "dot", text: RED._("mysql.status.ok") };
|
|
|
|
node.status(status);
|
|
|
|
}
|
|
|
|
if (done) { done(); }
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2016-09-14 23:44:53 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-10-27 14:42:27 +02:00
|
|
|
if (typeof msg.topic !== 'string') { node.error("msg.topic : "+RED._("mysql.errors.notstring")); done(); }
|
2016-09-14 23:44:53 +02:00
|
|
|
}
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-10-27 14:42:27 +02:00
|
|
|
node.error(RED._("mysql.errors.notconnected"),msg);
|
|
|
|
status = {fill:"red",shape:"ring",text:RED._("mysql.status.notconnected")};
|
2021-06-19 15:47:44 +02:00
|
|
|
if (done) { done(); }
|
2019-02-01 23:15:22 +01:00
|
|
|
}
|
|
|
|
if (!busy) {
|
|
|
|
busy = true;
|
|
|
|
node.status(status);
|
|
|
|
node.tout = setTimeout(function() { busy = false; node.status(status); },500);
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
});
|
2016-11-27 20:21:57 +01:00
|
|
|
|
2021-03-12 22:01:54 +01:00
|
|
|
node.on('close', function() {
|
2019-02-01 23:15:22 +01:00
|
|
|
if (node.tout) { clearTimeout(node.tout); }
|
2016-11-27 20:21:57 +01:00
|
|
|
node.mydbConfig.removeAllListeners();
|
|
|
|
node.status({});
|
|
|
|
});
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-10-27 14:42:27 +02:00
|
|
|
this.error(RED._("mysql.errors.notconfigured"));
|
2014-06-29 00:37:19 +02:00
|
|
|
}
|
2013-11-03 22:29:20 +01:00
|
|
|
}
|
2014-06-29 00:37:19 +02:00
|
|
|
RED.nodes.registerType("mysql",MysqlDBNodeIn);
|
|
|
|
}
|
2022-01-05 11:56:14 +01:00
|
|
|
|
|
|
|
function customQueryFormat(query, values) {
|
|
|
|
if (!values) {
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
return query.replace(/\:(\w+)/g, function(txt, key) {
|
|
|
|
if (values.hasOwnProperty(key)) {
|
|
|
|
return this.escape(values[key]);
|
|
|
|
}
|
|
|
|
return txt;
|
|
|
|
}.bind(this));
|
|
|
|
}
|