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