mysql2-core 0.3.3 → 0.3.5
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/README.md +608 -1
- package/lib/build.js +93 -107
- package/lib/index.js +635 -849
- package/package.json +1 -1
- package/src/build.ts +5 -5
- package/tsconfig.json +4 -4
package/lib/index.js
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -19,57 +7,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
19
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
8
|
});
|
|
21
9
|
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
function __export(m) {
|
|
50
|
-
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
10
|
+
import { createPool as createPool2 } from "mysql2";
|
|
11
|
+
import { buildToSave, buildToSaveBatch } from "./build";
|
|
12
|
+
export * from "./build";
|
|
13
|
+
export class resource {
|
|
51
14
|
}
|
|
52
|
-
|
|
53
|
-
var mysql2_1 = require("mysql2");
|
|
54
|
-
var build_1 = require("./build");
|
|
55
|
-
__export(require("./build"));
|
|
56
|
-
var resource = (function () {
|
|
57
|
-
function resource() {
|
|
58
|
-
}
|
|
59
|
-
return resource;
|
|
60
|
-
}());
|
|
61
|
-
exports.resource = resource;
|
|
62
|
-
function createPool(conf) {
|
|
15
|
+
export function createPool(conf) {
|
|
63
16
|
if (conf.max && conf.max > 0 && !conf.connectionLimit) {
|
|
64
17
|
conf.connectionLimit = conf.max;
|
|
65
18
|
}
|
|
66
|
-
|
|
19
|
+
const pool = createPool2(Object.assign(Object.assign({}, conf), { rowsAsArray: true }));
|
|
67
20
|
return pool;
|
|
68
21
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
pool.getConnection(function (err, connection) {
|
|
22
|
+
export function getConnection(pool) {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
pool.getConnection((err, connection) => {
|
|
73
25
|
if (err) {
|
|
74
26
|
return reject(err);
|
|
75
27
|
}
|
|
@@ -77,15 +29,14 @@ function getConnection(pool) {
|
|
|
77
29
|
});
|
|
78
30
|
});
|
|
79
31
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
connection.beginTransaction(function (err) {
|
|
32
|
+
export function beginTransaction(connection, rollbackIfError) {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
connection.beginTransaction((err) => {
|
|
84
35
|
if (err) {
|
|
85
36
|
if (!rollbackIfError) {
|
|
86
37
|
return reject(err);
|
|
87
38
|
}
|
|
88
|
-
connection.rollback(
|
|
39
|
+
connection.rollback(() => {
|
|
89
40
|
return reject(err);
|
|
90
41
|
});
|
|
91
42
|
}
|
|
@@ -93,15 +44,14 @@ function beginTransaction(connection, rollbackIfError) {
|
|
|
93
44
|
});
|
|
94
45
|
});
|
|
95
46
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
connection.commit(function (err) {
|
|
47
|
+
export function commit(connection, rollbackIfError) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
connection.commit((err) => {
|
|
100
50
|
if (err) {
|
|
101
51
|
if (!rollbackIfError) {
|
|
102
52
|
return reject(err);
|
|
103
53
|
}
|
|
104
|
-
connection.rollback(
|
|
54
|
+
connection.rollback(() => {
|
|
105
55
|
return reject(err);
|
|
106
56
|
});
|
|
107
57
|
}
|
|
@@ -109,10 +59,9 @@ function commit(connection, rollbackIfError) {
|
|
|
109
59
|
});
|
|
110
60
|
});
|
|
111
61
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
connection.rollback(function (err) {
|
|
62
|
+
export function rollback(connection) {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
connection.rollback((err) => {
|
|
116
65
|
if (err) {
|
|
117
66
|
return reject(err);
|
|
118
67
|
}
|
|
@@ -120,9 +69,8 @@ function rollback(connection) {
|
|
|
120
69
|
});
|
|
121
70
|
});
|
|
122
71
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
function PoolManager(pool) {
|
|
72
|
+
export class PoolManager {
|
|
73
|
+
constructor(pool) {
|
|
126
74
|
this.pool = pool;
|
|
127
75
|
this.driver = "mysql";
|
|
128
76
|
this.param = this.param.bind(this);
|
|
@@ -133,57 +81,54 @@ var PoolManager = (function () {
|
|
|
133
81
|
this.executeScalar = this.executeScalar.bind(this);
|
|
134
82
|
this.count = this.count.bind(this);
|
|
135
83
|
}
|
|
136
|
-
|
|
84
|
+
param(i) {
|
|
137
85
|
return "?";
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
_this.pool.getConnection(function (er0, connection) {
|
|
86
|
+
}
|
|
87
|
+
beginTransaction() {
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
this.pool.getConnection((er0, connection) => {
|
|
143
90
|
if (er0) {
|
|
144
91
|
return reject(er0);
|
|
145
92
|
}
|
|
146
|
-
connection.beginTransaction(
|
|
93
|
+
connection.beginTransaction((er1) => {
|
|
147
94
|
if (er1) {
|
|
148
|
-
connection.rollback(
|
|
95
|
+
connection.rollback(() => {
|
|
149
96
|
return reject(er1);
|
|
150
97
|
});
|
|
151
98
|
}
|
|
152
|
-
|
|
99
|
+
const tx = new PoolConnectionManager(connection);
|
|
153
100
|
return resolve(tx);
|
|
154
101
|
});
|
|
155
102
|
});
|
|
156
103
|
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
104
|
+
}
|
|
105
|
+
execute(sql, args, ctx) {
|
|
106
|
+
const p = ctx ? ctx : this.pool;
|
|
160
107
|
return execute(p, sql, args);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
108
|
+
}
|
|
109
|
+
executeBatch(statements, firstSuccess, ctx) {
|
|
110
|
+
const p = ctx ? ctx : this.pool;
|
|
164
111
|
return executeBatch(p, statements, firstSuccess);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
112
|
+
}
|
|
113
|
+
query(sql, args, m, bools, ctx) {
|
|
114
|
+
const p = ctx ? ctx : this.pool;
|
|
168
115
|
return query(p, sql, args, m, bools);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
116
|
+
}
|
|
117
|
+
queryOne(sql, args, m, bools, ctx) {
|
|
118
|
+
const p = ctx ? ctx : this.pool;
|
|
172
119
|
return queryOne(p, sql, args, m, bools);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
120
|
+
}
|
|
121
|
+
executeScalar(sql, args, ctx) {
|
|
122
|
+
const p = ctx ? ctx : this.pool;
|
|
176
123
|
return executeScalar(p, sql, args);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
124
|
+
}
|
|
125
|
+
count(sql, args, ctx) {
|
|
126
|
+
const p = ctx ? ctx : this.pool;
|
|
180
127
|
return count(p, sql, args);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
var PoolConnectionManager = (function () {
|
|
186
|
-
function PoolConnectionManager(connection) {
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
export class PoolConnectionManager {
|
|
131
|
+
constructor(connection) {
|
|
187
132
|
this.connection = connection;
|
|
188
133
|
this.driver = "mysql";
|
|
189
134
|
this.param = this.param.bind(this);
|
|
@@ -194,343 +139,267 @@ var PoolConnectionManager = (function () {
|
|
|
194
139
|
this.executeScalar = this.executeScalar.bind(this);
|
|
195
140
|
this.count = this.count.bind(this);
|
|
196
141
|
}
|
|
197
|
-
|
|
142
|
+
param(i) {
|
|
198
143
|
return "?";
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
_this.connection.commit(function (er3) {
|
|
144
|
+
}
|
|
145
|
+
commit() {
|
|
146
|
+
return new Promise((resolve, reject) => {
|
|
147
|
+
this.connection.commit((er3) => {
|
|
204
148
|
if (er3) {
|
|
205
149
|
return reject(er3);
|
|
206
150
|
}
|
|
207
151
|
return resolve();
|
|
208
152
|
});
|
|
209
153
|
});
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
_this.connection.rollback(function (er3) {
|
|
154
|
+
}
|
|
155
|
+
rollback() {
|
|
156
|
+
return new Promise((resolve, reject) => {
|
|
157
|
+
this.connection.rollback((er3) => {
|
|
215
158
|
if (er3) {
|
|
216
159
|
return reject(er3);
|
|
217
160
|
}
|
|
218
161
|
return resolve();
|
|
219
162
|
});
|
|
220
163
|
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
164
|
+
}
|
|
165
|
+
execute(sql, args, ctx) {
|
|
166
|
+
const p = ctx ? ctx : this.connection;
|
|
224
167
|
return execute(p, sql, args);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
|
|
168
|
+
}
|
|
169
|
+
executeBatch(statements, firstSuccess, ctx) {
|
|
170
|
+
const p = ctx ? ctx : this.connection;
|
|
228
171
|
return executeBatchConnection(p, statements, firstSuccess);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
|
|
172
|
+
}
|
|
173
|
+
query(sql, args, m, bools, ctx) {
|
|
174
|
+
const p = ctx ? ctx : this.connection;
|
|
232
175
|
return query(p, sql, args, m, bools);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
176
|
+
}
|
|
177
|
+
queryOne(sql, args, m, bools, ctx) {
|
|
178
|
+
const p = ctx ? ctx : this.connection;
|
|
236
179
|
return queryOne(p, sql, args, m, bools);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
180
|
+
}
|
|
181
|
+
executeScalar(sql, args, ctx) {
|
|
182
|
+
const p = ctx ? ctx : this.connection;
|
|
240
183
|
return executeScalar(p, sql, args);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
|
|
184
|
+
}
|
|
185
|
+
count(sql, args, ctx) {
|
|
186
|
+
const p = ctx ? ctx : this.connection;
|
|
244
187
|
return count(p, sql, args);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
return executeBatchConnectionTx(connection, statements, firstSuccess);
|
|
264
|
-
});
|
|
265
|
-
})];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
export function executeBatch(pool, statements, firstSuccess) {
|
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
+
if (!statements || statements.length === 0) {
|
|
193
|
+
return Promise.resolve(0);
|
|
194
|
+
}
|
|
195
|
+
else if (statements.length === 1) {
|
|
196
|
+
return execute(pool, statements[0].query, statements[0].params);
|
|
197
|
+
}
|
|
198
|
+
return new Promise((resolve, reject) => {
|
|
199
|
+
pool.getConnection((er0, connection) => {
|
|
200
|
+
if (er0) {
|
|
201
|
+
return reject(er0);
|
|
202
|
+
}
|
|
203
|
+
return executeBatchConnectionTx(connection, statements, firstSuccess);
|
|
204
|
+
});
|
|
266
205
|
});
|
|
267
206
|
});
|
|
268
207
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
208
|
+
export function executeBatchConnectionTx(connection, statements, firstSuccess) {
|
|
209
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
210
|
+
if (!statements || statements.length === 0) {
|
|
211
|
+
return Promise.resolve(0);
|
|
212
|
+
}
|
|
213
|
+
else if (statements.length === 1) {
|
|
214
|
+
return execute(connection, statements[0].query, statements[0].params);
|
|
215
|
+
}
|
|
216
|
+
if (resource.multipleStatements) {
|
|
217
|
+
if (firstSuccess) {
|
|
218
|
+
return new Promise((resolve, reject) => {
|
|
219
|
+
connection.beginTransaction((er1) => {
|
|
220
|
+
if (er1) {
|
|
221
|
+
connection.rollback(() => {
|
|
222
|
+
return reject(er1);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
connection.execute(statements[0].query, toArray(statements[0].params), (er2a, results0) => {
|
|
227
|
+
if (er2a) {
|
|
228
|
+
connection.rollback(() => {
|
|
229
|
+
return reject(er2a);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
if (results0 && results0.affectedRows === 0) {
|
|
234
|
+
connection.commit((er3) => {
|
|
235
|
+
if (er3) {
|
|
236
|
+
connection.rollback(() => {
|
|
237
|
+
return reject(er3);
|
|
296
238
|
});
|
|
297
239
|
}
|
|
298
|
-
else {
|
|
299
|
-
if (results0 && results0.affectedRows === 0) {
|
|
300
|
-
connection.commit(function (er3) {
|
|
301
|
-
if (er3) {
|
|
302
|
-
connection.rollback(function () {
|
|
303
|
-
return reject(er3);
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
return 0;
|
|
308
|
-
}
|
|
309
|
-
else {
|
|
310
|
-
var queries = [];
|
|
311
|
-
var params = [];
|
|
312
|
-
var l = statements.length;
|
|
313
|
-
for (var j = 1; j < l; j++) {
|
|
314
|
-
var item = statements[j];
|
|
315
|
-
if (item.query.endsWith(";")) {
|
|
316
|
-
queries.push(item.query);
|
|
317
|
-
}
|
|
318
|
-
else {
|
|
319
|
-
queries.push(item.query + ";");
|
|
320
|
-
}
|
|
321
|
-
if (item.params && item.params.length > 0) {
|
|
322
|
-
for (var _i = 0, _a = item.params; _i < _a.length; _i++) {
|
|
323
|
-
var p = _a[_i];
|
|
324
|
-
params.push(p);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
connection.query(queries.join(""), toArray(params), function (er2, results) {
|
|
329
|
-
if (er2) {
|
|
330
|
-
connection.rollback(function () {
|
|
331
|
-
return reject(er2);
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
else {
|
|
335
|
-
connection.commit(function (er3) {
|
|
336
|
-
if (er3) {
|
|
337
|
-
connection.rollback(function () {
|
|
338
|
-
return reject(er3);
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
var c = 0;
|
|
343
|
-
c += results0.affectedRows + results.affectedRows;
|
|
344
|
-
return resolve(c);
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
});
|
|
352
|
-
})];
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
return [2, new Promise(function (resolve, reject) {
|
|
356
|
-
connection.beginTransaction(function (er1) {
|
|
357
|
-
if (er1) {
|
|
358
|
-
connection.rollback(function () {
|
|
359
|
-
return reject(er1);
|
|
360
240
|
});
|
|
241
|
+
return 0;
|
|
361
242
|
}
|
|
362
243
|
else {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
statements.
|
|
244
|
+
const queries = [];
|
|
245
|
+
const params = [];
|
|
246
|
+
const l = statements.length;
|
|
247
|
+
for (let j = 1; j < l; j++) {
|
|
248
|
+
const item = statements[j];
|
|
366
249
|
if (item.query.endsWith(";")) {
|
|
367
|
-
|
|
250
|
+
queries.push(item.query);
|
|
368
251
|
}
|
|
369
252
|
else {
|
|
370
|
-
|
|
253
|
+
queries.push(item.query + ";");
|
|
371
254
|
}
|
|
372
255
|
if (item.params && item.params.length > 0) {
|
|
373
|
-
for (
|
|
374
|
-
|
|
375
|
-
params_1.push(p);
|
|
256
|
+
for (const p of item.params) {
|
|
257
|
+
params.push(p);
|
|
376
258
|
}
|
|
377
259
|
}
|
|
378
|
-
}
|
|
379
|
-
connection.query(
|
|
260
|
+
}
|
|
261
|
+
connection.query(queries.join(""), toArray(params), (er2, results) => {
|
|
380
262
|
if (er2) {
|
|
381
|
-
connection.rollback(
|
|
382
|
-
buildError(er2);
|
|
263
|
+
connection.rollback(() => {
|
|
383
264
|
return reject(er2);
|
|
384
265
|
});
|
|
385
266
|
}
|
|
386
267
|
else {
|
|
387
|
-
connection.commit(
|
|
268
|
+
connection.commit((er3) => {
|
|
388
269
|
if (er3) {
|
|
389
|
-
connection.rollback(
|
|
270
|
+
connection.rollback(() => {
|
|
390
271
|
return reject(er3);
|
|
391
272
|
});
|
|
392
273
|
}
|
|
393
274
|
});
|
|
394
|
-
|
|
275
|
+
let c = 0;
|
|
276
|
+
c += results0.affectedRows + results.affectedRows;
|
|
277
|
+
return resolve(c);
|
|
395
278
|
}
|
|
396
279
|
});
|
|
397
280
|
}
|
|
398
|
-
}
|
|
399
|
-
})
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
return new Promise((resolve, reject) => {
|
|
289
|
+
connection.beginTransaction((er1) => {
|
|
290
|
+
if (er1) {
|
|
291
|
+
connection.rollback(() => {
|
|
292
|
+
return reject(er1);
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
const queries = [];
|
|
297
|
+
const params = [];
|
|
298
|
+
statements.forEach((item) => {
|
|
299
|
+
if (item.query.endsWith(";")) {
|
|
300
|
+
queries.push(item.query);
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
queries.push(item.query + ";");
|
|
304
|
+
}
|
|
305
|
+
if (item.params && item.params.length > 0) {
|
|
306
|
+
for (const p of item.params) {
|
|
307
|
+
params.push(p);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
connection.query(queries.join(""), toArray(params), (er2, results) => {
|
|
312
|
+
if (er2) {
|
|
313
|
+
connection.rollback(() => {
|
|
314
|
+
buildError(er2);
|
|
315
|
+
return reject(er2);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
connection.commit((er3) => {
|
|
320
|
+
if (er3) {
|
|
321
|
+
connection.rollback(() => {
|
|
322
|
+
return reject(er3);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
return resolve(results.affectedRows);
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
if (firstSuccess) {
|
|
336
|
+
try {
|
|
337
|
+
yield beginTransaction(connection, true);
|
|
338
|
+
let count = yield execute(connection, statements[0].query, toArray(statements[0].params));
|
|
339
|
+
if (count === 0) {
|
|
414
340
|
commit(connection, true);
|
|
415
|
-
return
|
|
341
|
+
return 0;
|
|
342
|
+
}
|
|
343
|
+
const l = statements.length;
|
|
344
|
+
for (let j = 1; j < l; j++) {
|
|
345
|
+
const item = statements[j];
|
|
346
|
+
const c = yield execute(connection, item.query, toArray(item.params));
|
|
347
|
+
count = count + c;
|
|
416
348
|
}
|
|
417
|
-
l = statements.length;
|
|
418
|
-
j = 1;
|
|
419
|
-
_a.label = 5;
|
|
420
|
-
case 5:
|
|
421
|
-
if (!(j < l)) return [3, 8];
|
|
422
|
-
item = statements[j];
|
|
423
|
-
return [4, execute(connection, item.query, toArray(item.params))];
|
|
424
|
-
case 6:
|
|
425
|
-
c = _a.sent();
|
|
426
|
-
count_1 = count_1 + c;
|
|
427
|
-
_a.label = 7;
|
|
428
|
-
case 7:
|
|
429
|
-
j++;
|
|
430
|
-
return [3, 5];
|
|
431
|
-
case 8:
|
|
432
349
|
commit(connection, true);
|
|
433
|
-
return
|
|
434
|
-
|
|
435
|
-
|
|
350
|
+
return count;
|
|
351
|
+
}
|
|
352
|
+
catch (err) {
|
|
436
353
|
rollback(connection);
|
|
437
|
-
throw
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
item = statements[j];
|
|
451
|
-
return [4, execute(connection, item.query, toArray(item.params))];
|
|
452
|
-
case 14:
|
|
453
|
-
c = _a.sent();
|
|
454
|
-
count_2 = count_2 + c;
|
|
455
|
-
_a.label = 15;
|
|
456
|
-
case 15:
|
|
457
|
-
j++;
|
|
458
|
-
return [3, 13];
|
|
459
|
-
case 16:
|
|
354
|
+
throw err;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
try {
|
|
359
|
+
yield beginTransaction(connection, true);
|
|
360
|
+
let count = 0;
|
|
361
|
+
const l = statements.length;
|
|
362
|
+
for (let j = 0; j < l; j++) {
|
|
363
|
+
const item = statements[j];
|
|
364
|
+
const c = yield execute(connection, item.query, toArray(item.params));
|
|
365
|
+
count = count + c;
|
|
366
|
+
}
|
|
460
367
|
commit(connection, true);
|
|
461
|
-
return
|
|
462
|
-
|
|
463
|
-
|
|
368
|
+
return count;
|
|
369
|
+
}
|
|
370
|
+
catch (err) {
|
|
464
371
|
rollback(connection);
|
|
465
|
-
throw
|
|
466
|
-
|
|
372
|
+
throw err;
|
|
373
|
+
}
|
|
467
374
|
}
|
|
468
|
-
}
|
|
375
|
+
}
|
|
469
376
|
});
|
|
470
377
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
var queries = [];
|
|
497
|
-
var params = [];
|
|
498
|
-
var l = statements.length;
|
|
499
|
-
for (var j = 1; j < l; j++) {
|
|
500
|
-
var item = statements[j];
|
|
501
|
-
if (item.query.endsWith(";")) {
|
|
502
|
-
queries.push(item.query);
|
|
503
|
-
}
|
|
504
|
-
else {
|
|
505
|
-
queries.push(item.query + ";");
|
|
506
|
-
}
|
|
507
|
-
if (item.params && item.params.length > 0) {
|
|
508
|
-
for (var _i = 0, _a = item.params; _i < _a.length; _i++) {
|
|
509
|
-
var p = _a[_i];
|
|
510
|
-
params.push(p);
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
connection.query(queries.join(""), toArray(params), function (er2, results) {
|
|
515
|
-
if (er2) {
|
|
516
|
-
return reject(er2);
|
|
517
|
-
}
|
|
518
|
-
else {
|
|
519
|
-
var c = 0;
|
|
520
|
-
c += results0.affectedRows + results.affectedRows;
|
|
521
|
-
return resolve(c);
|
|
522
|
-
}
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
})];
|
|
528
|
-
}
|
|
529
|
-
else {
|
|
530
|
-
return [2, new Promise(function (resolve, reject) {
|
|
531
|
-
var queries = [];
|
|
532
|
-
var params = [];
|
|
533
|
-
statements.forEach(function (item) {
|
|
378
|
+
export function executeBatchConnection(connection, statements, firstSuccess) {
|
|
379
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
380
|
+
if (!statements || statements.length === 0) {
|
|
381
|
+
return Promise.resolve(0);
|
|
382
|
+
}
|
|
383
|
+
else if (statements.length === 1) {
|
|
384
|
+
return execute(connection, statements[0].query, statements[0].params);
|
|
385
|
+
}
|
|
386
|
+
if (resource.multipleStatements) {
|
|
387
|
+
if (firstSuccess) {
|
|
388
|
+
return new Promise((resolve, reject) => {
|
|
389
|
+
connection.execute(statements[0].query, toArray(statements[0].params), (er2a, results0) => {
|
|
390
|
+
if (er2a) {
|
|
391
|
+
return reject(er2a);
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
if (results0 && results0.affectedRows === 0) {
|
|
395
|
+
return 0;
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
const queries = [];
|
|
399
|
+
const params = [];
|
|
400
|
+
const l = statements.length;
|
|
401
|
+
for (let j = 1; j < l; j++) {
|
|
402
|
+
const item = statements[j];
|
|
534
403
|
if (item.query.endsWith(";")) {
|
|
535
404
|
queries.push(item.query);
|
|
536
405
|
}
|
|
@@ -538,181 +407,177 @@ function executeBatchConnection(connection, statements, firstSuccess) {
|
|
|
538
407
|
queries.push(item.query + ";");
|
|
539
408
|
}
|
|
540
409
|
if (item.params && item.params.length > 0) {
|
|
541
|
-
for (
|
|
542
|
-
var p = _a[_i];
|
|
410
|
+
for (const p of item.params) {
|
|
543
411
|
params.push(p);
|
|
544
412
|
}
|
|
545
413
|
}
|
|
546
|
-
}
|
|
547
|
-
connection.query(queries.join(""), toArray(params),
|
|
414
|
+
}
|
|
415
|
+
connection.query(queries.join(""), toArray(params), (er2, results) => {
|
|
548
416
|
if (er2) {
|
|
549
|
-
|
|
550
|
-
buildError(er2);
|
|
551
|
-
return reject(er2);
|
|
552
|
-
});
|
|
417
|
+
return reject(er2);
|
|
553
418
|
}
|
|
554
419
|
else {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
return reject(er3);
|
|
559
|
-
});
|
|
560
|
-
}
|
|
561
|
-
});
|
|
562
|
-
return resolve(results.affectedRows);
|
|
420
|
+
let c = 0;
|
|
421
|
+
c += results0.affectedRows + results.affectedRows;
|
|
422
|
+
return resolve(c);
|
|
563
423
|
}
|
|
564
424
|
});
|
|
565
|
-
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
return new Promise((resolve, reject) => {
|
|
432
|
+
const queries = [];
|
|
433
|
+
const params = [];
|
|
434
|
+
statements.forEach((item) => {
|
|
435
|
+
if (item.query.endsWith(";")) {
|
|
436
|
+
queries.push(item.query);
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
queries.push(item.query + ";");
|
|
440
|
+
}
|
|
441
|
+
if (item.params && item.params.length > 0) {
|
|
442
|
+
for (const p of item.params) {
|
|
443
|
+
params.push(p);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
connection.query(queries.join(""), toArray(params), (er2, results) => {
|
|
448
|
+
if (er2) {
|
|
449
|
+
connection.rollback(() => {
|
|
450
|
+
buildError(er2);
|
|
451
|
+
return reject(er2);
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
connection.commit((er3) => {
|
|
456
|
+
if (er3) {
|
|
457
|
+
connection.rollback(() => {
|
|
458
|
+
return reject(er3);
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
return resolve(results.affectedRows);
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
if (firstSuccess) {
|
|
470
|
+
try {
|
|
471
|
+
let count = yield execute(connection, statements[0].query, toArray(statements[0].params));
|
|
472
|
+
if (count === 0) {
|
|
473
|
+
return 0;
|
|
474
|
+
}
|
|
475
|
+
const l = statements.length;
|
|
476
|
+
try {
|
|
477
|
+
for (let j = 1; j < l; j++) {
|
|
478
|
+
const item = statements[j];
|
|
479
|
+
const c = yield execute(connection, item.query, toArray(item.params));
|
|
480
|
+
count = count + c;
|
|
481
|
+
}
|
|
482
|
+
return count;
|
|
566
483
|
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
if (!firstSuccess) return [3, 13];
|
|
570
|
-
_a.label = 2;
|
|
571
|
-
case 2:
|
|
572
|
-
_a.trys.push([2, 11, , 12]);
|
|
573
|
-
return [4, execute(connection, statements[0].query, toArray(statements[0].params))];
|
|
574
|
-
case 3:
|
|
575
|
-
count_3 = _a.sent();
|
|
576
|
-
if (count_3 === 0) {
|
|
577
|
-
return [2, 0];
|
|
484
|
+
catch (er1) {
|
|
485
|
+
throw er1;
|
|
578
486
|
}
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
j = 1;
|
|
584
|
-
_a.label = 5;
|
|
585
|
-
case 5:
|
|
586
|
-
if (!(j < l)) return [3, 8];
|
|
587
|
-
item = statements[j];
|
|
588
|
-
return [4, execute(connection, item.query, toArray(item.params))];
|
|
589
|
-
case 6:
|
|
590
|
-
c = _a.sent();
|
|
591
|
-
count_3 = count_3 + c;
|
|
592
|
-
_a.label = 7;
|
|
593
|
-
case 7:
|
|
594
|
-
j++;
|
|
595
|
-
return [3, 5];
|
|
596
|
-
case 8: return [2, count_3];
|
|
597
|
-
case 9:
|
|
598
|
-
er1_1 = _a.sent();
|
|
599
|
-
throw er1_1;
|
|
600
|
-
case 10: return [3, 12];
|
|
601
|
-
case 11:
|
|
602
|
-
er0_1 = _a.sent();
|
|
603
|
-
throw er0_1;
|
|
604
|
-
case 12: return [3, 20];
|
|
605
|
-
case 13:
|
|
606
|
-
count_4 = 0;
|
|
607
|
-
l = statements.length;
|
|
608
|
-
_a.label = 14;
|
|
609
|
-
case 14:
|
|
610
|
-
_a.trys.push([14, 19, , 20]);
|
|
611
|
-
j = 0;
|
|
612
|
-
_a.label = 15;
|
|
613
|
-
case 15:
|
|
614
|
-
if (!(j < l)) return [3, 18];
|
|
615
|
-
item = statements[j];
|
|
616
|
-
return [4, execute(connection, item.query, toArray(item.params))];
|
|
617
|
-
case 16:
|
|
618
|
-
c = _a.sent();
|
|
619
|
-
count_4 = count_4 + c;
|
|
620
|
-
_a.label = 17;
|
|
621
|
-
case 17:
|
|
622
|
-
j++;
|
|
623
|
-
return [3, 15];
|
|
624
|
-
case 18: return [2, count_4];
|
|
625
|
-
case 19:
|
|
626
|
-
err_3 = _a.sent();
|
|
627
|
-
throw err_3;
|
|
628
|
-
case 20: return [2];
|
|
487
|
+
}
|
|
488
|
+
catch (er0) {
|
|
489
|
+
throw er0;
|
|
490
|
+
}
|
|
629
491
|
}
|
|
630
|
-
|
|
492
|
+
else {
|
|
493
|
+
let count = 0;
|
|
494
|
+
const l = statements.length;
|
|
495
|
+
try {
|
|
496
|
+
for (let j = 0; j < l; j++) {
|
|
497
|
+
const item = statements[j];
|
|
498
|
+
const c = yield execute(connection, item.query, toArray(item.params));
|
|
499
|
+
count = count + c;
|
|
500
|
+
}
|
|
501
|
+
return count;
|
|
502
|
+
}
|
|
503
|
+
catch (err) {
|
|
504
|
+
throw err;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
631
508
|
});
|
|
632
509
|
}
|
|
633
|
-
exports.executeBatchConnection = executeBatchConnection;
|
|
634
510
|
function buildError(err) {
|
|
635
511
|
if (err.errno === 1062 && err.code === "ER_DUP_ENTRY") {
|
|
636
512
|
err.error = "duplicate";
|
|
637
513
|
}
|
|
638
514
|
return err;
|
|
639
515
|
}
|
|
640
|
-
function execute(pool, sql, args) {
|
|
641
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
642
|
-
|
|
643
|
-
return
|
|
644
|
-
p
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
}
|
|
654
|
-
});
|
|
655
|
-
})];
|
|
516
|
+
export function execute(pool, sql, args) {
|
|
517
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
518
|
+
const p = toArray(args);
|
|
519
|
+
return new Promise((resolve, reject) => {
|
|
520
|
+
return pool.execute(sql, p, (err, res) => {
|
|
521
|
+
if (err) {
|
|
522
|
+
buildError(err);
|
|
523
|
+
return reject(err);
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
return resolve(res.affectedRows);
|
|
527
|
+
}
|
|
528
|
+
});
|
|
656
529
|
});
|
|
657
530
|
});
|
|
658
531
|
}
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
var arrayResult = results.map(function (item) {
|
|
673
|
-
if (Array.isArray(item)) {
|
|
674
|
-
return formatData(fields, item);
|
|
675
|
-
}
|
|
676
|
-
else {
|
|
677
|
-
return item;
|
|
678
|
-
}
|
|
679
|
-
});
|
|
680
|
-
return resolve(handleResults(arrayResult, m, bools));
|
|
532
|
+
export function query(pool, sql, args, m, bools) {
|
|
533
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
534
|
+
const p = toArray(args);
|
|
535
|
+
return new Promise((resolve, reject) => {
|
|
536
|
+
return pool.query(sql, p, (err, results, fields) => {
|
|
537
|
+
if (err) {
|
|
538
|
+
return reject(err);
|
|
539
|
+
}
|
|
540
|
+
else {
|
|
541
|
+
if (results.length > 0) {
|
|
542
|
+
const arrayResult = results.map((item) => {
|
|
543
|
+
if (Array.isArray(item)) {
|
|
544
|
+
return formatData(fields, item);
|
|
681
545
|
}
|
|
682
546
|
else {
|
|
683
|
-
|
|
547
|
+
return item;
|
|
684
548
|
}
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
|
|
549
|
+
});
|
|
550
|
+
return resolve(handleResults(arrayResult, m, bools));
|
|
551
|
+
}
|
|
552
|
+
else {
|
|
553
|
+
resolve([]);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
});
|
|
688
557
|
});
|
|
689
558
|
});
|
|
690
559
|
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
return query(pool, sql, args, m, bools).then(function (r) {
|
|
560
|
+
export function queryOne(pool, sql, args, m, bools) {
|
|
561
|
+
return query(pool, sql, args, m, bools).then((r) => {
|
|
694
562
|
return r && r.length > 0 ? r[0] : null;
|
|
695
563
|
});
|
|
696
564
|
}
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
return queryOne(pool, sql, args).then(function (r) {
|
|
565
|
+
export function executeScalar(pool, sql, args) {
|
|
566
|
+
return queryOne(pool, sql, args).then((r) => {
|
|
700
567
|
if (!r) {
|
|
701
568
|
return null;
|
|
702
569
|
}
|
|
703
570
|
else {
|
|
704
|
-
|
|
571
|
+
const keys = Object.keys(r);
|
|
705
572
|
return r[keys[0]];
|
|
706
573
|
}
|
|
707
574
|
});
|
|
708
575
|
}
|
|
709
|
-
|
|
710
|
-
function count(pool, sql, args) {
|
|
576
|
+
export function count(pool, sql, args) {
|
|
711
577
|
return executeScalar(pool, sql, args);
|
|
712
578
|
}
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
var s = build_1.buildToSave(obj, table, attrs, ver, buildParam);
|
|
579
|
+
export function save(pool, obj, table, attrs, ver, buildParam) {
|
|
580
|
+
const s = buildToSave(obj, table, attrs, ver, buildParam);
|
|
716
581
|
if (!s.query) {
|
|
717
582
|
return Promise.resolve(-1);
|
|
718
583
|
}
|
|
@@ -723,9 +588,8 @@ function save(pool, obj, table, attrs, ver, buildParam) {
|
|
|
723
588
|
return execute(pool, s.query, s.params);
|
|
724
589
|
}
|
|
725
590
|
}
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
var s = build_1.buildToSaveBatch(objs, table, attrs, ver, buildParam);
|
|
591
|
+
export function saveBatch(pool, objs, table, attrs, ver, buildParam) {
|
|
592
|
+
const s = buildToSaveBatch(objs, table, attrs, ver, buildParam);
|
|
729
593
|
if (typeof pool === "function") {
|
|
730
594
|
return pool(s);
|
|
731
595
|
}
|
|
@@ -733,14 +597,13 @@ function saveBatch(pool, objs, table, attrs, ver, buildParam) {
|
|
|
733
597
|
return executeBatch(pool, s);
|
|
734
598
|
}
|
|
735
599
|
}
|
|
736
|
-
|
|
737
|
-
function toArray(arr) {
|
|
600
|
+
export function toArray(arr) {
|
|
738
601
|
if (!arr || arr.length === 0) {
|
|
739
602
|
return [];
|
|
740
603
|
}
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
for (
|
|
604
|
+
const p = [];
|
|
605
|
+
const l = arr.length;
|
|
606
|
+
for (let i = 0; i < l; i++) {
|
|
744
607
|
if (arr[i] === undefined || arr[i] == null) {
|
|
745
608
|
p.push(null);
|
|
746
609
|
}
|
|
@@ -751,7 +614,7 @@ function toArray(arr) {
|
|
|
751
614
|
}
|
|
752
615
|
else {
|
|
753
616
|
if (resource.string) {
|
|
754
|
-
|
|
617
|
+
const s = JSON.stringify(arr[i]);
|
|
755
618
|
p.push(s);
|
|
756
619
|
}
|
|
757
620
|
else {
|
|
@@ -766,10 +629,9 @@ function toArray(arr) {
|
|
|
766
629
|
}
|
|
767
630
|
return p;
|
|
768
631
|
}
|
|
769
|
-
|
|
770
|
-
function handleResults(r, m, bools) {
|
|
632
|
+
export function handleResults(r, m, bools) {
|
|
771
633
|
if (m) {
|
|
772
|
-
|
|
634
|
+
const res = mapArray(r, m);
|
|
773
635
|
if (bools && bools.length > 0) {
|
|
774
636
|
return handleBool(res, bools);
|
|
775
637
|
}
|
|
@@ -786,20 +648,17 @@ function handleResults(r, m, bools) {
|
|
|
786
648
|
}
|
|
787
649
|
}
|
|
788
650
|
}
|
|
789
|
-
|
|
790
|
-
function handleBool(objs, bools) {
|
|
651
|
+
export function handleBool(objs, bools) {
|
|
791
652
|
if (!bools || bools.length === 0 || !objs) {
|
|
792
653
|
return objs;
|
|
793
654
|
}
|
|
794
|
-
for (
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
for (var _a = 0, bools_1 = bools; _a < bools_1.length; _a++) {
|
|
798
|
-
var field = bools_1[_a];
|
|
655
|
+
for (const obj of objs) {
|
|
656
|
+
const o = obj;
|
|
657
|
+
for (const field of bools) {
|
|
799
658
|
if (field.name) {
|
|
800
|
-
|
|
659
|
+
const v = o[field.name];
|
|
801
660
|
if (typeof v !== "boolean" && v != null && v !== undefined) {
|
|
802
|
-
|
|
661
|
+
const b = field.true;
|
|
803
662
|
if (b == null || b === undefined) {
|
|
804
663
|
o[field.name] = "1" == v || "T" == v || "Y" == v || "true" == v || "on" == v;
|
|
805
664
|
}
|
|
@@ -812,11 +671,10 @@ function handleBool(objs, bools) {
|
|
|
812
671
|
}
|
|
813
672
|
return objs;
|
|
814
673
|
}
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
var key = item.name;
|
|
674
|
+
export function formatData(nameColumn, data, m) {
|
|
675
|
+
const result = {};
|
|
676
|
+
nameColumn.forEach((item, index) => {
|
|
677
|
+
let key = item.name;
|
|
820
678
|
if (m) {
|
|
821
679
|
key = m[item.name];
|
|
822
680
|
}
|
|
@@ -824,20 +682,18 @@ function formatData(nameColumn, data, m) {
|
|
|
824
682
|
});
|
|
825
683
|
return result;
|
|
826
684
|
}
|
|
827
|
-
|
|
828
|
-
function map(obj, m) {
|
|
685
|
+
export function map(obj, m) {
|
|
829
686
|
if (!m) {
|
|
830
687
|
return obj;
|
|
831
688
|
}
|
|
832
|
-
|
|
689
|
+
const mkeys = Object.keys(m);
|
|
833
690
|
if (mkeys.length === 0) {
|
|
834
691
|
return obj;
|
|
835
692
|
}
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
for (
|
|
839
|
-
|
|
840
|
-
var k0 = m[key];
|
|
693
|
+
const o = {};
|
|
694
|
+
const keys = Object.keys(obj);
|
|
695
|
+
for (const key of keys) {
|
|
696
|
+
let k0 = m[key];
|
|
841
697
|
if (!k0) {
|
|
842
698
|
k0 = key;
|
|
843
699
|
}
|
|
@@ -845,24 +701,22 @@ function map(obj, m) {
|
|
|
845
701
|
}
|
|
846
702
|
return o;
|
|
847
703
|
}
|
|
848
|
-
|
|
849
|
-
function mapArray(results, m) {
|
|
704
|
+
export function mapArray(results, m) {
|
|
850
705
|
if (!m) {
|
|
851
706
|
return results;
|
|
852
707
|
}
|
|
853
|
-
|
|
708
|
+
const mkeys = Object.keys(m);
|
|
854
709
|
if (mkeys.length === 0) {
|
|
855
710
|
return results;
|
|
856
711
|
}
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
for (
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
for (
|
|
864
|
-
|
|
865
|
-
var k0 = m[key];
|
|
712
|
+
const objs = [];
|
|
713
|
+
const length = results.length;
|
|
714
|
+
for (let i = 0; i < length; i++) {
|
|
715
|
+
const obj = results[i];
|
|
716
|
+
const obj2 = {};
|
|
717
|
+
const keys = Object.keys(obj);
|
|
718
|
+
for (const key of keys) {
|
|
719
|
+
let k0 = m[key];
|
|
866
720
|
if (!k0) {
|
|
867
721
|
k0 = key;
|
|
868
722
|
}
|
|
@@ -872,15 +726,13 @@ function mapArray(results, m) {
|
|
|
872
726
|
}
|
|
873
727
|
return objs;
|
|
874
728
|
}
|
|
875
|
-
|
|
876
|
-
function getFields(fields, all) {
|
|
729
|
+
export function getFields(fields, all) {
|
|
877
730
|
if (!fields || fields.length === 0) {
|
|
878
731
|
return undefined;
|
|
879
732
|
}
|
|
880
|
-
|
|
733
|
+
const ext = [];
|
|
881
734
|
if (all) {
|
|
882
|
-
for (
|
|
883
|
-
var s = fields_1[_i];
|
|
735
|
+
for (const s of fields) {
|
|
884
736
|
if (all.includes(s)) {
|
|
885
737
|
ext.push(s);
|
|
886
738
|
}
|
|
@@ -896,9 +748,8 @@ function getFields(fields, all) {
|
|
|
896
748
|
return fields;
|
|
897
749
|
}
|
|
898
750
|
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
var s = getFields(fields, all);
|
|
751
|
+
export function buildFields(fields, all) {
|
|
752
|
+
const s = getFields(fields, all);
|
|
902
753
|
if (!s || s.length === 0) {
|
|
903
754
|
return "*";
|
|
904
755
|
}
|
|
@@ -906,12 +757,11 @@ function buildFields(fields, all) {
|
|
|
906
757
|
return s.join(",");
|
|
907
758
|
}
|
|
908
759
|
}
|
|
909
|
-
|
|
910
|
-
function getMapField(name, mp) {
|
|
760
|
+
export function getMapField(name, mp) {
|
|
911
761
|
if (!mp) {
|
|
912
762
|
return name;
|
|
913
763
|
}
|
|
914
|
-
|
|
764
|
+
const x = mp[name];
|
|
915
765
|
if (!x) {
|
|
916
766
|
return name;
|
|
917
767
|
}
|
|
@@ -920,45 +770,39 @@ function getMapField(name, mp) {
|
|
|
920
770
|
}
|
|
921
771
|
return name;
|
|
922
772
|
}
|
|
923
|
-
|
|
924
|
-
function isEmpty(s) {
|
|
773
|
+
export function isEmpty(s) {
|
|
925
774
|
return !(s && s.length > 0);
|
|
926
775
|
}
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
function StringService(pool, table, column) {
|
|
776
|
+
export class StringService {
|
|
777
|
+
constructor(pool, table, column) {
|
|
930
778
|
this.pool = pool;
|
|
931
779
|
this.table = table;
|
|
932
780
|
this.column = column;
|
|
933
781
|
this.load = this.load.bind(this);
|
|
934
782
|
this.save = this.save.bind(this);
|
|
935
783
|
}
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
return arr.map(function (i) { return i[_this.column]; });
|
|
784
|
+
load(key, max) {
|
|
785
|
+
const s = `select ${this.column} from ${this.table} where ${this.column} like ? order by ${this.column} limit ${max}`;
|
|
786
|
+
return query(this.pool, s, ["" + key + "%"]).then((arr) => {
|
|
787
|
+
return arr.map((i) => i[this.column]);
|
|
941
788
|
});
|
|
942
|
-
}
|
|
943
|
-
|
|
789
|
+
}
|
|
790
|
+
save(values) {
|
|
944
791
|
if (!values || values.length === 0) {
|
|
945
792
|
return Promise.resolve(0);
|
|
946
793
|
}
|
|
947
|
-
|
|
948
|
-
for (
|
|
794
|
+
const arr = [];
|
|
795
|
+
for (let i = 1; i <= values.length; i++) {
|
|
949
796
|
arr.push("(?)");
|
|
950
797
|
}
|
|
951
|
-
|
|
798
|
+
const s = `insert ignore into ${this.table}(${this.column})values${arr.join(",")}`;
|
|
952
799
|
return execute(this.pool, s, values);
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
for (var _i = 0, ks_1 = ks; _i < ks_1.length; _i++) {
|
|
960
|
-
var k = ks_1[_i];
|
|
961
|
-
var attr = attrs[k];
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
export function version(attrs) {
|
|
803
|
+
const ks = Object.keys(attrs);
|
|
804
|
+
for (const k of ks) {
|
|
805
|
+
const attr = attrs[k];
|
|
962
806
|
if (attr.version) {
|
|
963
807
|
attr.name = k;
|
|
964
808
|
return attr;
|
|
@@ -966,9 +810,8 @@ function version(attrs) {
|
|
|
966
810
|
}
|
|
967
811
|
return undefined;
|
|
968
812
|
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
function MySQLWriter(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
|
|
813
|
+
export class MySQLWriter {
|
|
814
|
+
constructor(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
|
|
972
815
|
this.table = table;
|
|
973
816
|
this.attributes = attributes;
|
|
974
817
|
this.oneIfSuccess = oneIfSuccess;
|
|
@@ -981,24 +824,24 @@ var MySQLWriter = (function () {
|
|
|
981
824
|
}
|
|
982
825
|
this.param = buildParam;
|
|
983
826
|
this.map = toDB;
|
|
984
|
-
|
|
827
|
+
const x = version(attributes);
|
|
985
828
|
if (x) {
|
|
986
829
|
this.version = x.name;
|
|
987
830
|
}
|
|
988
831
|
}
|
|
989
|
-
|
|
832
|
+
write(obj) {
|
|
990
833
|
if (!obj) {
|
|
991
834
|
return Promise.resolve(0);
|
|
992
835
|
}
|
|
993
|
-
|
|
836
|
+
let obj2 = obj;
|
|
994
837
|
if (this.map) {
|
|
995
838
|
obj2 = this.map(obj);
|
|
996
839
|
}
|
|
997
|
-
|
|
840
|
+
const stmt = buildToSave(obj2, this.table, this.attributes, this.version, this.param);
|
|
998
841
|
if (stmt.query) {
|
|
999
842
|
if (this.execute) {
|
|
1000
843
|
if (this.oneIfSuccess) {
|
|
1001
|
-
return this.execute(stmt.query, stmt.params).then(
|
|
844
|
+
return this.execute(stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
|
|
1002
845
|
}
|
|
1003
846
|
else {
|
|
1004
847
|
return this.execute(stmt.query, stmt.params);
|
|
@@ -1006,7 +849,7 @@ var MySQLWriter = (function () {
|
|
|
1006
849
|
}
|
|
1007
850
|
else {
|
|
1008
851
|
if (this.oneIfSuccess) {
|
|
1009
|
-
return execute(this.pool, stmt.query, stmt.params).then(
|
|
852
|
+
return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
|
|
1010
853
|
}
|
|
1011
854
|
else {
|
|
1012
855
|
return execute(this.pool, stmt.query, stmt.params);
|
|
@@ -1016,12 +859,10 @@ var MySQLWriter = (function () {
|
|
|
1016
859
|
else {
|
|
1017
860
|
return Promise.resolve(0);
|
|
1018
861
|
}
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
var MySQLStreamWriter = (function () {
|
|
1024
|
-
function MySQLStreamWriter(pool, table, attributes, size, toDB, buildParam) {
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
export class MySQLStreamWriter {
|
|
865
|
+
constructor(pool, table, attributes, size, toDB, buildParam) {
|
|
1025
866
|
this.table = table;
|
|
1026
867
|
this.attributes = attributes;
|
|
1027
868
|
this.list = [];
|
|
@@ -1036,7 +877,7 @@ var MySQLStreamWriter = (function () {
|
|
|
1036
877
|
}
|
|
1037
878
|
this.param = buildParam;
|
|
1038
879
|
this.map = toDB;
|
|
1039
|
-
|
|
880
|
+
const x = version(attributes);
|
|
1040
881
|
if (x) {
|
|
1041
882
|
this.version = x.name;
|
|
1042
883
|
}
|
|
@@ -1044,11 +885,11 @@ var MySQLStreamWriter = (function () {
|
|
|
1044
885
|
this.size = size;
|
|
1045
886
|
}
|
|
1046
887
|
}
|
|
1047
|
-
|
|
888
|
+
write(obj) {
|
|
1048
889
|
if (!obj) {
|
|
1049
890
|
return Promise.resolve(0);
|
|
1050
891
|
}
|
|
1051
|
-
|
|
892
|
+
let obj2 = obj;
|
|
1052
893
|
if (this.map) {
|
|
1053
894
|
obj2 = this.map(obj);
|
|
1054
895
|
this.list.push(obj2);
|
|
@@ -1062,26 +903,25 @@ var MySQLStreamWriter = (function () {
|
|
|
1062
903
|
else {
|
|
1063
904
|
return this.flush();
|
|
1064
905
|
}
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
|
-
var _this = this;
|
|
906
|
+
}
|
|
907
|
+
flush() {
|
|
1068
908
|
if (!this.list || this.list.length === 0) {
|
|
1069
909
|
return Promise.resolve(0);
|
|
1070
910
|
}
|
|
1071
911
|
else {
|
|
1072
|
-
|
|
1073
|
-
|
|
912
|
+
const total = this.list.length;
|
|
913
|
+
const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.version, this.param);
|
|
1074
914
|
if (stmt.length > 0) {
|
|
1075
915
|
if (this.executeBatch) {
|
|
1076
|
-
return this.executeBatch(stmt).then(
|
|
1077
|
-
|
|
1078
|
-
return
|
|
916
|
+
return this.executeBatch(stmt).then((r) => {
|
|
917
|
+
this.list = [];
|
|
918
|
+
return total;
|
|
1079
919
|
});
|
|
1080
920
|
}
|
|
1081
921
|
else {
|
|
1082
|
-
return executeBatch(this.pool, stmt).then(
|
|
1083
|
-
|
|
1084
|
-
return
|
|
922
|
+
return executeBatch(this.pool, stmt).then((r) => {
|
|
923
|
+
this.list = [];
|
|
924
|
+
return total;
|
|
1085
925
|
});
|
|
1086
926
|
}
|
|
1087
927
|
}
|
|
@@ -1089,12 +929,10 @@ var MySQLStreamWriter = (function () {
|
|
|
1089
929
|
return Promise.resolve(0);
|
|
1090
930
|
}
|
|
1091
931
|
}
|
|
1092
|
-
}
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
var MySQLBatchWriter = (function () {
|
|
1097
|
-
function MySQLBatchWriter(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
export class MySQLBatchWriter {
|
|
935
|
+
constructor(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
|
|
1098
936
|
this.table = table;
|
|
1099
937
|
this.attributes = attributes;
|
|
1100
938
|
this.oneIfSuccess = oneIfSuccess;
|
|
@@ -1107,29 +945,28 @@ var MySQLBatchWriter = (function () {
|
|
|
1107
945
|
}
|
|
1108
946
|
this.param = buildParam;
|
|
1109
947
|
this.map = toDB;
|
|
1110
|
-
|
|
948
|
+
const x = version(attributes);
|
|
1111
949
|
if (x) {
|
|
1112
950
|
this.version = x.name;
|
|
1113
951
|
}
|
|
1114
952
|
}
|
|
1115
|
-
|
|
953
|
+
write(objs) {
|
|
1116
954
|
if (!objs || objs.length === 0) {
|
|
1117
955
|
return Promise.resolve(0);
|
|
1118
956
|
}
|
|
1119
|
-
|
|
957
|
+
let list = objs;
|
|
1120
958
|
if (this.map) {
|
|
1121
959
|
list = [];
|
|
1122
|
-
for (
|
|
1123
|
-
|
|
1124
|
-
var obj2 = this.map(obj);
|
|
960
|
+
for (const obj of objs) {
|
|
961
|
+
const obj2 = this.map(obj);
|
|
1125
962
|
list.push(obj2);
|
|
1126
963
|
}
|
|
1127
964
|
}
|
|
1128
|
-
|
|
965
|
+
const stmts = buildToSaveBatch(list, this.table, this.attributes, this.version, this.param);
|
|
1129
966
|
if (stmts.length > 0) {
|
|
1130
967
|
if (this.execute) {
|
|
1131
968
|
if (this.oneIfSuccess) {
|
|
1132
|
-
return this.execute(stmts).then(
|
|
969
|
+
return this.execute(stmts).then((ct) => stmts.length);
|
|
1133
970
|
}
|
|
1134
971
|
else {
|
|
1135
972
|
return this.execute(stmts);
|
|
@@ -1137,7 +974,7 @@ var MySQLBatchWriter = (function () {
|
|
|
1137
974
|
}
|
|
1138
975
|
else {
|
|
1139
976
|
if (this.oneIfSuccess) {
|
|
1140
|
-
return executeBatch(this.pool, stmts).then(
|
|
977
|
+
return executeBatch(this.pool, stmts).then((ct) => stmts.length);
|
|
1141
978
|
}
|
|
1142
979
|
else {
|
|
1143
980
|
return executeBatch(this.pool, stmts);
|
|
@@ -1147,12 +984,10 @@ var MySQLBatchWriter = (function () {
|
|
|
1147
984
|
else {
|
|
1148
985
|
return Promise.resolve(0);
|
|
1149
986
|
}
|
|
1150
|
-
}
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
var MySQLChecker = (function () {
|
|
1155
|
-
function MySQLChecker(pool, service, timeout) {
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
export class MySQLChecker {
|
|
990
|
+
constructor(pool, service, timeout) {
|
|
1156
991
|
this.pool = pool;
|
|
1157
992
|
this.timeout = timeout ? timeout : 4200;
|
|
1158
993
|
this.service = service ? service : "mysql";
|
|
@@ -1160,36 +995,31 @@ var MySQLChecker = (function () {
|
|
|
1160
995
|
this.name = this.name.bind(this);
|
|
1161
996
|
this.build = this.build.bind(this);
|
|
1162
997
|
}
|
|
1163
|
-
|
|
1164
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
else {
|
|
1175
|
-
resolve(obj);
|
|
1176
|
-
}
|
|
1177
|
-
});
|
|
998
|
+
check() {
|
|
999
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1000
|
+
const obj = {};
|
|
1001
|
+
const promise = new Promise((resolve, reject) => {
|
|
1002
|
+
this.pool.query("select current_time", (err, result) => {
|
|
1003
|
+
if (err) {
|
|
1004
|
+
return reject(err);
|
|
1005
|
+
}
|
|
1006
|
+
else {
|
|
1007
|
+
resolve(obj);
|
|
1008
|
+
}
|
|
1178
1009
|
});
|
|
1179
|
-
if (this.timeout > 0) {
|
|
1180
|
-
return [2, promiseTimeOut(this.timeout, promise)];
|
|
1181
|
-
}
|
|
1182
|
-
else {
|
|
1183
|
-
return [2, promise];
|
|
1184
|
-
}
|
|
1185
|
-
return [2];
|
|
1186
1010
|
});
|
|
1011
|
+
if (this.timeout > 0) {
|
|
1012
|
+
return promiseTimeOut(this.timeout, promise);
|
|
1013
|
+
}
|
|
1014
|
+
else {
|
|
1015
|
+
return promise;
|
|
1016
|
+
}
|
|
1187
1017
|
});
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1018
|
+
}
|
|
1019
|
+
name() {
|
|
1190
1020
|
return this.service;
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1021
|
+
}
|
|
1022
|
+
build(data, err) {
|
|
1193
1023
|
if (err) {
|
|
1194
1024
|
if (!data) {
|
|
1195
1025
|
data = {};
|
|
@@ -1197,22 +1027,20 @@ var MySQLChecker = (function () {
|
|
|
1197
1027
|
data["error"] = err;
|
|
1198
1028
|
}
|
|
1199
1029
|
return data;
|
|
1200
|
-
}
|
|
1201
|
-
|
|
1202
|
-
}());
|
|
1203
|
-
exports.MySQLChecker = MySQLChecker;
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1204
1032
|
function promiseTimeOut(timeoutInMilliseconds, promise) {
|
|
1205
1033
|
return Promise.race([
|
|
1206
1034
|
promise,
|
|
1207
|
-
new Promise(
|
|
1208
|
-
setTimeout(
|
|
1209
|
-
reject(
|
|
1035
|
+
new Promise((resolve, reject) => {
|
|
1036
|
+
setTimeout(() => {
|
|
1037
|
+
reject(`Timed out in: ${timeoutInMilliseconds} milliseconds!`);
|
|
1210
1038
|
}, timeoutInMilliseconds);
|
|
1211
1039
|
}),
|
|
1212
1040
|
]);
|
|
1213
1041
|
}
|
|
1214
|
-
|
|
1215
|
-
|
|
1042
|
+
export class Exporter {
|
|
1043
|
+
constructor(connection, buildQuery, ft, write, end, attributes) {
|
|
1216
1044
|
this.connection = connection;
|
|
1217
1045
|
this.buildQuery = buildQuery;
|
|
1218
1046
|
this.write = write;
|
|
@@ -1224,73 +1052,55 @@ var Exporter = (function () {
|
|
|
1224
1052
|
}
|
|
1225
1053
|
this.export = this.export.bind(this);
|
|
1226
1054
|
}
|
|
1227
|
-
|
|
1228
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
if (er) {
|
|
1271
|
-
reject(er);
|
|
1272
|
-
}
|
|
1273
|
-
else {
|
|
1274
|
-
_this.connection.end(function (err) {
|
|
1275
|
-
if (err) {
|
|
1276
|
-
reject(err);
|
|
1277
|
-
}
|
|
1278
|
-
else {
|
|
1279
|
-
resolve(idx);
|
|
1280
|
-
}
|
|
1281
|
-
});
|
|
1282
|
-
}
|
|
1283
|
-
});
|
|
1284
|
-
})];
|
|
1285
|
-
}
|
|
1055
|
+
export(ctx) {
|
|
1056
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1057
|
+
let idx = 0;
|
|
1058
|
+
const stmt = yield this.buildQuery(ctx);
|
|
1059
|
+
const reader = this.connection.query(stmt.query, stmt.params);
|
|
1060
|
+
let er;
|
|
1061
|
+
reader.on("error", (err) => (er = err));
|
|
1062
|
+
if (this.map) {
|
|
1063
|
+
reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
|
|
1064
|
+
++idx;
|
|
1065
|
+
this.connection.pause();
|
|
1066
|
+
const obj = mapOne(row, this.map);
|
|
1067
|
+
const data = this.format(obj);
|
|
1068
|
+
this.write(data);
|
|
1069
|
+
this.connection.resume();
|
|
1070
|
+
}));
|
|
1071
|
+
}
|
|
1072
|
+
else {
|
|
1073
|
+
reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
|
|
1074
|
+
++idx;
|
|
1075
|
+
this.connection.pause();
|
|
1076
|
+
const data = this.format(row);
|
|
1077
|
+
this.write(data);
|
|
1078
|
+
this.connection.resume();
|
|
1079
|
+
}));
|
|
1080
|
+
}
|
|
1081
|
+
return new Promise((resolve, reject) => {
|
|
1082
|
+
reader.on("end", () => {
|
|
1083
|
+
this.end();
|
|
1084
|
+
if (er) {
|
|
1085
|
+
reject(er);
|
|
1086
|
+
}
|
|
1087
|
+
else {
|
|
1088
|
+
this.connection.end((err) => {
|
|
1089
|
+
if (err) {
|
|
1090
|
+
reject(err);
|
|
1091
|
+
}
|
|
1092
|
+
else {
|
|
1093
|
+
resolve(idx);
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1286
1098
|
});
|
|
1287
1099
|
});
|
|
1288
|
-
}
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
var ExportService = (function () {
|
|
1293
|
-
function ExportService(connection, queryBuilder, formatter, writer, attributes) {
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
export class ExportService {
|
|
1103
|
+
constructor(connection, queryBuilder, formatter, writer, attributes) {
|
|
1294
1104
|
this.connection = connection;
|
|
1295
1105
|
this.queryBuilder = queryBuilder;
|
|
1296
1106
|
this.formatter = formatter;
|
|
@@ -1301,90 +1111,71 @@ var ExportService = (function () {
|
|
|
1301
1111
|
}
|
|
1302
1112
|
this.export = this.export.bind(this);
|
|
1303
1113
|
}
|
|
1304
|
-
|
|
1305
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
if (er) {
|
|
1353
|
-
reject(er);
|
|
1354
|
-
}
|
|
1355
|
-
else {
|
|
1356
|
-
_this.connection.end(function (err) {
|
|
1357
|
-
if (err) {
|
|
1358
|
-
reject(err);
|
|
1359
|
-
}
|
|
1360
|
-
else {
|
|
1361
|
-
resolve(idx);
|
|
1362
|
-
}
|
|
1363
|
-
});
|
|
1364
|
-
}
|
|
1365
|
-
});
|
|
1366
|
-
})];
|
|
1367
|
-
}
|
|
1114
|
+
export(ctx) {
|
|
1115
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1116
|
+
let idx = 0;
|
|
1117
|
+
const stmt = yield this.queryBuilder.build(ctx);
|
|
1118
|
+
const reader = this.connection.query(stmt.query, stmt.params);
|
|
1119
|
+
let er;
|
|
1120
|
+
reader.on("error", (err) => (er = err));
|
|
1121
|
+
if (this.map) {
|
|
1122
|
+
reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
|
|
1123
|
+
++idx;
|
|
1124
|
+
this.connection.pause();
|
|
1125
|
+
const obj = mapOne(row, this.map);
|
|
1126
|
+
const data = this.formatter.format(obj);
|
|
1127
|
+
this.writer.write(data);
|
|
1128
|
+
this.connection.resume();
|
|
1129
|
+
}));
|
|
1130
|
+
}
|
|
1131
|
+
else {
|
|
1132
|
+
reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
|
|
1133
|
+
++idx;
|
|
1134
|
+
this.connection.pause();
|
|
1135
|
+
const data = this.formatter.format(row);
|
|
1136
|
+
this.writer.write(data);
|
|
1137
|
+
this.connection.resume();
|
|
1138
|
+
}));
|
|
1139
|
+
}
|
|
1140
|
+
return new Promise((resolve, reject) => {
|
|
1141
|
+
reader.on("end", () => {
|
|
1142
|
+
if (this.writer.end) {
|
|
1143
|
+
this.writer.end();
|
|
1144
|
+
}
|
|
1145
|
+
else if (this.writer.flush) {
|
|
1146
|
+
this.writer.flush();
|
|
1147
|
+
}
|
|
1148
|
+
if (er) {
|
|
1149
|
+
reject(er);
|
|
1150
|
+
}
|
|
1151
|
+
else {
|
|
1152
|
+
this.connection.end((err) => {
|
|
1153
|
+
if (err) {
|
|
1154
|
+
reject(err);
|
|
1155
|
+
}
|
|
1156
|
+
else {
|
|
1157
|
+
resolve(idx);
|
|
1158
|
+
}
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
});
|
|
1368
1162
|
});
|
|
1369
1163
|
});
|
|
1370
|
-
}
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
function mapOne(results, m) {
|
|
1375
|
-
var obj = results;
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
export function mapOne(results, m) {
|
|
1167
|
+
const obj = results;
|
|
1376
1168
|
if (!m) {
|
|
1377
1169
|
return obj;
|
|
1378
1170
|
}
|
|
1379
|
-
|
|
1171
|
+
const mkeys = Object.keys(m);
|
|
1380
1172
|
if (mkeys.length === 0) {
|
|
1381
1173
|
return obj;
|
|
1382
1174
|
}
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
for (
|
|
1386
|
-
|
|
1387
|
-
var k0 = m[key];
|
|
1175
|
+
const obj2 = {};
|
|
1176
|
+
const keys = Object.keys(obj);
|
|
1177
|
+
for (const key of keys) {
|
|
1178
|
+
let k0 = m[key];
|
|
1388
1179
|
if (!k0) {
|
|
1389
1180
|
k0 = key;
|
|
1390
1181
|
}
|
|
@@ -1392,17 +1183,15 @@ function mapOne(results, m) {
|
|
|
1392
1183
|
}
|
|
1393
1184
|
return obj2;
|
|
1394
1185
|
}
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
var k = ks_2[_i];
|
|
1402
|
-
var attr = attrs[k];
|
|
1186
|
+
export function buildMap(attrs) {
|
|
1187
|
+
const mp = {};
|
|
1188
|
+
const ks = Object.keys(attrs);
|
|
1189
|
+
let isMap = false;
|
|
1190
|
+
for (const k of ks) {
|
|
1191
|
+
const attr = attrs[k];
|
|
1403
1192
|
attr.name = k;
|
|
1404
|
-
|
|
1405
|
-
|
|
1193
|
+
const field = attr.column ? attr.column : k;
|
|
1194
|
+
const s = field.toLowerCase();
|
|
1406
1195
|
if (s !== k) {
|
|
1407
1196
|
mp[s] = k;
|
|
1408
1197
|
isMap = true;
|
|
@@ -1413,17 +1202,14 @@ function buildMap(attrs) {
|
|
|
1413
1202
|
}
|
|
1414
1203
|
return undefined;
|
|
1415
1204
|
}
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
var k = ks_3[_i];
|
|
1422
|
-
var attr = attrs[k];
|
|
1205
|
+
export function select(table, attrs) {
|
|
1206
|
+
const cols = [];
|
|
1207
|
+
const ks = Object.keys(attrs);
|
|
1208
|
+
for (const k of ks) {
|
|
1209
|
+
const attr = attrs[k];
|
|
1423
1210
|
attr.name = k;
|
|
1424
|
-
|
|
1211
|
+
const field = attr.column ? attr.column : k;
|
|
1425
1212
|
cols.push(field);
|
|
1426
1213
|
}
|
|
1427
|
-
return
|
|
1214
|
+
return `select ${cols.join(",")} from ${table}`;
|
|
1428
1215
|
}
|
|
1429
|
-
exports.select = select;
|