Skip to content

Commit 8df7797

Browse files
author
Dave Conway-Jones
committed
2 parents 8d8195e + fea4784 commit 8df7797

File tree

4 files changed

+53
-63
lines changed

4 files changed

+53
-63
lines changed

storage/mysql/68-mysql.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</div>
2323
<div class="form-row">
2424
<label for="node-config-input-tz"><i class="fa fa-clock-o"></i> <span data-i18n="mysql.label.timezone"></span></label>
25-
<input type="text" id="node-config-input-tz">
25+
<input type="text" id="node-config-input-tz" placeholder="&#177;hh:mm">
2626
</div>
2727
<div class="form-row">
2828
<label for="node-config-input-charset"><i class="fa fa-language"></i> <span data-i18n="mysql.label.charset"></span></label>
@@ -32,6 +32,7 @@
3232
<label for="node-config-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
3333
<input type="text" id="node-config-input-name" data-i18n="[placeholder]node-red:common.label.name">
3434
</div>
35+
<div class="form-tips"><span data-i18n="[html]mysql.tip"></span></div>
3536
</script>
3637

3738
<script type="text/javascript">

storage/mysql/68-mysql.js

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module.exports = function(RED) {
33
"use strict";
44
var reconnect = RED.settings.mysqlReconnectTime || 20000;
5-
var mysqldb = require('mysql');
5+
var mysqldb = require('mysql2');
66

77
function MySQLNode(n) {
88
RED.nodes.createNode(this,n);
@@ -41,10 +41,10 @@ module.exports = function(RED) {
4141
timezone : node.tz,
4242
insecureAuth: true,
4343
multipleStatements: true,
44-
connectionLimit: 50,
45-
timeout: 30000,
44+
connectionLimit: RED.settings.mysqlConnectionLimit || 50,
4645
connectTimeout: 30000,
47-
charset: node.charset
46+
charset: node.charset,
47+
decimalNumbers: true
4848
});
4949
}
5050

@@ -112,65 +112,41 @@ module.exports = function(RED) {
112112
if (node.mydbConfig.connected) {
113113
if (typeof msg.topic === 'string') {
114114
//console.log("query:",msg.topic);
115-
var bind = [];
116-
if (Array.isArray(msg.payload)) {
117-
bind = msg.payload;
118-
node.mydbConfig.pool.on('acquire', function(connection) {
119-
connection.config.queryFormat = null;
120-
});
121-
}
122-
else if (typeof msg.payload === 'object' && msg.payload !== null) {
123-
bind = msg.payload;
124-
node.mydbConfig.pool.on('acquire', function(connection) {
125-
connection.config.queryFormat = function(query, values) {
126-
if (!values) {
127-
return query;
128-
}
129-
return query.replace(/\:(\w+)/g, function(txt, key) {
130-
if (values.hasOwnProperty(key)) {
131-
return this.escape(values[key]);
132-
}
133-
return txt;
134-
}.bind(this));
135-
};
136-
});
137-
}
138-
node.mydbConfig.pool.query(msg.topic, bind, function(err, rows) {
115+
node.mydbConfig.pool.getConnection(function (err, conn) {
139116
if (err) {
140-
status = {fill:"red",shape:"ring",text:RED._("mysql.status.error")+": "+err.code};
117+
conn.release()
118+
status = { fill: "red", shape: "ring", text: RED._("mysql.status.error") + ": " + err.code };
141119
node.status(status);
142-
node.error(err,msg);
120+
node.error(err, msg);
121+
if (done) { done(); }
122+
return
143123
}
144-
else {
145-
// if (rows.constructor.name === "OkPacket") {
146-
// msg.payload = JSON.parse(JSON.stringify(rows));
147-
// }
148-
// else if (rows.constructor.name === "Array") {
149-
// if (rows[0] && rows[0].constructor.name === "RowDataPacket") {
150-
// msg.payload = rows.map(v => Object.assign({}, v));
151-
// }
152-
// else if (rows[0] && rows[0].constructor.name === "Array") {
153-
// if (rows[0][0] && rows[0][0].constructor.name === "RowDataPacket") {
154-
// msg.payload = rows.map(function(v) {
155-
// if (!Array.isArray(v)) { return v; }
156-
// v.map(w => Object.assign({}, w))
157-
// });
158-
// }
159-
// else { msg.payload = rows; }
160-
// }
161-
// else { msg.payload = rows; }
162-
// }
163-
// else { msg.payload = rows; }
164-
msg.payload = rows;
165-
send(msg);
166-
status = {fill:"green",shape:"dot",text:RED._("mysql.status.ok")};
167-
node.status(status);
124+
125+
var bind = [];
126+
if (Array.isArray(msg.payload)) {
127+
bind = msg.payload;
128+
}
129+
else if (typeof msg.payload === 'object' && msg.payload !== null) {
130+
bind = msg.payload;
168131
}
169-
if (done) { done(); }
170-
// if (node.mydbConfig.pool._freeConnections.indexOf(node.mydbConfig.connection) === -1) {
171-
// node.mydbConfig.connection.release();
172-
// }
173-
});
132+
conn.config.queryFormat = Array.isArray(msg.payload) ? null : customQueryFormat
133+
conn.query(msg.topic, bind, function (err, rows) {
134+
conn.release()
135+
if (err) {
136+
status = { fill: "red", shape: "ring", text: RED._("mysql.status.error") + ": " + err.code };
137+
node.status(status);
138+
node.error(err, msg);
139+
}
140+
else {
141+
msg.payload = rows;
142+
send(msg);
143+
status = { fill: "green", shape: "dot", text: RED._("mysql.status.ok") };
144+
node.status(status);
145+
}
146+
if (done) { done(); }
147+
});
148+
})
149+
174150
}
175151
else {
176152
if (typeof msg.topic !== 'string') { node.error("msg.topic : "+RED._("mysql.errors.notstring")); done(); }
@@ -200,3 +176,15 @@ module.exports = function(RED) {
200176
}
201177
RED.nodes.registerType("mysql",MysqlDBNodeIn);
202178
}
179+
180+
function customQueryFormat(query, values) {
181+
if (!values) {
182+
return query;
183+
}
184+
return query.replace(/\:(\w+)/g, function(txt, key) {
185+
if (values.hasOwnProperty(key)) {
186+
return this.escape(values[key]);
187+
}
188+
return txt;
189+
}.bind(this));
190+
}

storage/mysql/locales/en-US/68-mysql.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"notstring": "the query is not defined as a string",
2020
"notconnected": "Database not connected",
2121
"notconfigured": "MySQL database not configured"
22-
}
22+
},
23+
"tip": "Tip: The timezone should be specified as &#177;hh:mm or leave blank for 'local'."
2324
}
2425
}

storage/mysql/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "node-red-node-mysql",
3-
"version": "0.3.0",
3+
"version": "1.0.0-beta-5",
44
"description": "A Node-RED node to read and write to a MySQL database",
55
"dependencies": {
6-
"mysql": "^2.18.1"
6+
"mysql2": "^2.3.3"
77
},
88
"repository": {
99
"type": "git",

0 commit comments

Comments
 (0)