baja-lite 1.0.1 → 1.0.2
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/redis.d.ts +0 -0
- package/cjs/redis.js +1 -0
- package/cjs/sql.d.ts +149 -125
- package/cjs/sql.js +388 -229
- package/cjs/test-mysql.js +5 -5
- package/cjs/test-sqlite.js +7 -7
- package/es/redis.d.ts +0 -0
- package/es/redis.js +1 -0
- package/es/sql.d.ts +149 -125
- package/es/sql.js +386 -227
- package/es/test-mysql.js +6 -6
- package/es/test-sqlite.js +8 -8
- package/package.json +1 -1
package/es/sql.js
CHANGED
|
@@ -61,21 +61,21 @@ export var DBType;
|
|
|
61
61
|
DBType[DBType["Redis"] = 4] = "Redis";
|
|
62
62
|
DBType[DBType["RedisLock"] = 5] = "RedisLock";
|
|
63
63
|
})(DBType || (DBType = {}));
|
|
64
|
-
export var
|
|
65
|
-
(function (
|
|
64
|
+
export var SyncMode;
|
|
65
|
+
(function (SyncMode) {
|
|
66
66
|
/** 同步执行 */
|
|
67
|
-
|
|
67
|
+
SyncMode[SyncMode["Sync"] = 0] = "Sync";
|
|
68
68
|
/** 异步执行 */
|
|
69
|
-
|
|
70
|
-
})(
|
|
71
|
-
export var
|
|
72
|
-
(function (
|
|
69
|
+
SyncMode[SyncMode["Async"] = 1] = "Async";
|
|
70
|
+
})(SyncMode || (SyncMode = {}));
|
|
71
|
+
export var InsertMode;
|
|
72
|
+
(function (InsertMode) {
|
|
73
73
|
/**
|
|
74
74
|
# 默认使用
|
|
75
75
|
** 支持单个、批量,语法 `INSERT INTO XX VALUES (第一条数据), (第二条数据);`
|
|
76
76
|
** 批量执行有性能优势,但无法利用数据库的sql预编译功能
|
|
77
77
|
*/
|
|
78
|
-
|
|
78
|
+
InsertMode[InsertMode["Insert"] = 0] = "Insert";
|
|
79
79
|
/**
|
|
80
80
|
# 利用临时表
|
|
81
81
|
## 执行步骤
|
|
@@ -87,16 +87,16 @@ export var SqlInsertMode;
|
|
|
87
87
|
1. 适用于:主键不会冲突、非自增
|
|
88
88
|
2. 临时表的结构复制正式表
|
|
89
89
|
*/
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
InsertMode[InsertMode["InsertWithTempTable"] = 1] = "InsertWithTempTable";
|
|
91
|
+
InsertMode[InsertMode["InsertIfNotExists"] = 2] = "InsertIfNotExists";
|
|
92
92
|
/**
|
|
93
93
|
# 插入或者更新
|
|
94
94
|
1. 判断依据是主键
|
|
95
95
|
*/
|
|
96
|
-
|
|
97
|
-
})(
|
|
98
|
-
export var
|
|
99
|
-
(function (
|
|
96
|
+
InsertMode[InsertMode["Replace"] = 3] = "Replace";
|
|
97
|
+
})(InsertMode || (InsertMode = {}));
|
|
98
|
+
export var DeleteMode;
|
|
99
|
+
(function (DeleteMode) {
|
|
100
100
|
/**
|
|
101
101
|
##常规删除 默认
|
|
102
102
|
### 例一
|
|
@@ -104,16 +104,16 @@ export var SqlDelMode;
|
|
|
104
104
|
### 例二
|
|
105
105
|
`DELETE FROM WHERE (id = 1 AND idx = 11) OR (id = 2 AND idx = 22)`
|
|
106
106
|
*/
|
|
107
|
-
|
|
107
|
+
DeleteMode[DeleteMode["Common"] = 0] = "Common";
|
|
108
108
|
/*
|
|
109
109
|
## 借助临时表
|
|
110
110
|
### 注意:必须保证where的字段都相同,否则会漏删数据
|
|
111
111
|
DELETE FROM 正式表 INNER JOIN 临时表 WHERE 字段1 = 字段1 AND 字段2 = 字段2
|
|
112
112
|
*/
|
|
113
|
-
|
|
114
|
-
})(
|
|
115
|
-
export var
|
|
116
|
-
(function (
|
|
113
|
+
DeleteMode[DeleteMode["TempTable"] = 1] = "TempTable";
|
|
114
|
+
})(DeleteMode || (DeleteMode = {}));
|
|
115
|
+
export var SelectMode;
|
|
116
|
+
(function (SelectMode) {
|
|
117
117
|
/**
|
|
118
118
|
##常规 默认
|
|
119
119
|
### 例一
|
|
@@ -121,46 +121,70 @@ export var SqlSelectMode;
|
|
|
121
121
|
### 例二
|
|
122
122
|
`SELECT * FROM WHERE (id = 1 AND idx = 11) OR (id = 2 AND idx = 22)`
|
|
123
123
|
*/
|
|
124
|
-
|
|
124
|
+
SelectMode[SelectMode["Common"] = 0] = "Common";
|
|
125
125
|
/*
|
|
126
126
|
## 借助临时表
|
|
127
127
|
### 注意:必须保证where的字段都相同,否则会漏删数据
|
|
128
128
|
SELECT * FROM 正式表 INNER JOIN 临时表 WHERE 字段1 = 字段1 AND 字段2 = 字段2
|
|
129
129
|
*/
|
|
130
|
-
|
|
131
|
-
})(
|
|
132
|
-
export var
|
|
133
|
-
(function (
|
|
134
|
-
/**
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
|
|
130
|
+
SelectMode[SelectMode["TempTable"] = 1] = "TempTable";
|
|
131
|
+
})(SelectMode || (SelectMode = {}));
|
|
132
|
+
export var TemplateResult;
|
|
133
|
+
(function (TemplateResult) {
|
|
134
|
+
/** 确定返回一条记录,如果不是一个,将报错,返回类型是T */
|
|
135
|
+
TemplateResult[TemplateResult["AssertOne"] = 0] = "AssertOne";
|
|
136
|
+
/** 可能返回一条记录,返回类型是T|null */
|
|
137
|
+
TemplateResult[TemplateResult["NotSureOne"] = 1] = "NotSureOne";
|
|
138
|
+
/** 返回多条记录 */
|
|
139
|
+
TemplateResult[TemplateResult["Many"] = 2] = "Many";
|
|
140
140
|
/** 仅查询记录数量 */
|
|
141
|
-
|
|
142
|
-
})(
|
|
143
|
-
export var
|
|
144
|
-
(function (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
})(
|
|
141
|
+
TemplateResult[TemplateResult["Count"] = 3] = "Count";
|
|
142
|
+
})(TemplateResult || (TemplateResult = {}));
|
|
143
|
+
export var SelectResult;
|
|
144
|
+
(function (SelectResult) {
|
|
145
|
+
SelectResult[SelectResult["One_Row_One_Column_Assert"] = 0] = "One_Row_One_Column_Assert";
|
|
146
|
+
SelectResult[SelectResult["One_Row_One_Column_NotSure"] = 1] = "One_Row_One_Column_NotSure";
|
|
147
|
+
SelectResult[SelectResult["One_Row_Many_Column_Assert"] = 2] = "One_Row_Many_Column_Assert";
|
|
148
|
+
SelectResult[SelectResult["One_Row_Many_Column_NotSure"] = 3] = "One_Row_Many_Column_NotSure";
|
|
149
|
+
SelectResult[SelectResult["Many_Row_One_Column"] = 4] = "Many_Row_One_Column";
|
|
150
|
+
SelectResult[SelectResult["Many_Row_Many_Column"] = 5] = "Many_Row_Many_Column";
|
|
151
|
+
})(SelectResult || (SelectResult = {}));
|
|
152
152
|
export var SqlType;
|
|
153
153
|
(function (SqlType) {
|
|
154
|
-
SqlType[SqlType["
|
|
155
|
-
SqlType[SqlType["
|
|
156
|
-
SqlType[SqlType["
|
|
154
|
+
SqlType[SqlType["tinyint"] = 0] = "tinyint";
|
|
155
|
+
SqlType[SqlType["smallint"] = 1] = "smallint";
|
|
156
|
+
SqlType[SqlType["mediumint"] = 2] = "mediumint";
|
|
157
157
|
SqlType[SqlType["int"] = 3] = "int";
|
|
158
|
-
SqlType[SqlType["
|
|
159
|
-
SqlType[SqlType["
|
|
160
|
-
SqlType[SqlType["
|
|
161
|
-
SqlType[SqlType["
|
|
162
|
-
SqlType[SqlType["
|
|
163
|
-
SqlType[SqlType["
|
|
158
|
+
SqlType[SqlType["bigint"] = 4] = "bigint";
|
|
159
|
+
SqlType[SqlType["float"] = 5] = "float";
|
|
160
|
+
SqlType[SqlType["double"] = 6] = "double";
|
|
161
|
+
SqlType[SqlType["decimal"] = 7] = "decimal";
|
|
162
|
+
SqlType[SqlType["date"] = 8] = "date";
|
|
163
|
+
SqlType[SqlType["time"] = 9] = "time";
|
|
164
|
+
SqlType[SqlType["year"] = 10] = "year";
|
|
165
|
+
SqlType[SqlType["datetime"] = 11] = "datetime";
|
|
166
|
+
SqlType[SqlType["timestamp"] = 12] = "timestamp";
|
|
167
|
+
SqlType[SqlType["char"] = 13] = "char";
|
|
168
|
+
SqlType[SqlType["varchar"] = 14] = "varchar";
|
|
169
|
+
SqlType[SqlType["tinyblob"] = 15] = "tinyblob";
|
|
170
|
+
SqlType[SqlType["tinytext"] = 16] = "tinytext";
|
|
171
|
+
SqlType[SqlType["blob"] = 17] = "blob";
|
|
172
|
+
SqlType[SqlType["text"] = 18] = "text";
|
|
173
|
+
SqlType[SqlType["mediumblob"] = 19] = "mediumblob";
|
|
174
|
+
SqlType[SqlType["mediumtext"] = 20] = "mediumtext";
|
|
175
|
+
SqlType[SqlType["longblob"] = 21] = "longblob";
|
|
176
|
+
SqlType[SqlType["longtext"] = 22] = "longtext";
|
|
177
|
+
SqlType[SqlType["set"] = 23] = "set";
|
|
178
|
+
SqlType[SqlType["enum"] = 24] = "enum";
|
|
179
|
+
SqlType[SqlType["json"] = 25] = "json";
|
|
180
|
+
SqlType[SqlType["geometry"] = 26] = "geometry";
|
|
181
|
+
SqlType[SqlType["point"] = 27] = "point";
|
|
182
|
+
SqlType[SqlType["linestring"] = 28] = "linestring";
|
|
183
|
+
SqlType[SqlType["polygon"] = 29] = "polygon";
|
|
184
|
+
SqlType[SqlType["multipoint"] = 30] = "multipoint";
|
|
185
|
+
SqlType[SqlType["multilinestring"] = 31] = "multilinestring";
|
|
186
|
+
SqlType[SqlType["multipolygon"] = 32] = "multipolygon";
|
|
187
|
+
SqlType[SqlType["geometrycollection"] = 33] = "geometrycollection";
|
|
164
188
|
})(SqlType || (SqlType = {}));
|
|
165
189
|
export const SqliteMemory = ':memory:';
|
|
166
190
|
const _defOption = {
|
|
@@ -181,7 +205,7 @@ class MysqlConnection {
|
|
|
181
205
|
return { affectedRows: 0, insertId: 0n };
|
|
182
206
|
}
|
|
183
207
|
;
|
|
184
|
-
if (sync ===
|
|
208
|
+
if (sync === SyncMode.Sync) {
|
|
185
209
|
logger.warn('MYSQL not suppouted sync mode');
|
|
186
210
|
return { affectedRows: 0, insertId: 0n };
|
|
187
211
|
}
|
|
@@ -214,7 +238,7 @@ class MysqlConnection {
|
|
|
214
238
|
return null;
|
|
215
239
|
}
|
|
216
240
|
;
|
|
217
|
-
if (sync ===
|
|
241
|
+
if (sync === SyncMode.Sync) {
|
|
218
242
|
logger.warn('MYSQL not suppouted sync mode');
|
|
219
243
|
return null;
|
|
220
244
|
}
|
|
@@ -250,7 +274,7 @@ class MysqlConnection {
|
|
|
250
274
|
return null;
|
|
251
275
|
}
|
|
252
276
|
;
|
|
253
|
-
if (sync ===
|
|
277
|
+
if (sync === SyncMode.Sync) {
|
|
254
278
|
logger.warn('MYSQL not suppouted sync mode');
|
|
255
279
|
return null;
|
|
256
280
|
}
|
|
@@ -284,7 +308,7 @@ class MysqlConnection {
|
|
|
284
308
|
return [];
|
|
285
309
|
}
|
|
286
310
|
;
|
|
287
|
-
if (sync ===
|
|
311
|
+
if (sync === SyncMode.Sync) {
|
|
288
312
|
logger.warn('MYSQL not suppouted sync mode');
|
|
289
313
|
return [];
|
|
290
314
|
}
|
|
@@ -318,7 +342,7 @@ class MysqlConnection {
|
|
|
318
342
|
return [];
|
|
319
343
|
}
|
|
320
344
|
;
|
|
321
|
-
if (sync ===
|
|
345
|
+
if (sync === SyncMode.Sync) {
|
|
322
346
|
logger.warn('MYSQL not suppouted sync mode');
|
|
323
347
|
return [];
|
|
324
348
|
}
|
|
@@ -345,7 +369,7 @@ class MysqlConnection {
|
|
|
345
369
|
});
|
|
346
370
|
}
|
|
347
371
|
realse(sync) {
|
|
348
|
-
if (sync ===
|
|
372
|
+
if (sync === SyncMode.Sync) {
|
|
349
373
|
try {
|
|
350
374
|
this[_daoConnection]?.release();
|
|
351
375
|
}
|
|
@@ -361,7 +385,7 @@ class Mysql {
|
|
|
361
385
|
this[_daoDB] = pool;
|
|
362
386
|
}
|
|
363
387
|
createConnection(sync) {
|
|
364
|
-
if (sync ===
|
|
388
|
+
if (sync === SyncMode.Sync) {
|
|
365
389
|
logger.error('MYSQL not suppouted sync mode');
|
|
366
390
|
return null;
|
|
367
391
|
}
|
|
@@ -373,7 +397,7 @@ class Mysql {
|
|
|
373
397
|
});
|
|
374
398
|
}
|
|
375
399
|
transaction(sync, fn, conn) {
|
|
376
|
-
if (sync ===
|
|
400
|
+
if (sync === SyncMode.Sync) {
|
|
377
401
|
logger.warn('MYSQL not suppouted sync mode');
|
|
378
402
|
return null;
|
|
379
403
|
}
|
|
@@ -382,7 +406,7 @@ class Mysql {
|
|
|
382
406
|
let needCommit = false;
|
|
383
407
|
let newConn = false;
|
|
384
408
|
if (!conn) {
|
|
385
|
-
conn = await this.createConnection(
|
|
409
|
+
conn = await this.createConnection(SyncMode.Async) ?? undefined;
|
|
386
410
|
newConn = true;
|
|
387
411
|
}
|
|
388
412
|
if (conn?.[_inTransaction] !== true) {
|
|
@@ -427,7 +451,7 @@ class Mysql {
|
|
|
427
451
|
});
|
|
428
452
|
}
|
|
429
453
|
close(sync) {
|
|
430
|
-
if (sync ===
|
|
454
|
+
if (sync === SyncMode.Sync) {
|
|
431
455
|
this[_daoDB]?.destroy();
|
|
432
456
|
}
|
|
433
457
|
;
|
|
@@ -451,7 +475,7 @@ class SqliteConnection {
|
|
|
451
475
|
return { affectedRows: 0, insertId: 0n };
|
|
452
476
|
}
|
|
453
477
|
;
|
|
454
|
-
if (sync ===
|
|
478
|
+
if (sync === SyncMode.Async) {
|
|
455
479
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
456
480
|
return { affectedRows: 0, insertId: 0n };
|
|
457
481
|
}
|
|
@@ -482,7 +506,7 @@ class SqliteConnection {
|
|
|
482
506
|
return null;
|
|
483
507
|
}
|
|
484
508
|
;
|
|
485
|
-
if (sync ===
|
|
509
|
+
if (sync === SyncMode.Async) {
|
|
486
510
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
487
511
|
return null;
|
|
488
512
|
}
|
|
@@ -508,7 +532,7 @@ class SqliteConnection {
|
|
|
508
532
|
return null;
|
|
509
533
|
}
|
|
510
534
|
;
|
|
511
|
-
if (sync ===
|
|
535
|
+
if (sync === SyncMode.Async) {
|
|
512
536
|
return null;
|
|
513
537
|
}
|
|
514
538
|
;
|
|
@@ -533,7 +557,7 @@ class SqliteConnection {
|
|
|
533
557
|
return [];
|
|
534
558
|
}
|
|
535
559
|
;
|
|
536
|
-
if (sync ===
|
|
560
|
+
if (sync === SyncMode.Async) {
|
|
537
561
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
538
562
|
return [];
|
|
539
563
|
}
|
|
@@ -559,7 +583,7 @@ class SqliteConnection {
|
|
|
559
583
|
return [];
|
|
560
584
|
}
|
|
561
585
|
;
|
|
562
|
-
if (sync ===
|
|
586
|
+
if (sync === SyncMode.Async) {
|
|
563
587
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
564
588
|
return [];
|
|
565
589
|
}
|
|
@@ -598,7 +622,7 @@ class Sqlite {
|
|
|
598
622
|
`);
|
|
599
623
|
}
|
|
600
624
|
createConnection(sync) {
|
|
601
|
-
if (sync ===
|
|
625
|
+
if (sync === SyncMode.Async) {
|
|
602
626
|
logger.error(`SQLITE not suppoted async mode`);
|
|
603
627
|
return null;
|
|
604
628
|
}
|
|
@@ -606,13 +630,13 @@ class Sqlite {
|
|
|
606
630
|
return new SqliteConnection(this[_daoDB]);
|
|
607
631
|
}
|
|
608
632
|
transaction(sync, fn, conn) {
|
|
609
|
-
if (sync ===
|
|
633
|
+
if (sync === SyncMode.Async) {
|
|
610
634
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
611
635
|
return null;
|
|
612
636
|
}
|
|
613
637
|
;
|
|
614
638
|
if (!conn) {
|
|
615
|
-
conn = this.createConnection(
|
|
639
|
+
conn = this.createConnection(SyncMode.Sync) ?? undefined;
|
|
616
640
|
}
|
|
617
641
|
if (conn[_inTransaction] !== true) {
|
|
618
642
|
return this[_daoDB].transaction(() => {
|
|
@@ -628,13 +652,13 @@ class Sqlite {
|
|
|
628
652
|
}
|
|
629
653
|
}
|
|
630
654
|
close(sync) {
|
|
631
|
-
if (sync ===
|
|
655
|
+
if (sync === SyncMode.Sync) {
|
|
632
656
|
this[_daoDB].close();
|
|
633
657
|
}
|
|
634
658
|
;
|
|
635
659
|
}
|
|
636
660
|
backup(sync, name) {
|
|
637
|
-
if (sync ===
|
|
661
|
+
if (sync === SyncMode.Sync) {
|
|
638
662
|
this[_daoDB].backup(name);
|
|
639
663
|
}
|
|
640
664
|
;
|
|
@@ -656,7 +680,7 @@ class SqliteRemoteConnection {
|
|
|
656
680
|
return { affectedRows: 0, insertId: 0n };
|
|
657
681
|
}
|
|
658
682
|
;
|
|
659
|
-
if (sync ===
|
|
683
|
+
if (sync === SyncMode.Sync) {
|
|
660
684
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
661
685
|
return { affectedRows: 0, insertId: 0n };
|
|
662
686
|
}
|
|
@@ -685,7 +709,7 @@ class SqliteRemoteConnection {
|
|
|
685
709
|
return null;
|
|
686
710
|
}
|
|
687
711
|
;
|
|
688
|
-
if (sync ===
|
|
712
|
+
if (sync === SyncMode.Sync) {
|
|
689
713
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
690
714
|
return null;
|
|
691
715
|
}
|
|
@@ -714,7 +738,7 @@ class SqliteRemoteConnection {
|
|
|
714
738
|
return null;
|
|
715
739
|
}
|
|
716
740
|
;
|
|
717
|
-
if (sync ===
|
|
741
|
+
if (sync === SyncMode.Sync) {
|
|
718
742
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
719
743
|
return null;
|
|
720
744
|
}
|
|
@@ -743,7 +767,7 @@ class SqliteRemoteConnection {
|
|
|
743
767
|
return [];
|
|
744
768
|
}
|
|
745
769
|
;
|
|
746
|
-
if (sync ===
|
|
770
|
+
if (sync === SyncMode.Sync) {
|
|
747
771
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
748
772
|
return [];
|
|
749
773
|
}
|
|
@@ -772,7 +796,7 @@ class SqliteRemoteConnection {
|
|
|
772
796
|
return [];
|
|
773
797
|
}
|
|
774
798
|
;
|
|
775
|
-
if (sync ===
|
|
799
|
+
if (sync === SyncMode.Sync) {
|
|
776
800
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
777
801
|
return [];
|
|
778
802
|
}
|
|
@@ -805,7 +829,7 @@ class SqliteRemote {
|
|
|
805
829
|
this[_sqliteRemoteName] = name;
|
|
806
830
|
}
|
|
807
831
|
createConnection(sync) {
|
|
808
|
-
if (sync ===
|
|
832
|
+
if (sync === SyncMode.Sync) {
|
|
809
833
|
logger.error('SQLITEREMOTE not suppouted sync mode');
|
|
810
834
|
return null;
|
|
811
835
|
}
|
|
@@ -819,7 +843,7 @@ class SqliteRemote {
|
|
|
819
843
|
return null;
|
|
820
844
|
}
|
|
821
845
|
close(sync) {
|
|
822
|
-
if (sync ===
|
|
846
|
+
if (sync === SyncMode.Async) {
|
|
823
847
|
return new Promise(async () => {
|
|
824
848
|
await this[_daoConnection].close();
|
|
825
849
|
});
|
|
@@ -827,7 +851,7 @@ class SqliteRemote {
|
|
|
827
851
|
;
|
|
828
852
|
}
|
|
829
853
|
backup(sync, name) {
|
|
830
|
-
if (sync ===
|
|
854
|
+
if (sync === SyncMode.Async) {
|
|
831
855
|
return new Promise(async () => {
|
|
832
856
|
await this[_daoConnection].backup(this[_sqliteRemoteName], name);
|
|
833
857
|
});
|
|
@@ -835,7 +859,7 @@ class SqliteRemote {
|
|
|
835
859
|
;
|
|
836
860
|
}
|
|
837
861
|
remove(sync) {
|
|
838
|
-
if (sync ===
|
|
862
|
+
if (sync === SyncMode.Async) {
|
|
839
863
|
return new Promise(async () => {
|
|
840
864
|
await this[_daoConnection].remove();
|
|
841
865
|
});
|
|
@@ -843,7 +867,7 @@ class SqliteRemote {
|
|
|
843
867
|
;
|
|
844
868
|
}
|
|
845
869
|
restore(sync, name) {
|
|
846
|
-
if (sync ===
|
|
870
|
+
if (sync === SyncMode.Async) {
|
|
847
871
|
return new Promise(async () => {
|
|
848
872
|
await this[_daoConnection].restore(this[_sqliteRemoteName], name);
|
|
849
873
|
});
|
|
@@ -905,17 +929,17 @@ function P(skipConn = false) {
|
|
|
905
929
|
const startTime = +new Date();
|
|
906
930
|
// option
|
|
907
931
|
const option = args[0] = Object.assign({}, globalThis[_GlobalSqlOption], this[_SqlOption], args[0]);
|
|
908
|
-
option.sync ?? (option.sync =
|
|
932
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
909
933
|
const dbName = option?.dbName ?? this[_daoDBName] ?? _primaryDB;
|
|
910
934
|
option.dao = globalThis[_dao][this[_dbType]][dbName];
|
|
911
935
|
Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
|
|
912
936
|
option.tableName = option?.tableName ?? this[_tableName];
|
|
913
937
|
const tableES = Sqlstring.escapeId(option.tableName);
|
|
914
938
|
if (this[_dbType] === DBType.Sqlite) {
|
|
915
|
-
Throw.if(option.sync ===
|
|
939
|
+
Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
|
|
916
940
|
// 连接共享
|
|
917
941
|
if (skipConn === false && !option.conn) {
|
|
918
|
-
option.conn = option.dao.createConnection(
|
|
942
|
+
option.conn = option.dao.createConnection(SyncMode.Sync);
|
|
919
943
|
}
|
|
920
944
|
else {
|
|
921
945
|
needRealseConn = false;
|
|
@@ -923,20 +947,20 @@ function P(skipConn = false) {
|
|
|
923
947
|
if (skipConn === false) {
|
|
924
948
|
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
925
949
|
// 检查表
|
|
926
|
-
const tableCheckResult = option.conn.pluck(
|
|
950
|
+
const tableCheckResult = option.conn.pluck(SyncMode.Sync, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
927
951
|
if (tableCheckResult) {
|
|
928
952
|
// 旧版本
|
|
929
|
-
const tableVersion = option.conn.pluck(
|
|
953
|
+
const tableVersion = option.conn.pluck(SyncMode.Sync, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
930
954
|
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
931
955
|
// 更新版本
|
|
932
|
-
const columns = iterare(option.conn.query(
|
|
956
|
+
const columns = iterare(option.conn.query(SyncMode.Sync, `PRAGMA table_info(${tableES})`))
|
|
933
957
|
.filter(c => this[_fields].hasOwnProperty(c.name))
|
|
934
958
|
.map(c => Sqlstring.escapeId(c.name))
|
|
935
959
|
.join(',');
|
|
936
960
|
const rtable = Sqlstring.escapeId(`${option.tableName}_${tableVersion.replace(/\./, '_')}`);
|
|
937
|
-
option.conn.execute(
|
|
938
|
-
option.conn.execute(
|
|
939
|
-
option.conn.execute(
|
|
961
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
962
|
+
option.conn.execute(SyncMode.Sync, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
963
|
+
option.conn.execute(SyncMode.Sync, `
|
|
940
964
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
941
965
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
942
966
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -944,22 +968,22 @@ function P(skipConn = false) {
|
|
|
944
968
|
`);
|
|
945
969
|
if (this[_index] && this[_index].length) {
|
|
946
970
|
for (const index of this[_index]) {
|
|
947
|
-
option.conn.execute(
|
|
971
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
948
972
|
}
|
|
949
973
|
}
|
|
950
|
-
option.conn.execute(
|
|
951
|
-
option.conn.execute(
|
|
952
|
-
option.conn.execute(
|
|
974
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
975
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
976
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
953
977
|
// 更新完毕,保存版本号
|
|
954
|
-
option.conn.execute(
|
|
978
|
+
option.conn.execute(SyncMode.Sync, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
955
979
|
}
|
|
956
980
|
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
957
|
-
option.conn.execute(
|
|
981
|
+
option.conn.execute(SyncMode.Sync, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
958
982
|
}
|
|
959
983
|
}
|
|
960
984
|
else { // 表不存在
|
|
961
985
|
// 创建表
|
|
962
|
-
option.conn.execute(
|
|
986
|
+
option.conn.execute(SyncMode.Sync, `
|
|
963
987
|
CREATE TABLE IF NOT EXISTS ${tableES} (
|
|
964
988
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
965
989
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -968,10 +992,10 @@ function P(skipConn = false) {
|
|
|
968
992
|
`);
|
|
969
993
|
if (this[_index] && this[_index].length) {
|
|
970
994
|
for (const index of this[_index]) {
|
|
971
|
-
option.conn.execute(
|
|
995
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
972
996
|
}
|
|
973
997
|
}
|
|
974
|
-
option.conn.execute(
|
|
998
|
+
option.conn.execute(SyncMode.Sync, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
975
999
|
}
|
|
976
1000
|
}
|
|
977
1001
|
try {
|
|
@@ -990,7 +1014,7 @@ function P(skipConn = false) {
|
|
|
990
1014
|
finally {
|
|
991
1015
|
if (needRealseConn && option && option.conn) {
|
|
992
1016
|
try {
|
|
993
|
-
option.conn.realse(
|
|
1017
|
+
option.conn.realse(SyncMode.Sync);
|
|
994
1018
|
}
|
|
995
1019
|
catch (error) {
|
|
996
1020
|
}
|
|
@@ -998,11 +1022,11 @@ function P(skipConn = false) {
|
|
|
998
1022
|
}
|
|
999
1023
|
}
|
|
1000
1024
|
else if (this[_dbType] === DBType.SqliteRemote) {
|
|
1001
|
-
Throw.if(option.sync ===
|
|
1025
|
+
Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
|
|
1002
1026
|
return new Promise(async (resolve) => {
|
|
1003
1027
|
// 连接共享
|
|
1004
1028
|
if (skipConn === false && !option.conn) {
|
|
1005
|
-
(option).conn = await option.dao.createConnection(
|
|
1029
|
+
(option).conn = await option.dao.createConnection(SyncMode.Async);
|
|
1006
1030
|
}
|
|
1007
1031
|
else {
|
|
1008
1032
|
needRealseConn = false;
|
|
@@ -1010,20 +1034,20 @@ function P(skipConn = false) {
|
|
|
1010
1034
|
if (skipConn === false) {
|
|
1011
1035
|
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
1012
1036
|
// 检查表
|
|
1013
|
-
const tableCheckResult = await option.conn.pluck(
|
|
1037
|
+
const tableCheckResult = await option.conn.pluck(SyncMode.Async, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
1014
1038
|
if (tableCheckResult) {
|
|
1015
1039
|
// 旧版本
|
|
1016
|
-
const tableVersion = await option.conn.pluck(
|
|
1040
|
+
const tableVersion = await option.conn.pluck(SyncMode.Async, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
1017
1041
|
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
1018
1042
|
// 更新版本
|
|
1019
|
-
const columns = iterare(await option.conn.query(
|
|
1043
|
+
const columns = iterare(await option.conn.query(SyncMode.Async, `PRAGMA table_info(${tableES})`))
|
|
1020
1044
|
.filter(c => this[_fields].hasOwnProperty(c.name))
|
|
1021
1045
|
.map(c => Sqlstring.escapeId(c.name))
|
|
1022
1046
|
.join(',');
|
|
1023
1047
|
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
1024
|
-
await option.conn.execute(
|
|
1025
|
-
await option.conn.execute(
|
|
1026
|
-
await option.conn.execute(
|
|
1048
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1049
|
+
await option.conn.execute(SyncMode.Async, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1050
|
+
await option.conn.execute(SyncMode.Async, `
|
|
1027
1051
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1028
1052
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1029
1053
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -1031,22 +1055,22 @@ function P(skipConn = false) {
|
|
|
1031
1055
|
`);
|
|
1032
1056
|
if (this[_index] && this[_index].length) {
|
|
1033
1057
|
for (const index of this[_index]) {
|
|
1034
|
-
await option.conn.execute(
|
|
1058
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
1035
1059
|
}
|
|
1036
1060
|
}
|
|
1037
|
-
await option.conn.execute(
|
|
1038
|
-
await option.conn.execute(
|
|
1039
|
-
await option.conn.execute(
|
|
1061
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1062
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1063
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1040
1064
|
// 更新完毕,保存版本号
|
|
1041
|
-
await option.conn.execute(
|
|
1065
|
+
await option.conn.execute(SyncMode.Async, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
1042
1066
|
}
|
|
1043
1067
|
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
1044
|
-
await option.conn.execute(
|
|
1068
|
+
await option.conn.execute(SyncMode.Async, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1045
1069
|
}
|
|
1046
1070
|
}
|
|
1047
1071
|
else { // 表不存在
|
|
1048
1072
|
// 创建表
|
|
1049
|
-
await option.conn.execute(
|
|
1073
|
+
await option.conn.execute(SyncMode.Async, `
|
|
1050
1074
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1051
1075
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1052
1076
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -1054,10 +1078,10 @@ function P(skipConn = false) {
|
|
|
1054
1078
|
`);
|
|
1055
1079
|
if (this[_index] && this[_index].length) {
|
|
1056
1080
|
for (const index of this[_index]) {
|
|
1057
|
-
await option.conn.execute(
|
|
1081
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${Sqlstring.escapeId(option.tableName)} ("${index}");`);
|
|
1058
1082
|
}
|
|
1059
1083
|
}
|
|
1060
|
-
await option.conn.execute(
|
|
1084
|
+
await option.conn.execute(SyncMode.Async, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1061
1085
|
}
|
|
1062
1086
|
}
|
|
1063
1087
|
try {
|
|
@@ -1072,7 +1096,7 @@ function P(skipConn = false) {
|
|
|
1072
1096
|
finally {
|
|
1073
1097
|
if (needRealseConn && option && option.conn) {
|
|
1074
1098
|
try {
|
|
1075
|
-
option.conn.realse(
|
|
1099
|
+
option.conn.realse(SyncMode.Sync);
|
|
1076
1100
|
}
|
|
1077
1101
|
catch (error) {
|
|
1078
1102
|
}
|
|
@@ -1085,7 +1109,7 @@ function P(skipConn = false) {
|
|
|
1085
1109
|
try {
|
|
1086
1110
|
// 连接共享
|
|
1087
1111
|
if (skipConn === false && !option.conn) {
|
|
1088
|
-
(option).conn = await option.dao.createConnection(
|
|
1112
|
+
(option).conn = await option.dao.createConnection(SyncMode.Async);
|
|
1089
1113
|
}
|
|
1090
1114
|
else {
|
|
1091
1115
|
needRealseConn = false;
|
|
@@ -1101,7 +1125,7 @@ function P(skipConn = false) {
|
|
|
1101
1125
|
finally {
|
|
1102
1126
|
if (needRealseConn && option && option.conn) {
|
|
1103
1127
|
try {
|
|
1104
|
-
option.conn.realse(
|
|
1128
|
+
option.conn.realse(SyncMode.Sync);
|
|
1105
1129
|
}
|
|
1106
1130
|
catch (error) {
|
|
1107
1131
|
}
|
|
@@ -1150,19 +1174,19 @@ export const Field = (config) => {
|
|
|
1150
1174
|
field.esName = Sqlstring.escapeId(propertyName);
|
|
1151
1175
|
const hasDef = field.hasOwnProperty('def') === true;
|
|
1152
1176
|
switch (field.type) {
|
|
1153
|
-
case SqlType.
|
|
1154
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1177
|
+
case SqlType.tinyint: {
|
|
1178
|
+
field[DBType.Mysql] = `${field.esName} tinyint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''}`;
|
|
1155
1179
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1156
1180
|
break;
|
|
1157
1181
|
}
|
|
1158
|
-
case SqlType.
|
|
1159
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1160
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName}
|
|
1182
|
+
case SqlType.smallint: {
|
|
1183
|
+
field[DBType.Mysql] = `${field.esName} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''}`;
|
|
1184
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1161
1185
|
break;
|
|
1162
1186
|
}
|
|
1163
|
-
case SqlType.
|
|
1164
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1165
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName}
|
|
1187
|
+
case SqlType.mediumint: {
|
|
1188
|
+
field[DBType.Mysql] = `${field.esName} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''}`;
|
|
1189
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1166
1190
|
break;
|
|
1167
1191
|
}
|
|
1168
1192
|
case SqlType.int: {
|
|
@@ -1170,6 +1194,26 @@ export const Field = (config) => {
|
|
|
1170
1194
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1171
1195
|
break;
|
|
1172
1196
|
}
|
|
1197
|
+
case SqlType.bigint: {
|
|
1198
|
+
field[DBType.Mysql] = `${field.esName} bigint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1199
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1200
|
+
break;
|
|
1201
|
+
}
|
|
1202
|
+
case SqlType.float: {
|
|
1203
|
+
field[DBType.Mysql] = `${field.esName} float(${config.length1 ?? 1}, ${config.length2 ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1204
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1205
|
+
break;
|
|
1206
|
+
}
|
|
1207
|
+
case SqlType.double: {
|
|
1208
|
+
field[DBType.Mysql] = `${field.esName} double(${config.length1 ?? 1}, ${config.length2 ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1209
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1210
|
+
break;
|
|
1211
|
+
}
|
|
1212
|
+
case SqlType.decimal: {
|
|
1213
|
+
field[DBType.Mysql] = `${field.esName} decimal(${config.length1 ?? 1}, ${config.length2 ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1214
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1215
|
+
break;
|
|
1216
|
+
}
|
|
1173
1217
|
case SqlType.longtext: {
|
|
1174
1218
|
field[DBType.Mysql] = `${field.esName} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1175
1219
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
@@ -1180,18 +1224,38 @@ export const Field = (config) => {
|
|
|
1180
1224
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1181
1225
|
break;
|
|
1182
1226
|
}
|
|
1183
|
-
case SqlType.
|
|
1184
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1227
|
+
case SqlType.text: {
|
|
1228
|
+
field[DBType.Mysql] = `${field.esName} text ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1185
1229
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1186
1230
|
break;
|
|
1187
1231
|
}
|
|
1188
|
-
case SqlType.
|
|
1189
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1232
|
+
case SqlType.date: {
|
|
1233
|
+
field[DBType.Mysql] = `${field.esName} date ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1190
1234
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1191
1235
|
break;
|
|
1192
1236
|
}
|
|
1193
|
-
case SqlType.
|
|
1194
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1237
|
+
case SqlType.time: {
|
|
1238
|
+
field[DBType.Mysql] = `${field.esName} time ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1239
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1240
|
+
break;
|
|
1241
|
+
}
|
|
1242
|
+
case SqlType.year: {
|
|
1243
|
+
field[DBType.Mysql] = `${field.esName} year ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1244
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1245
|
+
break;
|
|
1246
|
+
}
|
|
1247
|
+
case SqlType.datetime: {
|
|
1248
|
+
field[DBType.Mysql] = `${field.esName} datetime ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1249
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1250
|
+
break;
|
|
1251
|
+
}
|
|
1252
|
+
case SqlType.timestamp: {
|
|
1253
|
+
field[DBType.Mysql] = `${field.esName} timestamp ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1254
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1255
|
+
break;
|
|
1256
|
+
}
|
|
1257
|
+
case SqlType.char: {
|
|
1258
|
+
field[DBType.Mysql] = `${field.esName} char(${config.length1 ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1195
1259
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1196
1260
|
break;
|
|
1197
1261
|
}
|
|
@@ -1200,6 +1264,101 @@ export const Field = (config) => {
|
|
|
1200
1264
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1201
1265
|
break;
|
|
1202
1266
|
}
|
|
1267
|
+
case SqlType.tinyblob: {
|
|
1268
|
+
field[DBType.Mysql] = `${field.esName} tinyblob ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1269
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1270
|
+
break;
|
|
1271
|
+
}
|
|
1272
|
+
case SqlType.tinytext: {
|
|
1273
|
+
field[DBType.Mysql] = `${field.esName} tinytext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1274
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1275
|
+
break;
|
|
1276
|
+
}
|
|
1277
|
+
case SqlType.blob: {
|
|
1278
|
+
field[DBType.Mysql] = `${field.esName} binary ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1279
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1280
|
+
break;
|
|
1281
|
+
}
|
|
1282
|
+
case SqlType.text: {
|
|
1283
|
+
field[DBType.Mysql] = `${field.esName} text ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1284
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1285
|
+
break;
|
|
1286
|
+
}
|
|
1287
|
+
case SqlType.mediumblob: {
|
|
1288
|
+
field[DBType.Mysql] = `${field.esName} mediumblob ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1289
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1290
|
+
break;
|
|
1291
|
+
}
|
|
1292
|
+
case SqlType.mediumtext: {
|
|
1293
|
+
field[DBType.Mysql] = `${field.esName} mediumtext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1294
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1295
|
+
break;
|
|
1296
|
+
}
|
|
1297
|
+
case SqlType.longblob: {
|
|
1298
|
+
field[DBType.Mysql] = `${field.esName} longblob ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1299
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1300
|
+
break;
|
|
1301
|
+
}
|
|
1302
|
+
case SqlType.longtext: {
|
|
1303
|
+
field[DBType.Mysql] = `${field.esName} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1304
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1305
|
+
break;
|
|
1306
|
+
}
|
|
1307
|
+
case SqlType.set: {
|
|
1308
|
+
field[DBType.Mysql] = `${field.esName} set ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1309
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1310
|
+
break;
|
|
1311
|
+
}
|
|
1312
|
+
case SqlType.enum: {
|
|
1313
|
+
field[DBType.Mysql] = `${field.esName} enum ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1314
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1315
|
+
break;
|
|
1316
|
+
}
|
|
1317
|
+
case SqlType.json: {
|
|
1318
|
+
field[DBType.Mysql] = `${field.esName} json ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1319
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1320
|
+
break;
|
|
1321
|
+
}
|
|
1322
|
+
case SqlType.geometry: {
|
|
1323
|
+
field[DBType.Mysql] = `${field.esName} geometry ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1324
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1325
|
+
break;
|
|
1326
|
+
}
|
|
1327
|
+
case SqlType.point: {
|
|
1328
|
+
field[DBType.Mysql] = `${field.esName} point ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1329
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1330
|
+
break;
|
|
1331
|
+
}
|
|
1332
|
+
case SqlType.linestring: {
|
|
1333
|
+
field[DBType.Mysql] = `${field.esName} linestring ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1334
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1335
|
+
break;
|
|
1336
|
+
}
|
|
1337
|
+
case SqlType.polygon: {
|
|
1338
|
+
field[DBType.Mysql] = `${field.esName} polygon ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1339
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1340
|
+
break;
|
|
1341
|
+
}
|
|
1342
|
+
case SqlType.multipoint: {
|
|
1343
|
+
field[DBType.Mysql] = `${field.esName} multipoint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1344
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1345
|
+
break;
|
|
1346
|
+
}
|
|
1347
|
+
case SqlType.multilinestring: {
|
|
1348
|
+
field[DBType.Mysql] = `${field.esName} multilinestring ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1349
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1350
|
+
break;
|
|
1351
|
+
}
|
|
1352
|
+
case SqlType.multipolygon: {
|
|
1353
|
+
field[DBType.Mysql] = `${field.esName} multipolygon ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1354
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1355
|
+
break;
|
|
1356
|
+
}
|
|
1357
|
+
case SqlType.geometrycollection: {
|
|
1358
|
+
field[DBType.Mysql] = `${field.esName} geometrycollection ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1359
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1360
|
+
break;
|
|
1361
|
+
}
|
|
1203
1362
|
}
|
|
1204
1363
|
;
|
|
1205
1364
|
let __fields = Reflect.getMetadata(_fields, object);
|
|
@@ -1352,7 +1511,7 @@ export class SqlService {
|
|
|
1352
1511
|
const sqls = [];
|
|
1353
1512
|
const tableName = Sqlstring.escapeId(option.tableName);
|
|
1354
1513
|
switch (option?.mode) {
|
|
1355
|
-
case
|
|
1514
|
+
case InsertMode.InsertIfNotExists: {
|
|
1356
1515
|
const conditions = option.existConditionOtherThanIds || this[_ids];
|
|
1357
1516
|
Throw.if(!conditions, 'not found where condition for insertIfNotExists!');
|
|
1358
1517
|
Throw.if(conditions.length === 0, 'insertIfNotExists must have not null where!');
|
|
@@ -1391,7 +1550,7 @@ export class SqlService {
|
|
|
1391
1550
|
${selects};`;
|
|
1392
1551
|
sqls.push({ sql, params });
|
|
1393
1552
|
}
|
|
1394
|
-
case
|
|
1553
|
+
case InsertMode.Replace: {
|
|
1395
1554
|
const finalColumns = new Set();
|
|
1396
1555
|
const params = datas
|
|
1397
1556
|
.map(data => this[_transformer](data, { ...option, finalColumns, def: true }))
|
|
@@ -1419,7 +1578,7 @@ export class SqlService {
|
|
|
1419
1578
|
`;
|
|
1420
1579
|
sqls.push({ sql, params });
|
|
1421
1580
|
}
|
|
1422
|
-
case
|
|
1581
|
+
case InsertMode.Insert: {
|
|
1423
1582
|
const finalColumns = new Set();
|
|
1424
1583
|
const params = datas
|
|
1425
1584
|
.map(data => this[_transformer](data, { ...option, finalColumns, def: true }))
|
|
@@ -1447,7 +1606,7 @@ export class SqlService {
|
|
|
1447
1606
|
`;
|
|
1448
1607
|
sqls.push({ sql, params });
|
|
1449
1608
|
}
|
|
1450
|
-
case
|
|
1609
|
+
case InsertMode.InsertWithTempTable: {
|
|
1451
1610
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1452
1611
|
const tableTempESC = Sqlstring.escapeId(tableTemp);
|
|
1453
1612
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
@@ -1490,16 +1649,16 @@ export class SqlService {
|
|
|
1490
1649
|
return sqls;
|
|
1491
1650
|
}
|
|
1492
1651
|
insert(option) {
|
|
1493
|
-
option.mode ?? (option.mode =
|
|
1652
|
+
option.mode ?? (option.mode = InsertMode.Insert);
|
|
1494
1653
|
const isArray = option.data instanceof Array;
|
|
1495
1654
|
const datas = option.data instanceof Array ? option.data : [option.data];
|
|
1496
|
-
if (option.sync ===
|
|
1655
|
+
if (option.sync === SyncMode.Sync) {
|
|
1497
1656
|
const fn = () => {
|
|
1498
1657
|
const result = excuteSplit(ExcuteSplitMode.SyncTrust, datas, _data => {
|
|
1499
1658
|
const sqls = this._insert(_data, option);
|
|
1500
1659
|
let result = 0n;
|
|
1501
1660
|
for (const { sql, params } of sqls) {
|
|
1502
|
-
const dd = option.conn.execute(
|
|
1661
|
+
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1503
1662
|
if (dd.insertId) {
|
|
1504
1663
|
result += dd.insertId;
|
|
1505
1664
|
}
|
|
@@ -1515,17 +1674,17 @@ export class SqlService {
|
|
|
1515
1674
|
return fn();
|
|
1516
1675
|
}
|
|
1517
1676
|
else {
|
|
1518
|
-
return option?.dao?.transaction(
|
|
1677
|
+
return option?.dao?.transaction(SyncMode.Sync, fn, option?.conn);
|
|
1519
1678
|
}
|
|
1520
1679
|
}
|
|
1521
1680
|
else if (isArray) {
|
|
1522
1681
|
const fn = async () => {
|
|
1523
|
-
return await option?.dao?.transaction(
|
|
1682
|
+
return await option?.dao?.transaction(SyncMode.Async, async () => {
|
|
1524
1683
|
const result = await excuteSplit(ExcuteSplitMode.AsyncTrust, datas, async (_data) => {
|
|
1525
1684
|
const sqls = this._insert(_data, option);
|
|
1526
1685
|
let result = 0n;
|
|
1527
1686
|
for (const { sql, params } of sqls) {
|
|
1528
|
-
const dd = await option?.conn.execute(
|
|
1687
|
+
const dd = await option?.conn.execute(SyncMode.Async, sql, params);
|
|
1529
1688
|
if (dd.insertId) {
|
|
1530
1689
|
result += dd.insertId;
|
|
1531
1690
|
}
|
|
@@ -1540,7 +1699,7 @@ export class SqlService {
|
|
|
1540
1699
|
resolve((await fn()));
|
|
1541
1700
|
}
|
|
1542
1701
|
else {
|
|
1543
|
-
await option?.dao?.transaction(
|
|
1702
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1544
1703
|
}
|
|
1545
1704
|
});
|
|
1546
1705
|
}
|
|
@@ -1550,7 +1709,7 @@ export class SqlService {
|
|
|
1550
1709
|
const sqls = this._insert(_data, option);
|
|
1551
1710
|
let result = 0n;
|
|
1552
1711
|
for (const { sql, params } of sqls) {
|
|
1553
|
-
const dd = await option.conn.execute(
|
|
1712
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1554
1713
|
if (dd.insertId) {
|
|
1555
1714
|
result += dd.insertId;
|
|
1556
1715
|
}
|
|
@@ -1564,7 +1723,7 @@ export class SqlService {
|
|
|
1564
1723
|
resolve((await fn()));
|
|
1565
1724
|
}
|
|
1566
1725
|
else {
|
|
1567
|
-
await option?.dao?.transaction(
|
|
1726
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1568
1727
|
}
|
|
1569
1728
|
});
|
|
1570
1729
|
}
|
|
@@ -1605,13 +1764,13 @@ export class SqlService {
|
|
|
1605
1764
|
update(option) {
|
|
1606
1765
|
Throw.if(!this[_ids] || this[_ids].length === 0, 'not found id');
|
|
1607
1766
|
const datas = option.data instanceof Array ? option.data : [option.data];
|
|
1608
|
-
if (option.sync ===
|
|
1767
|
+
if (option.sync === SyncMode.Sync) {
|
|
1609
1768
|
const fn = () => {
|
|
1610
1769
|
const result = excuteSplit(ExcuteSplitMode.SyncTrust, datas, _data => {
|
|
1611
1770
|
const sqls = this._update(_data, option);
|
|
1612
1771
|
let result = 0;
|
|
1613
1772
|
for (const { sql, params } of sqls) {
|
|
1614
|
-
const dd = option.conn.execute(
|
|
1773
|
+
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1615
1774
|
if (dd.affectedRows) {
|
|
1616
1775
|
result += dd.affectedRows;
|
|
1617
1776
|
}
|
|
@@ -1624,7 +1783,7 @@ export class SqlService {
|
|
|
1624
1783
|
return fn();
|
|
1625
1784
|
}
|
|
1626
1785
|
else {
|
|
1627
|
-
return option?.dao?.transaction(
|
|
1786
|
+
return option?.dao?.transaction(SyncMode.Sync, fn, option?.conn);
|
|
1628
1787
|
}
|
|
1629
1788
|
}
|
|
1630
1789
|
else {
|
|
@@ -1633,7 +1792,7 @@ export class SqlService {
|
|
|
1633
1792
|
const sqls = this._update(_data, option);
|
|
1634
1793
|
let result = 0;
|
|
1635
1794
|
for (const { sql, params } of sqls) {
|
|
1636
|
-
const dd = await option.conn.execute(
|
|
1795
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1637
1796
|
if (dd.affectedRows) {
|
|
1638
1797
|
result += dd.affectedRows;
|
|
1639
1798
|
}
|
|
@@ -1647,7 +1806,7 @@ export class SqlService {
|
|
|
1647
1806
|
resolve((await fn()));
|
|
1648
1807
|
}
|
|
1649
1808
|
else {
|
|
1650
|
-
await option?.dao?.transaction(
|
|
1809
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1651
1810
|
}
|
|
1652
1811
|
});
|
|
1653
1812
|
}
|
|
@@ -1658,7 +1817,7 @@ export class SqlService {
|
|
|
1658
1817
|
Throw.if(!option.id && !option.where, 'not found id or where!');
|
|
1659
1818
|
Throw.if(!!option.id && !!this[_ids] && this[_ids].length > 1, 'muit id must set where!');
|
|
1660
1819
|
Throw.if(!!option.id && !!option.where, 'id and where only one can set!');
|
|
1661
|
-
option.mode ?? (option.mode =
|
|
1820
|
+
option.mode ?? (option.mode = DeleteMode.Common);
|
|
1662
1821
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1663
1822
|
const tableTempESC = Sqlstring.escapeId(tableTemp);
|
|
1664
1823
|
const tableNameESC = Sqlstring.escapeId(option?.tableName);
|
|
@@ -1669,7 +1828,7 @@ export class SqlService {
|
|
|
1669
1828
|
}
|
|
1670
1829
|
const wheres = option.where instanceof Array ? option.where : [option.where];
|
|
1671
1830
|
const sqls = [];
|
|
1672
|
-
if (option.mode ===
|
|
1831
|
+
if (option.mode === DeleteMode.Common) {
|
|
1673
1832
|
const params = new Array();
|
|
1674
1833
|
const whereSql = iterare(wheres).map(where => {
|
|
1675
1834
|
return `(
|
|
@@ -1726,11 +1885,11 @@ export class SqlService {
|
|
|
1726
1885
|
}
|
|
1727
1886
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
1728
1887
|
}
|
|
1729
|
-
if (option.sync ===
|
|
1888
|
+
if (option.sync === SyncMode.Sync) {
|
|
1730
1889
|
const fn = () => {
|
|
1731
1890
|
let result = 0;
|
|
1732
1891
|
for (const { sql, params } of sqls) {
|
|
1733
|
-
const dd = option.conn.execute(
|
|
1892
|
+
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1734
1893
|
result += dd.affectedRows;
|
|
1735
1894
|
}
|
|
1736
1895
|
return result;
|
|
@@ -1739,14 +1898,14 @@ export class SqlService {
|
|
|
1739
1898
|
return fn();
|
|
1740
1899
|
}
|
|
1741
1900
|
else {
|
|
1742
|
-
return option?.dao?.transaction(
|
|
1901
|
+
return option?.dao?.transaction(SyncMode.Sync, fn, option?.conn);
|
|
1743
1902
|
}
|
|
1744
1903
|
}
|
|
1745
1904
|
else {
|
|
1746
1905
|
const fn = async () => {
|
|
1747
1906
|
let result = 0;
|
|
1748
1907
|
for (const { sql, params } of sqls) {
|
|
1749
|
-
const dd = await option.conn.execute(
|
|
1908
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1750
1909
|
result += dd.affectedRows;
|
|
1751
1910
|
}
|
|
1752
1911
|
return result;
|
|
@@ -1756,25 +1915,25 @@ export class SqlService {
|
|
|
1756
1915
|
resolve((await fn()));
|
|
1757
1916
|
}
|
|
1758
1917
|
else {
|
|
1759
|
-
await option?.dao?.transaction(
|
|
1918
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1760
1919
|
}
|
|
1761
1920
|
});
|
|
1762
1921
|
}
|
|
1763
1922
|
}
|
|
1764
|
-
|
|
1765
|
-
switch (
|
|
1766
|
-
case
|
|
1923
|
+
_template(templateResult, result, error) {
|
|
1924
|
+
switch (templateResult) {
|
|
1925
|
+
case TemplateResult.AssertOne: {
|
|
1767
1926
|
Throw.if(!result || result.length !== 1, error);
|
|
1768
1927
|
return result[0];
|
|
1769
1928
|
}
|
|
1770
|
-
case
|
|
1929
|
+
case TemplateResult.NotSureOne: {
|
|
1771
1930
|
Throw.if(!result, error);
|
|
1772
1931
|
return result[0] ?? null;
|
|
1773
1932
|
}
|
|
1774
|
-
case
|
|
1933
|
+
case TemplateResult.Many: {
|
|
1775
1934
|
return result;
|
|
1776
1935
|
}
|
|
1777
|
-
case
|
|
1936
|
+
case TemplateResult.Count: {
|
|
1778
1937
|
return result[0].ct;
|
|
1779
1938
|
}
|
|
1780
1939
|
}
|
|
@@ -1785,8 +1944,8 @@ export class SqlService {
|
|
|
1785
1944
|
Throw.if(!option.id && !option.where, 'not found id or where!');
|
|
1786
1945
|
Throw.if(!!option.id && !!this[_ids] && this[_ids].length > 1, 'muit id must set where!');
|
|
1787
1946
|
Throw.if(!!option.id && !!option.where, 'id and where only one can set!');
|
|
1788
|
-
option.mode ?? (option.mode =
|
|
1789
|
-
option.
|
|
1947
|
+
option.mode ?? (option.mode = SelectMode.Common);
|
|
1948
|
+
option.templateResult ?? (option.templateResult = TemplateResult.AssertOne);
|
|
1790
1949
|
option.error ?? (option.error = 'error data!');
|
|
1791
1950
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1792
1951
|
const tableTempESC = Sqlstring.escapeId(tableTemp);
|
|
@@ -1796,11 +1955,11 @@ export class SqlService {
|
|
|
1796
1955
|
const ids = option.id instanceof Array ? option.id : [option.id];
|
|
1797
1956
|
option.where = ids.map(i => ({ [idName]: i }));
|
|
1798
1957
|
}
|
|
1799
|
-
const columns = option.
|
|
1958
|
+
const columns = option.templateResult === TemplateResult.Count ? 'COUNT(1) ct' : iterare((option.columns ?? this[_columns])).map((K) => `a.${this[_fields][K]?.esName}`).join(',');
|
|
1800
1959
|
const wheres = option.where instanceof Array ? option.where : [option.where];
|
|
1801
1960
|
const sqls = [];
|
|
1802
1961
|
let resultIndex = -1;
|
|
1803
|
-
if (option.mode ===
|
|
1962
|
+
if (option.mode === SelectMode.Common) {
|
|
1804
1963
|
const params = new Array();
|
|
1805
1964
|
const whereSql = iterare(wheres).map(where => {
|
|
1806
1965
|
return `SELECT ${columns} FROM ${tableNameESC} a WHERE
|
|
@@ -1821,79 +1980,79 @@ export class SqlService {
|
|
|
1821
1980
|
sqls.push({ sql: `SELECT ${columns} FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.esName} = b.${this[_fields][K]?.esName}`).join(' AND ')};` });
|
|
1822
1981
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
1823
1982
|
}
|
|
1824
|
-
if (option.sync ===
|
|
1983
|
+
if (option.sync === SyncMode.Sync) {
|
|
1825
1984
|
let result;
|
|
1826
1985
|
for (let i = 0; i < sqls.length; i++) {
|
|
1827
1986
|
if (i === resultIndex) {
|
|
1828
|
-
result = option.conn.query(
|
|
1987
|
+
result = option.conn.query(SyncMode.Sync, sqls[i]?.sql, sqls[i]?.params);
|
|
1829
1988
|
}
|
|
1830
1989
|
else {
|
|
1831
|
-
option.conn.execute(
|
|
1990
|
+
option.conn.execute(SyncMode.Sync, sqls[i]?.sql, sqls[i]?.params);
|
|
1832
1991
|
}
|
|
1833
1992
|
}
|
|
1834
|
-
return this.
|
|
1993
|
+
return this._template(option.templateResult, result, option.error);
|
|
1835
1994
|
}
|
|
1836
1995
|
else {
|
|
1837
1996
|
return new Promise(async (resolve) => {
|
|
1838
1997
|
let result;
|
|
1839
1998
|
for (let i = 0; i < sqls.length; i++) {
|
|
1840
1999
|
if (i === resultIndex) {
|
|
1841
|
-
result = await option.conn.query(
|
|
2000
|
+
result = await option.conn.query(SyncMode.Async, sqls[i]?.sql, sqls[i]?.params);
|
|
1842
2001
|
}
|
|
1843
2002
|
else {
|
|
1844
|
-
await option.conn.execute(
|
|
2003
|
+
await option.conn.execute(SyncMode.Async, sqls[i]?.sql, sqls[i]?.params);
|
|
1845
2004
|
}
|
|
1846
2005
|
}
|
|
1847
|
-
resolve(this.
|
|
2006
|
+
resolve(this._template(option.templateResult, result, option.error));
|
|
1848
2007
|
});
|
|
1849
2008
|
}
|
|
1850
2009
|
}
|
|
1851
|
-
|
|
2010
|
+
_select(templateResult, result, def, errorMsg, multiple) {
|
|
1852
2011
|
if (multiple === true) {
|
|
1853
|
-
switch (
|
|
1854
|
-
case
|
|
2012
|
+
switch (templateResult) {
|
|
2013
|
+
case SelectResult.One_Row_One_Column_NotSure: {
|
|
1855
2014
|
try {
|
|
1856
2015
|
return result.map((r) => Object.values(r)[0]);
|
|
1857
2016
|
}
|
|
1858
2017
|
catch (error) {
|
|
1859
2018
|
}
|
|
1860
2019
|
}
|
|
1861
|
-
case
|
|
2020
|
+
case SelectResult.One_Row_One_Column_Assert: {
|
|
1862
2021
|
try {
|
|
1863
2022
|
return iterare(result).map((r) => Object.values(r)[0]).filter((r) => r !== null).toArray();
|
|
1864
2023
|
}
|
|
1865
2024
|
catch (error) {
|
|
1866
2025
|
}
|
|
1867
2026
|
}
|
|
1868
|
-
case
|
|
2027
|
+
case SelectResult.One_Row_Many_Column_NotSure: {
|
|
1869
2028
|
try {
|
|
1870
2029
|
return result.map((r) => r[0]);
|
|
1871
2030
|
}
|
|
1872
2031
|
catch (error) {
|
|
1873
2032
|
}
|
|
1874
2033
|
}
|
|
1875
|
-
case
|
|
2034
|
+
case SelectResult.One_Row_Many_Column_Assert: {
|
|
1876
2035
|
try {
|
|
1877
2036
|
return iterare(result).map((r) => r[0]).filter((r) => r !== null).toArray();
|
|
1878
2037
|
}
|
|
1879
2038
|
catch (error) {
|
|
1880
2039
|
}
|
|
1881
2040
|
}
|
|
1882
|
-
case
|
|
2041
|
+
case SelectResult.Many_Row_One_Column: {
|
|
1883
2042
|
try {
|
|
1884
2043
|
return result.map((rx) => rx.map((r) => Object.values(r)[0]));
|
|
1885
2044
|
}
|
|
1886
2045
|
catch (error) {
|
|
1887
2046
|
}
|
|
1888
2047
|
}
|
|
1889
|
-
case
|
|
2048
|
+
case SelectResult.Many_Row_Many_Column: {
|
|
1890
2049
|
return result;
|
|
1891
2050
|
}
|
|
1892
2051
|
}
|
|
1893
2052
|
}
|
|
1894
2053
|
else {
|
|
1895
|
-
switch (
|
|
1896
|
-
case
|
|
2054
|
+
switch (templateResult) {
|
|
2055
|
+
case SelectResult.One_Row_One_Column_NotSure: {
|
|
1897
2056
|
try {
|
|
1898
2057
|
return Object.values(result[0])[0];
|
|
1899
2058
|
}
|
|
@@ -1901,7 +2060,7 @@ export class SqlService {
|
|
|
1901
2060
|
return def;
|
|
1902
2061
|
}
|
|
1903
2062
|
}
|
|
1904
|
-
case
|
|
2063
|
+
case SelectResult.One_Row_One_Column_Assert: {
|
|
1905
2064
|
try {
|
|
1906
2065
|
return Object.values(result[0])[0];
|
|
1907
2066
|
}
|
|
@@ -1911,15 +2070,15 @@ export class SqlService {
|
|
|
1911
2070
|
Throw.now(errorMsg ?? 'not found data!');
|
|
1912
2071
|
}
|
|
1913
2072
|
}
|
|
1914
|
-
case
|
|
2073
|
+
case SelectResult.One_Row_Many_Column_NotSure: {
|
|
1915
2074
|
return result[0] ?? null;
|
|
1916
2075
|
}
|
|
1917
|
-
case
|
|
2076
|
+
case SelectResult.One_Row_Many_Column_Assert: {
|
|
1918
2077
|
const data = result[0];
|
|
1919
2078
|
Throw.if(data === null, errorMsg ?? 'not found data!');
|
|
1920
2079
|
return data ?? null;
|
|
1921
2080
|
}
|
|
1922
|
-
case
|
|
2081
|
+
case SelectResult.Many_Row_One_Column: {
|
|
1923
2082
|
try {
|
|
1924
2083
|
return result.map((r) => Object.values(r)[0]);
|
|
1925
2084
|
}
|
|
@@ -1927,7 +2086,7 @@ export class SqlService {
|
|
|
1927
2086
|
return def;
|
|
1928
2087
|
}
|
|
1929
2088
|
}
|
|
1930
|
-
case
|
|
2089
|
+
case SelectResult.Many_Row_Many_Column: {
|
|
1931
2090
|
return result;
|
|
1932
2091
|
}
|
|
1933
2092
|
}
|
|
@@ -1935,7 +2094,7 @@ export class SqlService {
|
|
|
1935
2094
|
}
|
|
1936
2095
|
select(option) {
|
|
1937
2096
|
Throw.if(!option.sqlId && !option.sql, 'not found sql!');
|
|
1938
|
-
option.
|
|
2097
|
+
option.selectResult ?? (option.selectResult = SelectResult.Many_Row_Many_Column);
|
|
1939
2098
|
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, option.context, option.isPage));
|
|
1940
2099
|
option.defValue ?? (option.defValue = null);
|
|
1941
2100
|
logger.debug(option.sql);
|
|
@@ -1949,14 +2108,14 @@ export class SqlService {
|
|
|
1949
2108
|
}
|
|
1950
2109
|
return txt;
|
|
1951
2110
|
});
|
|
1952
|
-
if (option.sync ===
|
|
1953
|
-
const result = option.conn.query(
|
|
1954
|
-
return this.
|
|
2111
|
+
if (option.sync === SyncMode.Sync) {
|
|
2112
|
+
const result = option.conn.query(SyncMode.Sync, sql, params);
|
|
2113
|
+
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.multiple);
|
|
1955
2114
|
}
|
|
1956
2115
|
else {
|
|
1957
2116
|
return new Promise(async (resolve) => {
|
|
1958
|
-
const result = await option.conn.query(
|
|
1959
|
-
resolve(this.
|
|
2117
|
+
const result = await option.conn.query(SyncMode.Async, sql, params);
|
|
2118
|
+
resolve(this._select(option.selectResult, result, option.defValue, option.errorMsg, option.multiple));
|
|
1960
2119
|
});
|
|
1961
2120
|
}
|
|
1962
2121
|
}
|
|
@@ -1974,24 +2133,24 @@ export class SqlService {
|
|
|
1974
2133
|
}
|
|
1975
2134
|
return txt;
|
|
1976
2135
|
});
|
|
1977
|
-
if (option.sync ===
|
|
1978
|
-
const result = option.conn.execute(
|
|
2136
|
+
if (option.sync === SyncMode.Sync) {
|
|
2137
|
+
const result = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1979
2138
|
return result.affectedRows;
|
|
1980
2139
|
}
|
|
1981
2140
|
else {
|
|
1982
2141
|
return new Promise(async (resolve) => {
|
|
1983
|
-
const result = await option.conn.execute(
|
|
2142
|
+
const result = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1984
2143
|
resolve(result.affectedRows);
|
|
1985
2144
|
});
|
|
1986
2145
|
}
|
|
1987
2146
|
}
|
|
1988
2147
|
transaction(option) {
|
|
1989
|
-
if (option.sync ===
|
|
1990
|
-
return option.dao.transaction(
|
|
2148
|
+
if (option.sync === SyncMode.Sync) {
|
|
2149
|
+
return option.dao.transaction(SyncMode.Sync, option.fn);
|
|
1991
2150
|
}
|
|
1992
2151
|
else {
|
|
1993
2152
|
return new Promise(async (resolve) => {
|
|
1994
|
-
const rt = await option.dao.transaction(
|
|
2153
|
+
const rt = await option.dao.transaction(SyncMode.Async, option.fn);
|
|
1995
2154
|
resolve(rt);
|
|
1996
2155
|
});
|
|
1997
2156
|
}
|
|
@@ -2930,7 +3089,7 @@ class StreamQuery extends StreamBuild {
|
|
|
2930
3089
|
this._service = service;
|
|
2931
3090
|
}
|
|
2932
3091
|
select(option) {
|
|
2933
|
-
option.sync ?? (option.sync =
|
|
3092
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
2934
3093
|
const { where, params } = this.where();
|
|
2935
3094
|
const sql = `
|
|
2936
3095
|
SELECT
|
|
@@ -2938,29 +3097,29 @@ class StreamQuery extends StreamBuild {
|
|
|
2938
3097
|
FROM ${this._table}
|
|
2939
3098
|
${where ? ' WHERE ' : ''}
|
|
2940
3099
|
${where}`;
|
|
2941
|
-
if (option.sync ===
|
|
2942
|
-
switch (option.
|
|
2943
|
-
case
|
|
2944
|
-
case
|
|
2945
|
-
case
|
|
2946
|
-
case
|
|
2947
|
-
case
|
|
2948
|
-
case
|
|
3100
|
+
if (option.sync === SyncMode.Async) {
|
|
3101
|
+
switch (option.selectResult) {
|
|
3102
|
+
case SelectResult.Many_Row_Many_Column: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.Many_Row_Many_Column, errorMsg: option.errorMsg, sql, params });
|
|
3103
|
+
case SelectResult.Many_Row_One_Column: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.Many_Row_One_Column, errorMsg: option.errorMsg, sql, params });
|
|
3104
|
+
case SelectResult.One_Row_Many_Column_Assert: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.One_Row_Many_Column_Assert, errorMsg: option.errorMsg, sql, params });
|
|
3105
|
+
case SelectResult.One_Row_One_Column_Assert: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.One_Row_One_Column_Assert, errorMsg: option.errorMsg, sql, params });
|
|
3106
|
+
case SelectResult.One_Row_Many_Column_NotSure: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.One_Row_Many_Column_NotSure, errorMsg: option.errorMsg, sql, params });
|
|
3107
|
+
case SelectResult.One_Row_One_Column_NotSure: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.One_Row_One_Column_NotSure, errorMsg: option.errorMsg, sql, params });
|
|
2949
3108
|
}
|
|
2950
3109
|
}
|
|
2951
3110
|
else {
|
|
2952
|
-
switch (option.
|
|
2953
|
-
case
|
|
2954
|
-
case
|
|
2955
|
-
case
|
|
2956
|
-
case
|
|
2957
|
-
case
|
|
2958
|
-
case
|
|
3111
|
+
switch (option.selectResult) {
|
|
3112
|
+
case SelectResult.Many_Row_Many_Column: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.Many_Row_Many_Column, errorMsg: option.errorMsg, sql, params });
|
|
3113
|
+
case SelectResult.Many_Row_One_Column: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.Many_Row_One_Column, errorMsg: option.errorMsg, sql, params });
|
|
3114
|
+
case SelectResult.One_Row_Many_Column_Assert: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.One_Row_Many_Column_Assert, errorMsg: option.errorMsg, sql, params });
|
|
3115
|
+
case SelectResult.One_Row_One_Column_Assert: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.One_Row_One_Column_Assert, errorMsg: option.errorMsg, sql, params });
|
|
3116
|
+
case SelectResult.One_Row_Many_Column_NotSure: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.One_Row_Many_Column_NotSure, errorMsg: option.errorMsg, sql, params });
|
|
3117
|
+
case SelectResult.One_Row_One_Column_NotSure: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.One_Row_One_Column_NotSure, errorMsg: option.errorMsg, sql, params });
|
|
2959
3118
|
}
|
|
2960
3119
|
}
|
|
2961
3120
|
}
|
|
2962
3121
|
update(option) {
|
|
2963
|
-
option.sync ?? (option.sync =
|
|
3122
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
2964
3123
|
const { where, params } = this.where();
|
|
2965
3124
|
const sets = new Array(...this._updateColumns);
|
|
2966
3125
|
if (this._updates) {
|
|
@@ -2972,11 +3131,11 @@ class StreamQuery extends StreamBuild {
|
|
|
2972
3131
|
}
|
|
2973
3132
|
if (sets.length > 0) {
|
|
2974
3133
|
const sql = `UPDATE ${this._table} SET ${sets.join(',')} ${where}`;
|
|
2975
|
-
if (option.sync ===
|
|
2976
|
-
return this._service.excute({ sync:
|
|
3134
|
+
if (option.sync === SyncMode.Async) {
|
|
3135
|
+
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
2977
3136
|
}
|
|
2978
3137
|
else {
|
|
2979
|
-
return this._service.excute({ sync:
|
|
3138
|
+
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
2980
3139
|
}
|
|
2981
3140
|
}
|
|
2982
3141
|
else {
|
|
@@ -2984,14 +3143,14 @@ class StreamQuery extends StreamBuild {
|
|
|
2984
3143
|
}
|
|
2985
3144
|
}
|
|
2986
3145
|
delete(option) {
|
|
2987
|
-
option.sync ?? (option.sync =
|
|
3146
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
2988
3147
|
const { where, params } = this.where();
|
|
2989
3148
|
const sql = `DELETE FROM ${this._table} ${where}`;
|
|
2990
|
-
if (option.sync ===
|
|
2991
|
-
return this._service.excute({ sync:
|
|
3149
|
+
if (option.sync === SyncMode.Async) {
|
|
3150
|
+
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
2992
3151
|
}
|
|
2993
3152
|
else {
|
|
2994
|
-
return this._service.excute({ sync:
|
|
3153
|
+
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
2995
3154
|
}
|
|
2996
3155
|
}
|
|
2997
3156
|
}
|
|
@@ -3213,7 +3372,7 @@ export function MethodCache(config) {
|
|
|
3213
3372
|
};
|
|
3214
3373
|
};
|
|
3215
3374
|
}
|
|
3216
|
-
export const
|
|
3375
|
+
export const Boot = async function (options) {
|
|
3217
3376
|
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption, options);
|
|
3218
3377
|
if (options.sqlDir) {
|
|
3219
3378
|
globalThis[_path] = import('path');
|