Added a few SQL modes (#360)

This commit is contained in:
atsage
2018-03-20 15:04:23 -04:00
committed by Dave Conway-Jones
parent b9da5d87d8
commit babff3ff26
2 changed files with 149 additions and 21 deletions

View File

@@ -1,4 +1,3 @@
module.exports = function(RED) {
"use strict";
var reconnect = RED.settings.sqliteReconnectTime || 20000;
@@ -33,26 +32,75 @@ module.exports = function(RED) {
function SqliteNodeIn(n) {
RED.nodes.createNode(this,n);
this.mydb = n.mydb;
this.sqlquery = n.sqlquery||"msg.topic";
this.sql = n.sql;
this.mydbConfig = RED.nodes.getNode(this.mydb);
var node = this;
node.status({});
if (this.mydbConfig) {
this.mydbConfig.doConnect();
var node = this;
var bind = [];
node.on("input", function(msg) {
if (typeof msg.topic === 'string') {
//console.log("query:",msg.topic);
var 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);
if (this.sqlquery == "msg.topic"){
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 {
if (typeof msg.topic !== 'string') {
node.error("msg.topic : the query is not defined as a string",msg);
node.status({fill:"red",shape:"dot",text:"msg.topic error"});
}
});
}
}
else {
if (typeof msg.topic !== 'string') {
node.error("msg.topic : the query is not defined as a string",msg);
if (this.sqlquery == "fixed"){
if (typeof this.sql === 'string'){
bind = Array.isArray(msg.payload) ? msg.payload : [];
node.mydbConfig.db.all(this.sql, bind, function(err, row) {
if (err) { node.error(err,msg); }
else {
msg.payload = row;
node.send(msg);
}
});
}
else{
if (this.sql === null || this.sql == ""){
node.error("SQL statement config not set up",msg);
node.status({fill:"red",shape:"dot",text:"SQL config not set up"});
}
}
}
if (this.sqlquery == "prepared"){
if (typeof this.sql === 'string' && typeof msg.params !== "undefined" && typeof msg.params === "object"){
node.mydbConfig.db.all(this.sql, msg.params, function(err, row) {
if (err) { node.error(err,msg); }
else {
msg.payload = row;
node.send(msg);
}
});
}
else{
if (this.sql === null || this.sql == ""){
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");
node.status({fill:"red",shape:"dot",text:"msg.params not passed",msg});
}
else if (typeof msg.params != "object"){
node.error("msg.params not an object");
node.status({fill:"red",shape:"dot",text:"msg.params not an object",msg});
}
}
}
});