From babff3ff269f2eb34c1518d867d738045304dc0b Mon Sep 17 00:00:00 2001 From: atsage <30732752+atsage@users.noreply.github.com> Date: Tue, 20 Mar 2018 15:04:23 -0400 Subject: [PATCH] Added a few SQL modes (#360) --- storage/sqlite/sqlite.html | 94 +++++++++++++++++++++++++++++++++++--- storage/sqlite/sqlite.js | 76 ++++++++++++++++++++++++------ 2 files changed, 149 insertions(+), 21 deletions(-) diff --git a/storage/sqlite/sqlite.html b/storage/sqlite/sqlite.html index e091023f..a38a0519 100644 --- a/storage/sqlite/sqlite.html +++ b/storage/sqlite/sqlite.html @@ -20,22 +20,49 @@ diff --git a/storage/sqlite/sqlite.js b/storage/sqlite/sqlite.js index 41936fd5..e210bbcb 100644 --- a/storage/sqlite/sqlite.js +++ b/storage/sqlite/sqlite.js @@ -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}); + } } } });