mysql2-core 0.3.10 → 0.5.1
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 +666 -78
- package/lib/index.js +183 -200
- package/package.json +1 -1
- package/src/build.ts +132 -107
- package/src/index.ts +62 -81
- package/src/metadata.ts +1 -1
package/lib/index.js
CHANGED
|
@@ -59,35 +59,54 @@ export function metadata(attrs) {
|
|
|
59
59
|
}
|
|
60
60
|
return m;
|
|
61
61
|
}
|
|
62
|
-
export function buildToSave(obj, table, attrs,
|
|
63
|
-
if (!i) {
|
|
64
|
-
i = 1;
|
|
65
|
-
}
|
|
62
|
+
export function buildToSave(obj, table, attrs, pks, ver, buildParam) {
|
|
66
63
|
if (!buildParam) {
|
|
67
64
|
buildParam = param;
|
|
68
65
|
}
|
|
69
66
|
const ks = Object.keys(attrs);
|
|
70
|
-
|
|
67
|
+
if (!pks) {
|
|
68
|
+
pks = [];
|
|
69
|
+
for (const k of ks) {
|
|
70
|
+
const attr = attrs[k];
|
|
71
|
+
attr.name = k;
|
|
72
|
+
if (attr.key) {
|
|
73
|
+
pks.push(attr);
|
|
74
|
+
}
|
|
75
|
+
if (attr.version) {
|
|
76
|
+
ver = k;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
71
80
|
const cols = [];
|
|
72
81
|
const values = [];
|
|
73
82
|
const args = [];
|
|
74
|
-
let isVersion = false;
|
|
75
83
|
const o = obj;
|
|
84
|
+
let isUpdate = true;
|
|
85
|
+
let isVersion = false;
|
|
86
|
+
for (const k of pks) {
|
|
87
|
+
if (k.name) {
|
|
88
|
+
let v = o[k.name];
|
|
89
|
+
if (v == null) {
|
|
90
|
+
isUpdate = false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
let i = 1;
|
|
76
95
|
for (const k of ks) {
|
|
77
96
|
const attr = attrs[k];
|
|
78
|
-
attr
|
|
79
|
-
|
|
80
|
-
pks.push(attr);
|
|
97
|
+
if (!attr) {
|
|
98
|
+
continue;
|
|
81
99
|
}
|
|
82
100
|
let v = o[k];
|
|
83
|
-
if (v == null) {
|
|
101
|
+
if (v == null && attr.default !== undefined) {
|
|
84
102
|
v = attr.default;
|
|
85
103
|
}
|
|
86
104
|
if (v != null && !attr.ignored && !attr.noinsert) {
|
|
87
105
|
const field = attr.column ? attr.column : k;
|
|
88
106
|
cols.push(field);
|
|
89
|
-
if (
|
|
107
|
+
if (k === ver) {
|
|
90
108
|
isVersion = true;
|
|
109
|
+
ver = k;
|
|
91
110
|
values.push(`${1}`);
|
|
92
111
|
}
|
|
93
112
|
else {
|
|
@@ -102,11 +121,11 @@ export function buildToSave(obj, table, attrs, buildParam, i) {
|
|
|
102
121
|
values.push(p);
|
|
103
122
|
if (typeof v === "boolean") {
|
|
104
123
|
if (v === true) {
|
|
105
|
-
const v2 = attr.true !== undefined ? attr.true : `
|
|
124
|
+
const v2 = attr.true !== undefined ? attr.true : `1`;
|
|
106
125
|
args.push(v2);
|
|
107
126
|
}
|
|
108
127
|
else {
|
|
109
|
-
const v2 = attr.false !== undefined ? attr.false : `
|
|
128
|
+
const v2 = attr.false !== undefined ? attr.false : `0`;
|
|
110
129
|
args.push(v2);
|
|
111
130
|
}
|
|
112
131
|
}
|
|
@@ -117,17 +136,32 @@ export function buildToSave(obj, table, attrs, buildParam, i) {
|
|
|
117
136
|
}
|
|
118
137
|
}
|
|
119
138
|
}
|
|
120
|
-
if (
|
|
139
|
+
if (cols.length > 0 && ver && isVersion === false) {
|
|
140
|
+
const attr = attrs[ver];
|
|
141
|
+
if (attr) {
|
|
142
|
+
const field = attr.column ? attr.column : ver;
|
|
143
|
+
cols.push(field);
|
|
144
|
+
values.push("1");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (isUpdate === false || pks.length === 0) {
|
|
121
148
|
if (cols.length === 0) {
|
|
122
149
|
return { query: "", params: args };
|
|
123
150
|
}
|
|
124
151
|
else {
|
|
125
|
-
|
|
126
|
-
|
|
152
|
+
if (pks.length === 0) {
|
|
153
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")})`;
|
|
154
|
+
return { query: q, params: args };
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`;
|
|
158
|
+
return { query: q, params: args };
|
|
159
|
+
}
|
|
127
160
|
}
|
|
128
161
|
}
|
|
129
162
|
else {
|
|
130
163
|
const colSet = [];
|
|
164
|
+
let version = null;
|
|
131
165
|
for (const k of ks) {
|
|
132
166
|
const v = o[k];
|
|
133
167
|
if (v !== undefined) {
|
|
@@ -135,110 +169,60 @@ export function buildToSave(obj, table, attrs, buildParam, i) {
|
|
|
135
169
|
if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
|
|
136
170
|
const field = attr.column ? attr.column : k;
|
|
137
171
|
let x;
|
|
138
|
-
if (
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
else if (v === "") {
|
|
142
|
-
x = `''`;
|
|
143
|
-
}
|
|
144
|
-
else if (typeof v === "number") {
|
|
145
|
-
x = toString(v);
|
|
172
|
+
if (attr.version) {
|
|
173
|
+
version = k;
|
|
174
|
+
x = `${field} + 1`;
|
|
146
175
|
}
|
|
147
176
|
else {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
177
|
+
if (v === "") {
|
|
178
|
+
x = `''`;
|
|
179
|
+
}
|
|
180
|
+
else if (typeof v === "number") {
|
|
181
|
+
x = toString(v);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
x = buildParam(i++);
|
|
185
|
+
if (typeof v === "boolean") {
|
|
186
|
+
if (v === true) {
|
|
187
|
+
const v2 = attr.true !== undefined ? attr.true : `1`;
|
|
188
|
+
args.push(v2);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
const v2 = attr.false !== undefined ? attr.false : `0`;
|
|
192
|
+
args.push(v2);
|
|
193
|
+
}
|
|
153
194
|
}
|
|
154
195
|
else {
|
|
155
|
-
|
|
156
|
-
args.push(v2);
|
|
196
|
+
args.push(v);
|
|
157
197
|
}
|
|
158
198
|
}
|
|
159
|
-
else {
|
|
160
|
-
args.push(v);
|
|
161
|
-
}
|
|
162
199
|
}
|
|
163
200
|
colSet.push(`${field}=${x}`);
|
|
164
201
|
}
|
|
165
202
|
}
|
|
166
203
|
}
|
|
167
|
-
if (
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
else {
|
|
172
|
-
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")}`;
|
|
173
|
-
return { query: q, params: args };
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
export function buildToSaveBatch(objs, table, attrs, buildParam) {
|
|
178
|
-
if (!buildParam) {
|
|
179
|
-
buildParam = param;
|
|
180
|
-
}
|
|
181
|
-
const sts = [];
|
|
182
|
-
const ks = Object.keys(attrs);
|
|
183
|
-
for (const obj of objs) {
|
|
184
|
-
let i = 1;
|
|
185
|
-
const cols = [];
|
|
186
|
-
const values = [];
|
|
187
|
-
const args = [];
|
|
188
|
-
let isVersion = false;
|
|
189
|
-
const o = obj;
|
|
190
|
-
for (const k of ks) {
|
|
191
|
-
const attr = attrs[k];
|
|
192
|
-
let v = o[k];
|
|
193
|
-
if (v == null) {
|
|
194
|
-
v = attr.default;
|
|
204
|
+
if (cols.length > 0) {
|
|
205
|
+
if (colSet.length === 0) {
|
|
206
|
+
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`;
|
|
207
|
+
return { query: q, params: args };
|
|
195
208
|
}
|
|
196
|
-
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
if (attr.version) {
|
|
200
|
-
isVersion = true;
|
|
201
|
-
values.push(`${1}`);
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
if (v === "") {
|
|
205
|
-
values.push(`''`);
|
|
206
|
-
}
|
|
207
|
-
else if (typeof v === "number") {
|
|
208
|
-
values.push(toString(v));
|
|
209
|
-
}
|
|
210
|
-
else {
|
|
211
|
-
const p = buildParam(i++);
|
|
212
|
-
values.push(p);
|
|
213
|
-
if (typeof v === "boolean") {
|
|
214
|
-
if (v === true) {
|
|
215
|
-
const v2 = attr.true !== undefined ? attr.true : `'1'`;
|
|
216
|
-
args.push(v2);
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
const v2 = attr.false !== undefined ? attr.false : `'0'`;
|
|
220
|
-
args.push(v2);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
else {
|
|
224
|
-
args.push(v);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
209
|
+
else {
|
|
210
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")}`;
|
|
211
|
+
return { query: q, params: args };
|
|
228
212
|
}
|
|
229
213
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
214
|
+
else {
|
|
215
|
+
if (colSet.length === 0) {
|
|
216
|
+
return { query: "", params: args };
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
const colQuery = [];
|
|
220
|
+
for (const attr of pks) {
|
|
221
|
+
const field = attr.column ? attr.column : attr.name;
|
|
222
|
+
const na = attr.name ? attr.name : "";
|
|
223
|
+
const v = o[na];
|
|
237
224
|
let x;
|
|
238
|
-
if (v ===
|
|
239
|
-
x = "null";
|
|
240
|
-
}
|
|
241
|
-
else if (v === "") {
|
|
225
|
+
if (v === "") {
|
|
242
226
|
x = `''`;
|
|
243
227
|
}
|
|
244
228
|
else if (typeof v === "number") {
|
|
@@ -248,11 +232,11 @@ export function buildToSaveBatch(objs, table, attrs, buildParam) {
|
|
|
248
232
|
x = buildParam(i++);
|
|
249
233
|
if (typeof v === "boolean") {
|
|
250
234
|
if (v === true) {
|
|
251
|
-
const v2 = attr.true !== undefined ? attr.true : `
|
|
235
|
+
const v2 = attr.true !== undefined ? "" + attr.true : `1`;
|
|
252
236
|
args.push(v2);
|
|
253
237
|
}
|
|
254
238
|
else {
|
|
255
|
-
const v2 = attr.false !== undefined ? attr.false : `
|
|
239
|
+
const v2 = attr.false !== undefined ? "" + attr.false : `0`;
|
|
256
240
|
args.push(v2);
|
|
257
241
|
}
|
|
258
242
|
}
|
|
@@ -260,18 +244,46 @@ export function buildToSaveBatch(objs, table, attrs, buildParam) {
|
|
|
260
244
|
args.push(v);
|
|
261
245
|
}
|
|
262
246
|
}
|
|
263
|
-
|
|
247
|
+
colQuery.push(`${field}=${x}`);
|
|
248
|
+
}
|
|
249
|
+
if (version && version.length > 0) {
|
|
250
|
+
const v = o[version];
|
|
251
|
+
if (typeof v === "number" && !isNaN(v)) {
|
|
252
|
+
const attr = attrs[version];
|
|
253
|
+
if (attr) {
|
|
254
|
+
const field = attr.column ? attr.column : version;
|
|
255
|
+
colQuery.push(`${field}=${v}`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
264
258
|
}
|
|
259
|
+
const q = `update ${table} set ${colSet.join(",")} where ${colQuery.join(" and ")}`;
|
|
260
|
+
return { query: q, params: args };
|
|
265
261
|
}
|
|
266
262
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
export function buildToSaveBatch(objs, table, attrs, pks, ver, buildParam) {
|
|
266
|
+
if (!buildParam) {
|
|
267
|
+
buildParam = param;
|
|
268
|
+
}
|
|
269
|
+
const sts = [];
|
|
270
|
+
if (!pks) {
|
|
271
|
+
pks = [];
|
|
272
|
+
const ks = Object.keys(attrs);
|
|
273
|
+
for (const k of ks) {
|
|
274
|
+
const attr = attrs[k];
|
|
275
|
+
attr.name = k;
|
|
276
|
+
if (attr.key) {
|
|
277
|
+
pks.push(attr);
|
|
278
|
+
}
|
|
279
|
+
if (attr.version) {
|
|
280
|
+
ver = k;
|
|
281
|
+
}
|
|
271
282
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
283
|
+
}
|
|
284
|
+
for (const obj of objs) {
|
|
285
|
+
const smt = buildToSave(obj, table, attrs, pks, ver, buildParam);
|
|
286
|
+
if (smt.query) {
|
|
275
287
|
sts.push(smt);
|
|
276
288
|
}
|
|
277
289
|
}
|
|
@@ -301,32 +313,24 @@ export function getConnection(pool) {
|
|
|
301
313
|
});
|
|
302
314
|
});
|
|
303
315
|
}
|
|
304
|
-
export function beginTransaction(connection
|
|
316
|
+
export function beginTransaction(connection) {
|
|
305
317
|
return new Promise((resolve, reject) => {
|
|
306
|
-
connection.beginTransaction((
|
|
307
|
-
if (
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
return connection.rollback(() => {
|
|
312
|
-
return reject(err);
|
|
313
|
-
});
|
|
318
|
+
connection.beginTransaction((beginError) => {
|
|
319
|
+
if (beginError) {
|
|
320
|
+
connection.release();
|
|
321
|
+
return reject(beginError);
|
|
314
322
|
}
|
|
315
323
|
return resolve();
|
|
316
324
|
});
|
|
317
325
|
});
|
|
318
326
|
}
|
|
319
|
-
export function commit(connection
|
|
327
|
+
export function commit(connection) {
|
|
320
328
|
return new Promise((resolve, reject) => {
|
|
321
329
|
connection.commit((err) => {
|
|
322
330
|
if (err) {
|
|
323
|
-
|
|
324
|
-
return reject(err);
|
|
325
|
-
}
|
|
326
|
-
return connection.rollback(() => {
|
|
327
|
-
return reject(err);
|
|
328
|
-
});
|
|
331
|
+
return reject(err);
|
|
329
332
|
}
|
|
333
|
+
connection.release();
|
|
330
334
|
return resolve();
|
|
331
335
|
});
|
|
332
336
|
});
|
|
@@ -334,6 +338,7 @@ export function commit(connection, rollbackIfError) {
|
|
|
334
338
|
export function rollback(connection) {
|
|
335
339
|
return new Promise((resolve, reject) => {
|
|
336
340
|
connection.rollback((err) => {
|
|
341
|
+
connection.release();
|
|
337
342
|
if (err) {
|
|
338
343
|
return reject(err);
|
|
339
344
|
}
|
|
@@ -364,10 +369,8 @@ export class PoolManager {
|
|
|
364
369
|
}
|
|
365
370
|
connection.beginTransaction((beginError) => {
|
|
366
371
|
if (beginError) {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
return reject(beginError);
|
|
370
|
-
});
|
|
372
|
+
connection.release();
|
|
373
|
+
return reject(beginError);
|
|
371
374
|
}
|
|
372
375
|
return resolve(new PoolConnectionManager(connection));
|
|
373
376
|
});
|
|
@@ -377,8 +380,8 @@ export class PoolManager {
|
|
|
377
380
|
execute(sql, args) {
|
|
378
381
|
return execute(this.pool, sql, args);
|
|
379
382
|
}
|
|
380
|
-
executeBatch(statements,
|
|
381
|
-
return executeBatch(this.pool, statements,
|
|
383
|
+
executeBatch(statements, requireFirstAffected) {
|
|
384
|
+
return executeBatch(this.pool, statements, requireFirstAffected);
|
|
382
385
|
}
|
|
383
386
|
query(sql, args, m, bools) {
|
|
384
387
|
return query(this.pool, sql, args, m, bools);
|
|
@@ -405,6 +408,7 @@ export class PoolConnectionManager {
|
|
|
405
408
|
this.queryOne = this.queryOne.bind(this);
|
|
406
409
|
this.executeScalar = this.executeScalar.bind(this);
|
|
407
410
|
this.count = this.count.bind(this);
|
|
411
|
+
this.release = this.release.bind(this);
|
|
408
412
|
}
|
|
409
413
|
param(i) {
|
|
410
414
|
return "?";
|
|
@@ -413,10 +417,7 @@ export class PoolConnectionManager {
|
|
|
413
417
|
return new Promise((resolve, reject) => {
|
|
414
418
|
this.connection.commit((err) => {
|
|
415
419
|
if (err) {
|
|
416
|
-
return
|
|
417
|
-
this.release();
|
|
418
|
-
return reject(err);
|
|
419
|
-
});
|
|
420
|
+
return reject(err);
|
|
420
421
|
}
|
|
421
422
|
this.release();
|
|
422
423
|
return resolve();
|
|
@@ -444,10 +445,10 @@ export class PoolConnectionManager {
|
|
|
444
445
|
execute(sql, args) {
|
|
445
446
|
return execute(this.connection, sql, args);
|
|
446
447
|
}
|
|
447
|
-
executeBatch(statements,
|
|
448
|
-
return executeBatchConnection(this.connection, statements,
|
|
448
|
+
executeBatch(statements, requireFirstAffected) {
|
|
449
|
+
return executeBatchConnection(this.connection, statements, requireFirstAffected);
|
|
449
450
|
}
|
|
450
|
-
query(sql, args, m, bools
|
|
451
|
+
query(sql, args, m, bools) {
|
|
451
452
|
return query(this.connection, sql, args, m, bools);
|
|
452
453
|
}
|
|
453
454
|
queryOne(sql, args, m, bools) {
|
|
@@ -460,7 +461,7 @@ export class PoolConnectionManager {
|
|
|
460
461
|
return count(this.connection, sql, args);
|
|
461
462
|
}
|
|
462
463
|
}
|
|
463
|
-
export function executeBatch(pool, statements,
|
|
464
|
+
export function executeBatch(pool, statements, requireFirstAffected) {
|
|
464
465
|
return __awaiter(this, void 0, void 0, function* () {
|
|
465
466
|
if (!statements || statements.length === 0) {
|
|
466
467
|
return 0;
|
|
@@ -469,31 +470,28 @@ export function executeBatch(pool, statements, firstAffected) {
|
|
|
469
470
|
return execute(pool, statements[0].query, statements[0].params);
|
|
470
471
|
}
|
|
471
472
|
const connection = yield getConnection(pool);
|
|
472
|
-
|
|
473
|
-
return yield executeBatchConnectionTx(connection, statements, firstAffected);
|
|
474
|
-
}
|
|
475
|
-
finally {
|
|
476
|
-
connection.release();
|
|
477
|
-
}
|
|
473
|
+
return yield executeBatchConnectionTx(connection, statements, requireFirstAffected);
|
|
478
474
|
});
|
|
479
475
|
}
|
|
480
|
-
export function executeBatchConnectionTx(connection, statements,
|
|
476
|
+
export function executeBatchConnectionTx(connection, statements, requireFirstAffected) {
|
|
481
477
|
return __awaiter(this, void 0, void 0, function* () {
|
|
482
478
|
if (!statements || statements.length === 0) {
|
|
483
479
|
return 0;
|
|
484
480
|
}
|
|
485
481
|
if (statements.length === 1) {
|
|
486
|
-
|
|
482
|
+
const count = yield execute(connection, statements[0].query, statements[0].params);
|
|
483
|
+
connection.release();
|
|
484
|
+
return count;
|
|
487
485
|
}
|
|
488
|
-
yield beginTransaction(connection
|
|
486
|
+
yield beginTransaction(connection);
|
|
489
487
|
try {
|
|
490
488
|
let count = 0;
|
|
491
489
|
if (resource.multipleStatements) {
|
|
492
|
-
if (
|
|
490
|
+
if (requireFirstAffected) {
|
|
493
491
|
const firstCount = yield execute(connection, statements[0].query, statements[0].params);
|
|
494
492
|
count = firstCount;
|
|
495
493
|
if (firstCount === 0) {
|
|
496
|
-
yield commit(connection
|
|
494
|
+
yield commit(connection);
|
|
497
495
|
return 0;
|
|
498
496
|
}
|
|
499
497
|
const remaining = statements.slice(1);
|
|
@@ -504,12 +502,12 @@ export function executeBatchConnectionTx(connection, statements, firstAffected)
|
|
|
504
502
|
}
|
|
505
503
|
}
|
|
506
504
|
else {
|
|
507
|
-
const start =
|
|
508
|
-
if (
|
|
505
|
+
const start = requireFirstAffected ? 1 : 0;
|
|
506
|
+
if (requireFirstAffected) {
|
|
509
507
|
const firstCount = yield execute(connection, statements[0].query, statements[0].params);
|
|
510
508
|
count = firstCount;
|
|
511
509
|
if (firstCount === 0) {
|
|
512
|
-
yield commit(connection
|
|
510
|
+
yield commit(connection);
|
|
513
511
|
return 0;
|
|
514
512
|
}
|
|
515
513
|
}
|
|
@@ -518,7 +516,7 @@ export function executeBatchConnectionTx(connection, statements, firstAffected)
|
|
|
518
516
|
count += yield execute(connection, item.query, item.params);
|
|
519
517
|
}
|
|
520
518
|
}
|
|
521
|
-
yield commit(connection
|
|
519
|
+
yield commit(connection);
|
|
522
520
|
return count;
|
|
523
521
|
}
|
|
524
522
|
catch (err) {
|
|
@@ -527,11 +525,12 @@ export function executeBatchConnectionTx(connection, statements, firstAffected)
|
|
|
527
525
|
}
|
|
528
526
|
catch (_a) {
|
|
529
527
|
}
|
|
530
|
-
|
|
528
|
+
buildError(err);
|
|
529
|
+
throw err;
|
|
531
530
|
}
|
|
532
531
|
});
|
|
533
532
|
}
|
|
534
|
-
export function executeBatchConnection(connection, statements,
|
|
533
|
+
export function executeBatchConnection(connection, statements, requireFirstAffected) {
|
|
535
534
|
return __awaiter(this, void 0, void 0, function* () {
|
|
536
535
|
if (!statements || statements.length === 0) {
|
|
537
536
|
return 0;
|
|
@@ -540,7 +539,7 @@ export function executeBatchConnection(connection, statements, firstAffected) {
|
|
|
540
539
|
return execute(connection, statements[0].query, statements[0].params);
|
|
541
540
|
}
|
|
542
541
|
if (resource.multipleStatements) {
|
|
543
|
-
if (
|
|
542
|
+
if (requireFirstAffected) {
|
|
544
543
|
const firstCount = yield execute(connection, statements[0].query, statements[0].params);
|
|
545
544
|
if (firstCount === 0) {
|
|
546
545
|
return 0;
|
|
@@ -550,8 +549,8 @@ export function executeBatchConnection(connection, statements, firstAffected) {
|
|
|
550
549
|
return executeMultipleStatements(connection, statements);
|
|
551
550
|
}
|
|
552
551
|
let count = 0;
|
|
553
|
-
const start =
|
|
554
|
-
if (
|
|
552
|
+
const start = requireFirstAffected ? 1 : 0;
|
|
553
|
+
if (requireFirstAffected) {
|
|
555
554
|
const firstCount = yield execute(connection, statements[0].query, statements[0].params);
|
|
556
555
|
count = firstCount;
|
|
557
556
|
if (firstCount === 0) {
|
|
@@ -669,27 +668,6 @@ export function executeScalar(pool, sql, args) {
|
|
|
669
668
|
export function count(pool, sql, args) {
|
|
670
669
|
return executeScalar(pool, sql, args).then((res) => (res !== null ? res : 0));
|
|
671
670
|
}
|
|
672
|
-
export function save(pool, obj, table, attrs, buildParam) {
|
|
673
|
-
const s = buildToSave(obj, table, attrs, buildParam);
|
|
674
|
-
if (!s.query) {
|
|
675
|
-
return Promise.resolve(-1);
|
|
676
|
-
}
|
|
677
|
-
if (typeof pool === "function") {
|
|
678
|
-
return pool(s.query, s.params);
|
|
679
|
-
}
|
|
680
|
-
else {
|
|
681
|
-
return execute(pool, s.query, s.params);
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
export function saveBatch(pool, objs, table, attrs, buildParam) {
|
|
685
|
-
const s = buildToSaveBatch(objs, table, attrs, buildParam);
|
|
686
|
-
if (typeof pool === "function") {
|
|
687
|
-
return pool(s);
|
|
688
|
-
}
|
|
689
|
-
else {
|
|
690
|
-
return executeBatch(pool, s);
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
671
|
export function toArray(arr) {
|
|
694
672
|
if (!arr || arr.length === 0) {
|
|
695
673
|
return [];
|
|
@@ -881,6 +859,8 @@ export class StringAdapter {
|
|
|
881
859
|
return execute(this.pool, s, values);
|
|
882
860
|
}
|
|
883
861
|
}
|
|
862
|
+
export const StringRepository = StringAdapter;
|
|
863
|
+
export const StringService = StringAdapter;
|
|
884
864
|
export class MySQLWriter {
|
|
885
865
|
constructor(pool, table, attributes, oneIfSuccess, map, buildParam) {
|
|
886
866
|
this.pool = pool;
|
|
@@ -890,6 +870,9 @@ export class MySQLWriter {
|
|
|
890
870
|
this.map = map;
|
|
891
871
|
this.write = this.write.bind(this);
|
|
892
872
|
this.param = buildParam ? buildParam : param;
|
|
873
|
+
const m = metadata(attributes);
|
|
874
|
+
this.keys = m.keys;
|
|
875
|
+
this.version = m.version;
|
|
893
876
|
}
|
|
894
877
|
write(obj) {
|
|
895
878
|
if (!obj) {
|
|
@@ -899,7 +882,7 @@ export class MySQLWriter {
|
|
|
899
882
|
if (this.map) {
|
|
900
883
|
obj2 = this.map(obj);
|
|
901
884
|
}
|
|
902
|
-
const stmt = buildToSave(obj2, this.table, this.attributes, this.param);
|
|
885
|
+
const stmt = buildToSave(obj2, this.table, this.attributes, this.keys, this.version, this.param);
|
|
903
886
|
if (stmt.query) {
|
|
904
887
|
if (this.oneIfSuccess) {
|
|
905
888
|
return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
|
|
@@ -913,7 +896,7 @@ export class MySQLWriter {
|
|
|
913
896
|
}
|
|
914
897
|
}
|
|
915
898
|
}
|
|
916
|
-
export class
|
|
899
|
+
export class BufferedBatchWriter {
|
|
917
900
|
constructor(pool, table, attributes, size = 1000, map, buildParam) {
|
|
918
901
|
this.pool = pool;
|
|
919
902
|
this.table = table;
|
|
@@ -924,6 +907,9 @@ export class MySQLStreamWriter {
|
|
|
924
907
|
this.write = this.write.bind(this);
|
|
925
908
|
this.flush = this.flush.bind(this);
|
|
926
909
|
this.param = buildParam ? buildParam : param;
|
|
910
|
+
const m = metadata(attributes);
|
|
911
|
+
this.keys = m.keys;
|
|
912
|
+
this.version = m.version;
|
|
927
913
|
}
|
|
928
914
|
write(obj) {
|
|
929
915
|
if (!obj) {
|
|
@@ -949,11 +935,11 @@ export class MySQLStreamWriter {
|
|
|
949
935
|
return Promise.resolve(0);
|
|
950
936
|
}
|
|
951
937
|
else {
|
|
952
|
-
const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.param);
|
|
938
|
+
const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.keys, this.version, this.param);
|
|
953
939
|
if (stmt.length > 0) {
|
|
954
940
|
return executeBatch(this.pool, stmt).then((r) => {
|
|
955
941
|
this.list = [];
|
|
956
|
-
return
|
|
942
|
+
return r;
|
|
957
943
|
});
|
|
958
944
|
}
|
|
959
945
|
else {
|
|
@@ -963,15 +949,17 @@ export class MySQLStreamWriter {
|
|
|
963
949
|
}
|
|
964
950
|
}
|
|
965
951
|
}
|
|
966
|
-
export class
|
|
967
|
-
constructor(pool, table, attributes,
|
|
952
|
+
export class BatchWriter {
|
|
953
|
+
constructor(pool, table, attributes, map, buildParam) {
|
|
968
954
|
this.pool = pool;
|
|
969
955
|
this.table = table;
|
|
970
956
|
this.attributes = attributes;
|
|
971
|
-
this.oneIfSuccess = oneIfSuccess;
|
|
972
957
|
this.map = map;
|
|
973
958
|
this.write = this.write.bind(this);
|
|
974
959
|
this.param = buildParam ? buildParam : param;
|
|
960
|
+
const m = metadata(attributes);
|
|
961
|
+
this.keys = m.keys;
|
|
962
|
+
this.version = m.version;
|
|
975
963
|
}
|
|
976
964
|
write(objs) {
|
|
977
965
|
if (!objs || objs.length === 0) {
|
|
@@ -985,14 +973,9 @@ export class MySQLBatchWriter {
|
|
|
985
973
|
list.push(obj2);
|
|
986
974
|
}
|
|
987
975
|
}
|
|
988
|
-
const stmts = buildToSaveBatch(list, this.table, this.attributes, this.param);
|
|
976
|
+
const stmts = buildToSaveBatch(list, this.table, this.attributes, this.keys, this.version, this.param);
|
|
989
977
|
if (stmts.length > 0) {
|
|
990
|
-
|
|
991
|
-
return executeBatch(this.pool, stmts).then((ct) => stmts.length);
|
|
992
|
-
}
|
|
993
|
-
else {
|
|
994
|
-
return executeBatch(this.pool, stmts);
|
|
995
|
-
}
|
|
978
|
+
return executeBatch(this.pool, stmts);
|
|
996
979
|
}
|
|
997
980
|
else {
|
|
998
981
|
return Promise.resolve(0);
|