diff --git a/storage/mysql/68-mysql.js b/storage/mysql/68-mysql.js index d62fb9f1..7ef30e09 100644 --- a/storage/mysql/68-mysql.js +++ b/storage/mysql/68-mysql.js @@ -112,43 +112,41 @@ module.exports = function(RED) { if (node.mydbConfig.connected) { if (typeof msg.topic === 'string') { //console.log("query:",msg.topic); - var bind = []; - if (Array.isArray(msg.payload)) { - bind = msg.payload; - node.mydbConfig.pool.on('acquire', function(connection) { - connection.config.queryFormat = null; - }); - } - else if (typeof msg.payload === 'object' && msg.payload !== null) { - bind = msg.payload; - node.mydbConfig.pool.on('acquire', function(connection) { - connection.config.queryFormat = function(query, values) { - if (!values) { - return query; - } - return query.replace(/\:(\w+)/g, function(txt, key) { - if (values.hasOwnProperty(key)) { - return this.escape(values[key]); - } - return txt; - }.bind(this)); - }; - }); - } - node.mydbConfig.pool.query(msg.topic, bind, function(err, rows) { + node.mydbConfig.pool.getConnection(function (err, conn) { if (err) { - status = {fill:"red",shape:"ring",text:RED._("mysql.status.error")+": "+err.code}; + conn.release() + status = { fill: "red", shape: "ring", text: RED._("mysql.status.error") + ": " + err.code }; node.status(status); - node.error(err,msg); + node.error(err, msg); + if (done) { done(); } + return } - else { - msg.payload = rows; - send(msg); - status = {fill:"green",shape:"dot",text:RED._("mysql.status.ok")}; - node.status(status); + + var bind = []; + if (Array.isArray(msg.payload)) { + bind = msg.payload; } - if (done) { done(); } - }); + else if (typeof msg.payload === 'object' && msg.payload !== null) { + bind = msg.payload; + } + conn.config.queryFormat = Array.isArray(msg.payload) ? null : customQueryFormat + conn.query(msg.topic, bind, function (err, rows) { + conn.release() + if (err) { + status = { fill: "red", shape: "ring", text: RED._("mysql.status.error") + ": " + err.code }; + node.status(status); + node.error(err, msg); + } + else { + msg.payload = rows; + send(msg); + status = { fill: "green", shape: "dot", text: RED._("mysql.status.ok") }; + node.status(status); + } + if (done) { done(); } + }); + }) + } else { if (typeof msg.topic !== 'string') { node.error("msg.topic : "+RED._("mysql.errors.notstring")); done(); } @@ -178,3 +176,15 @@ module.exports = function(RED) { } RED.nodes.registerType("mysql",MysqlDBNodeIn); } + +function customQueryFormat(query, values) { + if (!values) { + return query; + } + return query.replace(/\:(\w+)/g, function(txt, key) { + if (values.hasOwnProperty(key)) { + return this.escape(values[key]); + } + return txt; + }.bind(this)); +}