iobroker.sql 1.16.1 → 2.1.0

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.
@@ -1,11 +1,11 @@
1
1
  // Generated by CoffeeScript 2.3.2
2
2
  (function() {
3
- let ConnectionFactory = require('sql-client/lib/connection-factory').ConnectionFactory;
4
- let SQLClient = require('sql-client/lib/sql-client').SQLClient;
5
- let SQLClientPool = require('sql-client/lib/sql-client-pool').SQLClientPool;
6
- let mssql = require('mssql');
3
+ const ConnectionFactory = require('sql-client/lib/connection-factory').ConnectionFactory;
4
+ const SQLClient = require('sql-client/lib/sql-client').SQLClient;
5
+ const SQLClientPool = require('sql-client/lib/sql-client-pool').SQLClientPool;
6
+ const mssql = require('mssql');
7
7
 
8
- let MSSQLConnectionFactory = class MSSQLConnectionFactory extends ConnectionFactory {
8
+ const MSSQLConnectionFactory = class MSSQLConnectionFactory extends ConnectionFactory {
9
9
  constructor() {
10
10
  super(...arguments);
11
11
  this.open_connection = this.open_connection.bind(this);
@@ -38,14 +38,14 @@
38
38
  };
39
39
  };
40
40
 
41
- let MSSQLClient = class MSSQLClient extends SQLClient {
41
+ const MSSQLClient = class MSSQLClient extends SQLClient {
42
42
  constructor(...options) {
43
43
  super(...options, new MSSQLConnectionFactory());
44
44
  }
45
45
 
46
46
  };
47
47
 
48
- let MSSQLClientPool = class MSSQLClientPool extends SQLClientPool {
48
+ const MSSQLClientPool = class MSSQLClientPool extends SQLClientPool {
49
49
  constructor(...options) {
50
50
  super(...options, new MSSQLConnectionFactory());
51
51
  }
package/lib/mssql.js CHANGED
@@ -33,21 +33,42 @@ exports.getFirstTs = function (dbname, db) {
33
33
  return `SELECT id, MIN(ts) AS ts FROM ${dbname}.dbo.${db} GROUP BY id;`;
34
34
  };
35
35
 
36
- exports.insert = function (dbname, index, state, from, db) {
37
- if (!state || state.val === null || state.val === undefined) {
38
- state.val = 'NULL';
39
- } else if (db === 'ts_bool') {
40
- state.val = state.val ? 1 : 0;
41
- } else if (db === 'ts_string') {
42
- state.val = `'${state.val.toString().replace(/'/g, '')}'`;
43
- }
44
-
45
- if (db === 'ts_counter') {
46
- return `INSERT INTO ${dbname}.dbo.${db} (id, ts, val, ack, _from, q) VALUES(` + index + `, ` + state.ts + `, ` + state.val + `);`;
47
- } else {
48
- return `INSERT INTO ${dbname}.dbo.${db} (id, ts, val, ack, _from, q) VALUES(${index}, ${state.ts}, ${state.val}, ${state.ack ? 1 : 0}, ${from || 0}, ${state.q || 0});`;
36
+ exports.insert = function (dbname, index, values) {
37
+ const insertValues = {}
38
+ values.forEach(value => {
39
+ // state, from, db
40
+ insertValues[value.db] = insertValues[value.db] || [];
41
+
42
+ if (!value.state || value.state.val === null || value.state.val === undefined) {
43
+ value.state.val = "NULL";
44
+ } else if (value.db === "ts_string") {
45
+ value.state.val = "'" + value.state.val.toString().replace(/'/g, '') + "'";
46
+ } else if (value.db === "ts_bool") {
47
+ value.state.val = value.state.val ? 1 : 0;
48
+ }
49
+
50
+ if (value.db === 'ts_counter') {
51
+ insertValues[value.db].push(`(${index}, ${value.state.ts}, ${value.state.val})`);
52
+ } else {
53
+ insertValues[value.db].push(`(${index}, ${value.state.ts}, ${value.state.val}, ${(value.state.ack ? 1 : 0)}, ${value.from || 0}, ${value.state.q || 0})`);
54
+ }
55
+ });
56
+
57
+ let query = '';
58
+ for (let db in insertValues) {
59
+ if (db === 'ts_counter') {
60
+ while (insertValues[db].length) {
61
+ query += `INSERT INTO ${dbname}.dbo.ts_counter (id, ts, val) VALUES ${insertValues[db].splice(0, 500).join(',')};`;
62
+ }
63
+ } else {
64
+ while (insertValues[db].length) {
65
+ query += `INSERT INTO ${dbname}.dbo.${db} (id, ts, val, ack, _from, q) VALUES ${insertValues[db].splice(0, 500).join(',')};`;
66
+ }
67
+ }
49
68
  }
50
- };
69
+
70
+ return query;
71
+ }
51
72
 
52
73
  exports.retention = function (dbname, index, db, retention) {
53
74
  const d = new Date();
@@ -108,8 +129,8 @@ exports.getCounterDiff = function (dbname, options) {
108
129
  };
109
130
 
110
131
  exports.getHistory = function (dbname, db, options) {
111
- let query = `SELECT `;
112
- if (!options.start && options.count) {
132
+ let query = `SELECT * FROM (SELECT `;
133
+ if ((!options.start && options.count) || (options.aggregate === 'none' && options.count)) {
113
134
  query += ` TOP ` + options.count;
114
135
  }
115
136
  query += ` ts, val` +
@@ -132,8 +153,19 @@ exports.getHistory = function (dbname, db, options) {
132
153
  }
133
154
  if (options.start) {
134
155
  where += (where ? ` AND` : '') + ` ${dbname}.dbo.${db}.ts >= ` + options.start;
156
+ }
157
+ if ((!options.start && options.count) || (options.aggregate === 'none' && options.count)) {
158
+ where += ` ORDER BY ts`;
135
159
 
136
- // add last value before start
160
+ if ((!options.start && options.count) || (options.aggregate === 'none' && options.count && options.returnNewestEntries) ) {
161
+ where += ` DESC`;
162
+ } else {
163
+ where += ` ASC`;
164
+ }
165
+ }
166
+ where += `) AS t`;
167
+ if (options.start) {
168
+ // add last value before start
137
169
  let subQuery;
138
170
  let subWhere;
139
171
  subQuery = ` SELECT TOP 1 ts, val` +
@@ -181,7 +213,7 @@ exports.getHistory = function (dbname, db, options) {
181
213
  if (where) query += ` WHERE ` + where;
182
214
 
183
215
  query += ` ORDER BY ts`;
184
- if (!options.start && options.count) {
216
+ if ((!options.start && options.count) || (options.aggregate === 'none' && options.count && options.returnNewestEntries) ) {
185
217
  query += ` DESC`;
186
218
  } else {
187
219
  query += ` ASC`;
@@ -235,4 +267,4 @@ exports.update = function (dbname, index, state, from, db) {
235
267
  query += ";";
236
268
 
237
269
  return query;
238
- };
270
+ };
@@ -0,0 +1,62 @@
1
+ // Generated by CoffeeScript 2.6.0
2
+ (function() {
3
+ const ConnectionFactory = require('sql-client/lib/connection-factory').ConnectionFactory;
4
+ const SQLClient = require('sql-client/lib/sql-client').SQLClient;
5
+ const SQLClientPool = require('sql-client/lib/sql-client-pool').SQLClientPool;
6
+ const mysql2 = require('mysql2');
7
+
8
+ const MySQL2ConnectionFactory = class MySQL2ConnectionFactory extends ConnectionFactory {
9
+ constructor() {
10
+ super(...arguments);
11
+ this.open_connection = this.open_connection.bind(this);
12
+ this.close_connection = this.close_connection.bind(this);
13
+ }
14
+
15
+ open_connection(options, callback) {
16
+ const opt = Object.assign({}, options);
17
+ delete opt.max_idle;
18
+ delete opt.max_active;
19
+ delete opt.max_wait;
20
+ delete opt.when_exhausted;
21
+ delete opt.options;
22
+ delete opt.server;
23
+
24
+ let connection = mysql2.createConnection(opt);
25
+ return connection.connect((err) => {
26
+ return callback(err, connection);
27
+ });
28
+ }
29
+
30
+ close_connection(connection, callback) {
31
+ if (typeof (connection != null ? connection.end : void 0) === 'function') {
32
+ return connection.end(callback);
33
+ } else {
34
+ return super.close_connection(connection, callback);
35
+ }
36
+ }
37
+
38
+ };
39
+
40
+ const MySQL2Client = class MySQL2Client extends SQLClient {
41
+ constructor(...options) {
42
+
43
+ super(...options, new MySQL2ConnectionFactory());
44
+ }
45
+
46
+ };
47
+
48
+ const MySQL2ClientPool = class MySQL2ClientPool extends SQLClientPool {
49
+ constructor(...options) {
50
+
51
+ super(...options, new MySQL2ConnectionFactory());
52
+ }
53
+
54
+ };
55
+
56
+ exports.MySQL2ConnectionFactory = MySQL2ConnectionFactory;
57
+
58
+ exports.MySQL2Client = MySQL2Client;
59
+
60
+ exports.MySQL2ClientPool = MySQL2ClientPool;
61
+
62
+ }).call(this);