From 60957233081a6f2517546d10f07c253ecf4df49f Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Fri, 12 Mar 2021 21:01:54 +0000 Subject: [PATCH] fix mysql array handling --- storage/mysql/68-mysql.js | 20 ++++++++++---------- storage/mysql/package.json | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/storage/mysql/68-mysql.js b/storage/mysql/68-mysql.js index 9d17322b..0e1ea5d9 100644 --- a/storage/mysql/68-mysql.js +++ b/storage/mysql/68-mysql.js @@ -19,9 +19,8 @@ module.exports = function(RED) { var node = this; function checkVer() { - node.connection.query("SELECT version();", [], function(err, rows) { + node.pool.query("SELECT version();", [], function(err, rows, fields) { if (err) { - node.connection.release(); node.error(err); node.status({fill:"red",shape:"ring",text:"Bad Ping"}); doConnect(); @@ -84,13 +83,13 @@ module.exports = function(RED) { } } - this.on('close', function (done) { + this.on('close', function(done) { if (this.tick) { clearTimeout(this.tick); } if (this.check) { clearInterval(this.check); } node.connected = false; node.connection.release(); node.emit("state"," "); - node.pool.end(function (err) { done(); }); + node.pool.end(function(err) { done(); }); }); } RED.nodes.registerType("MySQLdatabase",MySQLNode, { @@ -130,17 +129,17 @@ module.exports = function(RED) { if (Array.isArray(msg.payload)) { bind = msg.payload; } else if (typeof msg.payload === 'object' && msg.payload !== null) { bind=msg.payload; - node.mydbConfig.connection.config.queryFormat = function (query, values) { - if (!values){ + node.mydbConfig.connection.config.queryFormat = function(query, values) { + if (!values) { return query; } - return query.replace(/\:(\w+)/g, function (txt, key) { + return query.replace(/\:(\w+)/g, function(txt, key) { if (values.hasOwnProperty(key)) { return this.escape(values[key]); } return txt; }.bind(this)); - }; + }; } node.mydbConfig.connection.query(msg.topic, bind, function(err, rows) { if (err) { @@ -149,7 +148,7 @@ module.exports = function(RED) { node.error(err,msg); } else { - if (rows.constructor.name === "OkPacket") { + if ( (rows.constructor.name === "OkPacket") || (rows.constructor.name === "Array")) { msg.payload = JSON.parse(JSON.stringify(rows)); } else { msg.payload = rows; } @@ -157,6 +156,7 @@ module.exports = function(RED) { status = {fill:"green",shape:"dot",text:"OK"}; node.status(status); } + node.mydbConfig.connection.release(); }); } else { @@ -174,7 +174,7 @@ module.exports = function(RED) { } }); - node.on('close', function () { + node.on('close', function() { if (node.tout) { clearTimeout(node.tout); } node.mydbConfig.removeAllListeners(); node.status({}); diff --git a/storage/mysql/package.json b/storage/mysql/package.json index 8ac128f0..f2995a57 100644 --- a/storage/mysql/package.json +++ b/storage/mysql/package.json @@ -1,6 +1,6 @@ { "name": "node-red-node-mysql", - "version": "0.1.1", + "version": "0.1.2", "description": "A Node-RED node to read and write to a MySQL database", "dependencies": { "mysql": "^2.18.1"