Skip to content

Commit

Permalink
### __WORK IN PROGRESS__
Browse files Browse the repository at this point in the history
* (bluefox) Added the option "Do not create database". E.g. if DB was created and it does not required to do that, because the user does not have enough rights.
  • Loading branch information
GermanBluefox committed Aug 29, 2020
1 parent 1ef2c52 commit 0347e8c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 24 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,15 @@ sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
- **Minimum difference from last value to log**: The minimum interval between two values.
- **Storage retention**: How long the values will be stored in the DB.

## Changelog
<!--
Placeholder for the next version (at the beginning of the line):
### __WORK IN PROGRESS__
-->

## Changelog
### __WORK IN PROGRESS__
* (bluefox) Added the option "Do not create database". E.g. if DB was created and it does not required to do that, because the user does not have enough rights.

### 1.15.2 (2020-07-26)
* (Apollon77) prevent wrong errors that realId is missing

Expand Down
6 changes: 5 additions & 1 deletion admin/index_m.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,16 @@
<div class="row ms-sql real-sql">
<div class="input-field col s12 m6 l4">
<input id="multiRequests" type="checkbox" class="value" />
<label class="translate" for="multiRequests">Allow parallel requests:</label>
<span class="translate" for="multiRequests">Allow parallel requests:</span>
</div>
<div class="input-field col s12 m6 l4">
<input id="maxConnections" type="text" class="value" />
<label class="translate" for="maxConnections">Maximum concurrent connections:</label>
</div>
<div class="input-field col s12 m6 l4">
<input id="doNotCreateDatabase" type="checkbox" class="value" />
<label class="translate" for="doNotCreateDatabase">Do not create database (already created)</label>
</div>
</div>
<div class="row">
<div class="input-field col s12 m4">
Expand Down
12 changes: 12 additions & 0 deletions admin/words.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,5 +501,17 @@ systemDictionary = {
"it": "Scrivi valori NULL sui limiti di avvio / arresto:",
"es": "Escribe valores NULL en los límites de inicio / finalización:",
"pl": "Napisz wartości NULL na granicach start / stop:"
},
"Do not create database (already created)": {
"en": "Do not create database (already created)",
"de": "Datenbank nicht erstellen (bereits erstellt)",
"ru": "Не создавать базу данных (уже создана)",
"pt": "Não crie banco de dados (já criado)",
"nl": "Maak geen database (al aangemaakt)",
"fr": "Ne pas créer de base de données (déjà créée)",
"it": "Non creare database (già creato)",
"es": "No crear base de datos (ya creada)",
"pl": "Nie twórz bazy danych (już utworzona)",
"zh-cn": "不创建数据库(已创建)"
}
};
3 changes: 2 additions & 1 deletion io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@
"maxConnections": 100,
"changesRelogInterval": 0,
"changesMinDelta": 0,
"writeNulls": true
"writeNulls": true,
"doNotCreateDatabase": false
},
"instanceObjects": [
{
Expand Down
8 changes: 5 additions & 3 deletions lib/mssql.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
exports.init = function (dbname) {
return [
`CREATE DATABASE ${dbname};`,
exports.init = function (dbname, doNotCreateDatabase) {
const commands = [
`CREATE TABLE ${dbname}.dbo.sources (id INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1), name varchar(255));`,
`CREATE TABLE ${dbname}.dbo.datapoints (id INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1), name varchar(255), type INTEGER);`,
`CREATE TABLE ${dbname}.dbo.ts_number (id INTEGER, ts BIGINT, val REAL, ack BIT, _from INTEGER, q INTEGER);`,
Expand All @@ -12,6 +11,9 @@ exports.init = function (dbname) {
`CREATE TABLE ${dbname}.dbo.ts_counter (id INTEGER, ts BIGINT, val REAL);`,
`CREATE INDEX i_id on ${dbname}.dbo.ts_counter (id, ts);`,
];
!doNotCreateDatabase && commands.unshift(`CREATE DATABASE ${dbname};`);

return commands;
};

exports.destroy = function (dbname) {
Expand Down
9 changes: 6 additions & 3 deletions lib/mysql.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
exports.init = function (dbname) {
return [
"CREATE DATABASE `" + dbname + "` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;",
exports.init = function (dbname, doNotCreateDatabase) {
const commands = [
"CREATE TABLE `" + dbname + "`.sources (id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, name TEXT);",
"CREATE TABLE `" + dbname + "`.datapoints (id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, name TEXT, type INTEGER);",
"CREATE TABLE `" + dbname + "`.ts_number (id INTEGER, ts BIGINT, val REAL, ack BOOLEAN, _from INTEGER, q INTEGER, PRIMARY KEY(id, ts));",
"CREATE TABLE `" + dbname + "`.ts_string (id INTEGER, ts BIGINT, val TEXT, ack BOOLEAN, _from INTEGER, q INTEGER, PRIMARY KEY(id, ts));",
"CREATE TABLE `" + dbname + "`.ts_bool (id INTEGER, ts BIGINT, val BOOLEAN, ack BOOLEAN, _from INTEGER, q INTEGER, PRIMARY KEY(id, ts));",
"CREATE TABLE `" + dbname + "`.ts_counter (id INTEGER, ts BIGINT, val REAL);"
];

!doNotCreateDatabase && commands.unshift("CREATE DATABASE `" + dbname + "` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;");

return commands;
};

exports.destroy = function (dbname) {
Expand Down
33 changes: 20 additions & 13 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,26 @@ function connect(callback) {
return;
}

_client.execute('CREATE DATABASE ' + adapter.config.dbname + ';', (err /* , rows, fields */) => {
if (adapter.config.doNotCreateDatabase) {
_client.disconnect();
if (err && err.code !== '42P04') { // if error not about yet exists
_client = false;
adapter.log.error(err);
reconnectTimeout && clearTimeout(reconnectTimeout);
reconnectTimeout = setTimeout(() => connect(callback), 30000);
} else {
_client = true;
reconnectTimeout && clearTimeout(reconnectTimeout);
reconnectTimeout = setTimeout(() => connect(callback), 100);
}
});
_client = true;
reconnectTimeout && clearTimeout(reconnectTimeout);
reconnectTimeout = setTimeout(() => connect(callback), 100);
} else {
_client.execute('CREATE DATABASE ' + adapter.config.dbname + ';', (err /* , rows, fields */) => {
_client.disconnect();
if (err && err.code !== '42P04') { // if error not about yet exists
_client = false;
adapter.log.error(err);
reconnectTimeout && clearTimeout(reconnectTimeout);
reconnectTimeout = setTimeout(() => connect(callback), 30000);
} else {
_client = true;
reconnectTimeout && clearTimeout(reconnectTimeout);
reconnectTimeout = setTimeout(() => connect(callback), 100);
}
});
}
});
}

Expand Down Expand Up @@ -449,7 +456,7 @@ function connect(callback) {
}
}

allScripts(SQLFuncs.init(adapter.config.dbname), err => {
allScripts(SQLFuncs.init(adapter.config.dbname, adapter.config.doNotCreateDatabase), err => {
if (err) {
//adapter.log.error(err);
reconnectTimeout && clearTimeout(reconnectTimeout);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"@iobroker/adapter-core": "^2.4.0"
},
"devDependencies": {
"@alcalzone/release-script": "^1.4.1",
"@alcalzone/release-script": "^1.7.0",
"gulp": "^4.0.2",
"mocha": "^7.1.2",
"mocha": "^8.1.3",
"chai": "^4.2.0"
},
"bugs": {
Expand Down

0 comments on commit 0347e8c

Please sign in to comment.