mysql2-core 0.3.8 → 0.3.10

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 CHANGED
@@ -59,7 +59,7 @@ export function metadata(attrs) {
59
59
  }
60
60
  return m;
61
61
  }
62
- export function buildToSave(obj, table, attrs, ver, buildParam, i) {
62
+ export function buildToSave(obj, table, attrs, buildParam, i) {
63
63
  if (!i) {
64
64
  i = 1;
65
65
  }
@@ -86,7 +86,7 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
86
86
  if (v != null && !attr.ignored && !attr.noinsert) {
87
87
  const field = attr.column ? attr.column : k;
88
88
  cols.push(field);
89
- if (k === ver) {
89
+ if (attr.version) {
90
90
  isVersion = true;
91
91
  values.push(`${1}`);
92
92
  }
@@ -102,11 +102,11 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
102
102
  values.push(p);
103
103
  if (typeof v === "boolean") {
104
104
  if (v === true) {
105
- const v2 = attr.true ? attr.true : `'1'`;
105
+ const v2 = attr.true !== undefined ? attr.true : `'1'`;
106
106
  args.push(v2);
107
107
  }
108
108
  else {
109
- const v2 = attr.false ? attr.false : `'0'`;
109
+ const v2 = attr.false !== undefined ? attr.false : `'0'`;
110
110
  args.push(v2);
111
111
  }
112
112
  }
@@ -117,12 +117,6 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
117
117
  }
118
118
  }
119
119
  }
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
120
  if (pks.length === 0) {
127
121
  if (cols.length === 0) {
128
122
  return { query: "", params: args };
@@ -154,11 +148,11 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
154
148
  x = buildParam(i++);
155
149
  if (typeof v === "boolean") {
156
150
  if (v === true) {
157
- const v2 = attr.true ? "" + attr.true : `'1'`;
151
+ const v2 = attr.true !== undefined ? attr.true : `'1'`;
158
152
  args.push(v2);
159
153
  }
160
154
  else {
161
- const v2 = attr.false ? "" + attr.false : `'0'`;
155
+ const v2 = attr.false !== undefined ? attr.false : `'0'`;
162
156
  args.push(v2);
163
157
  }
164
158
  }
@@ -180,7 +174,7 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
180
174
  }
181
175
  }
182
176
  }
