Skip to content

Commit

Permalink
Check both undefined and string type in msg.payload (#3)
Browse files Browse the repository at this point in the history
* Add java and jdbc modules as dependencies

* Change file permission

* Check both undefined and string type in msg.payload
  • Loading branch information
kazuhitoyokoi authored Nov 25, 2021
1 parent 8dc14c0 commit e0ec2a3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions flows.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
{
"name": "user",
"type": "str",
"value": "user",
"value": "postgres",
"ui": {
"icon": "font-awesome/fa-user",
"label": {
Expand Down Expand Up @@ -117,7 +117,7 @@
"type": "function",
"z": "3e543cc0192c0fb2",
"name": "database",
"func": "if (cn) {\n node.status({fill: 'blue', shape: 'dot', text: 'requesting' });\n cn.conn.createStatement(function (err, statement) {\n if (typeof msg.payload !== 'undefined') {\n if (msg.payload.match(/^select/i)) {\n statement.executeQuery(msg.payload, function (err, resultset) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n } else {\n resultset.toObjArray(function (err, result) {\n msg.payload = result;\n node.send(msg);\n node.status({});\n });\n }\n });\n } else {\n statement.executeUpdate(msg.payload, function (err, result) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n } else {\n msg.payload = result;\n node.send(msg);\n node.status({});\n }\n });\n }\n } else {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n }\n });\n}",
"func": "if (cn) {\n node.status({fill: 'blue', shape: 'dot', text: 'requesting' });\n cn.conn.createStatement(function (err, statement) {\n if (typeof msg.payload === 'string') {\n if (msg.payload.match(/^select/i)) {\n statement.executeQuery(msg.payload, function (err, resultset) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n } else {\n resultset.toObjArray(function (err, result) {\n msg.payload = result;\n node.send(msg);\n node.status({});\n });\n }\n });\n } else {\n statement.executeUpdate(msg.payload, function (err, result) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n } else {\n msg.payload = result;\n node.send(msg);\n node.status({});\n }\n });\n }\n } else {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n }\n });\n}",
"outputs": 1,
"noerr": 0,
"initialize": "cn = null;\nif (!java.classpath.includes(env.get('driver'))) {\n java.classpath.push.apply(java.classpath, [env.get('driver')]);\n}\n\nvar db = new jdbc({\n url: env.get('url'),\n user: env.get('user'),\n password: env.get('password')\n});\n\ndb.reserve(function (err, conn) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'disconnected' });\n } else {\n cn = conn;\n db.initialize(function (err) {\n node.status({fill: 'green', shape: 'dot', text: 'connected' });\n });\n }\n});",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-jdbc",
"version": "0.1.1",
"version": "0.1.2",
"description": "Node-RED node to access database using JDBC driver",
"node-red": {
"nodes": {
Expand Down
4 changes: 2 additions & 2 deletions subflow.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
{
"name": "user",
"type": "str",
"value": "user",
"value": "postgres",
"ui": {
"icon": "font-awesome/fa-user",
"label": {
Expand Down Expand Up @@ -116,7 +116,7 @@
"type": "function",
"z": "3e543cc0192c0fb2",
"name": "database",
"func": "if (cn) {\n node.status({fill: 'blue', shape: 'dot', text: 'requesting' });\n cn.conn.createStatement(function (err, statement) {\n if (msg.payload.match(/^select/i)) {\n statement.executeQuery(msg.payload, function (err, resultset) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n } else {\n resultset.toObjArray(function (err, result) {\n msg.payload = result;\n node.send(msg);\n node.status({});\n });\n }\n });\n } else {\n statement.executeUpdate(msg.payload, function (err, result) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n } else {\n msg.payload = result;\n node.send(msg);\n node.status({});\n }\n });\n }\n });\n}",
"func": "if (cn) {\n node.status({fill: 'blue', shape: 'dot', text: 'requesting' });\n cn.conn.createStatement(function (err, statement) {\n if (typeof msg.payload === 'string') {\n if (msg.payload.match(/^select/i)) {\n statement.executeQuery(msg.payload, function (err, resultset) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n } else {\n resultset.toObjArray(function (err, result) {\n msg.payload = result;\n node.send(msg);\n node.status({});\n });\n }\n });\n } else {\n statement.executeUpdate(msg.payload, function (err, result) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n } else {\n msg.payload = result;\n node.send(msg);\n node.status({});\n }\n });\n }\n } else {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'error' });\n }\n });\n}",
"outputs": 1,
"noerr": 0,
"initialize": "cn = null;\nif (!java.classpath.includes(env.get('driver'))) {\n java.classpath.push.apply(java.classpath, [env.get('driver')]);\n}\n\nvar db = new jdbc({\n url: env.get('url'),\n user: env.get('user'),\n password: env.get('password')\n});\n\ndb.reserve(function (err, conn) {\n if (err) {\n node.error(err);\n node.status({fill: 'red', shape: 'ring', text: 'disconnected' });\n } else {\n cn = conn;\n db.initialize(function (err) {\n node.status({fill: 'green', shape: 'dot', text: 'connected' });\n });\n }\n});",
Expand Down

0 comments on commit e0ec2a3

Please sign in to comment.