|
2 | 2 | module.exports = function(RED) {
|
3 | 3 | "use strict";
|
4 | 4 | var reconnect = RED.settings.mysqlReconnectTime || 20000;
|
5 |
| - var mysqldb = require('mysql'); |
| 5 | + var mysqldb = require('mysql2'); |
6 | 6 |
|
7 | 7 | function MySQLNode(n) {
|
8 | 8 | RED.nodes.createNode(this,n);
|
@@ -41,10 +41,10 @@ module.exports = function(RED) {
|
41 | 41 | timezone : node.tz,
|
42 | 42 | insecureAuth: true,
|
43 | 43 | multipleStatements: true,
|
44 |
| - connectionLimit: 50, |
45 |
| - timeout: 30000, |
| 44 | + connectionLimit: RED.settings.mysqlConnectionLimit || 50, |
46 | 45 | connectTimeout: 30000,
|
47 |
| - charset: node.charset |
| 46 | + charset: node.charset, |
| 47 | + decimalNumbers: true |
48 | 48 | });
|
49 | 49 | }
|
50 | 50 |
|
@@ -112,65 +112,41 @@ module.exports = function(RED) {
|
112 | 112 | if (node.mydbConfig.connected) {
|
113 | 113 | if (typeof msg.topic === 'string') {
|
114 | 114 | //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) { |
139 | 116 | 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 }; |
141 | 119 | node.status(status);
|
142 |
| - node.error(err,msg); |
| 120 | + node.error(err, msg); |
| 121 | + if (done) { done(); } |
| 122 | + return |
143 | 123 | }
|
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; |
168 | 131 | }
|
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 | + |
174 | 150 | }
|
175 | 151 | else {
|
176 | 152 | if (typeof msg.topic !== 'string') { node.error("msg.topic : "+RED._("mysql.errors.notstring")); done(); }
|
@@ -200,3 +176,15 @@ module.exports = function(RED) {
|
200 | 176 | }
|
201 | 177 | RED.nodes.registerType("mysql",MysqlDBNodeIn);
|
202 | 178 | }
|
| 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 | +} |
0 commit comments