mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
fix pool on acquire event cause MaxListenersExceededWarning
before: every query need to register pool on acquire event to specify queryFormat based on payload type will cause MaxListenersExceededWarning after: from https://www.npmjs.com/package/mysql#pooling-connections pool.query is a shortcut for pool.getConnection() -> connection.query() -> connection.release() so use pool.getConnection and then set queryFormat before query method be called
This commit is contained in:
parent
2d28a2304f
commit
3e8f3f0acc
@ -112,65 +112,61 @@ module.exports = function(RED) {
|
|||||||
if (node.mydbConfig.connected) {
|
if (node.mydbConfig.connected) {
|
||||||
if (typeof msg.topic === 'string') {
|
if (typeof msg.topic === 'string') {
|
||||||
//console.log("query:",msg.topic);
|
//console.log("query:",msg.topic);
|
||||||
var bind = [];
|
node.mydbConfig.pool.getConnection(function (err, conn) {
|
||||||
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) {
|
|
||||||
if (err) {
|
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.status(status);
|
||||||
node.error(err,msg);
|
node.error(err, msg);
|
||||||
|
if (done) { done(); }
|
||||||
|
return
|
||||||
}
|
}
|
||||||
else {
|
var bind = [];
|
||||||
// if (rows.constructor.name === "OkPacket") {
|
if (Array.isArray(msg.payload)) {
|
||||||
// msg.payload = JSON.parse(JSON.stringify(rows));
|
bind = msg.payload;
|
||||||
// }
|
|
||||||
// else if (rows.constructor.name === "Array") {
|
|
||||||
// if (rows[0] && rows[0].constructor.name === "RowDataPacket") {
|
|
||||||
// msg.payload = rows.map(v => Object.assign({}, v));
|
|
||||||
// }
|
|
||||||
// else if (rows[0] && rows[0].constructor.name === "Array") {
|
|
||||||
// if (rows[0][0] && rows[0][0].constructor.name === "RowDataPacket") {
|
|
||||||
// msg.payload = rows.map(function(v) {
|
|
||||||
// if (!Array.isArray(v)) { return v; }
|
|
||||||
// v.map(w => Object.assign({}, w))
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// else { msg.payload = rows; }
|
|
||||||
// }
|
|
||||||
// else { msg.payload = rows; }
|
|
||||||
// }
|
|
||||||
// else { msg.payload = rows; }
|
|
||||||
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.payload === 'object' && msg.payload !== null) {
|
||||||
// if (node.mydbConfig.pool._freeConnections.indexOf(node.mydbConfig.connection) === -1) {
|
bind = msg.payload;
|
||||||
// node.mydbConfig.connection.release();
|
}
|
||||||
// }
|
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 {
|
||||||
|
// if (rows.constructor.name === "OkPacket") {
|
||||||
|
// msg.payload = JSON.parse(JSON.stringify(rows));
|
||||||
|
// }
|
||||||
|
// else if (rows.constructor.name === "Array") {
|
||||||
|
// if (rows[0] && rows[0].constructor.name === "RowDataPacket") {
|
||||||
|
// msg.payload = rows.map(v => Object.assign({}, v));
|
||||||
|
// }
|
||||||
|
// else if (rows[0] && rows[0].constructor.name === "Array") {
|
||||||
|
// if (rows[0][0] && rows[0][0].constructor.name === "RowDataPacket") {
|
||||||
|
// msg.payload = rows.map(function(v) {
|
||||||
|
// if (!Array.isArray(v)) { return v; }
|
||||||
|
// v.map(w => Object.assign({}, w))
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// else { msg.payload = rows; }
|
||||||
|
// }
|
||||||
|
// else { msg.payload = rows; }
|
||||||
|
// }
|
||||||
|
// else { msg.payload = rows; }
|
||||||
|
msg.payload = rows;
|
||||||
|
send(msg);
|
||||||
|
status = { fill: "green", shape: "dot", text: RED._("mysql.status.ok") };
|
||||||
|
node.status(status);
|
||||||
|
}
|
||||||
|
if (done) { done(); }
|
||||||
|
// if (node.mydbConfig.pool._freeConnections.indexOf(node.mydbConfig.connection) === -1) {
|
||||||
|
// node.mydbConfig.connection.release();
|
||||||
|
// }
|
||||||
|
});
|
||||||
|
})
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (typeof msg.topic !== 'string') { node.error("msg.topic : "+RED._("mysql.errors.notstring")); done(); }
|
if (typeof msg.topic !== 'string') { node.error("msg.topic : "+RED._("mysql.errors.notstring")); done(); }
|
||||||
@ -200,3 +196,15 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
RED.nodes.registerType("mysql",MysqlDBNodeIn);
|
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));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user