2014-07-04 17:12:00 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var reconnect = RED.settings.sqliteReconnectTime || 20000;
|
|
|
|
var sqlite3 = require('sqlite3');
|
|
|
|
|
|
|
|
function SqliteNodeDB(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
|
|
|
|
this.dbname = n.db;
|
2018-08-11 15:03:16 +02:00
|
|
|
this.mod = n.mode;
|
|
|
|
if (n.mode === "RWC") { this.mode = sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE; }
|
|
|
|
if (n.mode === "RW") { this.mode = sqlite3.OPEN_READWRITE; }
|
|
|
|
if (n.mode === "RO") { this.mode = sqlite3.OPEN_READONLY; }
|
2014-07-04 17:12:00 +02:00
|
|
|
var node = this;
|
|
|
|
|
|
|
|
node.doConnect = function() {
|
2018-08-11 15:03:16 +02:00
|
|
|
node.db = node.db || new sqlite3.Database(node.dbname,node.mode);
|
2015-04-16 11:58:34 +02:00
|
|
|
node.db.on('open', function() {
|
|
|
|
if (node.tick) { clearTimeout(node.tick); }
|
|
|
|
node.log("opened "+node.dbname+" ok");
|
2014-07-04 17:12:00 +02:00
|
|
|
});
|
|
|
|
node.db.on('error', function(err) {
|
2015-04-16 11:58:34 +02:00
|
|
|
node.error("failed to open "+node.dbname, err);
|
2015-03-18 09:47:09 +01:00
|
|
|
node.tick = setTimeout(function() { node.doConnect(); }, reconnect);
|
2014-07-04 17:12:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-30 12:17:15 +02:00
|
|
|
node.on('close', function (done) {
|
2014-07-04 17:12:00 +02:00
|
|
|
if (node.tick) { clearTimeout(node.tick); }
|
2018-05-30 12:17:15 +02:00
|
|
|
if (node.db) { node.db.close(done()); }
|
|
|
|
else { done(); }
|
2014-07-04 17:12:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("sqlitedb",SqliteNodeDB);
|
|
|
|
|
|
|
|
|
|
|
|
function SqliteNodeIn(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.mydb = n.mydb;
|
2018-03-20 20:04:23 +01:00
|
|
|
this.sqlquery = n.sqlquery||"msg.topic";
|
|
|
|
this.sql = n.sql;
|
2014-07-04 17:12:00 +02:00
|
|
|
this.mydbConfig = RED.nodes.getNode(this.mydb);
|
2018-03-20 20:04:23 +01:00
|
|
|
var node = this;
|
|
|
|
node.status({});
|
2014-07-04 17:12:00 +02:00
|
|
|
|
2018-08-22 14:58:55 +02:00
|
|
|
if (node.mydbConfig) {
|
|
|
|
node.mydbConfig.doConnect();
|
2018-08-11 15:03:16 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:this.mydbConfig.mod});
|
2018-03-20 20:04:23 +01:00
|
|
|
var bind = [];
|
2018-08-22 14:58:55 +02:00
|
|
|
|
|
|
|
var doQuery = function(msg) {
|
|
|
|
if (node.sqlquery == "msg.topic"){
|
2018-03-20 20:04:23 +01:00
|
|
|
if (typeof msg.topic === 'string') {
|
|
|
|
bind = Array.isArray(msg.payload) ? msg.payload : [];
|
|
|
|
node.mydbConfig.db.all(msg.topic, bind, function(err, row) {
|
|
|
|
if (err) { node.error(err,msg); }
|
|
|
|
else {
|
|
|
|
msg.payload = row;
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2018-07-19 13:33:43 +02:00
|
|
|
node.error("msg.topic : the query is not defined as a string",msg);
|
|
|
|
node.status({fill:"red",shape:"dot",text:"msg.topic error"});
|
|
|
|
}
|
|
|
|
}
|
2018-08-22 14:58:55 +02:00
|
|
|
if (node.sqlquery == "batch") {
|
2018-07-19 13:33:43 +02:00
|
|
|
if (typeof msg.topic === 'string') {
|
|
|
|
node.mydbConfig.db.exec(msg.topic, function(err) {
|
|
|
|
if (err) { node.error(err,msg);}
|
|
|
|
else {
|
|
|
|
msg.payload = [];
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.error("msg.topic : the query is not defined as string", msg);
|
|
|
|
node.status({fill:"red", shape:"dot",text:"msg.topic error"});
|
2018-03-20 20:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-22 14:58:55 +02:00
|
|
|
if (node.sqlquery == "fixed"){
|
|
|
|
if (typeof node.sql === 'string'){
|
2018-03-20 20:04:23 +01:00
|
|
|
bind = Array.isArray(msg.payload) ? msg.payload : [];
|
2018-08-22 14:58:55 +02:00
|
|
|
node.mydbConfig.db.all(node.sql, bind, function(err, row) {
|
2018-03-20 20:04:23 +01:00
|
|
|
if (err) { node.error(err,msg); }
|
|
|
|
else {
|
|
|
|
msg.payload = row;
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else{
|
2018-08-22 14:58:55 +02:00
|
|
|
if (node.sql === null || node.sql == ""){
|
2018-03-20 20:04:23 +01:00
|
|
|
node.error("SQL statement config not set up",msg);
|
|
|
|
node.status({fill:"red",shape:"dot",text:"SQL config not set up"});
|
2014-07-04 17:12:00 +02:00
|
|
|
}
|
2018-03-20 20:04:23 +01:00
|
|
|
}
|
2014-07-04 17:12:00 +02:00
|
|
|
}
|
2018-08-22 14:58:55 +02:00
|
|
|
if (node.sqlquery == "prepared"){
|
|
|
|
if (typeof node.sql === 'string' && typeof msg.params !== "undefined" && typeof msg.params === "object"){
|
|
|
|
node.mydbConfig.db.all(node.sql, msg.params, function(err, row) {
|
2018-03-20 20:04:23 +01:00
|
|
|
if (err) { node.error(err,msg); }
|
|
|
|
else {
|
|
|
|
msg.payload = row;
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else{
|
2018-08-22 14:58:55 +02:00
|
|
|
if (node.sql === null || node.sql == ""){
|
2018-03-20 20:04:23 +01:00
|
|
|
node.error("Prepared statement config not set up",msg);
|
|
|
|
node.status({fill:"red",shape:"dot",text:"Prepared statement not set up"});
|
|
|
|
}
|
|
|
|
if (typeof msg.params == "undefined"){
|
|
|
|
node.error("msg.params not passed");
|
2018-05-18 19:40:14 +02:00
|
|
|
node.status({fill:"red",shape:"dot",text:"msg.params not defined"});
|
2018-03-20 20:04:23 +01:00
|
|
|
}
|
|
|
|
else if (typeof msg.params != "object"){
|
|
|
|
node.error("msg.params not an object");
|
2018-05-18 19:40:14 +02:00
|
|
|
node.status({fill:"red",shape:"dot",text:"msg.params not an object"});
|
2018-03-20 20:04:23 +01:00
|
|
|
}
|
2014-07-04 17:12:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-22 14:58:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (msg.hasOwnProperty("extension")) {
|
|
|
|
node.mydbConfig.db.loadExtension(msg.extension, function(err) {
|
|
|
|
if (err) { node.error(err,msg); }
|
|
|
|
else { doQuery(msg); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else { doQuery(msg); }
|
2014-07-04 17:12:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2018-08-22 14:58:55 +02:00
|
|
|
node.error("Sqlite database not configured");
|
2014-07-04 17:12:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("sqlite",SqliteNodeIn);
|
|
|
|
}
|