From 84c402e8977155cdb4bb7b84d99b70487f62bf69 Mon Sep 17 00:00:00 2001 From: atsage Date: Fri, 29 Sep 2017 15:18:07 -0400 Subject: [PATCH 01/13] First commit --- storage/sqlite/sqlite.html | 92 +++++++++++++++++++++++++++++++++++--- storage/sqlite/sqlite.js | 75 ++++++++++++++++++++++++++----- 2 files changed, 149 insertions(+), 18 deletions(-) diff --git a/storage/sqlite/sqlite.html b/storage/sqlite/sqlite.html index e091023f1..9ee71f5f1 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 41936fd5f..894b1e380 100644 --- a/storage/sqlite/sqlite.js +++ b/storage/sqlite/sqlite.js @@ -33,28 +33,79 @@ module.exports = function(RED) { function SqliteNodeIn(n) { RED.nodes.createNode(this,n); this.mydb = n.mydb; + this.sqltype = n.sqltype; + this.sql = n.sql; this.mydbConfig = RED.nodes.getNode(this.mydb); if (this.mydbConfig) { this.mydbConfig.doConnect(); var node = this; 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.sqltype == "msg.topic"){ + 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); + } + }); + } + 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.sqltype == "normal"){ + if (typeof this.sql === 'string'){ + var 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.sqltype == "prepared"){ + if (typeof this.sql === 'string' && typeof msg.params !== "undefined" && typeof msg.params === "object"){ + node.mydbConfig.db.run(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}); + } + } + } + }); + this.on('close',function(msg){ + node.status({}); }); } else { From 14f8871a99fcdfb63d4c632986a860d69ce08ebd Mon Sep 17 00:00:00 2001 From: atsage Date: Tue, 3 Oct 2017 15:24:58 -0400 Subject: [PATCH 02/13] fix some errors --- storage/sqlite/sqlite.html | 4 ++-- storage/sqlite/sqlite.js | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/storage/sqlite/sqlite.html b/storage/sqlite/sqlite.html index 9ee71f5f1..3d68790bd 100644 --- a/storage/sqlite/sqlite.html +++ b/storage/sqlite/sqlite.html @@ -90,7 +90,7 @@ }, oneditprepare: function() { var ace = this; - this.editor = RED.editor.createEditor({ + this.editor = RED.editor.createEditor({ id: 'node-input-sql-editor', mode: 'ace/mode/sql', value: $("#node-input-sql").val(), @@ -124,7 +124,7 @@ $("#node-input-sqltype").change(); }, oneditsave: function() { - $("#node-input-sql").val(this.editor.getValue()); + $("#node-input-sql").val(this.editor.getValue()); this.editor.destroy(); delete this.editor; }, diff --git a/storage/sqlite/sqlite.js b/storage/sqlite/sqlite.js index 894b1e380..802faf00c 100644 --- a/storage/sqlite/sqlite.js +++ b/storage/sqlite/sqlite.js @@ -36,15 +36,17 @@ module.exports = function(RED) { this.sqltype = n.sqltype; this.sql = n.sql; this.mydbConfig = RED.nodes.getNode(this.mydb); + node.status({}); if (this.mydbConfig) { this.mydbConfig.doConnect(); var node = this; + var bind = []; node.on("input", function(msg) { if (this.sqltype == "msg.topic"){ if (typeof msg.topic === 'string') { //console.log("query:",msg.topic); - var bind = Array.isArray(msg.payload) ? msg.payload : []; + bind = Array.isArray(msg.payload) ? msg.payload : []; node.mydbConfig.db.all(msg.topic, bind, function(err, row) { if (err) { node.error(err,msg); } else { @@ -62,7 +64,7 @@ module.exports = function(RED) { } if (this.sqltype == "normal"){ if (typeof this.sql === 'string'){ - var bind = Array.isArray(msg.payload) ? msg.payload : []; + bind = Array.isArray(msg.payload) ? msg.payload : []; node.mydbConfig.db.all(this.sql, bind, function(err, row) { if (err) { node.error(err,msg); } else { @@ -104,9 +106,9 @@ module.exports = function(RED) { } } }); - this.on('close',function(msg){ + /*this.on('close',function(msg){ node.status({}); - }); + });*/ } else { this.error("Sqlite database not configured"); From a3bbd0f92de9cec98208d44001a4a892cbc394c1 Mon Sep 17 00:00:00 2001 From: atsage Date: Tue, 3 Oct 2017 15:45:37 -0400 Subject: [PATCH 03/13] fix some errors --- storage/sqlite/sqlite.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/sqlite/sqlite.js b/storage/sqlite/sqlite.js index 802faf00c..0448640f8 100644 --- a/storage/sqlite/sqlite.js +++ b/storage/sqlite/sqlite.js @@ -36,11 +36,11 @@ module.exports = function(RED) { this.sqltype = n.sqltype; 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 (this.sqltype == "msg.topic"){ From 2b8f7644639130506f5df19827eddd271e21db2f Mon Sep 17 00:00:00 2001 From: atsage <30732752+atsage@users.noreply.github.com> Date: Thu, 1 Mar 2018 14:44:40 -0500 Subject: [PATCH 04/13] Add backwards compatibility --- storage/sqlite/sqlite.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/storage/sqlite/sqlite.js b/storage/sqlite/sqlite.js index 0448640f8..19d9fb46a 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; @@ -36,16 +35,19 @@ module.exports = function(RED) { this.sqltype = n.sqltype; this.sql = n.sql; this.mydbConfig = RED.nodes.getNode(this.mydb); - var node = this; - node.status({}); + //default to msg.topic for backwards compatibility + if (this.sqltype === null or this.sqltype == ""){ + this.sqltype = "msg.topic"; + } + var node = this; + node.status({}); if (this.mydbConfig) { this.mydbConfig.doConnect(); - var bind = []; + var bind = []; node.on("input", function(msg) { if (this.sqltype == "msg.topic"){ if (typeof msg.topic === 'string') { - //console.log("query:",msg.topic); bind = Array.isArray(msg.payload) ? msg.payload : []; node.mydbConfig.db.all(msg.topic, bind, function(err, row) { if (err) { node.error(err,msg); } @@ -74,7 +76,7 @@ module.exports = function(RED) { }); } else{ - if (this.sql == null || this.sql == ""){ + 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"}); } @@ -82,7 +84,7 @@ module.exports = function(RED) { } if (this.sqltype == "prepared"){ if (typeof this.sql === 'string' && typeof msg.params !== "undefined" && typeof msg.params === "object"){ - node.mydbConfig.db.run(this.sql, msg.params, function(err, row) { + node.mydbConfig.db.all(this.sql, msg.params, function(err, row) { if (err) { node.error(err,msg); } else { msg.payload = row; @@ -91,7 +93,7 @@ module.exports = function(RED) { }); } else{ - if (this.sql == null || this.sql == ""){ + 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"}); } @@ -106,9 +108,6 @@ module.exports = function(RED) { } } }); - /*this.on('close',function(msg){ - node.status({}); - });*/ } else { this.error("Sqlite database not configured"); From ef968fe3f448ac4a379330753bbf9e21bf8bdcbe Mon Sep 17 00:00:00 2001 From: atsage <30732752+atsage@users.noreply.github.com> Date: Thu, 1 Mar 2018 14:46:51 -0500 Subject: [PATCH 05/13] Replace tabs with spaces --- storage/sqlite/sqlite.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/storage/sqlite/sqlite.html b/storage/sqlite/sqlite.html index 3d68790bd..72dac8789 100644 --- a/storage/sqlite/sqlite.html +++ b/storage/sqlite/sqlite.html @@ -39,7 +39,7 @@
-
+
@@ -123,7 +123,7 @@ }); $("#node-input-sqltype").change(); }, - oneditsave: function() { + oneditsave: function() { $("#node-input-sql").val(this.editor.getValue()); this.editor.destroy(); delete this.editor; From 4850b88ae28de99f72a846f8f1bc05b55ce45f1c Mon Sep 17 00:00:00 2001 From: atsage <30732752+atsage@users.noreply.github.com> Date: Thu, 1 Mar 2018 15:01:48 -0500 Subject: [PATCH 06/13] Correct or in if statement --- storage/sqlite/sqlite.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/sqlite/sqlite.js b/storage/sqlite/sqlite.js index 19d9fb46a..60d689d33 100644 --- a/storage/sqlite/sqlite.js +++ b/storage/sqlite/sqlite.js @@ -36,7 +36,7 @@ module.exports = function(RED) { this.sql = n.sql; this.mydbConfig = RED.nodes.getNode(this.mydb); //default to msg.topic for backwards compatibility - if (this.sqltype === null or this.sqltype == ""){ + if (this.sqltype === null || this.sqltype == ""){ this.sqltype = "msg.topic"; } var node = this; From 4636273fe9c0d95744629e2e177873c9a217d676 Mon Sep 17 00:00:00 2001 From: atsage <30732752+atsage@users.noreply.github.com> Date: Thu, 1 Mar 2018 15:39:10 -0500 Subject: [PATCH 07/13] Update sqlite.js --- storage/sqlite/sqlite.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/sqlite/sqlite.js b/storage/sqlite/sqlite.js index 60d689d33..14f1d709c 100644 --- a/storage/sqlite/sqlite.js +++ b/storage/sqlite/sqlite.js @@ -36,7 +36,7 @@ module.exports = function(RED) { this.sql = n.sql; this.mydbConfig = RED.nodes.getNode(this.mydb); //default to msg.topic for backwards compatibility - if (this.sqltype === null || this.sqltype == ""){ + if (typeof this.sqltype == "undefined" || this.sqltype === null || this.sqltype == ""){ this.sqltype = "msg.topic"; } var node = this; From 43517381a6936ba8a375d1fff29a0b4962e996f2 Mon Sep 17 00:00:00 2001 From: atsage <30732752+atsage@users.noreply.github.com> Date: Tue, 20 Mar 2018 14:03:08 -0400 Subject: [PATCH 08/13] Change backwards compatibility code --- storage/sqlite/sqlite.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/storage/sqlite/sqlite.js b/storage/sqlite/sqlite.js index 14f1d709c..e24be09ff 100644 --- a/storage/sqlite/sqlite.js +++ b/storage/sqlite/sqlite.js @@ -32,13 +32,9 @@ module.exports = function(RED) { function SqliteNodeIn(n) { RED.nodes.createNode(this,n); this.mydb = n.mydb; - this.sqltype = n.sqltype; + this.sqltype = n.sqltype||"msg.topic"; this.sql = n.sql; this.mydbConfig = RED.nodes.getNode(this.mydb); - //default to msg.topic for backwards compatibility - if (typeof this.sqltype == "undefined" || this.sqltype === null || this.sqltype == ""){ - this.sqltype = "msg.topic"; - } var node = this; node.status({}); From afe7428e213b7d9f735f7d42656fd82a28a68bc1 Mon Sep 17 00:00:00 2001 From: atsage <30732752+atsage@users.noreply.github.com> Date: Tue, 20 Mar 2018 14:15:11 -0400 Subject: [PATCH 09/13] Change SQL Type to SQL Query and update the info --- storage/sqlite/sqlite.html | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/storage/sqlite/sqlite.html b/storage/sqlite/sqlite.html index 72dac8789..1bcb54787 100644 --- a/storage/sqlite/sqlite.html +++ b/storage/sqlite/sqlite.html @@ -29,11 +29,11 @@
- - + + +
@@ -48,12 +48,12 @@