iobroker.sql 1.16.2 → 2.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.
@@ -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,40 @@ 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
+ }
47
+
48
+ if (value.db === 'ts_counter') {
49
+ insertValues[value.db].push(`(${index}, ${value.state.ts}, ${value.state.val})`);
50
+ } else {
51
+ insertValues[value.db].push(`(${index}, ${value.state.ts}, ${value.state.val}, ${(value.state.ack ? 1 : 0)}, ${value.from || 0}, ${value.state.q || 0})`);
52
+ }
53
+ });
54
+
55
+ let query = '';
56
+ for (let db in insertValues) {
57
+ if (db === 'ts_counter') {
58
+ while (insertValues[db].length) {
59
+ query += `INSERT INTO ${dbname}.dbo.ts_counter (id, ts, val) VALUES ${insertValues[db].splice(0, 500).join(',')};`;
60
+ }
61
+ } else {
62
+ while (insertValues[db].length) {
63
+ query += `INSERT INTO ${dbname}.dbo.${db} (id, ts, val, ack, _from, q) VALUES ${insertValues[db].splice(0, 500).join(',')};`;
64
+ }
65
+ }
49
66
  }
50
- };
67
+
68
+ return query;
69
+ }
51
70
 
52
71
  exports.retention = function (dbname, index, db, retention) {
53
72
  const d = new Date();
@@ -108,8 +127,8 @@ exports.getCounterDiff = function (dbname, options) {
108
127
  };
109
128
 
110
129
  exports.getHistory = function (dbname, db, options) {
111
- let query = `SELECT `;
112
- if (!options.start && options.count) {
130
+ let query = `SELECT * FROM (SELECT `;
131
+ if ((!options.start && options.count) || (options.aggregate === 'none' && options.count)) {
113
132
  query += ` TOP ` + options.count;
114
133
  }
115
134
  query += ` ts, val` +
@@ -132,8 +151,19 @@ exports.getHistory = function (dbname, db, options) {
132
151
  }
133
152
  if (options.start) {
134
153
  where += (where ? ` AND` : '') + ` ${dbname}.dbo.${db}.ts >= ` + options.start;
154
+ }
155
+ if ((!options.start && options.count) || (options.aggregate === 'none' && options.count)) {
156
+ where += ` ORDER BY ts`;
135
157
 
136
- // add last value before start
158
+ if ((!options.start && options.count) || (options.aggregate === 'none' && options.count && options.returnNewestEntries) ) {
159
+ where += ` DESC`;
160
+ } else {
161
+ where += ` ASC`;
162
+ }
163
+ }
164
+ where += `) AS t`;
165
+ if (options.start) {
166
+ // add last value before start
137
167
  let subQuery;
138
168
  let subWhere;
139
169
  subQuery = ` SELECT TOP 1 ts, val` +
@@ -181,7 +211,7 @@ exports.getHistory = function (dbname, db, options) {
181
211
  if (where) query += ` WHERE ` + where;
182
212
 
183
213
  query += ` ORDER BY ts`;
184
- if (!options.start && options.count) {
214
+ if ((!options.start && options.count) || (options.aggregate === 'none' && options.count && options.returnNewestEntries) ) {
185
215
  query += ` DESC`;
186
216
  } else {
187
217
  query += ` ASC`;
@@ -235,4 +265,4 @@ exports.update = function (dbname, index, state, from, db) {
235
265
  query += ";";
236
266
 
237
267
  return query;
238
- };
268
+ };
@@ -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);