2013-11-03 22:29:20 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
2013-11-22 11:24:08 +01:00
|
|
|
var reconnect = RED.settings.mysqlReconnectTime || 30000;
|
2013-11-03 22:29:20 +01:00
|
|
|
var mysqldb = require('mysql');
|
2013-11-22 21:34:57 +01:00
|
|
|
var querystring = require('querystring');
|
|
|
|
|
2014-02-23 19:32:49 +01:00
|
|
|
RED.httpAdmin.get('/MySQLdatabase/:id',function(req,res) {
|
2013-11-22 21:34:57 +01:00
|
|
|
var credentials = RED.nodes.getCredentials(req.params.id);
|
|
|
|
if (credentials) {
|
|
|
|
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!="")}));
|
|
|
|
} else {
|
|
|
|
res.send(JSON.stringify({}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-02-23 19:32:49 +01:00
|
|
|
RED.httpAdmin.delete('/MySQLdatabase/:id',function(req,res) {
|
2013-11-22 21:34:57 +01:00
|
|
|
RED.nodes.deleteCredentials(req.params.id);
|
|
|
|
res.send(200);
|
|
|
|
});
|
|
|
|
|
2014-02-23 19:32:49 +01:00
|
|
|
RED.httpAdmin.post('/MySQLdatabase/:id',function(req,res) {
|
2013-11-22 21:34:57 +01:00
|
|
|
var body = "";
|
|
|
|
req.on('data', function(chunk) {
|
|
|
|
body+=chunk;
|
|
|
|
});
|
|
|
|
req.on('end', function(){
|
|
|
|
var newCreds = querystring.parse(body);
|
|
|
|
var credentials = RED.nodes.getCredentials(req.params.id)||{};
|
|
|
|
if (newCreds.user == null || newCreds.user == "") {
|
|
|
|
delete credentials.user;
|
|
|
|
} else {
|
|
|
|
credentials.user = newCreds.user;
|
|
|
|
}
|
|
|
|
if (newCreds.password == "") {
|
|
|
|
delete credentials.password;
|
|
|
|
} else {
|
|
|
|
credentials.password = newCreds.password||credentials.password;
|
|
|
|
}
|
|
|
|
RED.nodes.addCredentials(req.params.id,credentials);
|
|
|
|
res.send(200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-03 22:29:20 +01:00
|
|
|
|
|
|
|
function MySQLNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.host = n.host;
|
|
|
|
this.port = n.port;
|
2014-02-23 19:32:49 +01:00
|
|
|
|
2013-11-22 21:49:31 +01:00
|
|
|
this.connected = false;
|
|
|
|
this.connecting = false;
|
2014-02-23 19:32:49 +01:00
|
|
|
|
2013-11-22 21:34:57 +01:00
|
|
|
if (n.user) {
|
|
|
|
var credentials = {};
|
|
|
|
credentials.user = n.user;
|
|
|
|
credentials.password = n.pass;
|
|
|
|
RED.nodes.addCredentials(n.id,credentials);
|
|
|
|
this.user = n.user;
|
|
|
|
this.password = n.pass;
|
|
|
|
} else {
|
|
|
|
var credentials = RED.nodes.getCredentials(n.id);
|
|
|
|
if (credentials) {
|
|
|
|
this.user = credentials.user;
|
|
|
|
this.password = credentials.password;
|
|
|
|
}
|
|
|
|
}
|
2014-02-23 19:32:49 +01:00
|
|
|
|
2013-11-03 22:29:20 +01:00
|
|
|
this.dbname = n.db;
|
|
|
|
var node = this;
|
|
|
|
|
2013-11-22 11:24:08 +01:00
|
|
|
function doConnect() {
|
2013-11-22 21:49:31 +01:00
|
|
|
node.connecting = true;
|
2013-11-22 11:24:08 +01:00
|
|
|
node.connection = mysqldb.createConnection({
|
|
|
|
host : node.host,
|
|
|
|
port : node.port,
|
|
|
|
user : node.user,
|
|
|
|
password : node.password,
|
|
|
|
database : node.dbname,
|
|
|
|
insecureAuth: true
|
|
|
|
});
|
2013-11-03 22:29:20 +01:00
|
|
|
|
2013-11-22 11:24:08 +01:00
|
|
|
node.connection.connect(function(err) {
|
2013-11-22 21:49:31 +01:00
|
|
|
node.connecting = false;
|
2013-11-22 11:24:08 +01:00
|
|
|
if (err) {
|
2013-11-22 21:49:31 +01:00
|
|
|
node.warn(err);
|
2013-11-22 11:24:08 +01:00
|
|
|
node.tick = setTimeout(doConnect, reconnect);
|
2013-11-22 21:49:31 +01:00
|
|
|
} else {
|
|
|
|
node.connected = true;
|
2013-11-22 11:24:08 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
node.connection.on('error', function(err) {
|
2013-11-22 21:49:31 +01:00
|
|
|
node.connected = false;
|
2013-11-22 11:24:08 +01:00
|
|
|
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
|
|
|
|
doConnect(); // silently reconnect...
|
|
|
|
} else {
|
|
|
|
node.error(err);
|
|
|
|
doConnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-02-23 19:32:49 +01:00
|
|
|
|
2013-11-22 21:49:31 +01:00
|
|
|
this.connect = function() {
|
|
|
|
if (!this.connected && !this.connecting) {
|
|
|
|
doConnect();
|
|
|
|
}
|
|
|
|
}
|
2014-02-23 19:32:49 +01:00
|
|
|
|
2013-11-22 21:49:31 +01:00
|
|
|
this.on('close', function () {
|
|
|
|
if (this.tick) { clearTimeout(this.tick); }
|
|
|
|
if (this.connection) {
|
|
|
|
node.connection.end(function(err) {
|
|
|
|
if (err) node.error(err);
|
|
|
|
});
|
|
|
|
}
|
2013-11-03 22:29:20 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("MySQLdatabase",MySQLNode);
|
|
|
|
|
|
|
|
|
|
|
|
function MysqlDBNodeIn(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.mydb = n.mydb;
|
|
|
|
this.mydbConfig = RED.nodes.getNode(this.mydb);
|
|
|
|
|
|
|
|
if (this.mydbConfig) {
|
2013-11-22 21:49:31 +01:00
|
|
|
this.mydbConfig.connect();
|
2013-11-03 22:29:20 +01:00
|
|
|
var node = this;
|
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (typeof msg.topic === 'string') {
|
|
|
|
//console.log("query:",msg.topic);
|
|
|
|
node.mydbConfig.connection.query(msg.topic, function(err, rows) {
|
|
|
|
if (err) { node.warn(err); }
|
|
|
|
else {
|
|
|
|
msg.payload = rows;
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (typeof msg.topic !== 'string') node.error("msg.topic : the query is not defined as a string");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.error("MySQL database not configured");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("mysql",MysqlDBNodeIn);
|
|
|
|
|
|
|
|
|
|
|
|
//function MysqlDBNodeOut(n) {
|
|
|
|
//RED.nodes.createNode(this,n);
|
|
|
|
//this.level = n.level;
|
|
|
|
//this.operation = n.operation;
|
|
|
|
//this.levelConfig = RED.nodes.getNode(this.level);
|
|
|
|
|
|
|
|
//if (this.levelConfig) {
|
|
|
|
//var node = this;
|
|
|
|
//node.on("input", function(msg) {
|
|
|
|
//if (typeof msg.topic === 'string') {
|
|
|
|
//if (node.operation === "delete") {
|
|
|
|
//node.levelConfig.db.del(msg.topic);
|
|
|
|
//}
|
|
|
|
//else {
|
|
|
|
//node.levelConfig.db.put(msg.topic, msg.payload, function(err) {
|
|
|
|
//if (err) node.error(err);
|
|
|
|
//});
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
//else {
|
|
|
|
//if (typeof msg.topic !== 'string') node.error("msg.topic : the key is not defined");
|
|
|
|
//}
|
|
|
|
//});
|
|
|
|
//}
|
|
|
|
//else {
|
|
|
|
//this.error("MySQL database not configured");
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
//RED.nodes.registerType("mysql out",MysqlDBNodeOut);
|