baja-lite 1.0.26 → 1.0.29
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/cjs/boot.js +18 -0
- package/cjs/sql.d.ts +94 -29
- package/cjs/sql.js +583 -265
- package/cjs/test-postgresql.d.ts +2 -0
- package/cjs/test-postgresql.js +93 -0
- package/es/boot.js +19 -1
- package/es/sql.d.ts +94 -29
- package/es/sql.js +582 -265
- package/es/test-postgresql.d.ts +2 -0
- package/es/test-postgresql.js +90 -0
- package/package.json +13 -8
- package/src/boot.ts +22 -1
- package/src/sql.ts +624 -244
- package/src/test-postgresql.ts +79 -0
package/cjs/sql.js
CHANGED
|
@@ -34,10 +34,10 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
34
34
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
35
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
36
|
};
|
|
37
|
-
var _b, _c, _d;
|
|
38
|
-
var
|
|
37
|
+
var _b, _c, _d, _e;
|
|
38
|
+
var _f, _g, _h, _j;
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
-
exports.SqlService = exports.DB = exports.Field = exports.SqlCache = exports.SqliteRemote = exports.SqliteRemoteConnection = exports.Sqlite = exports.Mysql = exports._defOption = exports.SqliteMemory = exports.ColumnMode = exports.SqlType = exports.SelectResult = exports.TemplateResult = exports.SelectMode = exports.DeleteMode = exports.InsertMode = exports.SyncMode = exports.MapperIfUndefined = exports.DBType = exports.logger = exports._fs = exports._path = exports._EventBus = exports._GlobalSqlOption = exports._Hump = exports._enums = exports._primaryDB = exports._dao = exports._sqlCache = exports.extensionCodec = void 0;
|
|
40
|
+
exports.SqlService = exports.DB = exports.Field = exports.SqlCache = exports.SqliteRemote = exports.SqliteRemoteConnection = exports.Sqlite = exports.Postgresql = exports.Mysql = exports._defOption = exports.SqliteMemory = exports.ColumnMode = exports.SqlType = exports.SelectResult = exports.TemplateResult = exports.SelectMode = exports.DeleteMode = exports.InsertMode = exports.SyncMode = exports.MapperIfUndefined = exports.DBType = exports.logger = exports._fs = exports._path = exports._EventBus = exports._GlobalSqlOption = exports._Hump = exports._enums = exports._primaryDB = exports._dao = exports._sqlCache = exports.extensionCodec = void 0;
|
|
41
41
|
exports.flatData = flatData;
|
|
42
42
|
exports.DeclareClass = DeclareClass;
|
|
43
43
|
exports.DeclareService = DeclareService;
|
|
@@ -50,7 +50,6 @@ exports.excuteWithCache = excuteWithCache;
|
|
|
50
50
|
exports.MethodCache = MethodCache;
|
|
51
51
|
const error_1 = require("./error");
|
|
52
52
|
const tslib_1 = __importDefault(require("tslib"));
|
|
53
|
-
const sqlstring_1 = __importDefault(require("sqlstring"));
|
|
54
53
|
const iterare_1 = __importDefault(require("iterare"));
|
|
55
54
|
const string_1 = require("./string");
|
|
56
55
|
const pino_1 = __importDefault(require("pino"));
|
|
@@ -111,7 +110,6 @@ exports._sqlCache = Symbol('sqlMap');
|
|
|
111
110
|
exports._dao = Symbol('dao');
|
|
112
111
|
exports._primaryDB = '______primaryDB_______';
|
|
113
112
|
const _dbType = Symbol('dbType');
|
|
114
|
-
const _formatDialect = Symbol('FormatDialect');
|
|
115
113
|
const _sqlite_version = Symbol('sqlite_version');
|
|
116
114
|
const _daoConnection = Symbol('daoConnection');
|
|
117
115
|
const _inTransaction = Symbol('inTransaction');
|
|
@@ -138,11 +136,12 @@ globalThis[_resultMap_SQLID] = {};
|
|
|
138
136
|
var DBType;
|
|
139
137
|
(function (DBType) {
|
|
140
138
|
DBType[DBType["Mysql"] = 0] = "Mysql";
|
|
141
|
-
DBType[DBType["
|
|
142
|
-
DBType[DBType["
|
|
143
|
-
DBType[DBType["
|
|
144
|
-
DBType[DBType["
|
|
145
|
-
DBType[DBType["
|
|
139
|
+
DBType[DBType["Postgresql"] = 1] = "Postgresql";
|
|
140
|
+
DBType[DBType["Sqlite"] = 2] = "Sqlite";
|
|
141
|
+
DBType[DBType["Mongo"] = 3] = "Mongo";
|
|
142
|
+
DBType[DBType["SqliteRemote"] = 4] = "SqliteRemote";
|
|
143
|
+
DBType[DBType["Redis"] = 5] = "Redis";
|
|
144
|
+
DBType[DBType["RedisLock"] = 6] = "RedisLock";
|
|
146
145
|
})(DBType || (exports.DBType = DBType = {}));
|
|
147
146
|
;
|
|
148
147
|
var MapperIfUndefined;
|
|
@@ -278,17 +277,7 @@ var SqlType;
|
|
|
278
277
|
SqlType[SqlType["mediumtext"] = 20] = "mediumtext";
|
|
279
278
|
SqlType[SqlType["longblob"] = 21] = "longblob";
|
|
280
279
|
SqlType[SqlType["longtext"] = 22] = "longtext";
|
|
281
|
-
SqlType[SqlType["
|
|
282
|
-
SqlType[SqlType["enum"] = 24] = "enum";
|
|
283
|
-
SqlType[SqlType["json"] = 25] = "json";
|
|
284
|
-
SqlType[SqlType["geometry"] = 26] = "geometry";
|
|
285
|
-
SqlType[SqlType["point"] = 27] = "point";
|
|
286
|
-
SqlType[SqlType["linestring"] = 28] = "linestring";
|
|
287
|
-
SqlType[SqlType["polygon"] = 29] = "polygon";
|
|
288
|
-
SqlType[SqlType["multipoint"] = 30] = "multipoint";
|
|
289
|
-
SqlType[SqlType["multilinestring"] = 31] = "multilinestring";
|
|
290
|
-
SqlType[SqlType["multipolygon"] = 32] = "multipolygon";
|
|
291
|
-
SqlType[SqlType["geometrycollection"] = 33] = "geometrycollection";
|
|
280
|
+
SqlType[SqlType["json"] = 23] = "json";
|
|
292
281
|
})(SqlType || (exports.SqlType = SqlType = {}));
|
|
293
282
|
var ColumnMode;
|
|
294
283
|
(function (ColumnMode) {
|
|
@@ -320,7 +309,7 @@ class MysqlConnection {
|
|
|
320
309
|
}
|
|
321
310
|
;
|
|
322
311
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
323
|
-
exports.logger.trace(
|
|
312
|
+
exports.logger.trace(sql, params);
|
|
324
313
|
}
|
|
325
314
|
return new Promise(async (resolve, reject) => {
|
|
326
315
|
try {
|
|
@@ -353,7 +342,7 @@ class MysqlConnection {
|
|
|
353
342
|
}
|
|
354
343
|
;
|
|
355
344
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
356
|
-
exports.logger.trace(
|
|
345
|
+
exports.logger.trace(sql, params);
|
|
357
346
|
}
|
|
358
347
|
return new Promise(async (resolve, reject) => {
|
|
359
348
|
try {
|
|
@@ -389,7 +378,7 @@ class MysqlConnection {
|
|
|
389
378
|
}
|
|
390
379
|
;
|
|
391
380
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
392
|
-
exports.logger.trace(
|
|
381
|
+
exports.logger.trace(sql, params);
|
|
393
382
|
}
|
|
394
383
|
return new Promise(async (resolve, reject) => {
|
|
395
384
|
try {
|
|
@@ -423,7 +412,7 @@ class MysqlConnection {
|
|
|
423
412
|
}
|
|
424
413
|
;
|
|
425
414
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
426
|
-
exports.logger.trace(
|
|
415
|
+
exports.logger.trace(sql, params);
|
|
427
416
|
}
|
|
428
417
|
return new Promise(async (resolve, reject) => {
|
|
429
418
|
try {
|
|
@@ -457,7 +446,7 @@ class MysqlConnection {
|
|
|
457
446
|
}
|
|
458
447
|
;
|
|
459
448
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
460
|
-
exports.logger.trace(
|
|
449
|
+
exports.logger.trace(sql, params);
|
|
461
450
|
}
|
|
462
451
|
return new Promise(async (resolve, reject) => {
|
|
463
452
|
try {
|
|
@@ -493,6 +482,11 @@ class Mysql {
|
|
|
493
482
|
constructor(pool) {
|
|
494
483
|
this[_daoDB] = pool;
|
|
495
484
|
}
|
|
485
|
+
async keepAlive() {
|
|
486
|
+
const connection = await this[_daoDB].getConnection();
|
|
487
|
+
connection.query('SELECT 1 FROM DUAL');
|
|
488
|
+
setTimeout(async () => await this.keepAlive(), 60000);
|
|
489
|
+
}
|
|
496
490
|
createConnection(sync) {
|
|
497
491
|
if (sync === SyncMode.Sync) {
|
|
498
492
|
exports.logger.error('MYSQL not suppouted sync mode');
|
|
@@ -578,11 +572,311 @@ class Mysql {
|
|
|
578
572
|
}
|
|
579
573
|
}
|
|
580
574
|
exports.Mysql = Mysql;
|
|
581
|
-
class
|
|
575
|
+
class PostgresqlConnection {
|
|
582
576
|
constructor(conn) {
|
|
583
577
|
this[_c] = false;
|
|
584
578
|
this[_daoConnection] = conn;
|
|
585
579
|
}
|
|
580
|
+
execute(sync, sql, params) {
|
|
581
|
+
exports.logger.debug(sql, params ?? '');
|
|
582
|
+
if (!sql) {
|
|
583
|
+
return { affectedRows: 0, insertId: 0n };
|
|
584
|
+
}
|
|
585
|
+
;
|
|
586
|
+
if (sync === SyncMode.Sync) {
|
|
587
|
+
exports.logger.warn('Postgresql not suppouted sync mode');
|
|
588
|
+
return { affectedRows: 0, insertId: 0n };
|
|
589
|
+
}
|
|
590
|
+
;
|
|
591
|
+
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
592
|
+
exports.logger.trace(sql, params);
|
|
593
|
+
}
|
|
594
|
+
return new Promise(async (resolve, reject) => {
|
|
595
|
+
try {
|
|
596
|
+
let index = 1;
|
|
597
|
+
const { rowCount } = await this[_daoConnection].query({
|
|
598
|
+
text: sql.replace(/\?/g, () => `$${index++}`),
|
|
599
|
+
values: params
|
|
600
|
+
});
|
|
601
|
+
const result = rowCount;
|
|
602
|
+
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
603
|
+
exports.logger.trace(result);
|
|
604
|
+
}
|
|
605
|
+
resolve({ affectedRows: result.affectedRows, insertId: result.insertId });
|
|
606
|
+
}
|
|
607
|
+
catch (error) {
|
|
608
|
+
exports.logger.error(`
|
|
609
|
+
error: ${error},
|
|
610
|
+
sql: ${sql},
|
|
611
|
+
params: ${params}
|
|
612
|
+
`);
|
|
613
|
+
reject(error);
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
pluck(sync, sql, params) {
|
|
618
|
+
exports.logger.debug(sql, params ?? '');
|
|
619
|
+
if (!sql) {
|
|
620
|
+
return null;
|
|
621
|
+
}
|
|
622
|
+
;
|
|
623
|
+
if (sync === SyncMode.Sync) {
|
|
624
|
+
exports.logger.warn('Postgresql not suppouted sync mode');
|
|
625
|
+
return null;
|
|
626
|
+
}
|
|
627
|
+
;
|
|
628
|
+
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
629
|
+
exports.logger.trace(sql, params);
|
|
630
|
+
}
|
|
631
|
+
return new Promise(async (resolve, reject) => {
|
|
632
|
+
try {
|
|
633
|
+
let index = 1;
|
|
634
|
+
const { rows } = await this[_daoConnection].query({
|
|
635
|
+
text: sql.replace(/\?/g, () => `$${index++}`),
|
|
636
|
+
values: params
|
|
637
|
+
});
|
|
638
|
+
if (rows && rows[0]) {
|
|
639
|
+
const r = Object.values(rows[0])[0];
|
|
640
|
+
if (r === null)
|
|
641
|
+
resolve(r);
|
|
642
|
+
else
|
|
643
|
+
resolve(r);
|
|
644
|
+
}
|
|
645
|
+
resolve(null);
|
|
646
|
+
}
|
|
647
|
+
catch (error) {
|
|
648
|
+
exports.logger.error(`
|
|
649
|
+
error: ${error},
|
|
650
|
+
sql: ${sql},
|
|
651
|
+
params: ${params}
|
|
652
|
+
`);
|
|
653
|
+
reject(error);
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
get(sync, sql, params) {
|
|
658
|
+
exports.logger.debug(sql, params ?? '');
|
|
659
|
+
if (!sql) {
|
|
660
|
+
return null;
|
|
661
|
+
}
|
|
662
|
+
;
|
|
663
|
+
if (sync === SyncMode.Sync) {
|
|
664
|
+
exports.logger.warn('Postgresql not suppouted sync mode');
|
|
665
|
+
return null;
|
|
666
|
+
}
|
|
667
|
+
;
|
|
668
|
+
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
669
|
+
exports.logger.trace(sql, params);
|
|
670
|
+
}
|
|
671
|
+
return new Promise(async (resolve, reject) => {
|
|
672
|
+
try {
|
|
673
|
+
let index = 1;
|
|
674
|
+
const { rows } = await this[_daoConnection].query({
|
|
675
|
+
text: sql.replace(/\?/g, () => `$${index++}`),
|
|
676
|
+
values: params
|
|
677
|
+
});
|
|
678
|
+
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
679
|
+
exports.logger.trace(rows);
|
|
680
|
+
}
|
|
681
|
+
if (rows && rows[0])
|
|
682
|
+
resolve(rows[0]);
|
|
683
|
+
resolve(null);
|
|
684
|
+
}
|
|
685
|
+
catch (error) {
|
|
686
|
+
exports.logger.error(`
|
|
687
|
+
error: ${error},
|
|
688
|
+
sql: ${sql},
|
|
689
|
+
params: ${params}
|
|
690
|
+
`);
|
|
691
|
+
reject(error);
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
raw(sync, sql, params) {
|
|
696
|
+
exports.logger.debug(sql, params ?? '');
|
|
697
|
+
if (!sql) {
|
|
698
|
+
return [];
|
|
699
|
+
}
|
|
700
|
+
;
|
|
701
|
+
if (sync === SyncMode.Sync) {
|
|
702
|
+
exports.logger.warn('Postgresql not suppouted sync mode');
|
|
703
|
+
return [];
|
|
704
|
+
}
|
|
705
|
+
;
|
|
706
|
+
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
707
|
+
exports.logger.trace(sql, params);
|
|
708
|
+
}
|
|
709
|
+
return new Promise(async (resolve, reject) => {
|
|
710
|
+
try {
|
|
711
|
+
let index = 1;
|
|
712
|
+
const { rows } = await this[_daoConnection].query({
|
|
713
|
+
text: sql.replace(/\?/g, () => `$${index++}`),
|
|
714
|
+
values: params
|
|
715
|
+
});
|
|
716
|
+
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
717
|
+
exports.logger.trace(rows);
|
|
718
|
+
}
|
|
719
|
+
if (rows)
|
|
720
|
+
resolve(rows.map((i) => Object.values(i)[0]));
|
|
721
|
+
resolve([]);
|
|
722
|
+
}
|
|
723
|
+
catch (error) {
|
|
724
|
+
exports.logger.error(`
|
|
725
|
+
error: ${error},
|
|
726
|
+
sql: ${sql},
|
|
727
|
+
params: ${params}
|
|
728
|
+
`);
|
|
729
|
+
reject(error);
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
query(sync, sql, params) {
|
|
734
|
+
exports.logger.debug(sql, params ?? '');
|
|
735
|
+
if (!sql) {
|
|
736
|
+
return [];
|
|
737
|
+
}
|
|
738
|
+
;
|
|
739
|
+
if (sync === SyncMode.Sync) {
|
|
740
|
+
exports.logger.warn('Postgresql not suppouted sync mode');
|
|
741
|
+
return [];
|
|
742
|
+
}
|
|
743
|
+
;
|
|
744
|
+
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
745
|
+
exports.logger.trace(sql, params);
|
|
746
|
+
}
|
|
747
|
+
return new Promise(async (resolve, reject) => {
|
|
748
|
+
try {
|
|
749
|
+
let index = 1;
|
|
750
|
+
const { rows } = await this[_daoConnection].query({
|
|
751
|
+
text: sql.replace(/\?/g, () => `$${index++}`),
|
|
752
|
+
values: params
|
|
753
|
+
});
|
|
754
|
+
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
755
|
+
exports.logger.trace(rows);
|
|
756
|
+
}
|
|
757
|
+
resolve(rows);
|
|
758
|
+
}
|
|
759
|
+
catch (error) {
|
|
760
|
+
exports.logger.error(`
|
|
761
|
+
error: ${error},
|
|
762
|
+
sql: ${sql},
|
|
763
|
+
params: ${params}
|
|
764
|
+
`);
|
|
765
|
+
reject(error);
|
|
766
|
+
}
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
realse(sync) {
|
|
770
|
+
if (sync === SyncMode.Sync) {
|
|
771
|
+
try {
|
|
772
|
+
this[_daoConnection]?.release();
|
|
773
|
+
}
|
|
774
|
+
catch (error) {
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
_c = _inTransaction;
|
|
781
|
+
class Postgresql {
|
|
782
|
+
constructor(pool) {
|
|
783
|
+
this[_daoDB] = pool;
|
|
784
|
+
}
|
|
785
|
+
async keepAlive() {
|
|
786
|
+
const connection = await this[_daoDB].connect();
|
|
787
|
+
connection.query('SELECT 1');
|
|
788
|
+
setTimeout(async () => await this.keepAlive(), 60000);
|
|
789
|
+
}
|
|
790
|
+
createConnection(sync) {
|
|
791
|
+
if (sync === SyncMode.Sync) {
|
|
792
|
+
exports.logger.error('Postgresql not suppouted sync mode');
|
|
793
|
+
return null;
|
|
794
|
+
}
|
|
795
|
+
;
|
|
796
|
+
return new Promise(async (resolve, reject) => {
|
|
797
|
+
try {
|
|
798
|
+
const connection = await this[_daoDB].connect();
|
|
799
|
+
exports.logger.debug('create new connection!');
|
|
800
|
+
resolve(new PostgresqlConnection(connection));
|
|
801
|
+
}
|
|
802
|
+
catch (error) {
|
|
803
|
+
reject(error);
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
transaction(sync, fn, conn) {
|
|
808
|
+
if (sync === SyncMode.Sync) {
|
|
809
|
+
exports.logger.warn('Postgresql not suppouted sync mode');
|
|
810
|
+
return null;
|
|
811
|
+
}
|
|
812
|
+
;
|
|
813
|
+
return new Promise(async (resolve, reject) => {
|
|
814
|
+
let needCommit = false;
|
|
815
|
+
let newConn = false;
|
|
816
|
+
if (!conn) {
|
|
817
|
+
conn = await this.createConnection(SyncMode.Async) ?? undefined;
|
|
818
|
+
newConn = true;
|
|
819
|
+
}
|
|
820
|
+
if (conn?.[_inTransaction] !== true) {
|
|
821
|
+
needCommit = true;
|
|
822
|
+
exports.logger.debug('beginTransaction begin!');
|
|
823
|
+
await conn[_daoConnection].query('BEGIN');
|
|
824
|
+
exports.logger.debug('beginTransaction end!');
|
|
825
|
+
}
|
|
826
|
+
conn[_inTransaction] = true;
|
|
827
|
+
try {
|
|
828
|
+
const result = await fn(conn);
|
|
829
|
+
if (needCommit === true) {
|
|
830
|
+
exports.logger.debug('commit begin!');
|
|
831
|
+
await conn[_daoConnection].query('COMMIT');
|
|
832
|
+
conn[_inTransaction] = false;
|
|
833
|
+
exports.logger.debug('commit end!');
|
|
834
|
+
}
|
|
835
|
+
resolve(result);
|
|
836
|
+
}
|
|
837
|
+
catch (error) {
|
|
838
|
+
exports.logger.debug('rollback begin!');
|
|
839
|
+
await conn[_daoConnection].query('ROLLBACK');
|
|
840
|
+
exports.logger.debug('rollback end!');
|
|
841
|
+
conn[_inTransaction] = false;
|
|
842
|
+
exports.logger.error(error);
|
|
843
|
+
reject(error);
|
|
844
|
+
}
|
|
845
|
+
finally {
|
|
846
|
+
try {
|
|
847
|
+
if (needCommit === true) {
|
|
848
|
+
conn[_inTransaction] = false;
|
|
849
|
+
}
|
|
850
|
+
if (newConn === true) {
|
|
851
|
+
exports.logger.debug('release begin!');
|
|
852
|
+
conn[_daoConnection].release();
|
|
853
|
+
exports.logger.debug('release end!');
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
catch (error) {
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
close(sync) {
|
|
862
|
+
if (sync === SyncMode.Sync) {
|
|
863
|
+
this[_daoDB]?.end();
|
|
864
|
+
}
|
|
865
|
+
;
|
|
866
|
+
}
|
|
867
|
+
backup(sync, name) {
|
|
868
|
+
}
|
|
869
|
+
remove(sync) {
|
|
870
|
+
}
|
|
871
|
+
restore(sync, name) {
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
exports.Postgresql = Postgresql;
|
|
875
|
+
class SqliteConnection {
|
|
876
|
+
constructor(conn) {
|
|
877
|
+
this[_d] = false;
|
|
878
|
+
this[_daoConnection] = conn;
|
|
879
|
+
}
|
|
586
880
|
execute(sync, sql, params) {
|
|
587
881
|
try {
|
|
588
882
|
exports.logger.debug(sql, params ?? '');
|
|
@@ -596,7 +890,7 @@ class SqliteConnection {
|
|
|
596
890
|
}
|
|
597
891
|
;
|
|
598
892
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
599
|
-
exports.logger.trace(
|
|
893
|
+
exports.logger.trace(sql, params);
|
|
600
894
|
}
|
|
601
895
|
const result = this[_daoConnection].prepare(sql).run(params ?? {});
|
|
602
896
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
@@ -627,7 +921,7 @@ class SqliteConnection {
|
|
|
627
921
|
}
|
|
628
922
|
;
|
|
629
923
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
630
|
-
exports.logger.trace(
|
|
924
|
+
exports.logger.trace(sql, params);
|
|
631
925
|
}
|
|
632
926
|
return this[_daoConnection].prepare(sql).pluck().get(params ?? {});
|
|
633
927
|
}
|
|
@@ -652,7 +946,7 @@ class SqliteConnection {
|
|
|
652
946
|
}
|
|
653
947
|
;
|
|
654
948
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
655
|
-
exports.logger.trace(
|
|
949
|
+
exports.logger.trace(sql, params);
|
|
656
950
|
}
|
|
657
951
|
return this[_daoConnection].prepare(sql).get(params ?? {});
|
|
658
952
|
}
|
|
@@ -678,7 +972,7 @@ class SqliteConnection {
|
|
|
678
972
|
}
|
|
679
973
|
;
|
|
680
974
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
681
|
-
exports.logger.trace(
|
|
975
|
+
exports.logger.trace(sql, params);
|
|
682
976
|
}
|
|
683
977
|
return this[_daoConnection].prepare(sql).raw().all(params ?? {});
|
|
684
978
|
}
|
|
@@ -704,7 +998,7 @@ class SqliteConnection {
|
|
|
704
998
|
}
|
|
705
999
|
;
|
|
706
1000
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
707
|
-
exports.logger.trace(
|
|
1001
|
+
exports.logger.trace(sql, params);
|
|
708
1002
|
}
|
|
709
1003
|
return this[_daoConnection].prepare(sql).all(params ?? {});
|
|
710
1004
|
}
|
|
@@ -720,7 +1014,7 @@ class SqliteConnection {
|
|
|
720
1014
|
realse(sync) {
|
|
721
1015
|
}
|
|
722
1016
|
}
|
|
723
|
-
|
|
1017
|
+
_d = _inTransaction;
|
|
724
1018
|
class Sqlite {
|
|
725
1019
|
constructor(db) {
|
|
726
1020
|
this[_daoDB] = db;
|
|
@@ -786,7 +1080,7 @@ class Sqlite {
|
|
|
786
1080
|
exports.Sqlite = Sqlite;
|
|
787
1081
|
class SqliteRemoteConnection {
|
|
788
1082
|
constructor(conn, name) {
|
|
789
|
-
this[
|
|
1083
|
+
this[_e] = false;
|
|
790
1084
|
this[_daoConnection] = conn;
|
|
791
1085
|
this[_sqliteRemoteName] = name;
|
|
792
1086
|
}
|
|
@@ -802,7 +1096,7 @@ class SqliteRemoteConnection {
|
|
|
802
1096
|
}
|
|
803
1097
|
;
|
|
804
1098
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
805
|
-
exports.logger.trace(
|
|
1099
|
+
exports.logger.trace(sql, params);
|
|
806
1100
|
}
|
|
807
1101
|
return new Promise(async (resolve, reject) => {
|
|
808
1102
|
try {
|
|
@@ -832,7 +1126,7 @@ class SqliteRemoteConnection {
|
|
|
832
1126
|
}
|
|
833
1127
|
;
|
|
834
1128
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
835
|
-
exports.logger.trace(
|
|
1129
|
+
exports.logger.trace(sql, params);
|
|
836
1130
|
}
|
|
837
1131
|
return new Promise(async (resolve, reject) => {
|
|
838
1132
|
try {
|
|
@@ -862,7 +1156,7 @@ class SqliteRemoteConnection {
|
|
|
862
1156
|
}
|
|
863
1157
|
;
|
|
864
1158
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
865
|
-
exports.logger.trace(
|
|
1159
|
+
exports.logger.trace(sql, params);
|
|
866
1160
|
}
|
|
867
1161
|
return new Promise(async (resolve, reject) => {
|
|
868
1162
|
try {
|
|
@@ -892,7 +1186,7 @@ class SqliteRemoteConnection {
|
|
|
892
1186
|
}
|
|
893
1187
|
;
|
|
894
1188
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
895
|
-
exports.logger.trace(
|
|
1189
|
+
exports.logger.trace(sql, params);
|
|
896
1190
|
}
|
|
897
1191
|
return new Promise(async (resolve, reject) => {
|
|
898
1192
|
try {
|
|
@@ -922,7 +1216,7 @@ class SqliteRemoteConnection {
|
|
|
922
1216
|
}
|
|
923
1217
|
;
|
|
924
1218
|
if (globalThis[exports._GlobalSqlOption].log === 'trace') {
|
|
925
|
-
exports.logger.trace(
|
|
1219
|
+
exports.logger.trace(sql, params);
|
|
926
1220
|
}
|
|
927
1221
|
return new Promise(async (resolve, reject) => {
|
|
928
1222
|
try {
|
|
@@ -944,7 +1238,7 @@ class SqliteRemoteConnection {
|
|
|
944
1238
|
}
|
|
945
1239
|
}
|
|
946
1240
|
exports.SqliteRemoteConnection = SqliteRemoteConnection;
|
|
947
|
-
|
|
1241
|
+
_e = _inTransaction;
|
|
948
1242
|
class SqliteRemote {
|
|
949
1243
|
constructor(db, name) {
|
|
950
1244
|
this[_daoDB] = db;
|
|
@@ -1303,10 +1597,10 @@ function replaceCdata(rawText) {
|
|
|
1303
1597
|
return rawText;
|
|
1304
1598
|
}
|
|
1305
1599
|
function _flatData(result, i, length, keys, V) {
|
|
1306
|
-
var
|
|
1600
|
+
var _f;
|
|
1307
1601
|
const key = keys[i];
|
|
1308
1602
|
if (i < length) {
|
|
1309
|
-
result[
|
|
1603
|
+
result[_f = key] ?? (result[_f] = {});
|
|
1310
1604
|
i++;
|
|
1311
1605
|
_flatData(result[key], i, length, keys, V);
|
|
1312
1606
|
}
|
|
@@ -1540,15 +1834,16 @@ function P(skipConn = false) {
|
|
|
1540
1834
|
const option = args[0] = Object.assign({}, globalThis[exports._GlobalSqlOption], this[_SqlOption], args[0]);
|
|
1541
1835
|
option.sync ?? (option.sync = SyncMode.Async);
|
|
1542
1836
|
option.tableName = option?.tableName ?? this[_tableName];
|
|
1543
|
-
|
|
1544
|
-
option.
|
|
1545
|
-
|
|
1837
|
+
option.dbName = option?.dbName ?? this[_daoDBName] ?? exports._primaryDB;
|
|
1838
|
+
option.dbType = this[_dbType] ?? globalThis[exports._GlobalSqlOption].dbType ?? DBType.Mysql;
|
|
1839
|
+
option.dao = globalThis[exports._dao][option.dbType][option.dbName];
|
|
1840
|
+
if (option.dbType === DBType.Sqlite) {
|
|
1546
1841
|
if (!option.dao) {
|
|
1547
|
-
const db = new Sqlite(new globalThis[exports._GlobalSqlOption].BetterSqlite3(dbName, { fileMustExist: false }));
|
|
1548
|
-
if (globalThis[exports._dao][
|
|
1549
|
-
globalThis[exports._dao][
|
|
1842
|
+
const db = new Sqlite(new globalThis[exports._GlobalSqlOption].BetterSqlite3(option.dbName, { fileMustExist: false }));
|
|
1843
|
+
if (globalThis[exports._dao][option.dbType][exports._primaryDB] === undefined) {
|
|
1844
|
+
globalThis[exports._dao][option.dbType][exports._primaryDB] = db;
|
|
1550
1845
|
}
|
|
1551
|
-
globalThis[exports._dao][
|
|
1846
|
+
globalThis[exports._dao][option.dbType][option.dbName] = db;
|
|
1552
1847
|
option.dao = db;
|
|
1553
1848
|
}
|
|
1554
1849
|
error_1.Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
|
|
@@ -1582,14 +1877,14 @@ function P(skipConn = false) {
|
|
|
1582
1877
|
}
|
|
1583
1878
|
}
|
|
1584
1879
|
}
|
|
1585
|
-
else if (
|
|
1880
|
+
else if (option.dbType === DBType.SqliteRemote) {
|
|
1586
1881
|
if (!option.dao) {
|
|
1587
|
-
globalThis[exports._GlobalSqlOption].SqliteRemote.service.initDB(dbName);
|
|
1588
|
-
const db = new SqliteRemote(globalThis[exports._GlobalSqlOption].SqliteRemote.service, dbName);
|
|
1589
|
-
if (globalThis[exports._dao][
|
|
1590
|
-
globalThis[exports._dao][
|
|
1882
|
+
globalThis[exports._GlobalSqlOption].SqliteRemote.service.initDB(option.dbName);
|
|
1883
|
+
const db = new SqliteRemote(globalThis[exports._GlobalSqlOption].SqliteRemote.service, option.dbName);
|
|
1884
|
+
if (globalThis[exports._dao][option.dbType][exports._primaryDB] === undefined) {
|
|
1885
|
+
globalThis[exports._dao][option.dbType][exports._primaryDB] = db;
|
|
1591
1886
|
}
|
|
1592
|
-
globalThis[exports._dao][
|
|
1887
|
+
globalThis[exports._dao][option.dbType][option.dbName] = db;
|
|
1593
1888
|
option.dao = db;
|
|
1594
1889
|
}
|
|
1595
1890
|
error_1.Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
|
|
@@ -1621,8 +1916,38 @@ function P(skipConn = false) {
|
|
|
1621
1916
|
}
|
|
1622
1917
|
});
|
|
1623
1918
|
}
|
|
1624
|
-
else if (
|
|
1625
|
-
error_1.Throw.if(!option.dao, `not found db:${String(dbName)}(${
|
|
1919
|
+
else if (option.dbType === DBType.Mysql) {
|
|
1920
|
+
error_1.Throw.if(!option.dao, `not found db:${String(option.dbName)}(${option.dbType})`);
|
|
1921
|
+
return new Promise(async (resolve, reject) => {
|
|
1922
|
+
try {
|
|
1923
|
+
// 连接共享
|
|
1924
|
+
if (skipConn === false && !option.conn) {
|
|
1925
|
+
(option).conn = await option.dao.createConnection(SyncMode.Async);
|
|
1926
|
+
}
|
|
1927
|
+
else {
|
|
1928
|
+
needRealseConn = false;
|
|
1929
|
+
}
|
|
1930
|
+
const result = await fn.call(this, ...args);
|
|
1931
|
+
exports.logger.info(`${propertyKey}:${option.tableName}:use ${+new Date() - startTime}ms`);
|
|
1932
|
+
resolve(result);
|
|
1933
|
+
}
|
|
1934
|
+
catch (error) {
|
|
1935
|
+
console.error(`service ${propertyKey} have an error:${error}, it's argumens: ${JSON.stringify(args.filter(i => typeof i !== 'object' || (typeof i === 'object' && !i.insert)))}`);
|
|
1936
|
+
reject(error);
|
|
1937
|
+
}
|
|
1938
|
+
finally {
|
|
1939
|
+
if (needRealseConn && option && option.conn) {
|
|
1940
|
+
try {
|
|
1941
|
+
option.conn.realse(SyncMode.Sync);
|
|
1942
|
+
}
|
|
1943
|
+
catch (error) {
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1949
|
+
else if (option.dbType === DBType.Postgresql) {
|
|
1950
|
+
error_1.Throw.if(!option.dao, `not found db:${String(option.dbName)}(${option.dbType})`);
|
|
1626
1951
|
return new Promise(async (resolve, reject) => {
|
|
1627
1952
|
try {
|
|
1628
1953
|
// 连接共享
|
|
@@ -1694,204 +2019,182 @@ const FieldFilter = (K, V, def, uuidColumn, option) => {
|
|
|
1694
2019
|
return [ret, V];
|
|
1695
2020
|
};
|
|
1696
2021
|
const MYSQLCHARSET = `CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci`;
|
|
2022
|
+
const POSTGRESCHARSET = `COLLATE "pg_catalog"."default"`;
|
|
1697
2023
|
const Field = (config) => {
|
|
1698
2024
|
config.type ?? (config.type = SqlType.varchar);
|
|
1699
2025
|
return (object, propertyName) => {
|
|
1700
2026
|
const field = config;
|
|
1701
2027
|
field.P = propertyName;
|
|
1702
2028
|
field.C = () => (0, object_1.P2C)(propertyName, globalThis[exports._Hump]);
|
|
1703
|
-
field.C2 = () =>
|
|
1704
|
-
field.C3 = () => `${
|
|
2029
|
+
field.C2 = () => (0, object_1.P2C)(propertyName, globalThis[exports._Hump]);
|
|
2030
|
+
field.C3 = () => `${(0, object_1.P2C)(propertyName, globalThis[exports._Hump])} ${propertyName}`;
|
|
1705
2031
|
const hasDef = field.hasOwnProperty('def') === true;
|
|
1706
2032
|
switch (field.type) {
|
|
1707
2033
|
case SqlType.tinyint: {
|
|
1708
|
-
field[DBType.Mysql] = () => `${field.C2()} tinyint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT
|
|
2034
|
+
field[DBType.Mysql] = () => `${field.C2()} tinyint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT ${field.def}` : ''}`;
|
|
2035
|
+
field[DBType.Postgresql] = () => `${field.C2()} int2 ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT ${field.def}` : ''}`;
|
|
1709
2036
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1710
2037
|
break;
|
|
1711
2038
|
}
|
|
1712
2039
|
case SqlType.smallint: {
|
|
1713
|
-
field[DBType.Mysql] = () => `${field.C2()} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT
|
|
2040
|
+
field[DBType.Mysql] = () => `${field.C2()} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT ${field.def}` : ''}`;
|
|
2041
|
+
field[DBType.Postgresql] = () => `${field.C2()} int2 ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT ${field.def}` : ''}`;
|
|
1714
2042
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1715
2043
|
break;
|
|
1716
2044
|
}
|
|
1717
2045
|
case SqlType.mediumint: {
|
|
1718
|
-
field[DBType.Mysql] = () => `${field.C2()} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT
|
|
2046
|
+
field[DBType.Mysql] = () => `${field.C2()} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT ${field.def}` : ''}`;
|
|
2047
|
+
field[DBType.Postgresql] = () => `${field.C2()} int4 ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT ${field.def}` : ''}`;
|
|
1719
2048
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1720
2049
|
break;
|
|
1721
2050
|
}
|
|
1722
2051
|
case SqlType.int: {
|
|
1723
|
-
field[DBType.Mysql] = () => `${field.C2()} int ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT
|
|
2052
|
+
field[DBType.Mysql] = () => `${field.C2()} int ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT ${field.def}` : ''}`;
|
|
2053
|
+
field[DBType.Postgresql] = () => `${field.C2()} int4 ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT ${field.def}` : ''}`;
|
|
1724
2054
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1725
2055
|
break;
|
|
1726
2056
|
}
|
|
1727
2057
|
case SqlType.bigint: {
|
|
1728
2058
|
field[DBType.Mysql] = () => `${field.C2()} bigint ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2059
|
+
field[DBType.Postgresql] = () => `${field.C2()} int8 ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1729
2060
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1730
2061
|
field.Data2SQL = (data) => BigInt(data ?? 0);
|
|
1731
2062
|
break;
|
|
1732
2063
|
}
|
|
1733
2064
|
case SqlType.float: {
|
|
1734
|
-
field[DBType.Mysql] = () => `${field.C2()}
|
|
2065
|
+
field[DBType.Mysql] = () => `${field.C2()} float4(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''} `;
|
|
2066
|
+
field[DBType.Postgresql] = () => `${field.C2()} float4(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''} `;
|
|
1735
2067
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} real`;
|
|
1736
2068
|
break;
|
|
1737
2069
|
}
|
|
1738
2070
|
case SqlType.double: {
|
|
1739
2071
|
field[DBType.Mysql] = () => `${field.C2()} double(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''} `;
|
|
2072
|
+
field[DBType.Postgresql] = () => `${field.C2()} float8(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''} `;
|
|
1740
2073
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} real`;
|
|
1741
2074
|
break;
|
|
1742
2075
|
}
|
|
1743
2076
|
case SqlType.decimal: {
|
|
1744
2077
|
field[DBType.Mysql] = () => `${field.C2()} decimal(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''} `;
|
|
2078
|
+
field[DBType.Postgresql] = () => `${field.C2()} numeric(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''} `;
|
|
1745
2079
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} real`;
|
|
1746
2080
|
break;
|
|
1747
2081
|
}
|
|
1748
2082
|
case SqlType.longtext: {
|
|
1749
2083
|
field[DBType.Mysql] = () => `${field.C2()} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2084
|
+
field[DBType.Postgresql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1750
2085
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1751
2086
|
break;
|
|
1752
2087
|
}
|
|
1753
2088
|
case SqlType.mediumtext: {
|
|
1754
2089
|
field[DBType.Mysql] = () => `${field.C2()} mediumtext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2090
|
+
field[DBType.Postgresql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1755
2091
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1756
2092
|
break;
|
|
1757
2093
|
}
|
|
1758
2094
|
case SqlType.text: {
|
|
1759
2095
|
field[DBType.Mysql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2096
|
+
field[DBType.Postgresql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1760
2097
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1761
2098
|
break;
|
|
1762
2099
|
}
|
|
1763
2100
|
case SqlType.date: {
|
|
1764
2101
|
field[DBType.Mysql] = () => `${field.C2()} date ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2102
|
+
field[DBType.Postgresql] = () => `${field.C2()} date ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1765
2103
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1766
2104
|
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1767
2105
|
break;
|
|
1768
2106
|
}
|
|
1769
2107
|
case SqlType.time: {
|
|
1770
2108
|
field[DBType.Mysql] = () => `${field.C2()} time ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2109
|
+
field[DBType.Postgresql] = () => `${field.C2()} time ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1771
2110
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1772
2111
|
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1773
2112
|
break;
|
|
1774
2113
|
}
|
|
1775
2114
|
case SqlType.year: {
|
|
1776
2115
|
field[DBType.Mysql] = () => `${field.C2()} year ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2116
|
+
field[DBType.Postgresql] = () => `${field.C2()} int4 ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1777
2117
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1778
2118
|
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1779
2119
|
break;
|
|
1780
2120
|
}
|
|
1781
2121
|
case SqlType.datetime: {
|
|
1782
2122
|
field[DBType.Mysql] = () => `${field.C2()} datetime ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2123
|
+
field[DBType.Postgresql] = () => `${field.C2()} timestamp ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1783
2124
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1784
2125
|
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1785
2126
|
break;
|
|
1786
2127
|
}
|
|
1787
2128
|
case SqlType.timestamp: {
|
|
1788
2129
|
field[DBType.Mysql] = () => `${field.C2()} timestamp ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2130
|
+
field[DBType.Postgresql] = () => `${field.C2()} timestamp ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1789
2131
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1790
2132
|
field.Data2SQL = (data) => typeof data === 'string' ? +new Date(data) : data;
|
|
1791
2133
|
break;
|
|
1792
2134
|
}
|
|
1793
2135
|
case SqlType.char: {
|
|
1794
2136
|
field[DBType.Mysql] = () => `${field.C2()} char(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2137
|
+
field[DBType.Postgresql] = () => `${field.C2()} char(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1795
2138
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1796
2139
|
break;
|
|
1797
2140
|
}
|
|
1798
2141
|
case SqlType.varchar: {
|
|
1799
2142
|
field[DBType.Mysql] = () => `${field.C2()} varchar(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2143
|
+
field[DBType.Postgresql] = () => `${field.C2()} varchar(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1800
2144
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1801
2145
|
break;
|
|
1802
2146
|
}
|
|
1803
2147
|
case SqlType.tinyblob: {
|
|
1804
2148
|
field[DBType.Mysql] = () => `${field.C2()} tinyblob ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2149
|
+
field[DBType.Postgresql] = () => `${field.C2()} bytea ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1805
2150
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1806
2151
|
break;
|
|
1807
2152
|
}
|
|
1808
2153
|
case SqlType.tinytext: {
|
|
1809
2154
|
field[DBType.Mysql] = () => `${field.C2()} tinytext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2155
|
+
field[DBType.Postgresql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1810
2156
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1811
2157
|
break;
|
|
1812
2158
|
}
|
|
1813
2159
|
case SqlType.blob: {
|
|
1814
2160
|
field[DBType.Mysql] = () => `${field.C2()} binary ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2161
|
+
field[DBType.Postgresql] = () => `${field.C2()} bytea ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1815
2162
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1816
2163
|
break;
|
|
1817
2164
|
}
|
|
1818
2165
|
case SqlType.text: {
|
|
1819
2166
|
field[DBType.Mysql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2167
|
+
field[DBType.Postgresql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1820
2168
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1821
2169
|
break;
|
|
1822
2170
|
}
|
|
1823
2171
|
case SqlType.mediumblob: {
|
|
1824
2172
|
field[DBType.Mysql] = () => `${field.C2()} mediumblob ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2173
|
+
field[DBType.Postgresql] = () => `${field.C2()} bytea ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1825
2174
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1826
2175
|
break;
|
|
1827
2176
|
}
|
|
1828
2177
|
case SqlType.mediumtext: {
|
|
1829
2178
|
field[DBType.Mysql] = () => `${field.C2()} mediumtext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2179
|
+
field[DBType.Postgresql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1830
2180
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1831
2181
|
break;
|
|
1832
2182
|
}
|
|
1833
2183
|
case SqlType.longblob: {
|
|
1834
2184
|
field[DBType.Mysql] = () => `${field.C2()} longblob ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2185
|
+
field[DBType.Postgresql] = () => `${field.C2()} bytea ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1835
2186
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1836
2187
|
break;
|
|
1837
2188
|
}
|
|
1838
2189
|
case SqlType.longtext: {
|
|
1839
2190
|
field[DBType.Mysql] = () => `${field.C2()} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1840
|
-
field[DBType.
|
|
1841
|
-
break;
|
|
1842
|
-
}
|
|
1843
|
-
case SqlType.set: {
|
|
1844
|
-
field[DBType.Mysql] = () => `${field.C2()} set ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1845
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1846
|
-
break;
|
|
1847
|
-
}
|
|
1848
|
-
case SqlType.enum: {
|
|
1849
|
-
field[DBType.Mysql] = () => `${field.C2()} enum ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2191
|
+
field[DBType.Postgresql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1850
2192
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1851
2193
|
break;
|
|
1852
2194
|
}
|
|
1853
2195
|
case SqlType.json: {
|
|
1854
2196
|
field[DBType.Mysql] = () => `${field.C2()} json ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1855
|
-
field[DBType.
|
|
1856
|
-
break;
|
|
1857
|
-
}
|
|
1858
|
-
case SqlType.geometry: {
|
|
1859
|
-
field[DBType.Mysql] = () => `${field.C2()} geometry ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1860
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1861
|
-
break;
|
|
1862
|
-
}
|
|
1863
|
-
case SqlType.point: {
|
|
1864
|
-
field[DBType.Mysql] = () => `${field.C2()} point ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1865
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1866
|
-
break;
|
|
1867
|
-
}
|
|
1868
|
-
case SqlType.linestring: {
|
|
1869
|
-
field[DBType.Mysql] = () => `${field.C2()} linestring ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1870
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1871
|
-
break;
|
|
1872
|
-
}
|
|
1873
|
-
case SqlType.polygon: {
|
|
1874
|
-
field[DBType.Mysql] = () => `${field.C2()} polygon ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1875
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1876
|
-
break;
|
|
1877
|
-
}
|
|
1878
|
-
case SqlType.multipoint: {
|
|
1879
|
-
field[DBType.Mysql] = () => `${field.C2()} multipoint ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1880
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1881
|
-
break;
|
|
1882
|
-
}
|
|
1883
|
-
case SqlType.multilinestring: {
|
|
1884
|
-
field[DBType.Mysql] = () => `${field.C2()} multilinestring ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1885
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1886
|
-
break;
|
|
1887
|
-
}
|
|
1888
|
-
case SqlType.multipolygon: {
|
|
1889
|
-
field[DBType.Mysql] = () => `${field.C2()} multipolygon ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1890
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1891
|
-
break;
|
|
1892
|
-
}
|
|
1893
|
-
case SqlType.geometrycollection: {
|
|
1894
|
-
field[DBType.Mysql] = () => `${field.C2()} geometrycollection ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
2197
|
+
field[DBType.Postgresql] = () => `${field.C2()} jsonb ${config.notNull === true ? 'NOT NULL' : ''} ${POSTGRESCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1895
2198
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1896
2199
|
break;
|
|
1897
2200
|
}
|
|
@@ -1946,11 +2249,12 @@ exports.Field = Field;
|
|
|
1946
2249
|
const formatDialects = {
|
|
1947
2250
|
[DBType.Mysql]: sql_formatter_1.mysql,
|
|
1948
2251
|
[DBType.Sqlite]: sql_formatter_1.sqlite,
|
|
1949
|
-
[DBType.SqliteRemote]: sql_formatter_1.sqlite
|
|
2252
|
+
[DBType.SqliteRemote]: sql_formatter_1.sqlite,
|
|
2253
|
+
[DBType.Postgresql]: sql_formatter_1.postgresql,
|
|
1950
2254
|
};
|
|
1951
2255
|
const DB = (config) => {
|
|
1952
2256
|
return function (constructor) {
|
|
1953
|
-
var
|
|
2257
|
+
var _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
|
|
1954
2258
|
const __ids = Reflect.getMetadata(_ids, config.clz.prototype) || new Array;
|
|
1955
2259
|
const __logicIds = Reflect.getMetadata(_logicIds, config.clz.prototype) || new Array;
|
|
1956
2260
|
const __fields = Reflect.getMetadata(_fields, config.clz.prototype);
|
|
@@ -1960,21 +2264,19 @@ const DB = (config) => {
|
|
|
1960
2264
|
const __deleteState = Reflect.getMetadata(_deleteState, config.clz.prototype);
|
|
1961
2265
|
const __index = Reflect.getMetadata(_index, config.clz.prototype);
|
|
1962
2266
|
const __def = Reflect.getMetadata(_def, config.clz.prototype);
|
|
1963
|
-
const __dbType = config.dbType
|
|
1964
|
-
const __formatDialect = formatDialects[__dbType];
|
|
2267
|
+
const __dbType = config.dbType;
|
|
1965
2268
|
const className = config.tableName?.replace(/_(\w)/g, (a, b) => b.toUpperCase());
|
|
1966
2269
|
const ClassName = className?.replace(/\w/, (v) => v.toUpperCase());
|
|
1967
2270
|
const vueName = config.tableName?.replace(/_/g, '-');
|
|
1968
2271
|
return _0 = class extends constructor {
|
|
1969
2272
|
constructor() {
|
|
1970
2273
|
super(...arguments);
|
|
1971
|
-
this[
|
|
1972
|
-
this[
|
|
1973
|
-
this[
|
|
1974
|
-
this[
|
|
1975
|
-
this[
|
|
1976
|
-
this[
|
|
1977
|
-
this[_l] = __formatDialect;
|
|
2274
|
+
this[_f] = config.tableName;
|
|
2275
|
+
this[_g] = className;
|
|
2276
|
+
this[_h] = ClassName;
|
|
2277
|
+
this[_j] = vueName;
|
|
2278
|
+
this[_k] = config.dbName;
|
|
2279
|
+
this[_l] = __dbType;
|
|
1978
2280
|
this[_m] = config.sqliteVersion;
|
|
1979
2281
|
this[_o] = Object.assign({}, exports._defOption, config);
|
|
1980
2282
|
this[_p] = __ids;
|
|
@@ -2009,13 +2311,12 @@ const DB = (config) => {
|
|
|
2009
2311
|
};
|
|
2010
2312
|
}
|
|
2011
2313
|
},
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
_l = _formatDialect,
|
|
2314
|
+
_f = _tableName,
|
|
2315
|
+
_g = _className,
|
|
2316
|
+
_h = _ClassName,
|
|
2317
|
+
_j = _vueName,
|
|
2318
|
+
_k = _daoDBName,
|
|
2319
|
+
_l = _dbType,
|
|
2019
2320
|
_m = _sqlite_version,
|
|
2020
2321
|
_o = _SqlOption,
|
|
2021
2322
|
_p = _ids,
|
|
@@ -2078,7 +2379,7 @@ function DeclareService(clz, config) {
|
|
|
2078
2379
|
class SqlService {
|
|
2079
2380
|
_insert(datas, option) {
|
|
2080
2381
|
const sqls = [];
|
|
2081
|
-
const tableName =
|
|
2382
|
+
const tableName = option.tableName;
|
|
2082
2383
|
switch (option?.mode) {
|
|
2083
2384
|
case InsertMode.InsertIfNotExists: {
|
|
2084
2385
|
const conditions = option.existConditionOtherThanIds || this[_ids];
|
|
@@ -2101,10 +2402,28 @@ class SqlService {
|
|
|
2101
2402
|
if (V === null) {
|
|
2102
2403
|
const field = this[_fields][column];
|
|
2103
2404
|
if (field?.uuid) {
|
|
2104
|
-
|
|
2405
|
+
switch (option.dbType) {
|
|
2406
|
+
case DBType.Postgresql:
|
|
2407
|
+
questMark.push('gen_random_uuid()');
|
|
2408
|
+
break;
|
|
2409
|
+
default:
|
|
2410
|
+
questMark.push('UUID()');
|
|
2411
|
+
break;
|
|
2412
|
+
}
|
|
2105
2413
|
}
|
|
2106
|
-
else if (field?.uuidShort
|
|
2107
|
-
|
|
2414
|
+
else if (field?.uuidShort) {
|
|
2415
|
+
switch (option.dbType) {
|
|
2416
|
+
case DBType.Postgresql:
|
|
2417
|
+
questMark.push(`encode(uuid_send(gen_random_uuid()::uuid),'base64')`);
|
|
2418
|
+
break;
|
|
2419
|
+
case DBType.Mysql:
|
|
2420
|
+
questMark.push('UUID_SHORT()');
|
|
2421
|
+
break;
|
|
2422
|
+
default:
|
|
2423
|
+
questMark.push('?');
|
|
2424
|
+
params.push(V);
|
|
2425
|
+
break;
|
|
2426
|
+
}
|
|
2108
2427
|
}
|
|
2109
2428
|
else {
|
|
2110
2429
|
questMark.push('?');
|
|
@@ -2129,7 +2448,7 @@ class SqlService {
|
|
|
2129
2448
|
const sql = (0, sql_formatter_1.formatDialect)(`INSERT INTO
|
|
2130
2449
|
${tableName}
|
|
2131
2450
|
(${columnNames})
|
|
2132
|
-
${questMarks.join(' UNION ALL ')};`, { dialect:
|
|
2451
|
+
${questMarks.join(' UNION ALL ')};`, { dialect: formatDialects[option.dbType] });
|
|
2133
2452
|
sqls.push({ sql, params });
|
|
2134
2453
|
break;
|
|
2135
2454
|
}
|
|
@@ -2149,10 +2468,28 @@ class SqlService {
|
|
|
2149
2468
|
if (V === null) {
|
|
2150
2469
|
const field = this[_fields][column];
|
|
2151
2470
|
if (field?.uuid) {
|
|
2152
|
-
|
|
2471
|
+
switch (option.dbType) {
|
|
2472
|
+
case DBType.Postgresql:
|
|
2473
|
+
questMark.push('gen_random_uuid()');
|
|
2474
|
+
break;
|
|
2475
|
+
default:
|
|
2476
|
+
questMark.push('UUID()');
|
|
2477
|
+
break;
|
|
2478
|
+
}
|
|
2153
2479
|
}
|
|
2154
|
-
else if (field?.uuidShort
|
|
2155
|
-
|
|
2480
|
+
else if (field?.uuidShort) {
|
|
2481
|
+
switch (option.dbType) {
|
|
2482
|
+
case DBType.Postgresql:
|
|
2483
|
+
questMark.push(`encode(uuid_send(gen_random_uuid()::uuid),'base64')`);
|
|
2484
|
+
break;
|
|
2485
|
+
case DBType.Mysql:
|
|
2486
|
+
questMark.push('UUID_SHORT()');
|
|
2487
|
+
break;
|
|
2488
|
+
default:
|
|
2489
|
+
questMark.push('?');
|
|
2490
|
+
params.push(V);
|
|
2491
|
+
break;
|
|
2492
|
+
}
|
|
2156
2493
|
}
|
|
2157
2494
|
else {
|
|
2158
2495
|
questMark.push('?');
|
|
@@ -2168,11 +2505,11 @@ class SqlService {
|
|
|
2168
2505
|
});
|
|
2169
2506
|
const columnNames = (0, iterare_1.default)(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2170
2507
|
const sql = (0, sql_formatter_1.formatDialect)(`
|
|
2171
|
-
${
|
|
2508
|
+
${option.dbType === DBType.Mysql ? '' : 'INSERT OR'} REPLACE INTO
|
|
2172
2509
|
${tableName}
|
|
2173
2510
|
(${columnNames})
|
|
2174
2511
|
VALUES ${questMarks};
|
|
2175
|
-
`, { dialect:
|
|
2512
|
+
`, { dialect: formatDialects[option.dbType] });
|
|
2176
2513
|
sqls.push({ sql, params });
|
|
2177
2514
|
break;
|
|
2178
2515
|
}
|
|
@@ -2192,10 +2529,28 @@ class SqlService {
|
|
|
2192
2529
|
if (V === null) {
|
|
2193
2530
|
const field = this[_fields][column];
|
|
2194
2531
|
if (field?.uuid) {
|
|
2195
|
-
|
|
2532
|
+
switch (option.dbType) {
|
|
2533
|
+
case DBType.Postgresql:
|
|
2534
|
+
questMark.push('gen_random_uuid()');
|
|
2535
|
+
break;
|
|
2536
|
+
default:
|
|
2537
|
+
questMark.push('UUID()');
|
|
2538
|
+
break;
|
|
2539
|
+
}
|
|
2196
2540
|
}
|
|
2197
|
-
else if (field?.uuidShort
|
|
2198
|
-
|
|
2541
|
+
else if (field?.uuidShort) {
|
|
2542
|
+
switch (option.dbType) {
|
|
2543
|
+
case DBType.Postgresql:
|
|
2544
|
+
questMark.push(`encode(uuid_send(gen_random_uuid()::uuid),'base64')`);
|
|
2545
|
+
break;
|
|
2546
|
+
case DBType.Mysql:
|
|
2547
|
+
questMark.push('UUID_SHORT()');
|
|
2548
|
+
break;
|
|
2549
|
+
default:
|
|
2550
|
+
questMark.push('?');
|
|
2551
|
+
params.push(V);
|
|
2552
|
+
break;
|
|
2553
|
+
}
|
|
2199
2554
|
}
|
|
2200
2555
|
else {
|
|
2201
2556
|
questMark.push('?');
|
|
@@ -2215,13 +2570,13 @@ class SqlService {
|
|
|
2215
2570
|
${tableName}
|
|
2216
2571
|
(${columnNames})
|
|
2217
2572
|
VALUES ${questMarks};
|
|
2218
|
-
`, { dialect:
|
|
2573
|
+
`, { dialect: formatDialects[option.dbType] });
|
|
2219
2574
|
sqls.push({ sql, params });
|
|
2220
2575
|
break;
|
|
2221
2576
|
}
|
|
2222
2577
|
case InsertMode.InsertWithTempTable: {
|
|
2223
2578
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
2224
|
-
const tableTempESC =
|
|
2579
|
+
const tableTempESC = tableTemp;
|
|
2225
2580
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
2226
2581
|
const finalColumns = new Set();
|
|
2227
2582
|
const params = [];
|
|
@@ -2238,10 +2593,28 @@ class SqlService {
|
|
|
2238
2593
|
if (V === null) {
|
|
2239
2594
|
const field = this[_fields][column];
|
|
2240
2595
|
if (field?.uuid) {
|
|
2241
|
-
|
|
2596
|
+
switch (option.dbType) {
|
|
2597
|
+
case DBType.Postgresql:
|
|
2598
|
+
questMark.push('gen_random_uuid()');
|
|
2599
|
+
break;
|
|
2600
|
+
default:
|
|
2601
|
+
questMark.push('UUID()');
|
|
2602
|
+
break;
|
|
2603
|
+
}
|
|
2242
2604
|
}
|
|
2243
|
-
else if (field?.uuidShort
|
|
2244
|
-
|
|
2605
|
+
else if (field?.uuidShort) {
|
|
2606
|
+
switch (option.dbType) {
|
|
2607
|
+
case DBType.Postgresql:
|
|
2608
|
+
questMark.push(`encode(uuid_send(gen_random_uuid()::uuid),'base64')`);
|
|
2609
|
+
break;
|
|
2610
|
+
case DBType.Mysql:
|
|
2611
|
+
questMark.push('UUID_SHORT()');
|
|
2612
|
+
break;
|
|
2613
|
+
default:
|
|
2614
|
+
questMark.push('?');
|
|
2615
|
+
params.push(V);
|
|
2616
|
+
break;
|
|
2617
|
+
}
|
|
2245
2618
|
}
|
|
2246
2619
|
else {
|
|
2247
2620
|
questMark.push('?');
|
|
@@ -2264,11 +2637,11 @@ class SqlService {
|
|
|
2264
2637
|
${tableTemp}
|
|
2265
2638
|
(${columnNames})
|
|
2266
2639
|
VALUES ${questMarks};
|
|
2267
|
-
`, { dialect:
|
|
2640
|
+
`, { dialect: formatDialects[option.dbType] }), params
|
|
2268
2641
|
});
|
|
2269
2642
|
sqls.push({
|
|
2270
|
-
sql: (0, sql_formatter_1.formatDialect)(`INSERT INTO ${
|
|
2271
|
-
SELECT ${columnNames} FROM ${tableTemp};`, { dialect:
|
|
2643
|
+
sql: (0, sql_formatter_1.formatDialect)(`INSERT INTO ${option.tableName} (${columnNames})
|
|
2644
|
+
SELECT ${columnNames} FROM ${tableTemp};`, { dialect: formatDialects[option.dbType] })
|
|
2272
2645
|
});
|
|
2273
2646
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
2274
2647
|
break;
|
|
@@ -2288,7 +2661,7 @@ class SqlService {
|
|
|
2288
2661
|
for (const { sql, params } of sqls) {
|
|
2289
2662
|
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
2290
2663
|
if (dd.insertId) {
|
|
2291
|
-
result += dd.insertId;
|
|
2664
|
+
result += BigInt(dd.insertId);
|
|
2292
2665
|
}
|
|
2293
2666
|
}
|
|
2294
2667
|
return result;
|
|
@@ -2298,7 +2671,7 @@ class SqlService {
|
|
|
2298
2671
|
else
|
|
2299
2672
|
return result[0];
|
|
2300
2673
|
};
|
|
2301
|
-
if (
|
|
2674
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2302
2675
|
return fn();
|
|
2303
2676
|
}
|
|
2304
2677
|
else {
|
|
@@ -2313,30 +2686,19 @@ class SqlService {
|
|
|
2313
2686
|
for (const { sql, params } of sqls) {
|
|
2314
2687
|
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
2315
2688
|
if (dd.insertId) {
|
|
2316
|
-
result += dd.insertId;
|
|
2689
|
+
result += BigInt(dd.insertId);
|
|
2317
2690
|
}
|
|
2318
2691
|
}
|
|
2319
2692
|
return result;
|
|
2320
2693
|
}, { everyLength: option?.every === true ? 1 : option?.maxDeal });
|
|
2321
2694
|
return result;
|
|
2322
2695
|
};
|
|
2323
|
-
if (
|
|
2696
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2324
2697
|
return fn();
|
|
2325
2698
|
}
|
|
2326
2699
|
else {
|
|
2327
2700
|
return option?.dao?.transaction(SyncMode.Async, fn, option?.conn);
|
|
2328
2701
|
}
|
|
2329
|
-
// return new Promise<bigint[]>(async (resolve, reject) => {
|
|
2330
|
-
// try {
|
|
2331
|
-
// if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2332
|
-
// resolve((await fn())!);
|
|
2333
|
-
// } else {
|
|
2334
|
-
// await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())!), option?.conn);
|
|
2335
|
-
// }
|
|
2336
|
-
// } catch (error) {
|
|
2337
|
-
// reject(error);
|
|
2338
|
-
// }
|
|
2339
|
-
// });
|
|
2340
2702
|
}
|
|
2341
2703
|
else {
|
|
2342
2704
|
const fn = async () => {
|
|
@@ -2353,28 +2715,17 @@ class SqlService {
|
|
|
2353
2715
|
}, { everyLength: 1 });
|
|
2354
2716
|
return result[0];
|
|
2355
2717
|
};
|
|
2356
|
-
if (
|
|
2718
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2357
2719
|
return fn();
|
|
2358
2720
|
}
|
|
2359
2721
|
else {
|
|
2360
2722
|
return option?.dao?.transaction(SyncMode.Async, fn, option?.conn);
|
|
2361
2723
|
}
|
|
2362
|
-
// return new Promise<bigint>(async (resolve, reject) => {
|
|
2363
|
-
// try {
|
|
2364
|
-
// if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2365
|
-
// resolve((await fn())!);
|
|
2366
|
-
// } else {
|
|
2367
|
-
// await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())!), option?.conn);
|
|
2368
|
-
// }
|
|
2369
|
-
// } catch (error) {
|
|
2370
|
-
// reject(error);
|
|
2371
|
-
// }
|
|
2372
|
-
// });
|
|
2373
2724
|
}
|
|
2374
2725
|
}
|
|
2375
2726
|
_update(datas, option) {
|
|
2376
2727
|
const sqls = [];
|
|
2377
|
-
const tableName =
|
|
2728
|
+
const tableName = option?.tableName;
|
|
2378
2729
|
const where = `WHEN ${(0, iterare_1.default)(this[_ids]).map(c => `${this[_fields][c]?.C2()} = ?`).join(' AND ')} THEN ?`;
|
|
2379
2730
|
const columnMaps = Object.fromEntries(this[_columnsNoId].map(c => [c, {
|
|
2380
2731
|
where: new Array(),
|
|
@@ -2401,7 +2752,7 @@ class SqlService {
|
|
|
2401
2752
|
params.push(...columnMaps[K].params);
|
|
2402
2753
|
return `${this[_fields][K]?.C2()} = CASE ${columnMaps[K].where.join(' ')} ELSE ${this[_fields][K]?.C2()} END`;
|
|
2403
2754
|
})
|
|
2404
|
-
.join(',')};`, { dialect:
|
|
2755
|
+
.join(',')};`, { dialect: formatDialects[option.dbType] });
|
|
2405
2756
|
sqls.push({ sql, params });
|
|
2406
2757
|
return sqls;
|
|
2407
2758
|
}
|
|
@@ -2423,7 +2774,7 @@ class SqlService {
|
|
|
2423
2774
|
}, { everyLength: option?.maxDeal });
|
|
2424
2775
|
return result.reduce((a, b) => a + b);
|
|
2425
2776
|
};
|
|
2426
|
-
if (
|
|
2777
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2427
2778
|
return fn();
|
|
2428
2779
|
}
|
|
2429
2780
|
else {
|
|
@@ -2445,25 +2796,12 @@ class SqlService {
|
|
|
2445
2796
|
}, { everyLength: option?.maxDeal });
|
|
2446
2797
|
return result.reduce((a, b) => a + b);
|
|
2447
2798
|
};
|
|
2448
|
-
if (
|
|
2799
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2449
2800
|
return fn();
|
|
2450
2801
|
}
|
|
2451
2802
|
else {
|
|
2452
2803
|
return option?.dao?.transaction(SyncMode.Async, fn, option?.conn);
|
|
2453
2804
|
}
|
|
2454
|
-
return new Promise(async (resolve, reject) => {
|
|
2455
|
-
try {
|
|
2456
|
-
if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2457
|
-
resolve((await fn()));
|
|
2458
|
-
}
|
|
2459
|
-
else {
|
|
2460
|
-
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
2461
|
-
}
|
|
2462
|
-
}
|
|
2463
|
-
catch (error) {
|
|
2464
|
-
reject(error);
|
|
2465
|
-
}
|
|
2466
|
-
});
|
|
2467
2805
|
}
|
|
2468
2806
|
}
|
|
2469
2807
|
delete(option) {
|
|
@@ -2474,8 +2812,8 @@ class SqlService {
|
|
|
2474
2812
|
error_1.Throw.if(!!option.id && !!option.where, 'id and where only one can set!');
|
|
2475
2813
|
option.mode ?? (option.mode = DeleteMode.Common);
|
|
2476
2814
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
2477
|
-
const tableTempESC =
|
|
2478
|
-
const tableNameESC =
|
|
2815
|
+
const tableTempESC = tableTemp;
|
|
2816
|
+
const tableNameESC = option?.tableName;
|
|
2479
2817
|
if (option.id) {
|
|
2480
2818
|
const idName = this[_ids][0];
|
|
2481
2819
|
const ids = option.id instanceof Array ? option.id : [option.id];
|
|
@@ -2499,11 +2837,11 @@ class SqlService {
|
|
|
2499
2837
|
sql: (0, sql_formatter_1.formatDialect)(`
|
|
2500
2838
|
UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.C2()} = ?
|
|
2501
2839
|
WHERE ${whereSql};
|
|
2502
|
-
`, { dialect:
|
|
2840
|
+
`, { dialect: formatDialects[option.dbType] }), params
|
|
2503
2841
|
});
|
|
2504
2842
|
}
|
|
2505
2843
|
else {
|
|
2506
|
-
sqls.push({ sql: (0, sql_formatter_1.formatDialect)(`DELETE FROM ${tableNameESC} WHERE ${whereSql};`, { dialect:
|
|
2844
|
+
sqls.push({ sql: (0, sql_formatter_1.formatDialect)(`DELETE FROM ${tableNameESC} WHERE ${whereSql};`, { dialect: formatDialects[option.dbType] }), params });
|
|
2507
2845
|
}
|
|
2508
2846
|
}
|
|
2509
2847
|
else {
|
|
@@ -2511,18 +2849,18 @@ class SqlService {
|
|
|
2511
2849
|
const delWhere = Object.keys(wheres[0]);
|
|
2512
2850
|
const _sqls = this._createTable({ tableName: tableTemp, temp: true, columns: delWhere, data: wheres, index: 'all', id: 'none' });
|
|
2513
2851
|
sqls.push(..._sqls);
|
|
2514
|
-
switch (
|
|
2852
|
+
switch (option.dbType) {
|
|
2515
2853
|
case DBType.Mysql: {
|
|
2516
2854
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2517
2855
|
sqls.push({
|
|
2518
2856
|
sql: (0, sql_formatter_1.formatDialect)(`UPDATE ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')}
|
|
2519
|
-
SET a.${this[_fields][this[_stateFileName]]?.C2()} = ?;`, { dialect:
|
|
2857
|
+
SET a.${this[_fields][this[_stateFileName]]?.C2()} = ?;`, { dialect: formatDialects[option.dbType] }),
|
|
2520
2858
|
params: [this[_deleteState]]
|
|
2521
2859
|
});
|
|
2522
2860
|
}
|
|
2523
2861
|
else {
|
|
2524
2862
|
sqls.push({
|
|
2525
|
-
sql: (0, sql_formatter_1.formatDialect)(`DELETE a.* FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')};`, { dialect:
|
|
2863
|
+
sql: (0, sql_formatter_1.formatDialect)(`DELETE a.* FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')};`, { dialect: formatDialects[option.dbType] })
|
|
2526
2864
|
});
|
|
2527
2865
|
}
|
|
2528
2866
|
break;
|
|
@@ -2533,12 +2871,12 @@ class SqlService {
|
|
|
2533
2871
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2534
2872
|
sqls.push({
|
|
2535
2873
|
sql: (0, sql_formatter_1.formatDialect)(`UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.C2()} = ?
|
|
2536
|
-
WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`, { dialect:
|
|
2874
|
+
WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`, { dialect: formatDialects[option.dbType] }),
|
|
2537
2875
|
params: [this[_deleteState]]
|
|
2538
2876
|
});
|
|
2539
2877
|
}
|
|
2540
2878
|
else {
|
|
2541
|
-
sqls.push({ sql: (0, sql_formatter_1.formatDialect)(`DELETE FROM ${tableNameESC} WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`, { dialect:
|
|
2879
|
+
sqls.push({ sql: (0, sql_formatter_1.formatDialect)(`DELETE FROM ${tableNameESC} WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`, { dialect: formatDialects[option.dbType] }) });
|
|
2542
2880
|
}
|
|
2543
2881
|
break;
|
|
2544
2882
|
}
|
|
@@ -2554,7 +2892,7 @@ class SqlService {
|
|
|
2554
2892
|
}
|
|
2555
2893
|
return result;
|
|
2556
2894
|
};
|
|
2557
|
-
if (
|
|
2895
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2558
2896
|
return fn();
|
|
2559
2897
|
}
|
|
2560
2898
|
else {
|
|
@@ -2570,23 +2908,12 @@ class SqlService {
|
|
|
2570
2908
|
}
|
|
2571
2909
|
return result;
|
|
2572
2910
|
};
|
|
2573
|
-
if (
|
|
2911
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2574
2912
|
return fn();
|
|
2575
2913
|
}
|
|
2576
2914
|
else {
|
|
2577
2915
|
return option?.dao?.transaction(SyncMode.Async, fn, option?.conn);
|
|
2578
2916
|
}
|
|
2579
|
-
// return new Promise<number>(async (resolve, reject) => {
|
|
2580
|
-
// try {
|
|
2581
|
-
// if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2582
|
-
// resolve((await fn())!);
|
|
2583
|
-
// } else {
|
|
2584
|
-
// await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())!), option?.conn);
|
|
2585
|
-
// }
|
|
2586
|
-
// } catch (error) {
|
|
2587
|
-
// reject(error);
|
|
2588
|
-
// }
|
|
2589
|
-
// });
|
|
2590
2917
|
}
|
|
2591
2918
|
}
|
|
2592
2919
|
_template(templateResult, result, error) {
|
|
@@ -2619,8 +2946,8 @@ class SqlService {
|
|
|
2619
2946
|
option.templateResult ?? (option.templateResult = TemplateResult.AssertOne);
|
|
2620
2947
|
option.error ?? (option.error = 'error data!');
|
|
2621
2948
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
2622
|
-
const tableTempESC =
|
|
2623
|
-
const tableNameESC =
|
|
2949
|
+
const tableTempESC = tableTemp;
|
|
2950
|
+
const tableNameESC = option?.tableName;
|
|
2624
2951
|
if (option.id) {
|
|
2625
2952
|
const idName = this[_ids][0];
|
|
2626
2953
|
const ids = option.id instanceof Array ? option.id : [option.id];
|
|
@@ -2638,7 +2965,7 @@ class SqlService {
|
|
|
2638
2965
|
params.push(V);
|
|
2639
2966
|
return `${this[_fields][K]?.C2()} = ?`;
|
|
2640
2967
|
}).join(' AND ')}`;
|
|
2641
|
-
}).join(' UNION ALL '), { dialect:
|
|
2968
|
+
}).join(' UNION ALL '), { dialect: formatDialects[option.dbType] });
|
|
2642
2969
|
sqls.push({ sql: whereSql, params });
|
|
2643
2970
|
resultIndex = 0;
|
|
2644
2971
|
}
|
|
@@ -2648,7 +2975,7 @@ class SqlService {
|
|
|
2648
2975
|
const _sqls = this._createTable({ tableName: tableTemp, temp: true, columns: delWhere, data: wheres, index: 'all', id: 'none' });
|
|
2649
2976
|
sqls.push(..._sqls);
|
|
2650
2977
|
resultIndex = sqls.length;
|
|
2651
|
-
sqls.push({ sql: (0, sql_formatter_1.formatDialect)(`SELECT ${columns} FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')};`, { dialect:
|
|
2978
|
+
sqls.push({ sql: (0, sql_formatter_1.formatDialect)(`SELECT ${columns} FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')};`, { dialect: formatDialects[option.dbType] }) });
|
|
2652
2979
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
2653
2980
|
}
|
|
2654
2981
|
if (option.sync === SyncMode.Sync) {
|
|
@@ -2796,7 +3123,7 @@ class SqlService {
|
|
|
2796
3123
|
return '?';
|
|
2797
3124
|
}
|
|
2798
3125
|
return txt;
|
|
2799
|
-
}), { dialect:
|
|
3126
|
+
}), { dialect: formatDialects[option.dbType] });
|
|
2800
3127
|
if (option.sync === SyncMode.Sync) {
|
|
2801
3128
|
const result = option.conn.query(SyncMode.Sync, sql, params);
|
|
2802
3129
|
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.hump, option.mapper, option.mapperIfUndefined);
|
|
@@ -2837,7 +3164,7 @@ class SqlService {
|
|
|
2837
3164
|
return '?';
|
|
2838
3165
|
}
|
|
2839
3166
|
return txt;
|
|
2840
|
-
}), { dialect:
|
|
3167
|
+
}), { dialect: formatDialects[option.dbType] });
|
|
2841
3168
|
if (option.sync === SyncMode.Sync) {
|
|
2842
3169
|
const result = option.conn.execute(SyncMode.Sync, sql, params);
|
|
2843
3170
|
return result.affectedRows;
|
|
@@ -2870,8 +3197,8 @@ class SqlService {
|
|
|
2870
3197
|
});
|
|
2871
3198
|
}
|
|
2872
3199
|
}
|
|
2873
|
-
stream(
|
|
2874
|
-
return new StreamQuery(
|
|
3200
|
+
stream() {
|
|
3201
|
+
return new StreamQuery(this, this[_fields], this[_columns]);
|
|
2875
3202
|
}
|
|
2876
3203
|
page(option) {
|
|
2877
3204
|
const result = {
|
|
@@ -3022,10 +3349,10 @@ class SqlService {
|
|
|
3022
3349
|
return { title, titleSpan, columnTitles };
|
|
3023
3350
|
}
|
|
3024
3351
|
init(option) {
|
|
3025
|
-
var
|
|
3026
|
-
const tableES =
|
|
3027
|
-
(
|
|
3028
|
-
if (
|
|
3352
|
+
var _f;
|
|
3353
|
+
const tableES = option.tableName;
|
|
3354
|
+
(_f = option).force ?? (_f.force = false);
|
|
3355
|
+
if (option.dbType === DBType.Sqlite) {
|
|
3029
3356
|
if (option?.force) {
|
|
3030
3357
|
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${tableES};`);
|
|
3031
3358
|
}
|
|
@@ -3039,9 +3366,9 @@ class SqlService {
|
|
|
3039
3366
|
// 更新版本
|
|
3040
3367
|
const columns = (0, iterare_1.default)(option.conn.query(SyncMode.Sync, `PRAGMA table_info(${tableES})`))
|
|
3041
3368
|
.filter(c => this[_fields].hasOwnProperty((0, object_1.C2P)(c.name, globalThis[exports._Hump])))
|
|
3042
|
-
.map(c =>
|
|
3369
|
+
.map(c => c.name)
|
|
3043
3370
|
.join(',');
|
|
3044
|
-
const rtable =
|
|
3371
|
+
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
3045
3372
|
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
3046
3373
|
option.conn.execute(SyncMode.Sync, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
3047
3374
|
option.conn.execute(SyncMode.Sync, `
|
|
@@ -3052,7 +3379,7 @@ class SqlService {
|
|
|
3052
3379
|
`);
|
|
3053
3380
|
if (this[_index] && this[_index].length) {
|
|
3054
3381
|
for (const index of this[_index]) {
|
|
3055
|
-
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${
|
|
3382
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${`${index}_${Math.random()}`.replace(/\./, '')} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
3056
3383
|
}
|
|
3057
3384
|
}
|
|
3058
3385
|
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
@@ -3076,13 +3403,13 @@ class SqlService {
|
|
|
3076
3403
|
`);
|
|
3077
3404
|
if (this[_index] && this[_index].length) {
|
|
3078
3405
|
for (const index of this[_index]) {
|
|
3079
|
-
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${
|
|
3406
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${`${index}_${Math.random()}`.replace(/\./, '')} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
3080
3407
|
}
|
|
3081
3408
|
}
|
|
3082
3409
|
option.conn.execute(SyncMode.Sync, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
3083
3410
|
}
|
|
3084
3411
|
}
|
|
3085
|
-
else if (
|
|
3412
|
+
else if (option.dbType === DBType.SqliteRemote) {
|
|
3086
3413
|
return new Promise(async (resolve, reject) => {
|
|
3087
3414
|
try {
|
|
3088
3415
|
if (option?.force) {
|
|
@@ -3098,7 +3425,7 @@ class SqlService {
|
|
|
3098
3425
|
// 更新版本
|
|
3099
3426
|
const columns = (0, iterare_1.default)(await option.conn.query(SyncMode.Async, `PRAGMA table_info(${tableES})`))
|
|
3100
3427
|
.filter(c => this[_fields].hasOwnProperty((0, object_1.C2P)(c.name, globalThis[exports._Hump])))
|
|
3101
|
-
.map(c =>
|
|
3428
|
+
.map(c => c.name)
|
|
3102
3429
|
.join(',');
|
|
3103
3430
|
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
3104
3431
|
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
@@ -3111,7 +3438,7 @@ class SqlService {
|
|
|
3111
3438
|
`);
|
|
3112
3439
|
if (this[_index] && this[_index].length) {
|
|
3113
3440
|
for (const index of this[_index]) {
|
|
3114
|
-
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${
|
|
3441
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${`${index}_${Math.random()}`.replace(/\./, '')} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
3115
3442
|
}
|
|
3116
3443
|
}
|
|
3117
3444
|
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
@@ -3134,7 +3461,7 @@ class SqlService {
|
|
|
3134
3461
|
`);
|
|
3135
3462
|
if (this[_index] && this[_index].length) {
|
|
3136
3463
|
for (const index of this[_index]) {
|
|
3137
|
-
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${
|
|
3464
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${`${index}_${Math.random()}`.replace(/\./, '')} ON ${option.tableName} ("${this[_fields][index]?.C2()}");`);
|
|
3138
3465
|
}
|
|
3139
3466
|
}
|
|
3140
3467
|
await option.conn.execute(SyncMode.Async, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
@@ -3148,8 +3475,7 @@ class SqlService {
|
|
|
3148
3475
|
}
|
|
3149
3476
|
}
|
|
3150
3477
|
close(option) {
|
|
3151
|
-
|
|
3152
|
-
delete globalThis[exports._dao][this[_dbType]][dbName];
|
|
3478
|
+
delete globalThis[exports._dao][option.dbType][option.dbName];
|
|
3153
3479
|
if (option?.sync === SyncMode.Async) {
|
|
3154
3480
|
return option.dao.close(SyncMode.Async);
|
|
3155
3481
|
}
|
|
@@ -3169,7 +3495,7 @@ class SqlService {
|
|
|
3169
3495
|
4. 自定义字段名称:字符串数组
|
|
3170
3496
|
** `index` 表的索引,设置方式同ID
|
|
3171
3497
|
*/
|
|
3172
|
-
_createTable({ tableName, temp = true, columns, data, id = 'auto', index = 'auto' } = {}) {
|
|
3498
|
+
_createTable({ tableName, temp = true, columns, data, id = 'auto', index = 'auto', dbType } = {}) {
|
|
3173
3499
|
const sqls = [];
|
|
3174
3500
|
columns = columns || this[_columns];
|
|
3175
3501
|
let ids;
|
|
@@ -3198,14 +3524,14 @@ class SqlService {
|
|
|
3198
3524
|
else {
|
|
3199
3525
|
indexs = index;
|
|
3200
3526
|
}
|
|
3201
|
-
tableName =
|
|
3202
|
-
switch (
|
|
3527
|
+
tableName = tableName ?? this[_tableName];
|
|
3528
|
+
switch (dbType) {
|
|
3203
3529
|
case DBType.Mysql: {
|
|
3204
3530
|
let sql = (0, sql_formatter_1.formatDialect)(`CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3205
3531
|
${columns.map(K => this[_fields][K][DBType.Mysql]()).join(',')}
|
|
3206
3532
|
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) USING BTREE ` : ''}
|
|
3207
3533
|
${indexs && indexs.length ? `,${indexs.map(i => `KEY ${this[_fields][i]?.C2()} (${this[_fields][i]?.C2()})`).join(',')} ` : ''}
|
|
3208
|
-
) ENGINE=MEMORY;`, { dialect:
|
|
3534
|
+
) ENGINE=MEMORY;`, { dialect: sql_formatter_1.mysql });
|
|
3209
3535
|
sqls.push({ sql });
|
|
3210
3536
|
if (data && data.length > 0) {
|
|
3211
3537
|
const params = [];
|
|
@@ -3218,7 +3544,7 @@ class SqlService {
|
|
|
3218
3544
|
}).join(',')}`;
|
|
3219
3545
|
first = false;
|
|
3220
3546
|
return r;
|
|
3221
|
-
}).join(' UNION ALL ')}`, { dialect:
|
|
3547
|
+
}).join(' UNION ALL ')}`, { dialect: sql_formatter_1.mysql });
|
|
3222
3548
|
sqls.push({ sql, params });
|
|
3223
3549
|
}
|
|
3224
3550
|
break;
|
|
@@ -3228,11 +3554,11 @@ class SqlService {
|
|
|
3228
3554
|
let sql = (0, sql_formatter_1.formatDialect)(`CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3229
3555
|
${columns.map(K => this[_fields][K][DBType.Sqlite]()).join(',')}
|
|
3230
3556
|
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) ` : ''}
|
|
3231
|
-
);`, { dialect:
|
|
3557
|
+
);`, { dialect: sql_formatter_1.sqlite });
|
|
3232
3558
|
sqls.push({ sql });
|
|
3233
3559
|
if (indexs) {
|
|
3234
3560
|
for (const index of indexs) {
|
|
3235
|
-
sql = (0, sql_formatter_1.formatDialect)(`CREATE INDEX ${
|
|
3561
|
+
sql = (0, sql_formatter_1.formatDialect)(`CREATE INDEX ${`${index}_${Math.random()}`.replace(/\./, '')} ON ${tableName} (${this[_fields][index]?.C2()});`, { dialect: sql_formatter_1.sqlite });
|
|
3236
3562
|
sqls.push({ sql });
|
|
3237
3563
|
}
|
|
3238
3564
|
}
|
|
@@ -3247,7 +3573,7 @@ class SqlService {
|
|
|
3247
3573
|
}).join(',')}`;
|
|
3248
3574
|
first = false;
|
|
3249
3575
|
return r;
|
|
3250
|
-
}).join(' UNION ALL ')}`, { dialect:
|
|
3576
|
+
}).join(' UNION ALL ')}`, { dialect: sql_formatter_1.sqlite });
|
|
3251
3577
|
sqls.push({ sql, params });
|
|
3252
3578
|
}
|
|
3253
3579
|
break;
|
|
@@ -3358,7 +3684,7 @@ const IF_EXEC = function (def) {
|
|
|
3358
3684
|
};
|
|
3359
3685
|
};
|
|
3360
3686
|
class StreamQuery {
|
|
3361
|
-
constructor(
|
|
3687
|
+
constructor(service, __fields, __columns) {
|
|
3362
3688
|
this._prefix = 0;
|
|
3363
3689
|
this._index = 0;
|
|
3364
3690
|
this._wheres = [];
|
|
@@ -3376,7 +3702,6 @@ class StreamQuery {
|
|
|
3376
3702
|
this._startRow = 0;
|
|
3377
3703
|
this._pageSize = 0;
|
|
3378
3704
|
this._prefix = parseInt(`${Math.random() * 1000}`);
|
|
3379
|
-
this._table = table;
|
|
3380
3705
|
this._service = service;
|
|
3381
3706
|
this[_fields] = __fields;
|
|
3382
3707
|
this[_columns] = __columns;
|
|
@@ -3501,7 +3826,7 @@ class StreamQuery {
|
|
|
3501
3826
|
this._andQuerys.push(fn);
|
|
3502
3827
|
}
|
|
3503
3828
|
else {
|
|
3504
|
-
const stream = new StreamQuery(this.
|
|
3829
|
+
const stream = new StreamQuery(this._service, this[_fields], this[_columns]);
|
|
3505
3830
|
const ret = fn(stream);
|
|
3506
3831
|
if (ret !== false) {
|
|
3507
3832
|
this._andQuerys.push(stream);
|
|
@@ -3514,7 +3839,7 @@ class StreamQuery {
|
|
|
3514
3839
|
this._andQuerys.push(fn);
|
|
3515
3840
|
}
|
|
3516
3841
|
else {
|
|
3517
|
-
const stream = new StreamQuery(this.
|
|
3842
|
+
const stream = new StreamQuery(this._service, this[_fields], this[_columns]);
|
|
3518
3843
|
const ret = fn(stream);
|
|
3519
3844
|
if (ret !== false) {
|
|
3520
3845
|
this._orQuerys.push(stream);
|
|
@@ -3530,7 +3855,6 @@ class StreamQuery {
|
|
|
3530
3855
|
desc2(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} ASC`)); return this; }
|
|
3531
3856
|
limit(startRow, pageSize) { this._startRow = startRow; this._pageSize = pageSize; return this; }
|
|
3532
3857
|
page(pageNumber, pageSize) { this._startRow = ((pageNumber || 1) - 1) * pageSize; this._pageSize = pageSize; return this; }
|
|
3533
|
-
table(_table) { this._table = _table; return this; }
|
|
3534
3858
|
distinct(on = true) { this._distinct = on; return this; }
|
|
3535
3859
|
countDistinct(key, countName) { this._columns.push(`COUNT(DISTINCT ${this[_fields][String(key)]?.C2()}) ${countName || `${this[_fields][String(key)]?.C2()}`}`); return this; }
|
|
3536
3860
|
count(countName) { this._columns.push(`COUNT(1) ${countName ?? 'ct'}`); return this; }
|
|
@@ -3574,7 +3898,7 @@ class StreamQuery {
|
|
|
3574
3898
|
let sql = `
|
|
3575
3899
|
SELECT
|
|
3576
3900
|
${this._distinct ? 'DISTINCT' : ''} ${this._columns && this._columns.length > 0 ? this._columns.join(',') : this[_columns].map(key => this[_fields][String(key)]?.C3()).join(',')}
|
|
3577
|
-
FROM ${this.
|
|
3901
|
+
FROM ${option.tableName ?? this._service[_tableName]}
|
|
3578
3902
|
${where ? ' WHERE ' : ''}
|
|
3579
3903
|
${where}
|
|
3580
3904
|
${this._groups.length > 0 ? `GROUP BY ${this._groups.join(',')} ` : ''}
|
|
@@ -3623,7 +3947,7 @@ class StreamQuery {
|
|
|
3623
3947
|
let sql = `
|
|
3624
3948
|
SELECT
|
|
3625
3949
|
${this._distinct ? 'DISTINCT' : ''} ${this._columns && this._columns.length > 0 ? this._columns.join(',') : this[_columns].map(key => this[_fields][String(key)]?.C3()).join(',')}
|
|
3626
|
-
FROM ${this.
|
|
3950
|
+
FROM ${option.tableName ?? this._service[_tableName]}
|
|
3627
3951
|
${where ? ' WHERE ' : ''}
|
|
3628
3952
|
${where}
|
|
3629
3953
|
${this._groups.length > 0 ? `GROUP BY ${this._groups.join(',')} ` : ''}
|
|
@@ -3637,7 +3961,7 @@ class StreamQuery {
|
|
|
3637
3961
|
}
|
|
3638
3962
|
const sqlCount = `
|
|
3639
3963
|
SELECT COUNT(1)
|
|
3640
|
-
FROM ${this.
|
|
3964
|
+
FROM ${option.tableName ?? this._service[_tableName]}
|
|
3641
3965
|
${where ? ' WHERE ' : ''}
|
|
3642
3966
|
${where}
|
|
3643
3967
|
${this._groups.length > 0 ? `GROUP BY ${this._groups.join(',')} ` : ''}
|
|
@@ -3708,7 +4032,7 @@ class StreamQuery {
|
|
|
3708
4032
|
}
|
|
3709
4033
|
}
|
|
3710
4034
|
if (sets.length > 0) {
|
|
3711
|
-
const sql = `UPDATE ${this.
|
|
4035
|
+
const sql = `UPDATE ${option.tableName ?? this._service[_tableName]} SET ${sets.join(',')}
|
|
3712
4036
|
${where ? ' WHERE ' : ''}
|
|
3713
4037
|
${where}
|
|
3714
4038
|
`;
|
|
@@ -3727,7 +4051,7 @@ class StreamQuery {
|
|
|
3727
4051
|
option ?? (option = {});
|
|
3728
4052
|
option.sync ?? (option.sync = SyncMode.Async);
|
|
3729
4053
|
const { where, params } = this._where();
|
|
3730
|
-
const sql = `DELETE FROM ${this.
|
|
4054
|
+
const sql = `DELETE FROM ${option.tableName ?? this._service[_tableName]}
|
|
3731
4055
|
${where ? ' WHERE ' : ''}
|
|
3732
4056
|
${where}
|
|
3733
4057
|
`;
|
|
@@ -4324,12 +4648,6 @@ __decorate([
|
|
|
4324
4648
|
__metadata("design:paramtypes", [Number, Number]),
|
|
4325
4649
|
__metadata("design:returntype", void 0)
|
|
4326
4650
|
], StreamQuery.prototype, "page", null);
|
|
4327
|
-
__decorate([
|
|
4328
|
-
IF_PROCEED(),
|
|
4329
|
-
__metadata("design:type", Function),
|
|
4330
|
-
__metadata("design:paramtypes", [String]),
|
|
4331
|
-
__metadata("design:returntype", void 0)
|
|
4332
|
-
], StreamQuery.prototype, "table", null);
|
|
4333
4651
|
__decorate([
|
|
4334
4652
|
IF_PROCEED(),
|
|
4335
4653
|
__metadata("design:type", Function),
|