iobroker.sql 4.0.0 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/main.js CHANGED
@@ -1322,6 +1322,13 @@ class SqlAdapter extends adapter_core_1.Adapter {
1322
1322
  settings.blockTime &&
1323
1323
  this.sqlDPs[id].state &&
1324
1324
  this.sqlDPs[id].state.ts + settings.blockTime > state.ts) {
1325
+ // Keep the blocked value for reLogHelper. It is deliberately NOT put into "skipped":
1326
+ // that channel is flushed as soon as the next value passes, which would write blocked
1327
+ // values after all and undermine blockTime, documented as "no further value is stored"
1328
+ // during the window. Here it only replaces the stale value the relog timer would
1329
+ // otherwise repeat - a PV inverter dropping from 500 W to 0 W and then staying there
1330
+ // used to be relogged as 500 W for hours (issue #273).
1331
+ this.sqlDPs[id].blocked = state;
1325
1332
  settings.enableDebugLogs &&
1326
1333
  this.log.debug(`value ignored blockTime ${id}, value=${state.val}, ts=${state.ts}, lastState.ts=${this.sqlDPs[id].state.ts}, blockTime=${settings.blockTime}`);
1327
1334
  return;
@@ -1448,6 +1455,8 @@ class SqlAdapter extends adapter_core_1.Adapter {
1448
1455
  }
1449
1456
  this.sqlDPs[id].timeout = null;
1450
1457
  this.sqlDPs[id].state = state;
1458
+ // a newer value was logged, so any value dropped by blockTime before is obsolete
1459
+ this.sqlDPs[id].blocked = null;
1451
1460
  this.sqlDPs[id].lastLogTime = state.ts;
1452
1461
  if (settings.enableDebugLogs) {
1453
1462
  this.log.debug(`Value logged ${id}, value=${this.sqlDPs[id].state.val}, ts=${this.sqlDPs[id].state.ts}`);
@@ -1461,6 +1470,8 @@ class SqlAdapter extends adapter_core_1.Adapter {
1461
1470
  else {
1462
1471
  if (!timerRelog) {
1463
1472
  this.sqlDPs[id].state = state;
1473
+ // a newer value was logged, so any value dropped by blockTime before is obsolete
1474
+ this.sqlDPs[id].blocked = null;
1464
1475
  }
1465
1476
  this.sqlDPs[id].lastLogTime = state.ts;
1466
1477
  if (settings.enableDebugLogs) {
@@ -1482,6 +1493,11 @@ class SqlAdapter extends adapter_core_1.Adapter {
1482
1493
  if (this.sqlDPs[_id].skipped) {
1483
1494
  this.pushHistory(_id, this.sqlDPs[_id].skipped, true);
1484
1495
  }
1496
+ else if (this.sqlDPs[_id].blocked) {
1497
+ // a value dropped by blockTime is newer than the last logged one, so relog that
1498
+ // instead of repeating a value the datapoint has long left behind (issue #273)
1499
+ this.pushHistory(_id, this.sqlDPs[_id].blocked, true);
1500
+ }
1485
1501
  else if (this.sqlDPs[_id].state) {
1486
1502
  this.pushHistory(_id, this.sqlDPs[_id].state, true);
1487
1503
  }
@@ -1659,26 +1675,43 @@ class SqlAdapter extends adapter_core_1.Adapter {
1659
1675
  }
1660
1676
  }
1661
1677
  }
1662
- _insertValueIntoDB(query, id, cb) {
1663
- this.log.debug(query);
1678
+ /**
1679
+ * Write one batch of values.
1680
+ *
1681
+ * `sqlFuncs.insert()` chunks at 500 rows per statement and emits one statement per table, so a batch
1682
+ * can be more than one query. They must be sent one by one: no driver here is configured for
1683
+ * multi-statement batches (mysql2 needs `multipleStatements`, node-sqlite3's run() takes a single
1684
+ * statement), and enabling that would only widen the injection surface of the `query` message.
1685
+ * All statements share one borrowed connection, and the first failure aborts the rest - same as the
1686
+ * single concatenated query did before.
1687
+ */
1688
+ _insertValuesIntoDB(queries, id, cb) {
1664
1689
  this.borrowClientFromPool((err, client) => {
1665
1690
  if (err || !client) {
1666
1691
  this.returnClientToPool(client);
1667
1692
  this.log.error(err?.toString() || 'No client');
1668
1693
  cb?.(); // BF asked (2021.12.14): may be return here err?
1694
+ return;
1669
1695
  }
1670
- else {
1671
- client.execute(query, (err /* , rows, fields */) => {
1696
+ const next = (i) => {
1697
+ if (i >= queries.length) {
1672
1698
  this.returnClientToPool(client);
1699
+ this.checkRetention(id);
1700
+ cb?.(); // BF asked (2021.12.14): may be return here err?
1701
+ return;
1702
+ }
1703
+ this.log.debug(queries[i]);
1704
+ client.execute(queries[i], (err /* , rows, fields */) => {
1673
1705
  if (err) {
1674
- this.log.error(`Cannot insert ${query}: ${err} (id: ${id})`);
1675
- }
1676
- else {
1677
- this.checkRetention(id);
1706
+ this.returnClientToPool(client);
1707
+ this.log.error(`Cannot insert ${queries[i]}: ${err} (id: ${id})`);
1708
+ cb?.(); // BF asked (2021.12.14): may be return here err?
1709
+ return;
1678
1710
  }
1679
- cb?.(); // BF asked (2021.12.14): may be return here err?
1711
+ next(i + 1);
1680
1712
  });
1681
- }
1713
+ };
1714
+ next(0);
1682
1715
  });
1683
1716
  }
1684
1717
  _executeQuery(query, id, cb) {
@@ -2036,8 +2069,8 @@ class SqlAdapter extends adapter_core_1.Adapter {
2036
2069
  }
2037
2070
  }
2038
2071
  else {
2039
- const query = this.sqlFuncs.insert(this.config.dbname, this.sqlDPs[id].index, list);
2040
- this._insertValueIntoDB(query, id, cb);
2072
+ const queries = this.sqlFuncs.insert(this.config.dbname, this.sqlDPs[id].index, list);
2073
+ this._insertValuesIntoDB(queries, id, cb);
2041
2074
  }
2042
2075
  }
2043
2076
  processTasks() {
@@ -2076,8 +2109,8 @@ class SqlAdapter extends adapter_core_1.Adapter {
2076
2109
  }
2077
2110
  }
2078
2111
  }
2079
- const query = this.sqlFuncs.insert(this.config.dbname, this.tasks[0].index, this.tasks[0].list);
2080
- this._insertValueIntoDB(query, this.tasks[0].id, () => {
2112
+ const queries = this.sqlFuncs.insert(this.config.dbname, this.tasks[0].index, this.tasks[0].list);
2113
+ this._insertValuesIntoDB(queries, this.tasks[0].id, () => {
2081
2114
  callbacks.forEach(cb => cb());
2082
2115
  this.tasks.shift();
2083
2116
  this.lockTasks = false;