Skip to content

Commit 4641d10

Browse files
author
Dave Conway-Jones
committed
mysql: add charset option (defaults as-is to old UTF8)
1 parent 667c758 commit 4641d10

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

storage/mysql/68-mysql.html

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<label for="node-config-input-tz"><i class="fa fa-clock-o"></i> Timezone</label>
2525
<input type="text" id="node-config-input-tz">
2626
</div>
27+
<div class="form-row">
28+
<label for="node-config-input-charset"><i class="fa fa-language"></i> Charset</label>
29+
<input type="text" id="node-config-input-charset">
30+
</div>
2731
<div class="form-row">
2832
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
2933
<input type="text" id="node-config-input-name" placeholder="Name">
@@ -37,10 +41,9 @@
3741
name: {value:""},
3842
host: {value:"127.0.0.1",required:true},
3943
port: {value:"3306",required:true},
40-
//user: {value:"",required:true},
41-
//pass: {value:"",required:true},
4244
db: {value:"",required:true},
43-
tz: {value:""}
45+
tz: {value:""},
46+
charset: {value:"UTF8"}
4447
},
4548
credentials: {
4649
user: {type: "text"},
@@ -52,6 +55,12 @@
5255
});
5356
</script>
5457

58+
<script type="text/html" data-help-name="MySQLdatabase">
59+
<p>Add the credentials for accessing your database here.</p>
60+
<p>Timezone can be set like GMT, EST5EDT, UTC, etc</p>
61+
<p>The Charset defaults to the "old" 3 byte Mysql UTF8. If you need support for emojis etc then use UTF8MB4.</p>
62+
</script>
63+
5564

5665
<script type="text/html" data-template-name="mysql">
5766
<div class="form-row">

storage/mysql/68-mysql.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = function(RED) {
99
this.host = n.host;
1010
this.port = n.port;
1111
this.tz = n.tz || "local";
12+
this.charset = (n.charset || "UTF8_GENERAL_CI").toUpperCase();
1213

1314
this.connected = false;
1415
this.connecting = false;
@@ -41,7 +42,8 @@ module.exports = function(RED) {
4142
timezone : node.tz,
4243
insecureAuth: true,
4344
multipleStatements: true,
44-
connectionLimit: 25
45+
connectionLimit: 25,
46+
charset: node.charset
4547
});
4648
}
4749

@@ -136,7 +138,7 @@ module.exports = function(RED) {
136138
if (values.hasOwnProperty(key)) {
137139
return this.escape(values[key]);
138140
}
139-
return txt;
141+
return txt;
140142
}.bind(this));
141143
};
142144
}

storage/mysql/README.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@ Usage
1515

1616
Allows basic access to a MySQL database.
1717

18-
This node uses the <b>query</b> operation against the configured database. This does allow both INSERTS and DELETES.
18+
This node uses the **query** operation against the configured database. This does allow both INSERTS and DELETES.
1919

20-
By its very nature it allows SQL injection... so <i>be careful out there...</i>
20+
By its very nature it allows SQL injection... so *be careful out there...*
2121

22-
The `msg.topic` must hold the <i>query</i> for the database, and the result is returned in `msg.payload`.
22+
The `msg.topic` must hold the *query* for the database, and the result is returned in `msg.payload`.
2323

2424
Typically the returned payload will be an array of the result rows.
2525

26-
If nothing is found for the key then <i>null</i> is returned.
26+
If nothing is found for the key then *null* is returned.
2727

28-
The reconnect retry timeout in milliseconds can be changed by adding a line to <b>settings.js</b>
29-
<pre>mysqlReconnectTime: 30000,</pre></p>
28+
The reconnect retry timeout in milliseconds can be changed by adding a line to **settings.js**
29+
```javascript
30+
mysqlReconnectTime: 30000,
31+
```
32+
33+
The timezone can be set like GMT, EST5EDT, UTC, etc.
34+
35+
The charset defaults to the "old" Mysql 3 byte UTF. If you need support for emojis etc then use UTF8MB4.
3036

3137

3238
Preparing Queries
@@ -46,7 +52,8 @@ msg.payload.newUsername="example-user";
4652
msg.topic="INSERT INTO users (`userid`, `username`) VALUES (:userToChange, :newUsername) ON DUPLICATE KEY UPDATE `username`=:newUsername;"
4753
return msg;
4854
```
55+
4956
Documentation
5057
-----
51-
58+
5259
<a href="https://www.npmjs.com/package/mysql" target="_new">Documentation</a> of the used Node.js package

storage/mysql/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-mysql",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "A Node-RED node to read and write to a MySQL database",
55
"dependencies": {
66
"mysql": "^2.18.1"

0 commit comments

Comments
 (0)