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/cjs/sql.js
CHANGED
|
@@ -37,7 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
37
37
|
var _a, _b, _c;
|
|
38
38
|
var _d, _e, _f;
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
-
exports.
|
|
40
|
+
exports.Boot = exports.MethodCache = exports.excuteWithCache = exports.clearMethodCache = exports.MethodLock = exports.excuteWithLock = exports.GetRedisLock = exports.getRedisDB = exports.SqlService = exports.DeclareService = exports.DeclareClass = exports.DB = exports.Field = exports.SqliteMemory = exports.SqlType = exports.SelectResult = exports.TemplateResult = exports.SelectMode = exports.DeleteMode = exports.InsertMode = exports.SyncMode = exports.DBType = void 0;
|
|
41
41
|
require("reflect-metadata");
|
|
42
42
|
const error_1 = require("./error");
|
|
43
43
|
const tslib_1 = __importDefault(require("tslib"));
|
|
@@ -90,21 +90,21 @@ var DBType;
|
|
|
90
90
|
DBType[DBType["Redis"] = 4] = "Redis";
|
|
91
91
|
DBType[DBType["RedisLock"] = 5] = "RedisLock";
|
|
92
92
|
})(DBType || (exports.DBType = DBType = {}));
|
|
93
|
-
var
|
|
94
|
-
(function (
|
|
93
|
+
var SyncMode;
|
|
94
|
+
(function (SyncMode) {
|
|
95
95
|
/** 同步执行 */
|
|
96
|
-
|
|
96
|
+
SyncMode[SyncMode["Sync"] = 0] = "Sync";
|
|
97
97
|
/** 异步执行 */
|
|
98
|
-
|
|
99
|
-
})(
|
|
100
|
-
var
|
|
101
|
-
(function (
|
|
98
|
+
SyncMode[SyncMode["Async"] = 1] = "Async";
|
|
99
|
+
})(SyncMode || (exports.SyncMode = SyncMode = {}));
|
|
100
|
+
var InsertMode;
|
|
101
|
+
(function (InsertMode) {
|
|
102
102
|
/**
|
|
103
103
|
# 默认使用
|
|
104
104
|
** 支持单个、批量,语法 `INSERT INTO XX VALUES (第一条数据), (第二条数据);`
|
|
105
105
|
** 批量执行有性能优势,但无法利用数据库的sql预编译功能
|
|
106
106
|
*/
|
|
107
|
-
|
|
107
|
+
InsertMode[InsertMode["Insert"] = 0] = "Insert";
|
|
108
108
|
/**
|
|
109
109
|
# 利用临时表
|
|
110
110
|
## 执行步骤
|
|
@@ -116,16 +116,16 @@ var SqlInsertMode;
|
|
|
116
116
|
1. 适用于:主键不会冲突、非自增
|
|
117
117
|
2. 临时表的结构复制正式表
|
|
118
118
|
*/
|
|
119
|
-
|
|
120
|
-
|
|
119
|
+
InsertMode[InsertMode["InsertWithTempTable"] = 1] = "InsertWithTempTable";
|
|
120
|
+
InsertMode[InsertMode["InsertIfNotExists"] = 2] = "InsertIfNotExists";
|
|
121
121
|
/**
|
|
122
122
|
# 插入或者更新
|
|
123
123
|
1. 判断依据是主键
|
|
124
124
|
*/
|
|
125
|
-
|
|
126
|
-
})(
|
|
127
|
-
var
|
|
128
|
-
(function (
|
|
125
|
+
InsertMode[InsertMode["Replace"] = 3] = "Replace";
|
|
126
|
+
})(InsertMode || (exports.InsertMode = InsertMode = {}));
|
|
127
|
+
var DeleteMode;
|
|
128
|
+
(function (DeleteMode) {
|
|
129
129
|
/**
|
|
130
130
|
##常规删除 默认
|
|
131
131
|
### 例一
|
|
@@ -133,16 +133,16 @@ var SqlDelMode;
|
|
|
133
133
|
### 例二
|
|
134
134
|
`DELETE FROM WHERE (id = 1 AND idx = 11) OR (id = 2 AND idx = 22)`
|
|
135
135
|
*/
|
|
136
|
-
|
|
136
|
+
DeleteMode[DeleteMode["Common"] = 0] = "Common";
|
|
137
137
|
/*
|
|
138
138
|
## 借助临时表
|
|
139
139
|
### 注意:必须保证where的字段都相同,否则会漏删数据
|
|
140
140
|
DELETE FROM 正式表 INNER JOIN 临时表 WHERE 字段1 = 字段1 AND 字段2 = 字段2
|
|
141
141
|
*/
|
|
142
|
-
|
|
143
|
-
})(
|
|
144
|
-
var
|
|
145
|
-
(function (
|
|
142
|
+
DeleteMode[DeleteMode["TempTable"] = 1] = "TempTable";
|
|
143
|
+
})(DeleteMode || (exports.DeleteMode = DeleteMode = {}));
|
|
144
|
+
var SelectMode;
|
|
145
|
+
(function (SelectMode) {
|
|
146
146
|
/**
|
|
147
147
|
##常规 默认
|
|
148
148
|
### 例一
|
|
@@ -150,46 +150,70 @@ var SqlSelectMode;
|
|
|
150
150
|
### 例二
|
|
151
151
|
`SELECT * FROM WHERE (id = 1 AND idx = 11) OR (id = 2 AND idx = 22)`
|
|
152
152
|
*/
|
|
153
|
-
|
|
153
|
+
SelectMode[SelectMode["Common"] = 0] = "Common";
|
|
154
154
|
/*
|
|
155
155
|
## 借助临时表
|
|
156
156
|
### 注意:必须保证where的字段都相同,否则会漏删数据
|
|
157
157
|
SELECT * FROM 正式表 INNER JOIN 临时表 WHERE 字段1 = 字段1 AND 字段2 = 字段2
|
|
158
158
|
*/
|
|
159
|
-
|
|
160
|
-
})(
|
|
161
|
-
var
|
|
162
|
-
(function (
|
|
163
|
-
/**
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
|
|
159
|
+
SelectMode[SelectMode["TempTable"] = 1] = "TempTable";
|
|
160
|
+
})(SelectMode || (exports.SelectMode = SelectMode = {}));
|
|
161
|
+
var TemplateResult;
|
|
162
|
+
(function (TemplateResult) {
|
|
163
|
+
/** 确定返回一条记录,如果不是一个,将报错,返回类型是T */
|
|
164
|
+
TemplateResult[TemplateResult["AssertOne"] = 0] = "AssertOne";
|
|
165
|
+
/** 可能返回一条记录,返回类型是T|null */
|
|
166
|
+
TemplateResult[TemplateResult["NotSureOne"] = 1] = "NotSureOne";
|
|
167
|
+
/** 返回多条记录 */
|
|
168
|
+
TemplateResult[TemplateResult["Many"] = 2] = "Many";
|
|
169
169
|
/** 仅查询记录数量 */
|
|
170
|
-
|
|
171
|
-
})(
|
|
172
|
-
var
|
|
173
|
-
(function (
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
})(
|
|
170
|
+
TemplateResult[TemplateResult["Count"] = 3] = "Count";
|
|
171
|
+
})(TemplateResult || (exports.TemplateResult = TemplateResult = {}));
|
|
172
|
+
var SelectResult;
|
|
173
|
+
(function (SelectResult) {
|
|
174
|
+
SelectResult[SelectResult["One_Row_One_Column_Assert"] = 0] = "One_Row_One_Column_Assert";
|
|
175
|
+
SelectResult[SelectResult["One_Row_One_Column_NotSure"] = 1] = "One_Row_One_Column_NotSure";
|
|
176
|
+
SelectResult[SelectResult["One_Row_Many_Column_Assert"] = 2] = "One_Row_Many_Column_Assert";
|
|
177
|
+
SelectResult[SelectResult["One_Row_Many_Column_NotSure"] = 3] = "One_Row_Many_Column_NotSure";
|
|
178
|
+
SelectResult[SelectResult["Many_Row_One_Column"] = 4] = "Many_Row_One_Column";
|
|
179
|
+
SelectResult[SelectResult["Many_Row_Many_Column"] = 5] = "Many_Row_Many_Column";
|
|
180
|
+
})(SelectResult || (exports.SelectResult = SelectResult = {}));
|
|
181
181
|
var SqlType;
|
|
182
182
|
(function (SqlType) {
|
|
183
|
-
SqlType[SqlType["
|
|
184
|
-
SqlType[SqlType["
|
|
185
|
-
SqlType[SqlType["
|
|
183
|
+
SqlType[SqlType["tinyint"] = 0] = "tinyint";
|
|
184
|
+
SqlType[SqlType["smallint"] = 1] = "smallint";
|
|
185
|
+
SqlType[SqlType["mediumint"] = 2] = "mediumint";
|
|
186
186
|
SqlType[SqlType["int"] = 3] = "int";
|
|
187
|
-
SqlType[SqlType["
|
|
188
|
-
SqlType[SqlType["
|
|
189
|
-
SqlType[SqlType["
|
|
190
|
-
SqlType[SqlType["
|
|
191
|
-
SqlType[SqlType["
|
|
192
|
-
SqlType[SqlType["
|
|
187
|
+
SqlType[SqlType["bigint"] = 4] = "bigint";
|
|
188
|
+
SqlType[SqlType["float"] = 5] = "float";
|
|
189
|
+
SqlType[SqlType["double"] = 6] = "double";
|
|
190
|
+
SqlType[SqlType["decimal"] = 7] = "decimal";
|
|
191
|
+
SqlType[SqlType["date"] = 8] = "date";
|
|
192
|
+
SqlType[SqlType["time"] = 9] = "time";
|
|
193
|
+
SqlType[SqlType["year"] = 10] = "year";
|
|
194
|
+
SqlType[SqlType["datetime"] = 11] = "datetime";
|
|
195
|
+
SqlType[SqlType["timestamp"] = 12] = "timestamp";
|
|
196
|
+
SqlType[SqlType["char"] = 13] = "char";
|
|
197
|
+
SqlType[SqlType["varchar"] = 14] = "varchar";
|
|
198
|
+
SqlType[SqlType["tinyblob"] = 15] = "tinyblob";
|
|
199
|
+
SqlType[SqlType["tinytext"] = 16] = "tinytext";
|
|
200
|
+
SqlType[SqlType["blob"] = 17] = "blob";
|
|
201
|
+
SqlType[SqlType["text"] = 18] = "text";
|
|
202
|
+
SqlType[SqlType["mediumblob"] = 19] = "mediumblob";
|
|
203
|
+
SqlType[SqlType["mediumtext"] = 20] = "mediumtext";
|
|
204
|
+
SqlType[SqlType["longblob"] = 21] = "longblob";
|
|
205
|
+
SqlType[SqlType["longtext"] = 22] = "longtext";
|
|
206
|
+
SqlType[SqlType["set"] = 23] = "set";
|
|
207
|
+
SqlType[SqlType["enum"] = 24] = "enum";
|
|
208
|
+
SqlType[SqlType["json"] = 25] = "json";
|
|
209
|
+
SqlType[SqlType["geometry"] = 26] = "geometry";
|
|
210
|
+
SqlType[SqlType["point"] = 27] = "point";
|
|
211
|
+
SqlType[SqlType["linestring"] = 28] = "linestring";
|
|
212
|
+
SqlType[SqlType["polygon"] = 29] = "polygon";
|
|
213
|
+
SqlType[SqlType["multipoint"] = 30] = "multipoint";
|
|
214
|
+
SqlType[SqlType["multilinestring"] = 31] = "multilinestring";
|
|
215
|
+
SqlType[SqlType["multipolygon"] = 32] = "multipolygon";
|
|
216
|
+
SqlType[SqlType["geometrycollection"] = 33] = "geometrycollection";
|
|
193
217
|
})(SqlType || (exports.SqlType = SqlType = {}));
|
|
194
218
|
exports.SqliteMemory = ':memory:';
|
|
195
219
|
const _defOption = {
|
|
@@ -210,7 +234,7 @@ class MysqlConnection {
|
|
|
210
234
|
return { affectedRows: 0, insertId: 0n };
|
|
211
235
|
}
|
|
212
236
|
;
|
|
213
|
-
if (sync ===
|
|
237
|
+
if (sync === SyncMode.Sync) {
|
|
214
238
|
logger.warn('MYSQL not suppouted sync mode');
|
|
215
239
|
return { affectedRows: 0, insertId: 0n };
|
|
216
240
|
}
|
|
@@ -243,7 +267,7 @@ class MysqlConnection {
|
|
|
243
267
|
return null;
|
|
244
268
|
}
|
|
245
269
|
;
|
|
246
|
-
if (sync ===
|
|
270
|
+
if (sync === SyncMode.Sync) {
|
|
247
271
|
logger.warn('MYSQL not suppouted sync mode');
|
|
248
272
|
return null;
|
|
249
273
|
}
|
|
@@ -279,7 +303,7 @@ class MysqlConnection {
|
|
|
279
303
|
return null;
|
|
280
304
|
}
|
|
281
305
|
;
|
|
282
|
-
if (sync ===
|
|
306
|
+
if (sync === SyncMode.Sync) {
|
|
283
307
|
logger.warn('MYSQL not suppouted sync mode');
|
|
284
308
|
return null;
|
|
285
309
|
}
|
|
@@ -313,7 +337,7 @@ class MysqlConnection {
|
|
|
313
337
|
return [];
|
|
314
338
|
}
|
|
315
339
|
;
|
|
316
|
-
if (sync ===
|
|
340
|
+
if (sync === SyncMode.Sync) {
|
|
317
341
|
logger.warn('MYSQL not suppouted sync mode');
|
|
318
342
|
return [];
|
|
319
343
|
}
|
|
@@ -347,7 +371,7 @@ class MysqlConnection {
|
|
|
347
371
|
return [];
|
|
348
372
|
}
|
|
349
373
|
;
|
|
350
|
-
if (sync ===
|
|
374
|
+
if (sync === SyncMode.Sync) {
|
|
351
375
|
logger.warn('MYSQL not suppouted sync mode');
|
|
352
376
|
return [];
|
|
353
377
|
}
|
|
@@ -374,7 +398,7 @@ class MysqlConnection {
|
|
|
374
398
|
});
|
|
375
399
|
}
|
|
376
400
|
realse(sync) {
|
|
377
|
-
if (sync ===
|
|
401
|
+
if (sync === SyncMode.Sync) {
|
|
378
402
|
try {
|
|
379
403
|
this[_daoConnection]?.release();
|
|
380
404
|
}
|
|
@@ -390,7 +414,7 @@ class Mysql {
|
|
|
390
414
|
this[_daoDB] = pool;
|
|
391
415
|
}
|
|
392
416
|
createConnection(sync) {
|
|
393
|
-
if (sync ===
|
|
417
|
+
if (sync === SyncMode.Sync) {
|
|
394
418
|
logger.error('MYSQL not suppouted sync mode');
|
|
395
419
|
return null;
|
|
396
420
|
}
|
|
@@ -402,7 +426,7 @@ class Mysql {
|
|
|
402
426
|
});
|
|
403
427
|
}
|
|
404
428
|
transaction(sync, fn, conn) {
|
|
405
|
-
if (sync ===
|
|
429
|
+
if (sync === SyncMode.Sync) {
|
|
406
430
|
logger.warn('MYSQL not suppouted sync mode');
|
|
407
431
|
return null;
|
|
408
432
|
}
|
|
@@ -411,7 +435,7 @@ class Mysql {
|
|
|
411
435
|
let needCommit = false;
|
|
412
436
|
let newConn = false;
|
|
413
437
|
if (!conn) {
|
|
414
|
-
conn = await this.createConnection(
|
|
438
|
+
conn = await this.createConnection(SyncMode.Async) ?? undefined;
|
|
415
439
|
newConn = true;
|
|
416
440
|
}
|
|
417
441
|
if (conn?.[_inTransaction] !== true) {
|
|
@@ -456,7 +480,7 @@ class Mysql {
|
|
|
456
480
|
});
|
|
457
481
|
}
|
|
458
482
|
close(sync) {
|
|
459
|
-
if (sync ===
|
|
483
|
+
if (sync === SyncMode.Sync) {
|
|
460
484
|
this[_daoDB]?.destroy();
|
|
461
485
|
}
|
|
462
486
|
;
|
|
@@ -480,7 +504,7 @@ class SqliteConnection {
|
|
|
480
504
|
return { affectedRows: 0, insertId: 0n };
|
|
481
505
|
}
|
|
482
506
|
;
|
|
483
|
-
if (sync ===
|
|
507
|
+
if (sync === SyncMode.Async) {
|
|
484
508
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
485
509
|
return { affectedRows: 0, insertId: 0n };
|
|
486
510
|
}
|
|
@@ -511,7 +535,7 @@ class SqliteConnection {
|
|
|
511
535
|
return null;
|
|
512
536
|
}
|
|
513
537
|
;
|
|
514
|
-
if (sync ===
|
|
538
|
+
if (sync === SyncMode.Async) {
|
|
515
539
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
516
540
|
return null;
|
|
517
541
|
}
|
|
@@ -537,7 +561,7 @@ class SqliteConnection {
|
|
|
537
561
|
return null;
|
|
538
562
|
}
|
|
539
563
|
;
|
|
540
|
-
if (sync ===
|
|
564
|
+
if (sync === SyncMode.Async) {
|
|
541
565
|
return null;
|
|
542
566
|
}
|
|
543
567
|
;
|
|
@@ -562,7 +586,7 @@ class SqliteConnection {
|
|
|
562
586
|
return [];
|
|
563
587
|
}
|
|
564
588
|
;
|
|
565
|
-
if (sync ===
|
|
589
|
+
if (sync === SyncMode.Async) {
|
|
566
590
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
567
591
|
return [];
|
|
568
592
|
}
|
|
@@ -588,7 +612,7 @@ class SqliteConnection {
|
|
|
588
612
|
return [];
|
|
589
613
|
}
|
|
590
614
|
;
|
|
591
|
-
if (sync ===
|
|
615
|
+
if (sync === SyncMode.Async) {
|
|
592
616
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
593
617
|
return [];
|
|
594
618
|
}
|
|
@@ -627,7 +651,7 @@ class Sqlite {
|
|
|
627
651
|
`);
|
|
628
652
|
}
|
|
629
653
|
createConnection(sync) {
|
|
630
|
-
if (sync ===
|
|
654
|
+
if (sync === SyncMode.Async) {
|
|
631
655
|
logger.error(`SQLITE not suppoted async mode`);
|
|
632
656
|
return null;
|
|
633
657
|
}
|
|
@@ -635,13 +659,13 @@ class Sqlite {
|
|
|
635
659
|
return new SqliteConnection(this[_daoDB]);
|
|
636
660
|
}
|
|
637
661
|
transaction(sync, fn, conn) {
|
|
638
|
-
if (sync ===
|
|
662
|
+
if (sync === SyncMode.Async) {
|
|
639
663
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
640
664
|
return null;
|
|
641
665
|
}
|
|
642
666
|
;
|
|
643
667
|
if (!conn) {
|
|
644
|
-
conn = this.createConnection(
|
|
668
|
+
conn = this.createConnection(SyncMode.Sync) ?? undefined;
|
|
645
669
|
}
|
|
646
670
|
if (conn[_inTransaction] !== true) {
|
|
647
671
|
return this[_daoDB].transaction(() => {
|
|
@@ -657,13 +681,13 @@ class Sqlite {
|
|
|
657
681
|
}
|
|
658
682
|
}
|
|
659
683
|
close(sync) {
|
|
660
|
-
if (sync ===
|
|
684
|
+
if (sync === SyncMode.Sync) {
|
|
661
685
|
this[_daoDB].close();
|
|
662
686
|
}
|
|
663
687
|
;
|
|
664
688
|
}
|
|
665
689
|
backup(sync, name) {
|
|
666
|
-
if (sync ===
|
|
690
|
+
if (sync === SyncMode.Sync) {
|
|
667
691
|
this[_daoDB].backup(name);
|
|
668
692
|
}
|
|
669
693
|
;
|
|
@@ -685,7 +709,7 @@ class SqliteRemoteConnection {
|
|
|
685
709
|
return { affectedRows: 0, insertId: 0n };
|
|
686
710
|
}
|
|
687
711
|
;
|
|
688
|
-
if (sync ===
|
|
712
|
+
if (sync === SyncMode.Sync) {
|
|
689
713
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
690
714
|
return { affectedRows: 0, insertId: 0n };
|
|
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 null;
|
|
744
768
|
}
|
|
745
769
|
;
|
|
746
|
-
if (sync ===
|
|
770
|
+
if (sync === SyncMode.Sync) {
|
|
747
771
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
748
772
|
return null;
|
|
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
|
}
|
|
@@ -801,7 +825,7 @@ class SqliteRemoteConnection {
|
|
|
801
825
|
return [];
|
|
802
826
|
}
|
|
803
827
|
;
|
|
804
|
-
if (sync ===
|
|
828
|
+
if (sync === SyncMode.Sync) {
|
|
805
829
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
806
830
|
return [];
|
|
807
831
|
}
|
|
@@ -834,7 +858,7 @@ class SqliteRemote {
|
|
|
834
858
|
this[_sqliteRemoteName] = name;
|
|
835
859
|
}
|
|
836
860
|
createConnection(sync) {
|
|
837
|
-
if (sync ===
|
|
861
|
+
if (sync === SyncMode.Sync) {
|
|
838
862
|
logger.error('SQLITEREMOTE not suppouted sync mode');
|
|
839
863
|
return null;
|
|
840
864
|
}
|
|
@@ -848,7 +872,7 @@ class SqliteRemote {
|
|
|
848
872
|
return null;
|
|
849
873
|
}
|
|
850
874
|
close(sync) {
|
|
851
|
-
if (sync ===
|
|
875
|
+
if (sync === SyncMode.Async) {
|
|
852
876
|
return new Promise(async () => {
|
|
853
877
|
await this[_daoConnection].close();
|
|
854
878
|
});
|
|
@@ -856,7 +880,7 @@ class SqliteRemote {
|
|
|
856
880
|
;
|
|
857
881
|
}
|
|
858
882
|
backup(sync, name) {
|
|
859
|
-
if (sync ===
|
|
883
|
+
if (sync === SyncMode.Async) {
|
|
860
884
|
return new Promise(async () => {
|
|
861
885
|
await this[_daoConnection].backup(this[_sqliteRemoteName], name);
|
|
862
886
|
});
|
|
@@ -864,7 +888,7 @@ class SqliteRemote {
|
|
|
864
888
|
;
|
|
865
889
|
}
|
|
866
890
|
remove(sync) {
|
|
867
|
-
if (sync ===
|
|
891
|
+
if (sync === SyncMode.Async) {
|
|
868
892
|
return new Promise(async () => {
|
|
869
893
|
await this[_daoConnection].remove();
|
|
870
894
|
});
|
|
@@ -872,7 +896,7 @@ class SqliteRemote {
|
|
|
872
896
|
;
|
|
873
897
|
}
|
|
874
898
|
restore(sync, name) {
|
|
875
|
-
if (sync ===
|
|
899
|
+
if (sync === SyncMode.Async) {
|
|
876
900
|
return new Promise(async () => {
|
|
877
901
|
await this[_daoConnection].restore(this[_sqliteRemoteName], name);
|
|
878
902
|
});
|
|
@@ -934,17 +958,17 @@ function P(skipConn = false) {
|
|
|
934
958
|
const startTime = +new Date();
|
|
935
959
|
// option
|
|
936
960
|
const option = args[0] = Object.assign({}, globalThis[_GlobalSqlOption], this[_SqlOption], args[0]);
|
|
937
|
-
option.sync ?? (option.sync =
|
|
961
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
938
962
|
const dbName = option?.dbName ?? this[_daoDBName] ?? _primaryDB;
|
|
939
963
|
option.dao = globalThis[_dao][this[_dbType]][dbName];
|
|
940
964
|
error_1.Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
|
|
941
965
|
option.tableName = option?.tableName ?? this[_tableName];
|
|
942
966
|
const tableES = sqlstring_1.default.escapeId(option.tableName);
|
|
943
967
|
if (this[_dbType] === DBType.Sqlite) {
|
|
944
|
-
error_1.Throw.if(option.sync ===
|
|
968
|
+
error_1.Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
|
|
945
969
|
// 连接共享
|
|
946
970
|
if (skipConn === false && !option.conn) {
|
|
947
|
-
option.conn = option.dao.createConnection(
|
|
971
|
+
option.conn = option.dao.createConnection(SyncMode.Sync);
|
|
948
972
|
}
|
|
949
973
|
else {
|
|
950
974
|
needRealseConn = false;
|
|
@@ -952,20 +976,20 @@ function P(skipConn = false) {
|
|
|
952
976
|
if (skipConn === false) {
|
|
953
977
|
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
954
978
|
// 检查表
|
|
955
|
-
const tableCheckResult = option.conn.pluck(
|
|
979
|
+
const tableCheckResult = option.conn.pluck(SyncMode.Sync, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
956
980
|
if (tableCheckResult) {
|
|
957
981
|
// 旧版本
|
|
958
|
-
const tableVersion = option.conn.pluck(
|
|
982
|
+
const tableVersion = option.conn.pluck(SyncMode.Sync, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
959
983
|
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
960
984
|
// 更新版本
|
|
961
|
-
const columns = (0, iterare_1.default)(option.conn.query(
|
|
985
|
+
const columns = (0, iterare_1.default)(option.conn.query(SyncMode.Sync, `PRAGMA table_info(${tableES})`))
|
|
962
986
|
.filter(c => this[_fields].hasOwnProperty(c.name))
|
|
963
987
|
.map(c => sqlstring_1.default.escapeId(c.name))
|
|
964
988
|
.join(',');
|
|
965
989
|
const rtable = sqlstring_1.default.escapeId(`${option.tableName}_${tableVersion.replace(/\./, '_')}`);
|
|
966
|
-
option.conn.execute(
|
|
967
|
-
option.conn.execute(
|
|
968
|
-
option.conn.execute(
|
|
990
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
991
|
+
option.conn.execute(SyncMode.Sync, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
992
|
+
option.conn.execute(SyncMode.Sync, `
|
|
969
993
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
970
994
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
971
995
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -973,22 +997,22 @@ function P(skipConn = false) {
|
|
|
973
997
|
`);
|
|
974
998
|
if (this[_index] && this[_index].length) {
|
|
975
999
|
for (const index of this[_index]) {
|
|
976
|
-
option.conn.execute(
|
|
1000
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
977
1001
|
}
|
|
978
1002
|
}
|
|
979
|
-
option.conn.execute(
|
|
980
|
-
option.conn.execute(
|
|
981
|
-
option.conn.execute(
|
|
1003
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1004
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1005
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
982
1006
|
// 更新完毕,保存版本号
|
|
983
|
-
option.conn.execute(
|
|
1007
|
+
option.conn.execute(SyncMode.Sync, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
984
1008
|
}
|
|
985
1009
|
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
986
|
-
option.conn.execute(
|
|
1010
|
+
option.conn.execute(SyncMode.Sync, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
987
1011
|
}
|
|
988
1012
|
}
|
|
989
1013
|
else { // 表不存在
|
|
990
1014
|
// 创建表
|
|
991
|
-
option.conn.execute(
|
|
1015
|
+
option.conn.execute(SyncMode.Sync, `
|
|
992
1016
|
CREATE TABLE IF NOT EXISTS ${tableES} (
|
|
993
1017
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
994
1018
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -997,10 +1021,10 @@ function P(skipConn = false) {
|
|
|
997
1021
|
`);
|
|
998
1022
|
if (this[_index] && this[_index].length) {
|
|
999
1023
|
for (const index of this[_index]) {
|
|
1000
|
-
option.conn.execute(
|
|
1024
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
1001
1025
|
}
|
|
1002
1026
|
}
|
|
1003
|
-
option.conn.execute(
|
|
1027
|
+
option.conn.execute(SyncMode.Sync, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1004
1028
|
}
|
|
1005
1029
|
}
|
|
1006
1030
|
try {
|
|
@@ -1019,7 +1043,7 @@ function P(skipConn = false) {
|
|
|
1019
1043
|
finally {
|
|
1020
1044
|
if (needRealseConn && option && option.conn) {
|
|
1021
1045
|
try {
|
|
1022
|
-
option.conn.realse(
|
|
1046
|
+
option.conn.realse(SyncMode.Sync);
|
|
1023
1047
|
}
|
|
1024
1048
|
catch (error) {
|
|
1025
1049
|
}
|
|
@@ -1027,11 +1051,11 @@ function P(skipConn = false) {
|
|
|
1027
1051
|
}
|
|
1028
1052
|
}
|
|
1029
1053
|
else if (this[_dbType] === DBType.SqliteRemote) {
|
|
1030
|
-
error_1.Throw.if(option.sync ===
|
|
1054
|
+
error_1.Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
|
|
1031
1055
|
return new Promise(async (resolve) => {
|
|
1032
1056
|
// 连接共享
|
|
1033
1057
|
if (skipConn === false && !option.conn) {
|
|
1034
|
-
(option).conn = await option.dao.createConnection(
|
|
1058
|
+
(option).conn = await option.dao.createConnection(SyncMode.Async);
|
|
1035
1059
|
}
|
|
1036
1060
|
else {
|
|
1037
1061
|
needRealseConn = false;
|
|
@@ -1039,20 +1063,20 @@ function P(skipConn = false) {
|
|
|
1039
1063
|
if (skipConn === false) {
|
|
1040
1064
|
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
1041
1065
|
// 检查表
|
|
1042
|
-
const tableCheckResult = await option.conn.pluck(
|
|
1066
|
+
const tableCheckResult = await option.conn.pluck(SyncMode.Async, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
1043
1067
|
if (tableCheckResult) {
|
|
1044
1068
|
// 旧版本
|
|
1045
|
-
const tableVersion = await option.conn.pluck(
|
|
1069
|
+
const tableVersion = await option.conn.pluck(SyncMode.Async, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
1046
1070
|
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
1047
1071
|
// 更新版本
|
|
1048
|
-
const columns = (0, iterare_1.default)(await option.conn.query(
|
|
1072
|
+
const columns = (0, iterare_1.default)(await option.conn.query(SyncMode.Async, `PRAGMA table_info(${tableES})`))
|
|
1049
1073
|
.filter(c => this[_fields].hasOwnProperty(c.name))
|
|
1050
1074
|
.map(c => sqlstring_1.default.escapeId(c.name))
|
|
1051
1075
|
.join(',');
|
|
1052
1076
|
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
1053
|
-
await option.conn.execute(
|
|
1054
|
-
await option.conn.execute(
|
|
1055
|
-
await option.conn.execute(
|
|
1077
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1078
|
+
await option.conn.execute(SyncMode.Async, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1079
|
+
await option.conn.execute(SyncMode.Async, `
|
|
1056
1080
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1057
1081
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1058
1082
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -1060,22 +1084,22 @@ function P(skipConn = false) {
|
|
|
1060
1084
|
`);
|
|
1061
1085
|
if (this[_index] && this[_index].length) {
|
|
1062
1086
|
for (const index of this[_index]) {
|
|
1063
|
-
await option.conn.execute(
|
|
1087
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
1064
1088
|
}
|
|
1065
1089
|
}
|
|
1066
|
-
await option.conn.execute(
|
|
1067
|
-
await option.conn.execute(
|
|
1068
|
-
await option.conn.execute(
|
|
1090
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1091
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1092
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1069
1093
|
// 更新完毕,保存版本号
|
|
1070
|
-
await option.conn.execute(
|
|
1094
|
+
await option.conn.execute(SyncMode.Async, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
1071
1095
|
}
|
|
1072
1096
|
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
1073
|
-
await option.conn.execute(
|
|
1097
|
+
await option.conn.execute(SyncMode.Async, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1074
1098
|
}
|
|
1075
1099
|
}
|
|
1076
1100
|
else { // 表不存在
|
|
1077
1101
|
// 创建表
|
|
1078
|
-
await option.conn.execute(
|
|
1102
|
+
await option.conn.execute(SyncMode.Async, `
|
|
1079
1103
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1080
1104
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1081
1105
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -1083,10 +1107,10 @@ function P(skipConn = false) {
|
|
|
1083
1107
|
`);
|
|
1084
1108
|
if (this[_index] && this[_index].length) {
|
|
1085
1109
|
for (const index of this[_index]) {
|
|
1086
|
-
await option.conn.execute(
|
|
1110
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${sqlstring_1.default.escapeId(option.tableName)} ("${index}");`);
|
|
1087
1111
|
}
|
|
1088
1112
|
}
|
|
1089
|
-
await option.conn.execute(
|
|
1113
|
+
await option.conn.execute(SyncMode.Async, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1090
1114
|
}
|
|
1091
1115
|
}
|
|
1092
1116
|
try {
|
|
@@ -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
|
}
|
|
@@ -1114,7 +1138,7 @@ function P(skipConn = false) {
|
|
|
1114
1138
|
try {
|
|
1115
1139
|
// 连接共享
|
|
1116
1140
|
if (skipConn === false && !option.conn) {
|
|
1117
|
-
(option).conn = await option.dao.createConnection(
|
|
1141
|
+
(option).conn = await option.dao.createConnection(SyncMode.Async);
|
|
1118
1142
|
}
|
|
1119
1143
|
else {
|
|
1120
1144
|
needRealseConn = false;
|
|
@@ -1130,7 +1154,7 @@ function P(skipConn = false) {
|
|
|
1130
1154
|
finally {
|
|
1131
1155
|
if (needRealseConn && option && option.conn) {
|
|
1132
1156
|
try {
|
|
1133
|
-
option.conn.realse(
|
|
1157
|
+
option.conn.realse(SyncMode.Sync);
|
|
1134
1158
|
}
|
|
1135
1159
|
catch (error) {
|
|
1136
1160
|
}
|
|
@@ -1179,19 +1203,19 @@ const Field = (config) => {
|
|
|
1179
1203
|
field.esName = sqlstring_1.default.escapeId(propertyName);
|
|
1180
1204
|
const hasDef = field.hasOwnProperty('def') === true;
|
|
1181
1205
|
switch (field.type) {
|
|
1182
|
-
case SqlType.
|
|
1183
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1206
|
+
case SqlType.tinyint: {
|
|
1207
|
+
field[DBType.Mysql] = `${field.esName} tinyint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''}`;
|
|
1184
1208
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1185
1209
|
break;
|
|
1186
1210
|
}
|
|
1187
|
-
case SqlType.
|
|
1188
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1189
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName}
|
|
1211
|
+
case SqlType.smallint: {
|
|
1212
|
+
field[DBType.Mysql] = `${field.esName} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''}`;
|
|
1213
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1190
1214
|
break;
|
|
1191
1215
|
}
|
|
1192
|
-
case SqlType.
|
|
1193
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1194
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName}
|
|
1216
|
+
case SqlType.mediumint: {
|
|
1217
|
+
field[DBType.Mysql] = `${field.esName} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''}`;
|
|
1218
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1195
1219
|
break;
|
|
1196
1220
|
}
|
|
1197
1221
|
case SqlType.int: {
|
|
@@ -1199,6 +1223,26 @@ const Field = (config) => {
|
|
|
1199
1223
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1200
1224
|
break;
|
|
1201
1225
|
}
|
|
1226
|
+
case SqlType.bigint: {
|
|
1227
|
+
field[DBType.Mysql] = `${field.esName} bigint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1228
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1229
|
+
break;
|
|
1230
|
+
}
|
|
1231
|
+
case SqlType.float: {
|
|
1232
|
+
field[DBType.Mysql] = `${field.esName} float(${config.length1 ?? 1}, ${config.length2 ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1233
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1234
|
+
break;
|
|
1235
|
+
}
|
|
1236
|
+
case SqlType.double: {
|
|
1237
|
+
field[DBType.Mysql] = `${field.esName} double(${config.length1 ?? 1}, ${config.length2 ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1238
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1239
|
+
break;
|
|
1240
|
+
}
|
|
1241
|
+
case SqlType.decimal: {
|
|
1242
|
+
field[DBType.Mysql] = `${field.esName} decimal(${config.length1 ?? 1}, ${config.length2 ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1243
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1244
|
+
break;
|
|
1245
|
+
}
|
|
1202
1246
|
case SqlType.longtext: {
|
|
1203
1247
|
field[DBType.Mysql] = `${field.esName} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1204
1248
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
@@ -1209,18 +1253,38 @@ const Field = (config) => {
|
|
|
1209
1253
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1210
1254
|
break;
|
|
1211
1255
|
}
|
|
1212
|
-
case SqlType.
|
|
1213
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1256
|
+
case SqlType.text: {
|
|
1257
|
+
field[DBType.Mysql] = `${field.esName} text ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1214
1258
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1215
1259
|
break;
|
|
1216
1260
|
}
|
|
1217
|
-
case SqlType.
|
|
1218
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1261
|
+
case SqlType.date: {
|
|
1262
|
+
field[DBType.Mysql] = `${field.esName} date ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1219
1263
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1220
1264
|
break;
|
|
1221
1265
|
}
|
|
1222
|
-
case SqlType.
|
|
1223
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1266
|
+
case SqlType.time: {
|
|
1267
|
+
field[DBType.Mysql] = `${field.esName} time ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1268
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1269
|
+
break;
|
|
1270
|
+
}
|
|
1271
|
+
case SqlType.year: {
|
|
1272
|
+
field[DBType.Mysql] = `${field.esName} year ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1273
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1274
|
+
break;
|
|
1275
|
+
}
|
|
1276
|
+
case SqlType.datetime: {
|
|
1277
|
+
field[DBType.Mysql] = `${field.esName} datetime ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1278
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1279
|
+
break;
|
|
1280
|
+
}
|
|
1281
|
+
case SqlType.timestamp: {
|
|
1282
|
+
field[DBType.Mysql] = `${field.esName} timestamp ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1283
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1284
|
+
break;
|
|
1285
|
+
}
|
|
1286
|
+
case SqlType.char: {
|
|
1287
|
+
field[DBType.Mysql] = `${field.esName} char(${config.length1 ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1224
1288
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1225
1289
|
break;
|
|
1226
1290
|
}
|
|
@@ -1229,6 +1293,101 @@ const Field = (config) => {
|
|
|
1229
1293
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1230
1294
|
break;
|
|
1231
1295
|
}
|
|
1296
|
+
case SqlType.tinyblob: {
|
|
1297
|
+
field[DBType.Mysql] = `${field.esName} tinyblob ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1298
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1299
|
+
break;
|
|
1300
|
+
}
|
|
1301
|
+
case SqlType.tinytext: {
|
|
1302
|
+
field[DBType.Mysql] = `${field.esName} tinytext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1303
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1304
|
+
break;
|
|
1305
|
+
}
|
|
1306
|
+
case SqlType.blob: {
|
|
1307
|
+
field[DBType.Mysql] = `${field.esName} binary ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1308
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1309
|
+
break;
|
|
1310
|
+
}
|
|
1311
|
+
case SqlType.text: {
|
|
1312
|
+
field[DBType.Mysql] = `${field.esName} text ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1313
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1314
|
+
break;
|
|
1315
|
+
}
|
|
1316
|
+
case SqlType.mediumblob: {
|
|
1317
|
+
field[DBType.Mysql] = `${field.esName} mediumblob ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1318
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1319
|
+
break;
|
|
1320
|
+
}
|
|
1321
|
+
case SqlType.mediumtext: {
|
|
1322
|
+
field[DBType.Mysql] = `${field.esName} mediumtext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1323
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1324
|
+
break;
|
|
1325
|
+
}
|
|
1326
|
+
case SqlType.longblob: {
|
|
1327
|
+
field[DBType.Mysql] = `${field.esName} longblob ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1328
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1329
|
+
break;
|
|
1330
|
+
}
|
|
1331
|
+
case SqlType.longtext: {
|
|
1332
|
+
field[DBType.Mysql] = `${field.esName} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1333
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1334
|
+
break;
|
|
1335
|
+
}
|
|
1336
|
+
case SqlType.set: {
|
|
1337
|
+
field[DBType.Mysql] = `${field.esName} set ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1338
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1339
|
+
break;
|
|
1340
|
+
}
|
|
1341
|
+
case SqlType.enum: {
|
|
1342
|
+
field[DBType.Mysql] = `${field.esName} enum ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1343
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1344
|
+
break;
|
|
1345
|
+
}
|
|
1346
|
+
case SqlType.json: {
|
|
1347
|
+
field[DBType.Mysql] = `${field.esName} json ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1348
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1349
|
+
break;
|
|
1350
|
+
}
|
|
1351
|
+
case SqlType.geometry: {
|
|
1352
|
+
field[DBType.Mysql] = `${field.esName} geometry ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1353
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1354
|
+
break;
|
|
1355
|
+
}
|
|
1356
|
+
case SqlType.point: {
|
|
1357
|
+
field[DBType.Mysql] = `${field.esName} point ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1358
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1359
|
+
break;
|
|
1360
|
+
}
|
|
1361
|
+
case SqlType.linestring: {
|
|
1362
|
+
field[DBType.Mysql] = `${field.esName} linestring ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1363
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1364
|
+
break;
|
|
1365
|
+
}
|
|
1366
|
+
case SqlType.polygon: {
|
|
1367
|
+
field[DBType.Mysql] = `${field.esName} polygon ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1368
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1369
|
+
break;
|
|
1370
|
+
}
|
|
1371
|
+
case SqlType.multipoint: {
|
|
1372
|
+
field[DBType.Mysql] = `${field.esName} multipoint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1373
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1374
|
+
break;
|
|
1375
|
+
}
|
|
1376
|
+
case SqlType.multilinestring: {
|
|
1377
|
+
field[DBType.Mysql] = `${field.esName} multilinestring ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1378
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1379
|
+
break;
|
|
1380
|
+
}
|
|
1381
|
+
case SqlType.multipolygon: {
|
|
1382
|
+
field[DBType.Mysql] = `${field.esName} multipolygon ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1383
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1384
|
+
break;
|
|
1385
|
+
}
|
|
1386
|
+
case SqlType.geometrycollection: {
|
|
1387
|
+
field[DBType.Mysql] = `${field.esName} geometrycollection ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1388
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1389
|
+
break;
|
|
1390
|
+
}
|
|
1232
1391
|
}
|
|
1233
1392
|
;
|
|
1234
1393
|
let __fields = Reflect.getMetadata(_fields, object);
|
|
@@ -1385,7 +1544,7 @@ class SqlService {
|
|
|
1385
1544
|
const sqls = [];
|
|
1386
1545
|
const tableName = sqlstring_1.default.escapeId(option.tableName);
|
|
1387
1546
|
switch (option?.mode) {
|
|
1388
|
-
case
|
|
1547
|
+
case InsertMode.InsertIfNotExists: {
|
|
1389
1548
|
const conditions = option.existConditionOtherThanIds || this[_ids];
|
|
1390
1549
|
error_1.Throw.if(!conditions, 'not found where condition for insertIfNotExists!');
|
|
1391
1550
|
error_1.Throw.if(conditions.length === 0, 'insertIfNotExists must have not null where!');
|
|
@@ -1424,7 +1583,7 @@ class SqlService {
|
|
|
1424
1583
|
${selects};`;
|
|
1425
1584
|
sqls.push({ sql, params });
|
|
1426
1585
|
}
|
|
1427
|
-
case
|
|
1586
|
+
case InsertMode.Replace: {
|
|
1428
1587
|
const finalColumns = new Set();
|
|
1429
1588
|
const params = datas
|
|
1430
1589
|
.map(data => this[_transformer](data, { ...option, finalColumns, def: true }))
|
|
@@ -1452,7 +1611,7 @@ class SqlService {
|
|
|
1452
1611
|
`;
|
|
1453
1612
|
sqls.push({ sql, params });
|
|
1454
1613
|
}
|
|
1455
|
-
case
|
|
1614
|
+
case InsertMode.Insert: {
|
|
1456
1615
|
const finalColumns = new Set();
|
|
1457
1616
|
const params = datas
|
|
1458
1617
|
.map(data => this[_transformer](data, { ...option, finalColumns, def: true }))
|
|
@@ -1480,7 +1639,7 @@ class SqlService {
|
|
|
1480
1639
|
`;
|
|
1481
1640
|
sqls.push({ sql, params });
|
|
1482
1641
|
}
|
|
1483
|
-
case
|
|
1642
|
+
case InsertMode.InsertWithTempTable: {
|
|
1484
1643
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1485
1644
|
const tableTempESC = sqlstring_1.default.escapeId(tableTemp);
|
|
1486
1645
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
@@ -1523,16 +1682,16 @@ class SqlService {
|
|
|
1523
1682
|
return sqls;
|
|
1524
1683
|
}
|
|
1525
1684
|
insert(option) {
|
|
1526
|
-
option.mode ?? (option.mode =
|
|
1685
|
+
option.mode ?? (option.mode = InsertMode.Insert);
|
|
1527
1686
|
const isArray = option.data instanceof Array;
|
|
1528
1687
|
const datas = option.data instanceof Array ? option.data : [option.data];
|
|
1529
|
-
if (option.sync ===
|
|
1688
|
+
if (option.sync === SyncMode.Sync) {
|
|
1530
1689
|
const fn = () => {
|
|
1531
1690
|
const result = (0, fn_1.excuteSplit)(fn_1.ExcuteSplitMode.SyncTrust, datas, _data => {
|
|
1532
1691
|
const sqls = this._insert(_data, option);
|
|
1533
1692
|
let result = 0n;
|
|
1534
1693
|
for (const { sql, params } of sqls) {
|
|
1535
|
-
const dd = option.conn.execute(
|
|
1694
|
+
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1536
1695
|
if (dd.insertId) {
|
|
1537
1696
|
result += dd.insertId;
|
|
1538
1697
|
}
|
|
@@ -1548,17 +1707,17 @@ class SqlService {
|
|
|
1548
1707
|
return fn();
|
|
1549
1708
|
}
|
|
1550
1709
|
else {
|
|
1551
|
-
return option?.dao?.transaction(
|
|
1710
|
+
return option?.dao?.transaction(SyncMode.Sync, fn, option?.conn);
|
|
1552
1711
|
}
|
|
1553
1712
|
}
|
|
1554
1713
|
else if (isArray) {
|
|
1555
1714
|
const fn = async () => {
|
|
1556
|
-
return await option?.dao?.transaction(
|
|
1715
|
+
return await option?.dao?.transaction(SyncMode.Async, async () => {
|
|
1557
1716
|
const result = await (0, fn_1.excuteSplit)(fn_1.ExcuteSplitMode.AsyncTrust, datas, async (_data) => {
|
|
1558
1717
|
const sqls = this._insert(_data, option);
|
|
1559
1718
|
let result = 0n;
|
|
1560
1719
|
for (const { sql, params } of sqls) {
|
|
1561
|
-
const dd = await option?.conn.execute(
|
|
1720
|
+
const dd = await option?.conn.execute(SyncMode.Async, sql, params);
|
|
1562
1721
|
if (dd.insertId) {
|
|
1563
1722
|
result += dd.insertId;
|
|
1564
1723
|
}
|
|
@@ -1573,7 +1732,7 @@ class SqlService {
|
|
|
1573
1732
|
resolve((await fn()));
|
|
1574
1733
|
}
|
|
1575
1734
|
else {
|
|
1576
|
-
await option?.dao?.transaction(
|
|
1735
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1577
1736
|
}
|
|
1578
1737
|
});
|
|
1579
1738
|
}
|
|
@@ -1583,7 +1742,7 @@ class SqlService {
|
|
|
1583
1742
|
const sqls = this._insert(_data, option);
|
|
1584
1743
|
let result = 0n;
|
|
1585
1744
|
for (const { sql, params } of sqls) {
|
|
1586
|
-
const dd = await option.conn.execute(
|
|
1745
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1587
1746
|
if (dd.insertId) {
|
|
1588
1747
|
result += dd.insertId;
|
|
1589
1748
|
}
|
|
@@ -1597,7 +1756,7 @@ class SqlService {
|
|
|
1597
1756
|
resolve((await fn()));
|
|
1598
1757
|
}
|
|
1599
1758
|
else {
|
|
1600
|
-
await option?.dao?.transaction(
|
|
1759
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1601
1760
|
}
|
|
1602
1761
|
});
|
|
1603
1762
|
}
|
|
@@ -1638,13 +1797,13 @@ class SqlService {
|
|
|
1638
1797
|
update(option) {
|
|
1639
1798
|
error_1.Throw.if(!this[_ids] || this[_ids].length === 0, 'not found id');
|
|
1640
1799
|
const datas = option.data instanceof Array ? option.data : [option.data];
|
|
1641
|
-
if (option.sync ===
|
|
1800
|
+
if (option.sync === SyncMode.Sync) {
|
|
1642
1801
|
const fn = () => {
|
|
1643
1802
|
const result = (0, fn_1.excuteSplit)(fn_1.ExcuteSplitMode.SyncTrust, datas, _data => {
|
|
1644
1803
|
const sqls = this._update(_data, option);
|
|
1645
1804
|
let result = 0;
|
|
1646
1805
|
for (const { sql, params } of sqls) {
|
|
1647
|
-
const dd = option.conn.execute(
|
|
1806
|
+
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1648
1807
|
if (dd.affectedRows) {
|
|
1649
1808
|
result += dd.affectedRows;
|
|
1650
1809
|
}
|
|
@@ -1657,7 +1816,7 @@ class SqlService {
|
|
|
1657
1816
|
return fn();
|
|
1658
1817
|
}
|
|
1659
1818
|
else {
|
|
1660
|
-
return option?.dao?.transaction(
|
|
1819
|
+
return option?.dao?.transaction(SyncMode.Sync, fn, option?.conn);
|
|
1661
1820
|
}
|
|
1662
1821
|
}
|
|
1663
1822
|
else {
|
|
@@ -1666,7 +1825,7 @@ class SqlService {
|
|
|
1666
1825
|
const sqls = this._update(_data, option);
|
|
1667
1826
|
let result = 0;
|
|
1668
1827
|
for (const { sql, params } of sqls) {
|
|
1669
|
-
const dd = await option.conn.execute(
|
|
1828
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1670
1829
|
if (dd.affectedRows) {
|
|
1671
1830
|
result += dd.affectedRows;
|
|
1672
1831
|
}
|
|
@@ -1680,7 +1839,7 @@ class SqlService {
|
|
|
1680
1839
|
resolve((await fn()));
|
|
1681
1840
|
}
|
|
1682
1841
|
else {
|
|
1683
|
-
await option?.dao?.transaction(
|
|
1842
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1684
1843
|
}
|
|
1685
1844
|
});
|
|
1686
1845
|
}
|
|
@@ -1691,7 +1850,7 @@ class SqlService {
|
|
|
1691
1850
|
error_1.Throw.if(!option.id && !option.where, 'not found id or where!');
|
|
1692
1851
|
error_1.Throw.if(!!option.id && !!this[_ids] && this[_ids].length > 1, 'muit id must set where!');
|
|
1693
1852
|
error_1.Throw.if(!!option.id && !!option.where, 'id and where only one can set!');
|
|
1694
|
-
option.mode ?? (option.mode =
|
|
1853
|
+
option.mode ?? (option.mode = DeleteMode.Common);
|
|
1695
1854
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1696
1855
|
const tableTempESC = sqlstring_1.default.escapeId(tableTemp);
|
|
1697
1856
|
const tableNameESC = sqlstring_1.default.escapeId(option?.tableName);
|
|
@@ -1702,7 +1861,7 @@ class SqlService {
|
|
|
1702
1861
|
}
|
|
1703
1862
|
const wheres = option.where instanceof Array ? option.where : [option.where];
|
|
1704
1863
|
const sqls = [];
|
|
1705
|
-
if (option.mode ===
|
|
1864
|
+
if (option.mode === DeleteMode.Common) {
|
|
1706
1865
|
const params = new Array();
|
|
1707
1866
|
const whereSql = (0, iterare_1.default)(wheres).map(where => {
|
|
1708
1867
|
return `(
|
|
@@ -1759,11 +1918,11 @@ class SqlService {
|
|
|
1759
1918
|
}
|
|
1760
1919
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
1761
1920
|
}
|
|
1762
|
-
if (option.sync ===
|
|
1921
|
+
if (option.sync === SyncMode.Sync) {
|
|
1763
1922
|
const fn = () => {
|
|
1764
1923
|
let result = 0;
|
|
1765
1924
|
for (const { sql, params } of sqls) {
|
|
1766
|
-
const dd = option.conn.execute(
|
|
1925
|
+
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1767
1926
|
result += dd.affectedRows;
|
|
1768
1927
|
}
|
|
1769
1928
|
return result;
|
|
@@ -1772,14 +1931,14 @@ class SqlService {
|
|
|
1772
1931
|
return fn();
|
|
1773
1932
|
}
|
|
1774
1933
|
else {
|
|
1775
|
-
return option?.dao?.transaction(
|
|
1934
|
+
return option?.dao?.transaction(SyncMode.Sync, fn, option?.conn);
|
|
1776
1935
|
}
|
|
1777
1936
|
}
|
|
1778
1937
|
else {
|
|
1779
1938
|
const fn = async () => {
|
|
1780
1939
|
let result = 0;
|
|
1781
1940
|
for (const { sql, params } of sqls) {
|
|
1782
|
-
const dd = await option.conn.execute(
|
|
1941
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1783
1942
|
result += dd.affectedRows;
|
|
1784
1943
|
}
|
|
1785
1944
|
return result;
|
|
@@ -1789,25 +1948,25 @@ class SqlService {
|
|
|
1789
1948
|
resolve((await fn()));
|
|
1790
1949
|
}
|
|
1791
1950
|
else {
|
|
1792
|
-
await option?.dao?.transaction(
|
|
1951
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1793
1952
|
}
|
|
1794
1953
|
});
|
|
1795
1954
|
}
|
|
1796
1955
|
}
|
|
1797
|
-
|
|
1798
|
-
switch (
|
|
1799
|
-
case
|
|
1956
|
+
_template(templateResult, result, error) {
|
|
1957
|
+
switch (templateResult) {
|
|
1958
|
+
case TemplateResult.AssertOne: {
|
|
1800
1959
|
error_1.Throw.if(!result || result.length !== 1, error);
|
|
1801
1960
|
return result[0];
|
|
1802
1961
|
}
|
|
1803
|
-
case
|
|
1962
|
+
case TemplateResult.NotSureOne: {
|
|
1804
1963
|
error_1.Throw.if(!result, error);
|
|
1805
1964
|
return result[0] ?? null;
|
|
1806
1965
|
}
|
|
1807
|
-
case
|
|
1966
|
+
case TemplateResult.Many: {
|
|
1808
1967
|
return result;
|
|
1809
1968
|
}
|
|
1810
|
-
case
|
|
1969
|
+
case TemplateResult.Count: {
|
|
1811
1970
|
return result[0].ct;
|
|
1812
1971
|
}
|
|
1813
1972
|
}
|
|
@@ -1818,8 +1977,8 @@ class SqlService {
|
|
|
1818
1977
|
error_1.Throw.if(!option.id && !option.where, 'not found id or where!');
|
|
1819
1978
|
error_1.Throw.if(!!option.id && !!this[_ids] && this[_ids].length > 1, 'muit id must set where!');
|
|
1820
1979
|
error_1.Throw.if(!!option.id && !!option.where, 'id and where only one can set!');
|
|
1821
|
-
option.mode ?? (option.mode =
|
|
1822
|
-
option.
|
|
1980
|
+
option.mode ?? (option.mode = SelectMode.Common);
|
|
1981
|
+
option.templateResult ?? (option.templateResult = TemplateResult.AssertOne);
|
|
1823
1982
|
option.error ?? (option.error = 'error data!');
|
|
1824
1983
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1825
1984
|
const tableTempESC = sqlstring_1.default.escapeId(tableTemp);
|
|
@@ -1829,11 +1988,11 @@ class SqlService {
|
|
|
1829
1988
|
const ids = option.id instanceof Array ? option.id : [option.id];
|
|
1830
1989
|
option.where = ids.map(i => ({ [idName]: i }));
|
|
1831
1990
|
}
|
|
1832
|
-
const columns = option.
|
|
1991
|
+
const columns = option.templateResult === TemplateResult.Count ? 'COUNT(1) ct' : (0, iterare_1.default)((option.columns ?? this[_columns])).map((K) => `a.${this[_fields][K]?.esName}`).join(',');
|
|
1833
1992
|
const wheres = option.where instanceof Array ? option.where : [option.where];
|
|
1834
1993
|
const sqls = [];
|
|
1835
1994
|
let resultIndex = -1;
|
|
1836
|
-
if (option.mode ===
|
|
1995
|
+
if (option.mode === SelectMode.Common) {
|
|
1837
1996
|
const params = new Array();
|
|
1838
1997
|
const whereSql = (0, iterare_1.default)(wheres).map(where => {
|
|
1839
1998
|
return `SELECT ${columns} FROM ${tableNameESC} a WHERE
|
|
@@ -1854,79 +2013,79 @@ class SqlService {
|
|
|
1854
2013
|
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 ')};` });
|
|
1855
2014
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
1856
2015
|
}
|
|
1857
|
-
if (option.sync ===
|
|
2016
|
+
if (option.sync === SyncMode.Sync) {
|
|
1858
2017
|
let result;
|
|
1859
2018
|
for (let i = 0; i < sqls.length; i++) {
|
|
1860
2019
|
if (i === resultIndex) {
|
|
1861
|
-
result = option.conn.query(
|
|
2020
|
+
result = option.conn.query(SyncMode.Sync, sqls[i]?.sql, sqls[i]?.params);
|
|
1862
2021
|
}
|
|
1863
2022
|
else {
|
|
1864
|
-
option.conn.execute(
|
|
2023
|
+
option.conn.execute(SyncMode.Sync, sqls[i]?.sql, sqls[i]?.params);
|
|
1865
2024
|
}
|
|
1866
2025
|
}
|
|
1867
|
-
return this.
|
|
2026
|
+
return this._template(option.templateResult, result, option.error);
|
|
1868
2027
|
}
|
|
1869
2028
|
else {
|
|
1870
2029
|
return new Promise(async (resolve) => {
|
|
1871
2030
|
let result;
|
|
1872
2031
|
for (let i = 0; i < sqls.length; i++) {
|
|
1873
2032
|
if (i === resultIndex) {
|
|
1874
|
-
result = await option.conn.query(
|
|
2033
|
+
result = await option.conn.query(SyncMode.Async, sqls[i]?.sql, sqls[i]?.params);
|
|
1875
2034
|
}
|
|
1876
2035
|
else {
|
|
1877
|
-
await option.conn.execute(
|
|
2036
|
+
await option.conn.execute(SyncMode.Async, sqls[i]?.sql, sqls[i]?.params);
|
|
1878
2037
|
}
|
|
1879
2038
|
}
|
|
1880
|
-
resolve(this.
|
|
2039
|
+
resolve(this._template(option.templateResult, result, option.error));
|
|
1881
2040
|
});
|
|
1882
2041
|
}
|
|
1883
2042
|
}
|
|
1884
|
-
|
|
2043
|
+
_select(templateResult, result, def, errorMsg, multiple) {
|
|
1885
2044
|
if (multiple === true) {
|
|
1886
|
-
switch (
|
|
1887
|
-
case
|
|
2045
|
+
switch (templateResult) {
|
|
2046
|
+
case SelectResult.One_Row_One_Column_NotSure: {
|
|
1888
2047
|
try {
|
|
1889
2048
|
return result.map((r) => Object.values(r)[0]);
|
|
1890
2049
|
}
|
|
1891
2050
|
catch (error) {
|
|
1892
2051
|
}
|
|
1893
2052
|
}
|
|
1894
|
-
case
|
|
2053
|
+
case SelectResult.One_Row_One_Column_Assert: {
|
|
1895
2054
|
try {
|
|
1896
2055
|
return (0, iterare_1.default)(result).map((r) => Object.values(r)[0]).filter((r) => r !== null).toArray();
|
|
1897
2056
|
}
|
|
1898
2057
|
catch (error) {
|
|
1899
2058
|
}
|
|
1900
2059
|
}
|
|
1901
|
-
case
|
|
2060
|
+
case SelectResult.One_Row_Many_Column_NotSure: {
|
|
1902
2061
|
try {
|
|
1903
2062
|
return result.map((r) => r[0]);
|
|
1904
2063
|
}
|
|
1905
2064
|
catch (error) {
|
|
1906
2065
|
}
|
|
1907
2066
|
}
|
|
1908
|
-
case
|
|
2067
|
+
case SelectResult.One_Row_Many_Column_Assert: {
|
|
1909
2068
|
try {
|
|
1910
2069
|
return (0, iterare_1.default)(result).map((r) => r[0]).filter((r) => r !== null).toArray();
|
|
1911
2070
|
}
|
|
1912
2071
|
catch (error) {
|
|
1913
2072
|
}
|
|
1914
2073
|
}
|
|
1915
|
-
case
|
|
2074
|
+
case SelectResult.Many_Row_One_Column: {
|
|
1916
2075
|
try {
|
|
1917
2076
|
return result.map((rx) => rx.map((r) => Object.values(r)[0]));
|
|
1918
2077
|
}
|
|
1919
2078
|
catch (error) {
|
|
1920
2079
|
}
|
|
1921
2080
|
}
|
|
1922
|
-
case
|
|
2081
|
+
case SelectResult.Many_Row_Many_Column: {
|
|
1923
2082
|
return result;
|
|
1924
2083
|
}
|
|
1925
2084
|
}
|
|
1926
2085
|
}
|
|
1927
2086
|
else {
|
|
1928
|
-
switch (
|
|
1929
|
-
case
|
|
2087
|
+
switch (templateResult) {
|
|
2088
|
+
case SelectResult.One_Row_One_Column_NotSure: {
|
|
1930
2089
|
try {
|
|
1931
2090
|
return Object.values(result[0])[0];
|
|
1932
2091
|
}
|
|
@@ -1934,7 +2093,7 @@ class SqlService {
|
|
|
1934
2093
|
return def;
|
|
1935
2094
|
}
|
|
1936
2095
|
}
|
|
1937
|
-
case
|
|
2096
|
+
case SelectResult.One_Row_One_Column_Assert: {
|
|
1938
2097
|
try {
|
|
1939
2098
|
return Object.values(result[0])[0];
|
|
1940
2099
|
}
|
|
@@ -1944,15 +2103,15 @@ class SqlService {
|
|
|
1944
2103
|
error_1.Throw.now(errorMsg ?? 'not found data!');
|
|
1945
2104
|
}
|
|
1946
2105
|
}
|
|
1947
|
-
case
|
|
2106
|
+
case SelectResult.One_Row_Many_Column_NotSure: {
|
|
1948
2107
|
return result[0] ?? null;
|
|
1949
2108
|
}
|
|
1950
|
-
case
|
|
2109
|
+
case SelectResult.One_Row_Many_Column_Assert: {
|
|
1951
2110
|
const data = result[0];
|
|
1952
2111
|
error_1.Throw.if(data === null, errorMsg ?? 'not found data!');
|
|
1953
2112
|
return data ?? null;
|
|
1954
2113
|
}
|
|
1955
|
-
case
|
|
2114
|
+
case SelectResult.Many_Row_One_Column: {
|
|
1956
2115
|
try {
|
|
1957
2116
|
return result.map((r) => Object.values(r)[0]);
|
|
1958
2117
|
}
|
|
@@ -1960,7 +2119,7 @@ class SqlService {
|
|
|
1960
2119
|
return def;
|
|
1961
2120
|
}
|
|
1962
2121
|
}
|
|
1963
|
-
case
|
|
2122
|
+
case SelectResult.Many_Row_Many_Column: {
|
|
1964
2123
|
return result;
|
|
1965
2124
|
}
|
|
1966
2125
|
}
|
|
@@ -1968,7 +2127,7 @@ class SqlService {
|
|
|
1968
2127
|
}
|
|
1969
2128
|
select(option) {
|
|
1970
2129
|
error_1.Throw.if(!option.sqlId && !option.sql, 'not found sql!');
|
|
1971
|
-
option.
|
|
2130
|
+
option.selectResult ?? (option.selectResult = SelectResult.Many_Row_Many_Column);
|
|
1972
2131
|
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, option.context, option.isPage));
|
|
1973
2132
|
option.defValue ?? (option.defValue = null);
|
|
1974
2133
|
logger.debug(option.sql);
|
|
@@ -1982,14 +2141,14 @@ class SqlService {
|
|
|
1982
2141
|
}
|
|
1983
2142
|
return txt;
|
|
1984
2143
|
});
|
|
1985
|
-
if (option.sync ===
|
|
1986
|
-
const result = option.conn.query(
|
|
1987
|
-
return this.
|
|
2144
|
+
if (option.sync === SyncMode.Sync) {
|
|
2145
|
+
const result = option.conn.query(SyncMode.Sync, sql, params);
|
|
2146
|
+
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.multiple);
|
|
1988
2147
|
}
|
|
1989
2148
|
else {
|
|
1990
2149
|
return new Promise(async (resolve) => {
|
|
1991
|
-
const result = await option.conn.query(
|
|
1992
|
-
resolve(this.
|
|
2150
|
+
const result = await option.conn.query(SyncMode.Async, sql, params);
|
|
2151
|
+
resolve(this._select(option.selectResult, result, option.defValue, option.errorMsg, option.multiple));
|
|
1993
2152
|
});
|
|
1994
2153
|
}
|
|
1995
2154
|
}
|
|
@@ -2007,24 +2166,24 @@ class SqlService {
|
|
|
2007
2166
|
}
|
|
2008
2167
|
return txt;
|
|
2009
2168
|
});
|
|
2010
|
-
if (option.sync ===
|
|
2011
|
-
const result = option.conn.execute(
|
|
2169
|
+
if (option.sync === SyncMode.Sync) {
|
|
2170
|
+
const result = option.conn.execute(SyncMode.Sync, sql, params);
|
|
2012
2171
|
return result.affectedRows;
|
|
2013
2172
|
}
|
|
2014
2173
|
else {
|
|
2015
2174
|
return new Promise(async (resolve) => {
|
|
2016
|
-
const result = await option.conn.execute(
|
|
2175
|
+
const result = await option.conn.execute(SyncMode.Async, sql, params);
|
|
2017
2176
|
resolve(result.affectedRows);
|
|
2018
2177
|
});
|
|
2019
2178
|
}
|
|
2020
2179
|
}
|
|
2021
2180
|
transaction(option) {
|
|
2022
|
-
if (option.sync ===
|
|
2023
|
-
return option.dao.transaction(
|
|
2181
|
+
if (option.sync === SyncMode.Sync) {
|
|
2182
|
+
return option.dao.transaction(SyncMode.Sync, option.fn);
|
|
2024
2183
|
}
|
|
2025
2184
|
else {
|
|
2026
2185
|
return new Promise(async (resolve) => {
|
|
2027
|
-
const rt = await option.dao.transaction(
|
|
2186
|
+
const rt = await option.dao.transaction(SyncMode.Async, option.fn);
|
|
2028
2187
|
resolve(rt);
|
|
2029
2188
|
});
|
|
2030
2189
|
}
|
|
@@ -2964,7 +3123,7 @@ class StreamQuery extends StreamBuild {
|
|
|
2964
3123
|
this._service = service;
|
|
2965
3124
|
}
|
|
2966
3125
|
select(option) {
|
|
2967
|
-
option.sync ?? (option.sync =
|
|
3126
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
2968
3127
|
const { where, params } = this.where();
|
|
2969
3128
|
const sql = `
|
|
2970
3129
|
SELECT
|
|
@@ -2972,29 +3131,29 @@ class StreamQuery extends StreamBuild {
|
|
|
2972
3131
|
FROM ${this._table}
|
|
2973
3132
|
${where ? ' WHERE ' : ''}
|
|
2974
3133
|
${where}`;
|
|
2975
|
-
if (option.sync ===
|
|
2976
|
-
switch (option.
|
|
2977
|
-
case
|
|
2978
|
-
case
|
|
2979
|
-
case
|
|
2980
|
-
case
|
|
2981
|
-
case
|
|
2982
|
-
case
|
|
3134
|
+
if (option.sync === SyncMode.Async) {
|
|
3135
|
+
switch (option.selectResult) {
|
|
3136
|
+
case SelectResult.Many_Row_Many_Column: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.Many_Row_Many_Column, errorMsg: option.errorMsg, sql, params });
|
|
3137
|
+
case SelectResult.Many_Row_One_Column: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.Many_Row_One_Column, errorMsg: option.errorMsg, sql, params });
|
|
3138
|
+
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 });
|
|
3139
|
+
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 });
|
|
3140
|
+
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 });
|
|
3141
|
+
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 });
|
|
2983
3142
|
}
|
|
2984
3143
|
}
|
|
2985
3144
|
else {
|
|
2986
|
-
switch (option.
|
|
2987
|
-
case
|
|
2988
|
-
case
|
|
2989
|
-
case
|
|
2990
|
-
case
|
|
2991
|
-
case
|
|
2992
|
-
case
|
|
3145
|
+
switch (option.selectResult) {
|
|
3146
|
+
case SelectResult.Many_Row_Many_Column: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.Many_Row_Many_Column, errorMsg: option.errorMsg, sql, params });
|
|
3147
|
+
case SelectResult.Many_Row_One_Column: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.Many_Row_One_Column, errorMsg: option.errorMsg, sql, params });
|
|
3148
|
+
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 });
|
|
3149
|
+
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 });
|
|
3150
|
+
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 });
|
|
3151
|
+
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 });
|
|
2993
3152
|
}
|
|
2994
3153
|
}
|
|
2995
3154
|
}
|
|
2996
3155
|
update(option) {
|
|
2997
|
-
option.sync ?? (option.sync =
|
|
3156
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
2998
3157
|
const { where, params } = this.where();
|
|
2999
3158
|
const sets = new Array(...this._updateColumns);
|
|
3000
3159
|
if (this._updates) {
|
|
@@ -3006,11 +3165,11 @@ class StreamQuery extends StreamBuild {
|
|
|
3006
3165
|
}
|
|
3007
3166
|
if (sets.length > 0) {
|
|
3008
3167
|
const sql = `UPDATE ${this._table} SET ${sets.join(',')} ${where}`;
|
|
3009
|
-
if (option.sync ===
|
|
3010
|
-
return this._service.excute({ sync:
|
|
3168
|
+
if (option.sync === SyncMode.Async) {
|
|
3169
|
+
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3011
3170
|
}
|
|
3012
3171
|
else {
|
|
3013
|
-
return this._service.excute({ sync:
|
|
3172
|
+
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
3014
3173
|
}
|
|
3015
3174
|
}
|
|
3016
3175
|
else {
|
|
@@ -3018,14 +3177,14 @@ class StreamQuery extends StreamBuild {
|
|
|
3018
3177
|
}
|
|
3019
3178
|
}
|
|
3020
3179
|
delete(option) {
|
|
3021
|
-
option.sync ?? (option.sync =
|
|
3180
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
3022
3181
|
const { where, params } = this.where();
|
|
3023
3182
|
const sql = `DELETE FROM ${this._table} ${where}`;
|
|
3024
|
-
if (option.sync ===
|
|
3025
|
-
return this._service.excute({ sync:
|
|
3183
|
+
if (option.sync === SyncMode.Async) {
|
|
3184
|
+
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3026
3185
|
}
|
|
3027
3186
|
else {
|
|
3028
|
-
return this._service.excute({ sync:
|
|
3187
|
+
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
3029
3188
|
}
|
|
3030
3189
|
}
|
|
3031
3190
|
}
|
|
@@ -3254,7 +3413,7 @@ function MethodCache(config) {
|
|
|
3254
3413
|
};
|
|
3255
3414
|
}
|
|
3256
3415
|
exports.MethodCache = MethodCache;
|
|
3257
|
-
const
|
|
3416
|
+
const Boot = async function (options) {
|
|
3258
3417
|
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption, options);
|
|
3259
3418
|
if (options.sqlDir) {
|
|
3260
3419
|
globalThis[_path] = Promise.resolve().then(() => __importStar(require('path')));
|
|
@@ -3377,4 +3536,4 @@ const LetsGo = async function (options) {
|
|
|
3377
3536
|
globalThis[_EventBus] = event;
|
|
3378
3537
|
}
|
|
3379
3538
|
};
|
|
3380
|
-
exports.
|
|
3539
|
+
exports.Boot = Boot;
|