mysql: add charset option (defaults as-is to old UTF8)

This commit is contained in:
Dave Conway-Jones 2020-04-18 18:03:59 +01:00
parent 667c7588f9
commit 4641d10beb
No known key found for this signature in database
GPG Key ID: 302A6725C594817F
4 changed files with 31 additions and 13 deletions

View File

@ -24,6 +24,10 @@
<label for="node-config-input-tz"><i class="fa fa-clock-o"></i> Timezone</label>
<input type="text" id="node-config-input-tz">
</div>
<div class="form-row">
<label for="node-config-input-charset"><i class="fa fa-language"></i> Charset</label>
<input type="text" id="node-config-input-charset">
</div>
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
@ -37,10 +41,9 @@
name: {value:""},
host: {value:"127.0.0.1",required:true},
port: {value:"3306",required:true},
//user: {value:"",required:true},
//pass: {value:"",required:true},
db: {value:"",required:true},
tz: {value:""}
tz: {value:""},
charset: {value:"UTF8"}
},
credentials: {
user: {type: "text"},
@ -52,6 +55,12 @@
});
</script>
<script type="text/html" data-help-name="MySQLdatabase">
<p>Add the credentials for accessing your database here.</p>
<p>Timezone can be set like GMT, EST5EDT, UTC, etc</p>
<p>The Charset defaults to the "old" 3 byte Mysql UTF8. If you need support for emojis etc then use UTF8MB4.</p>
</script>
<script type="text/html" data-template-name="mysql">
<div class="form-row">

View File

@ -9,6 +9,7 @@ module.exports = function(RED) {
this.host = n.host;
this.port = n.port;
this.tz = n.tz || "local";
this.charset = (n.charset || "UTF8_GENERAL_CI").toUpperCase();
this.connected = false;
this.connecting = false;
@ -41,7 +42,8 @@ module.exports = function(RED) {
timezone : node.tz,
insecureAuth: true,
multipleStatements: true,
connectionLimit: 25
connectionLimit: 25,
charset: node.charset
});
}
@ -136,7 +138,7 @@ module.exports = function(RED) {
if (values.hasOwnProperty(key)) {
return this.escape(values[key]);
}
return txt;
return txt;
}.bind(this));
};
}

View File

@ -15,18 +15,24 @@ Usage
Allows basic access to a MySQL database.
This node uses the <b>query</b> operation against the configured database. This does allow both INSERTS and DELETES.
This node uses the **query** operation against the configured database. This does allow both INSERTS and DELETES.
By its very nature it allows SQL injection... so <i>be careful out there...</i>
By its very nature it allows SQL injection... so *be careful out there...*
The `msg.topic` must hold the <i>query</i> for the database, and the result is returned in `msg.payload`.
The `msg.topic` must hold the *query* for the database, and the result is returned in `msg.payload`.
Typically the returned payload will be an array of the result rows.
If nothing is found for the key then <i>null</i> is returned.
If nothing is found for the key then *null* is returned.
The reconnect retry timeout in milliseconds can be changed by adding a line to <b>settings.js</b>
<pre>mysqlReconnectTime: 30000,</pre></p>
The reconnect retry timeout in milliseconds can be changed by adding a line to **settings.js**
```javascript
mysqlReconnectTime: 30000,
```
The timezone can be set like GMT, EST5EDT, UTC, etc.
The charset defaults to the "old" Mysql 3 byte UTF. If you need support for emojis etc then use UTF8MB4.
Preparing Queries
@ -46,7 +52,8 @@ msg.payload.newUsername="example-user";
msg.topic="INSERT INTO users (`userid`, `username`) VALUES (:userToChange, :newUsername) ON DUPLICATE KEY UPDATE `username`=:newUsername;"
return msg;
```
Documentation
-----
<a href="https://www.npmjs.com/package/mysql" target="_new">Documentation</a> of the used Node.js package

View File

@ -1,6 +1,6 @@
{
"name": "node-red-node-mysql",
"version": "0.1.0",
"version": "0.1.1",
"description": "A Node-RED node to read and write to a MySQL database",
"dependencies": {
"mysql": "^2.18.1"