183
- export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
177
+ export function buildToSaveBatch(objs, table, attrs, buildParam) {
184
178
  if (!buildParam) {
185
179
  buildParam = param;
186
180
  }
@@ -199,10 +193,10 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
199
193
  if (v == null) {
200
194
  v = attr.default;
201
195
  }
202
- if (v != null && v !== undefined && !attr.ignored && !attr.noinsert) {
196
+ if (v != null && !attr.ignored && !attr.noinsert) {
203
197
  const field = attr.column ? attr.column : k;
204
198
  cols.push(field);
205
- if (k === ver) {
199
+ if (attr.version) {
206
200
  isVersion = true;
207
201
  values.push(`${1}`);
208
202
  }
@@ -218,11 +212,11 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
218
212
  values.push(p);
219
213
  if (typeof v === "boolean") {
220
214
  if (v === true) {
221
- const v2 = attr.true ? attr.true : `'1'`;
215
+ const v2 = attr.true !== undefined ? attr.true : `'1'`;
222
216
  args.push(v2);
223
217
  }
224
218
  else {
225
- const v2 = attr.false ? attr.false : `'0'`;
219
+ const v2 = attr.false !== undefined ? attr.false : `'0'`;
226
220
  args.push(v2);
227
221
  }
228
222
  }
@@ -233,18 +227,12 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
233
227
  }
234
228
  }
235
229
  }
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
230
  const colSet = [];
243
231
  for (const k of ks) {
244
232
  const v = o[k];
245
233
  if (v !== undefined) {
246
234
  const attr = attrs[k];
247
- if (attr && !attr.key && !attr.ignored && k !== ver && !attr.noupdate) {
235
+ if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
248
236
  const field = attr.column ? attr.column : k;
249
237
  let x;
250
238
  if (v === null) {
@@ -260,11 +248,11 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
260
248
  x = buildParam(i++);
261
249
  if (typeof v === "boolean") {
262
250
  if (v === true) {
263
- const v2 = attr.true ? "" + attr.true : `'1'`;
251
+ const v2 = attr.true !== undefined ? attr.true : `'1'`;
264
252
  args.push(v2);
265
253
  }
266
254
  else {
267
- const v2 = attr.false ? "" + attr.false : `'0'`;
255
+ const v2 = attr.false !== undefined ? attr.false : `'0'`;
268
256
  args.push(v2);
269
257
  }
270
258
  }
@@ -301,8 +289,7 @@ export function createPool(conf) {
301
289
  if (conf.max && conf.max > 0 && !conf.connectionLimit) {
302
290
  conf.connectionLimit = conf.max;
303
291
  }
304
- const pool = createPool2(Object.assign(Object.assign({}, conf), { rowsAsArray: true }));
305
- return pool;
292
+ return createPool2(Object.assign(Object.assign({}, conf), { rowsAsArray: true }));
306
293
  }
307
294
  export function getConnection(pool) {
308
295
  return new Promise((resolve, reject) => {
@@ -321,7 +308,7 @@ export function beginTransaction(connection, rollbackIfError) {
321
308
  if (!rollbackIfError) {
322
309
  return reject(err);
323
310
  }
324
- connection.rollback(() => {
311
+ return connection.rollback(() => {
325
312
  return reject(err);
326
313
  });
327
314
  }
@@ -336,11 +323,11 @@ export function commit(connection, rollbackIfError) {
336
323
  if (!rollbackIfError) {
337
324
  return reject(err);
338
325
  }
339
- connection.rollback(() => {
326
+ return connection.rollback(() => {
340
327
  return reject(err);
341
328
  });
342
329
  }
343
- resolve();
330
+ return resolve();
344
331
  });
345
332
  });
346
333
  }
@@ -350,7 +337,7 @@ export function rollback(connection) {
350
337
  if (err) {
351
338
  return reject(err);
352
339
  }
353
- resolve();
340
+ return resolve();
354
341
  });
355
342
  });
356
343
  }
@@ -371,50 +358,45 @@ export class PoolManager {
371
358
  }
372
359
  beginTransaction() {
373
360
  return new Promise((resolve, reject) => {
374
- this.pool.getConnection((er0, connection) => {
375
- if (er0) {
376
- return reject(er0);
361
+ this.pool.getConnection((err, connection) => {
362
+ if (err) {
363
+ return reject(err);
377
364
  }
378
- connection.beginTransaction((er1) => {
379
- if (er1) {
380
- connection.rollback(() => {
381
- return reject(er1);
365
+ connection.beginTransaction((beginError) => {
366
+ if (beginError) {
367
+ return connection.rollback(() => {
368
+ connection.release();
369
+ return reject(beginError);
382
370
  });
383
371
  }
384
- const tx = new PoolConnectionManager(connection);
385
- return resolve(tx);
372
+ return resolve(new PoolConnectionManager(connection));
386
373
  });
387
374
  });
388
375
  });
389
376
  }
390
- execute(sql, args, ctx) {
391
- const p = ctx ? ctx : this.pool;
392
- return execute(p, sql, args);
377
+ execute(sql, args) {
378
+ return execute(this.pool, sql, args);
393
379
  }
394
- executeBatch(statements, firstSuccess, ctx) {
395
- const p = ctx ? ctx : this.pool;
396
- return executeBatch(p, statements, firstSuccess);
380
+ executeBatch(statements, firstAffected) {
381
+ return executeBatch(this.pool, statements, firstAffected);
397
382
  }
398
- query(sql, args, m, bools, ctx) {
399
- const p = ctx ? ctx : this.pool;
400
- return query(p, sql, args, m, bools);
383
+ query(sql, args, m, bools) {
384
+ return query(this.pool, sql, args, m, bools);
401
385
  }
402
- queryOne(sql, args, m, bools, ctx) {
403
- const p = ctx ? ctx : this.pool;
404
- return queryOne(p, sql, args, m, bools);
386
+ queryOne(sql, args, m, bools) {
387
+ return queryOne(this.pool, sql, args, m, bools);
405
388
  }
406
- executeScalar(sql, args, ctx) {
407
- const p = ctx ? ctx : this.pool;
408
- return executeScalar(p, sql, args);
389
+ executeScalar(sql, args) {
390
+ return executeScalar(this.pool, sql, args);
409
391
  }
410
- count(sql, args, ctx) {
411
- const p = ctx ? ctx : this.pool;
412
- return count(p, sql, args);
392
+ count(sql, args) {
393
+ return count(this.pool, sql, args);
413
394
  }
414
395
  }
415
396
  export class PoolConnectionManager {
416
397
  constructor(connection) {
417
398
  this.connection = connection;
399
+ this.released = false;
418
400
  this.driver = "mysql";
419
401
  this.param = this.param.bind(this);
420
402
  this.execute = this.execute.bind(this);
@@ -429,368 +411,204 @@ export class PoolConnectionManager {
429
411
  }
430
412
  commit() {
431
413
  return new Promise((resolve, reject) => {
432
- this.connection.commit((er3) => {
433
- if (er3) {
434
- return reject(er3);
414
+ this.connection.commit((err) => {
415
+ if (err) {
416
+ return this.connection.rollback(() => {
417
+ this.release();
418
+ return reject(err);
419
+ });
435
420
  }
421
+ this.release();
436
422
  return resolve();
437
423
  });
438
424
  });
439
425
  }
440
426
  rollback() {
441
427
  return new Promise((resolve, reject) => {
442
- this.connection.rollback((er3) => {
443
- if (er3) {
444
- return reject(er3);
428
+ this.connection.rollback((err) => {
429
+ this.release();
430
+ if (err) {
431
+ return reject(err);
445
432
  }
446
433
  return resolve();
447
434
  });
448
435
  });
449
436
  }
450
- execute(sql, args, ctx) {
451
- const p = ctx ? ctx : this.connection;
452
- return execute(p, sql, args);
437
+ release() {
438
+ if (this.released) {
439
+ return;
440
+ }
441
+ this.released = true;
442
+ this.connection.release();
443
+ }
444
+ execute(sql, args) {
445
+ return execute(this.connection, sql, args);
453
446
  }
454
- executeBatch(statements, firstSuccess, ctx) {
455
- const p = ctx ? ctx : this.connection;
456
- return executeBatchConnection(p, statements, firstSuccess);
447
+ executeBatch(statements, firstAffected) {
448
+ return executeBatchConnection(this.connection, statements, firstAffected);
457
449
  }
458
450
  query(sql, args, m, bools, ctx) {
459
- const p = ctx ? ctx : this.connection;
460
- return query(p, sql, args, m, bools);
451
+ return query(this.connection, sql, args, m, bools);
461
452
  }
462
- queryOne(sql, args, m, bools, ctx) {
463
- const p = ctx ? ctx : this.connection;
464
- return queryOne(p, sql, args, m, bools);
453
+ queryOne(sql, args, m, bools) {
454
+ return queryOne(this.connection, sql, args, m, bools);
465
455
  }
466
- executeScalar(sql, args, ctx) {
467
- const p = ctx ? ctx : this.connection;
468
- return executeScalar(p, sql, args);
456
+ executeScalar(sql, args) {
457
+ return executeScalar(this.connection, sql, args);
469
458
  }
470
- count(sql, args, ctx) {
471
- const p = ctx ? ctx : this.connection;
472
- return count(p, sql, args);
459
+ count(sql, args) {
460
+ return count(this.connection, sql, args);
473
461
  }
474
462
  }
475
- export function executeBatch(pool, statements, firstSuccess) {
463
+ export function executeBatch(pool, statements, firstAffected) {
476
464
  return __awaiter(this, void 0, void 0, function* () {
477
465
  if (!statements || statements.length === 0) {
478
- return Promise.resolve(0);
466
+ return 0;
479
467
  }
480
- else if (statements.length === 1) {
468
+ if (statements.length === 1) {
481
469
  return execute(pool, statements[0].query, statements[0].params);
482
470
  }
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
- });
490
- });
471
+ const connection = yield getConnection(pool);
472
+ try {
473
+ return yield executeBatchConnectionTx(connection, statements, firstAffected);
474
+ }
475
+ finally {
476
+ connection.release();
477
+ }
491
478
  });
492
479
  }
493
- export function executeBatchConnectionTx(connection, statements, firstSuccess) {
480
+ export function executeBatchConnectionTx(connection, statements, firstAffected) {
494
481
  return __awaiter(this, void 0, void 0, function* () {
495
482
  if (!statements || statements.length === 0) {
496
- return Promise.resolve(0);
483
+ return 0;
497
484
  }
498
- else if (statements.length === 1) {
485
+ if (statements.length === 1) {
499
486
  return execute(connection, statements[0].query, statements[0].params);
500
487
  }
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);
523
- });
524
- }
525
- });
526
- return 0;
527
- }
528
- else {
529
- const queries = [];
530
- const params = [];
531
- const l = statements.length;
532
- for (let j = 1; j < l; j++) {
533
- const item = statements[j];
534
- if (item.query.endsWith(";")) {
535
- queries.push(item.query);
536
- }
537
- else {
538
- queries.push(item.query + ";");
539
- }
540
- if (item.params && item.params.length > 0) {
541
- for (const p of item.params) {
542
- params.push(p);
543
- }
544
- }
545
- }
546
- connection.query(queries.join(""), toArray(params), (er2, results) => {
547
- if (er2) {
548
- connection.rollback(() => {
549
- return reject(er2);
550
- });
551
- }
552
- else {
553
- connection.commit((er3) => {
554
- if (er3) {
555
- connection.rollback(() => {
556
- return reject(er3);
557
- });
558
- }
559
- });
560
- let c = 0;
561
- c += results0.affectedRows + results.affectedRows;
562
- return resolve(c);
563
- }
564
- });
565
- }
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) {
625
- commit(connection, true);
488
+ yield beginTransaction(connection, true);
489
+ try {
490
+ let count = 0;
491
+ if (resource.multipleStatements) {
492
+ if (firstAffected) {
493
+ const firstCount = yield execute(connection, statements[0].query, statements[0].params);
494
+ count = firstCount;
495
+ if (firstCount === 0) {
496
+ yield commit(connection, true);
626
497
  return 0;
627
498
  }
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;
633
- }
634
- commit(connection, true);
635
- return count;
499
+ const remaining = statements.slice(1);
500
+ count += yield executeMultipleStatements(connection, remaining);
636
501
  }
637
- catch (err) {
638
- rollback(connection);
639
- throw err;
502
+ else {
503
+ count = yield executeMultipleStatements(connection, statements);
640
504
  }
641
505
  }
642
506
  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;
507
+ const start = firstAffected ? 1 : 0;
508
+ if (firstAffected) {
509
+ const firstCount = yield execute(connection, statements[0].query, statements[0].params);
510
+ count = firstCount;
511
+ if (firstCount === 0) {
512
+ yield commit(connection, true);
513
+ return 0;
651
514
  }
652
- commit(connection, true);
653
- return count;
654
515
  }
655
- catch (err) {
656
- rollback(connection);
657
- throw err;
516
+ for (let i = start; i < statements.length; i++) {
517
+ const item = statements[i];
518
+ count += yield execute(connection, item.query, item.params);
658
519
  }
659
520
  }
521
+ yield commit(connection, true);
522
+ return count;
523
+ }
524
+ catch (err) {
525
+ try {
526
+ yield rollback(connection);
527
+ }
528
+ catch (_a) {
529
+ }
530
+ throw buildError(err);
660
531
  }
661
532
  });
662
533
  }
663
- export function executeBatchConnection(connection, statements, firstSuccess) {
534
+ export function executeBatchConnection(connection, statements, firstAffected) {
664
535
  return __awaiter(this, void 0, void 0, function* () {
665
536
  if (!statements || statements.length === 0) {
666
- return Promise.resolve(0);
537
+ return 0;
667
538
  }
668
- else if (statements.length === 1) {
539
+ if (statements.length === 1) {
669
540
  return execute(connection, statements[0].query, statements[0].params);
670
541
  }
671
542
  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];
688
- if (item.query.endsWith(";")) {
689
- queries.push(item.query);
690
- }
691
- else {
692
- queries.push(item.query + ";");
693
- }
694
- if (item.params && item.params.length > 0) {
695
- for (const p of item.params) {
696
- params.push(p);
697
- }
698
- }
699
- }
700
- connection.query(queries.join(""), toArray(params), (er2, results) => {
701
- if (er2) {
702
- return reject(er2);
703
- }
704
- else {
705
- let c = 0;
706
- c += results0.affectedRows + results.affectedRows;
707
- return resolve(c);
708
- }
709
- });
710
- }
711
- }
712
- });
713
- });
543
+ if (firstAffected) {
544
+ const firstCount = yield execute(connection, statements[0].query, statements[0].params);
545
+ if (firstCount === 0) {
546
+ return 0;
547
+ }
548
+ return firstCount + (yield executeMultipleStatements(connection, statements.slice(1)));
714
549
  }
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
- });
550
+ return executeMultipleStatements(connection, statements);
551
+ }
552
+ let count = 0;
553
+ const start = firstAffected ? 1 : 0;
554
+ if (firstAffected) {
555
+ const firstCount = yield execute(connection, statements[0].query, statements[0].params);
556
+ count = firstCount;
557
+ if (firstCount === 0) {
558
+ return 0;
751
559
  }
752
560
  }
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;
768
- }
769
- catch (er1) {
770
- throw er1;
771
- }
772
- }
773
- catch (er0) {
774
- throw er0;
775
- }
561
+ for (let i = start; i < statements.length; i++) {
562
+ const item = statements[i];
563
+ count += yield execute(connection, item.query, item.params);
564
+ }
565
+ return count;
566
+ });
567
+ }
568
+ function executeMultipleStatements(connection, statements) {
569
+ return __awaiter(this, void 0, void 0, function* () {
570
+ if (!statements || statements.length === 0) {
571
+ return 0;
572
+ }
573
+ const queries = [];
574
+ const params = [];
575
+ for (const item of statements) {
576
+ queries.push(ensureSemicolon(item.query));
577
+ if (item.params && item.params.length > 0) {
578
+ params.push(...item.params);
776
579
  }
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;
580
+ }
581
+ return new Promise((resolve, reject) => {
582
+ connection.query(queries.join(""), toArray(params), (err, results) => {
583
+ if (err) {
584
+ buildError(err);
585
+ return reject(err);
790
586
  }
587
+ return resolve(getAffectedRows(results));
588
+ });
589
+ });
590
+ });
591
+ }
592
+ function ensureSemicolon(sql) {
593
+ return sql.endsWith(";") ? sql : `${sql};`;
594
+ }
595
+ function getAffectedRows(results) {
596
+ if (!results) {
597
+ return 0;
598
+ }
599
+ if (Array.isArray(results)) {
600
+ let count = 0;
601
+ for (const result of results) {
602
+ if (result && typeof result.affectedRows === "number") {
603
+ count += result.affectedRows;
791
604
  }
792
605
  }
793
- });
606
+ return count;
607
+ }
608
+ if (typeof results.affectedRows === "number") {
609
+ return results.affectedRows;
610
+ }
611
+ return 0;
794
612
  }
795
613
  function buildError(err) {
796
614
  if (err.errno === 1062 && err.code === "ER_DUP_ENTRY") {
@@ -802,14 +620,12 @@ export function execute(pool, sql, args) {
802
620
  return __awaiter(this, void 0, void 0, function* () {
803
621
  const p = toArray(args);
804
622
  return new Promise((resolve, reject) => {
805
- return pool.execute(sql, p, (err, res) => {
623
+ pool.execute(sql, p, (err, res) => {
806
624
  if (err) {
807
625
  buildError(err);
808
626
  return reject(err);
809
627
  }
810
- else {
811
- return resolve(res.affectedRows);
812
- }
628
+ return resolve(res.affectedRows);
813
629
  });
814
630
  });
815
631
  });
@@ -818,26 +634,20 @@ export function query(pool, sql, args, m, bools) {
818
634
  return __awaiter(this, void 0, void 0, function* () {
819
635
  const p = toArray(args);
820
636
  return new Promise((resolve, reject) => {
821
- return pool.query(sql, p, (err, results, fields) => {
637
+ pool.query(sql, p, (err, results, fields) => {
822
638
  if (err) {
823
639
  return reject(err);
824
640
  }
825
- else {
826
- if (results.length > 0) {
827
- const arrayResult = results.map((item) => {
828
- if (Array.isArray(item)) {
829
- return formatData(fields, item);
830
- }
831
- else {
832
- return item;
833
- }
834
- });
835
- return resolve(handleResults(arrayResult, m, bools));
836
- }
837
- else {
838
- resolve([]);
839
- }
641
+ if (results.length === 0) {
642
+ return resolve([]);
840
643
  }
644
+ const arrayResult = results.map((item) => {
645
+ if (Array.isArray(item)) {
646
+ return formatData(fields, item);
647
+ }
648
+ return item;
649
+ });
650
+ return resolve(handleResults(arrayResult, m, bools));
841
651
  });
842
652
  });
843
653
  });
@@ -852,17 +662,15 @@ export function executeScalar(pool, sql, args) {
852
662
  if (!r) {
853
663
  return null;
854
664
  }
855
- else {
856
- const keys = Object.keys(r);
857
- return r[keys[0]];
858
- }
665
+ const keys = Object.keys(r);
666
+ return r[keys[0]];
859
667
  });
860
668
  }
861
669
  export function count(pool, sql, args) {
862
- return executeScalar(pool, sql, args);
670
+ return executeScalar(pool, sql, args).then((res) => (res !== null ? res : 0));
863
671
  }
864
- export function save(pool, obj, table, attrs, ver, buildParam) {
865
- const s = buildToSave(obj, table, attrs, ver, buildParam);
672
+ export function save(pool, obj, table, attrs, buildParam) {
673
+ const s = buildToSave(obj, table, attrs, buildParam);
866
674
  if (!s.query) {
867
675
  return Promise.resolve(-1);
868
676
  }
@@ -873,8 +681,8 @@ export function save(pool, obj, table, attrs, ver, buildParam) {
873
681
  return execute(pool, s.query, s.params);
874
682
  }
875
683
  }
876
- export function saveBatch(pool, objs, table, attrs, ver, buildParam) {
877
- const s = buildToSaveBatch(objs, table, attrs, ver, buildParam);
684
+ export function saveBatch(pool, objs, table, attrs, buildParam) {
685
+ const s = buildToSaveBatch(objs, table, attrs, buildParam);
878
686
  if (typeof pool === "function") {
879
687
  return pool(s);
880
688
  }
@@ -887,30 +695,25 @@ export function toArray(arr) {
887
695
  return [];
888
696
  }
889
697
  const p = [];
890
- const l = arr.length;
891
- for (let i = 0; i < l; i++) {
892
- if (arr[i] === undefined || arr[i] == null) {
698
+ for (const value of arr) {
699
+ if (value === undefined || value === null) {
893
700
  p.push(null);
701
+ continue;
894
702
  }
895
- else {
896
- if (typeof arr[i] === "object") {
897
- if (arr[i] instanceof Date) {
898
- p.push(arr[i]);
899
- }
900
- else {
901
- if (resource.string) {
902
- const s = JSON.stringify(arr[i]);
903
- p.push(s);
904
- }
905
- else {
906
- p.push(arr[i]);
907
- }
908
- }
703
+ if (typeof value === "object") {
704
+ if (value instanceof Date) {
705
+ p.push(value);
706
+ continue;
707
+ }
708
+ if (resource.string) {
709
+ p.push(JSON.stringify(value));
909
710
  }
910
711
  else {
911
- p.push(arr[i]);
712
+ p.push(value);
912
713
  }
714
+ continue;
913
715
  }
716
+ p.push(value);
914
717
  }
915
718
  return p;
916
719
  }
@@ -920,18 +723,12 @@ export function handleResults(r, m, bools) {
920
723
  if (bools && bools.length > 0) {
921
724
  return handleBool(res, bools);
922
725
  }
923
- else {
924
- return res;
925
- }
726
+ return res;
926
727
  }
927
- else {
928
- if (bools && bools.length > 0) {
929
- return handleBool(r, bools);
930
- }
931
- else {
932
- return r;
933
- }
728
+ if (bools && bools.length > 0) {
729
+ return handleBool(r, bools);
934
730
  }
731
+ return r;
935
732
  }
936
733
  export function handleBool(objs, bools) {
937
734
  if (!bools || bools.length === 0 || !objs) {
@@ -940,17 +737,19 @@ export function handleBool(objs, bools) {
940
737
  for (const obj of objs) {
941
738
  const o = obj;
942
739
  for (const field of bools) {
943
- if (field.name) {
944
- const v = o[field.name];
945
- if (typeof v !== "boolean" && v != null && v !== undefined) {
946
- const b = field.true;
947
- if (b == null || b === undefined) {
948
- o[field.name] = "1" == v || "T" == v || "Y" == v || "true" == v || "on" == v;
949
- }
950
- else {
951
- o[field.name] = v == b ? true : false;
952
- }
953
- }
740
+ if (!field.name) {
741
+ continue;
742
+ }
743
+ const v = o[field.name];
744
+ if (typeof v === "boolean" || v == null) {
745
+ continue;
746
+ }
747
+ const b = field.true;
748
+ if (b == null) {
749
+ o[field.name] = "1" == v || "T" == v || "Y" == v || "true" == v || "on" == v;
750
+ }
751
+ else {
752
+ o[field.name] = v == b;
954
753
  }
955
754
  }
956
755
  }
@@ -960,7 +759,7 @@ export function formatData(nameColumn, data, m) {
960
759
  const result = {};
961
760
  nameColumn.forEach((item, index) => {
962
761
  let key = item.name;
963
- if (m) {
762
+ if (m && m[item.name]) {
964
763
  key = m[item.name];
965
764
  }
966
765
  result[key] = data[index];
@@ -995,9 +794,7 @@ export function mapArray(results, m) {
995
794
  return results;
996
795
  }
997
796
  const objs = [];
998
- const length = results.length;
999
- for (let i = 0; i < length; i++) {
1000
- const obj = results[i];
797
+ for (const obj of results) {
1001
798
  const obj2 = {};
1002
799
  const keys = Object.keys(obj);
1003
800
  for (const key of keys) {
@@ -1058,7 +855,7 @@ export function getMapField(name, mp) {
1058
855
  export function isEmpty(s) {
1059
856
  return !(s && s.length > 0);
1060
857
  }
1061
- export class StringService {
858
+ export class StringAdapter {
1062
859
  constructor(pool, table, column) {
1063
860
  this.pool = pool;
1064
861
  this.table = table;
@@ -1084,35 +881,15 @@ export class StringService {
1084
881
  return execute(this.pool, s, values);
1085
882
  }
1086
883
  }
1087
- export function version(attrs) {
1088
- const ks = Object.keys(attrs);
1089
- for (const k of ks) {
1090
- const attr = attrs[k];
1091
- if (attr.version) {
1092
- attr.name = k;
1093
- return attr;
1094
- }
1095
- }
1096
- return undefined;
1097
- }
1098
884
  export class MySQLWriter {
1099
- constructor(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
885
+ constructor(pool, table, attributes, oneIfSuccess, map, buildParam) {
886
+ this.pool = pool;
1100
887
  this.table = table;
1101
888
  this.attributes = attributes;
1102
889
  this.oneIfSuccess = oneIfSuccess;
890
+ this.map = map;
1103
891
  this.write = this.write.bind(this);
1104
- if (typeof pool === "function") {
1105
- this.execute = pool;
1106
- }
1107
- else {
1108
- this.pool = pool;
1109
- }
1110
- this.param = buildParam;
1111
- this.map = toDB;
1112
- const x = version(attributes);
1113
- if (x) {
1114
- this.version = x.name;
1115
- }
892
+ this.param = buildParam ? buildParam : param;
1116
893
  }
1117
894
  write(obj) {
1118
895
  if (!obj) {
@@ -1122,23 +899,13 @@ export class MySQLWriter {
1122
899
  if (this.map) {
1123
900
  obj2 = this.map(obj);
1124
901
  }
1125
- const stmt = buildToSave(obj2, this.table, this.attributes, this.version, this.param);
902
+ const stmt = buildToSave(obj2, this.table, this.attributes, this.param);
1126
903
  if (stmt.query) {
1127
- if (this.execute) {
1128
- if (this.oneIfSuccess) {
1129
- return this.execute(stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
1130
- }
1131
- else {
1132
- return this.execute(stmt.query, stmt.params);
1133
- }
904
+ if (this.oneIfSuccess) {
905
+ return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
1134
906
  }
1135
907
  else {
1136
- if (this.oneIfSuccess) {
1137
- return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
1138
- }
1139
- else {
1140
- return execute(this.pool, stmt.query, stmt.params);
1141
- }
908
+ return execute(this.pool, stmt.query, stmt.params);
1142
909
  }
1143
910
  }
1144
911
  else {
@@ -1147,28 +914,16 @@ export class MySQLWriter {
1147
914
  }
1148
915
  }
1149
916
  export class MySQLStreamWriter {
1150
- constructor(pool, table, attributes, size, toDB, buildParam) {
917
+ constructor(pool, table, attributes, size = 1000, map, buildParam) {
918
+ this.pool = pool;
1151
919
  this.table = table;
1152
920
  this.attributes = attributes;
921
+ this.size = size;
922
+ this.map = map;
1153
923
  this.list = [];
1154
- this.size = 0;
1155
924
  this.write = this.write.bind(this);
1156
925
  this.flush = this.flush.bind(this);
1157
- if (typeof pool === "function") {
1158
- this.executeBatch = pool;
1159
- }
1160
- else {
1161
- this.pool = pool;
1162
- }
1163
- this.param = buildParam;
1164
- this.map = toDB;
1165
- const x = version(attributes);
1166
- if (x) {
1167
- this.version = x.name;
1168
- }
1169
- if (size) {
1170
- this.size = size;
1171
- }
926
+ this.param = buildParam ? buildParam : param;
1172
927
  }
1173
928
  write(obj) {
1174
929
  if (!obj) {
@@ -1194,46 +949,29 @@ export class MySQLStreamWriter {
1194
949
  return Promise.resolve(0);
1195
950
  }
1196
951
  else {
1197
- const total = this.list.length;
1198
- const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.version, this.param);
952
+ const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.param);
1199
953
  if (stmt.length > 0) {
1200
- if (this.executeBatch) {
1201
- return this.executeBatch(stmt).then((r) => {
1202
- this.list = [];
1203
- return total;
1204
- });
1205
- }
1206
- else {
1207
- return executeBatch(this.pool, stmt).then((r) => {
1208
- this.list = [];
1209
- return total;
1210
- });
1211
- }
954
+ return executeBatch(this.pool, stmt).then((r) => {
955
+ this.list = [];
956
+ return stmt.length;
957
+ });
1212
958
  }
1213
959
  else {
960
+ this.list = [];
1214
961
  return Promise.resolve(0);
1215
962
  }
1216
963
  }
1217
964
  }
1218
965
  }
1219
966
  export class MySQLBatchWriter {
1220
- constructor(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
967
+ constructor(pool, table, attributes, oneIfSuccess, map, buildParam) {
968
+ this.pool = pool;
1221
969
  this.table = table;
1222
970
  this.attributes = attributes;
1223
971
  this.oneIfSuccess = oneIfSuccess;
972
+ this.map = map;
1224
973
  this.write = this.write.bind(this);
1225
- if (typeof pool === "function") {
1226
- this.execute = pool;
1227
- }
1228
- else {
1229
- this.pool = pool;
1230
- }
1231
- this.param = buildParam;
1232
- this.map = toDB;
1233
- const x = version(attributes);
1234
- if (x) {
1235
- this.version = x.name;
1236
- }
974
+ this.param = buildParam ? buildParam : param;
1237
975
  }
1238
976
  write(objs) {
1239
977
  if (!objs || objs.length === 0) {
@@ -1247,23 +985,13 @@ export class MySQLBatchWriter {
1247
985
  list.push(obj2);
1248
986
  }
1249
987
  }
1250
- const stmts = buildToSaveBatch(list, this.table, this.attributes, this.version, this.param);
988
+ const stmts = buildToSaveBatch(list, this.table, this.attributes, this.param);
1251
989
  if (stmts.length > 0) {
1252
- if (this.execute) {
1253
- if (this.oneIfSuccess) {
1254
- return this.execute(stmts).then((ct) => stmts.length);
1255
- }
1256
- else {
1257
- return this.execute(stmts);
1258
- }
990
+ if (this.oneIfSuccess) {
991
+ return executeBatch(this.pool, stmts).then((ct) => stmts.length);
1259
992
  }
1260
993
  else {
1261
- if (this.oneIfSuccess) {
1262
- return executeBatch(this.pool, stmts).then((ct) => stmts.length);
1263
- }
1264
- else {
1265
- return executeBatch(this.pool, stmts);
1266
- }
994
+ return executeBatch(this.pool, stmts);
1267
995
  }
1268
996
  }
1269
997
  else {
@@ -1313,13 +1041,16 @@ export class MySQLChecker {
1313
1041
  }
1314
1042
  }
1315
1043
  export class Exporter {
1316
- constructor(connection, buildQuery, ft, write, end, attributes) {
1044
+ constructor(connection, filename, buildQuery, format, write, end, attributes, logInfo, progressSize = 10000) {
1317
1045
  this.connection = connection;
1046
+ this.filename = filename;
1318
1047
  this.buildQuery = buildQuery;
1048
+ this.format = format;
1319
1049
  this.write = write;
1320
1050
  this.end = end;
1321
1051
  this.attributes = attributes;
1322
- this.format = ft;
1052
+ this.logInfo = logInfo;
1053
+ this.progressSize = progressSize;
1323
1054
  if (attributes) {
1324
1055
  this.map = buildMap(attributes);
1325
1056
  }
@@ -1327,28 +1058,43 @@ export class Exporter {
1327
1058
  }
1328
1059
  export(ctx) {
1329
1060
  return __awaiter(this, void 0, void 0, function* () {
1330
- let idx = 0;
1331
1061
  const stmt = yield this.buildQuery(ctx);
1332
1062
  const reader = this.connection.query(stmt.query, stmt.params);
1333
1063
  let er;
1064
+ let i = 0;
1065
+ let j = 0;
1334
1066
  reader.on("error", (err) => (er = err));
1335
1067
  if (this.map) {
1336
1068
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1337
- ++idx;
1069
+ ++i;
1070
+ j++;
1338
1071
  this.connection.pause();
1339
1072
  const obj = mapOne(row, this.map);
1340
1073
  const data = this.format(obj);
1341
1074
  this.write(data);
1342
1075
  this.connection.resume();
1076
+ if (j >= this.progressSize) {
1077
+ if (this.logInfo) {
1078
+ this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1079
+ }
1080
+ j = 0;
1081
+ }
1343
1082
  }));
1344
1083
  }
1345
1084
  else {
1346
1085
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1347
- ++idx;
1086
+ ++i;
1087
+ j++;
1348
1088
  this.connection.pause();
1349
1089
  const data = this.format(row);
1350
1090
  this.write(data);
1351
1091
  this.connection.resume();
1092
+ if (j >= this.progressSize) {
1093
+ if (this.logInfo) {
1094
+ this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1095
+ }
1096
+ j = 0;
1097
+ }
1352
1098
  }));
1353
1099
  }
1354
1100
  return new Promise((resolve, reject) => {
@@ -1363,7 +1109,7 @@ export class Exporter {
1363
1109
  reject(err);
1364
1110
  }
1365
1111
  else {
1366
- resolve(idx);
1112
+ resolve(i);
1367
1113
  }
1368
1114
  });
1369
1115
  }
@@ -1373,12 +1119,15 @@ export class Exporter {
1373
1119
  }
1374
1120
  }
1375
1121
  export class ExportService {
1376
- constructor(connection, queryBuilder, formatter, writer, attributes) {
1122
+ constructor(connection, filename, queryBuilder, formatter, writer, attributes, logInfo, progressSize = 10000) {
1377
1123
  this.connection = connection;
1124
+ this.filename = filename;
1378
1125
  this.queryBuilder = queryBuilder;
1379
1126
  this.formatter = formatter;
1380
1127
  this.writer = writer;
1381
1128
  this.attributes = attributes;
1129
+ this.logInfo = logInfo;
1130
+ this.progressSize = progressSize;
1382
1131
  if (attributes) {
1383
1132
  this.map = buildMap(attributes);
1384
1133
  }
@@ -1386,28 +1135,43 @@ export class ExportService {
1386
1135
  }
1387
1136
  export(ctx) {
1388
1137
  return __awaiter(this, void 0, void 0, function* () {
1389
- let idx = 0;
1390
1138
  const stmt = yield this.queryBuilder.build(ctx);
1391
1139
  const reader = this.connection.query(stmt.query, stmt.params);
1392
1140
  let er;
1393
1141
  reader.on("error", (err) => (er = err));
1142
+ let i = 0;
1143
+ let j = 0;
1394
1144
  if (this.map) {
1395
1145
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1396
- ++idx;
1146
+ ++i;
1147
+ j++;
1397
1148
  this.connection.pause();
1398
1149
  const obj = mapOne(row, this.map);
1399
1150
  const data = this.formatter.format(obj);
1400
1151
  this.writer.write(data);
1401
1152
  this.connection.resume();
1153
+ if (j >= this.progressSize) {
1154
+ if (this.logInfo) {
1155
+ this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1156
+ }
1157
+ j = 0;
1158
+ }
1402
1159
  }));
1403
1160
  }
1404
1161
  else {
1405
1162
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1406
- ++idx;
1163
+ ++i;
1164
+ j++;
1407
1165
  this.connection.pause();
1408
1166
  const data = this.formatter.format(row);
1409
1167
  this.writer.write(data);
1410
1168
  this.connection.resume();
1169
+ if (j >= this.progressSize) {
1170
+ if (this.logInfo) {
1171
+ this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1172
+ }
1173
+ j = 0;
1174
+ }
1411
1175
  }));
1412
1176
  }
1413
1177
  return new Promise((resolve, reject) => {
@@ -1427,7 +1191,7 @@ export class ExportService {
1427
1191
  reject(err);
1428
1192
  }
1429
1193
  else {
1430
- resolve(idx);
1194
+ resolve(i);
1431
1195
  }
1432
1196
  });
1433
1197
  }