baja-lite 1.0.1 → 1.0.3
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/code.d.ts +0 -0
- package/cjs/code.js +2 -0
- package/cjs/index.d.ts +0 -1
- package/cjs/index.js +0 -1
- package/cjs/object.js +3 -7
- package/cjs/redis.d.ts +0 -0
- package/cjs/redis.js +1 -0
- package/cjs/sql.d.ts +193 -143
- package/cjs/sql.js +836 -282
- package/cjs/string.d.ts +0 -1
- package/cjs/string.js +1 -6
- package/cjs/test-mysql.js +8 -8
- package/cjs/test-sqlite.js +7 -7
- package/es/code.d.ts +0 -0
- package/es/code.js +2 -0
- package/es/index.d.ts +0 -1
- package/es/index.js +0 -1
- package/es/object.js +3 -4
- package/es/redis.d.ts +0 -0
- package/es/redis.js +1 -0
- package/es/sql.d.ts +193 -143
- package/es/sql.js +834 -280
- package/es/string.d.ts +0 -1
- package/es/string.js +0 -4
- package/es/test-mysql.js +9 -9
- package/es/test-sqlite.js +8 -8
- package/package.json +19 -20
- package/src/code.ts +1 -0
- package/src/constant.ts +14 -0
- package/src/error.ts +11 -0
- package/src/fn.ts +287 -0
- package/src/index.ts +7 -0
- package/src/math.ts +367 -0
- package/src/object.ts +213 -0
- package/src/redis.ts +0 -0
- package/src/set-ex.ts +362 -0
- package/src/sql.ts +4021 -0
- package/src/string.ts +111 -0
- package/src/test-mysql.ts +96 -0
- package/src/test-sqlite.ts +78 -0
- package/src/test.ts +2 -0
- package/cjs/now.d.ts +0 -7
- package/cjs/now.js +0 -26
- package/es/now.d.ts +0 -7
- package/es/now.js +0 -16
package/es/sql.js
CHANGED
|
@@ -18,6 +18,7 @@ import { emptyString } from './string';
|
|
|
18
18
|
import pino from 'pino';
|
|
19
19
|
import { excuteSplit, ExcuteSplitMode, sleep } from './fn';
|
|
20
20
|
import { add } from './math';
|
|
21
|
+
import mustache from 'mustache';
|
|
21
22
|
// #region 常量
|
|
22
23
|
const _daoDBName = Symbol('dbName');
|
|
23
24
|
const _tableName = Symbol('tableName');
|
|
@@ -30,7 +31,7 @@ const _deleteState = Symbol('deleteState');
|
|
|
30
31
|
const _transformer = Symbol('transformer');
|
|
31
32
|
const _index = Symbol('index');
|
|
32
33
|
const _def = Symbol('def');
|
|
33
|
-
const _sqlCache = Symbol('
|
|
34
|
+
const _sqlCache = Symbol('sqlMap');
|
|
34
35
|
const _dao = Symbol('dao');
|
|
35
36
|
const _primaryDB = Symbol('primaryDB');
|
|
36
37
|
const _dbType = Symbol('dbType');
|
|
@@ -61,21 +62,21 @@ export var DBType;
|
|
|
61
62
|
DBType[DBType["Redis"] = 4] = "Redis";
|
|
62
63
|
DBType[DBType["RedisLock"] = 5] = "RedisLock";
|
|
63
64
|
})(DBType || (DBType = {}));
|
|
64
|
-
export var
|
|
65
|
-
(function (
|
|
65
|
+
export var SyncMode;
|
|
66
|
+
(function (SyncMode) {
|
|
66
67
|
/** 同步执行 */
|
|
67
|
-
|
|
68
|
+
SyncMode[SyncMode["Sync"] = 0] = "Sync";
|
|
68
69
|
/** 异步执行 */
|
|
69
|
-
|
|
70
|
-
})(
|
|
71
|
-
export var
|
|
72
|
-
(function (
|
|
70
|
+
SyncMode[SyncMode["Async"] = 1] = "Async";
|
|
71
|
+
})(SyncMode || (SyncMode = {}));
|
|
72
|
+
export var InsertMode;
|
|
73
|
+
(function (InsertMode) {
|
|
73
74
|
/**
|
|
74
75
|
# 默认使用
|
|
75
76
|
** 支持单个、批量,语法 `INSERT INTO XX VALUES (第一条数据), (第二条数据);`
|
|
76
77
|
** 批量执行有性能优势,但无法利用数据库的sql预编译功能
|
|
77
78
|
*/
|
|
78
|
-
|
|
79
|
+
InsertMode[InsertMode["Insert"] = 0] = "Insert";
|
|
79
80
|
/**
|
|
80
81
|
# 利用临时表
|
|
81
82
|
## 执行步骤
|
|
@@ -87,16 +88,16 @@ export var SqlInsertMode;
|
|
|
87
88
|
1. 适用于:主键不会冲突、非自增
|
|
88
89
|
2. 临时表的结构复制正式表
|
|
89
90
|
*/
|
|
90
|
-
|
|
91
|
-
|
|
91
|
+
InsertMode[InsertMode["InsertWithTempTable"] = 1] = "InsertWithTempTable";
|
|
92
|
+
InsertMode[InsertMode["InsertIfNotExists"] = 2] = "InsertIfNotExists";
|
|
92
93
|
/**
|
|
93
94
|
# 插入或者更新
|
|
94
95
|
1. 判断依据是主键
|
|
95
96
|
*/
|
|
96
|
-
|
|
97
|
-
})(
|
|
98
|
-
export var
|
|
99
|
-
(function (
|
|
97
|
+
InsertMode[InsertMode["Replace"] = 3] = "Replace";
|
|
98
|
+
})(InsertMode || (InsertMode = {}));
|
|
99
|
+
export var DeleteMode;
|
|
100
|
+
(function (DeleteMode) {
|
|
100
101
|
/**
|
|
101
102
|
##常规删除 默认
|
|
102
103
|
### 例一
|
|
@@ -104,16 +105,16 @@ export var SqlDelMode;
|
|
|
104
105
|
### 例二
|
|
105
106
|
`DELETE FROM WHERE (id = 1 AND idx = 11) OR (id = 2 AND idx = 22)`
|
|
106
107
|
*/
|
|
107
|
-
|
|
108
|
+
DeleteMode[DeleteMode["Common"] = 0] = "Common";
|
|
108
109
|
/*
|
|
109
110
|
## 借助临时表
|
|
110
111
|
### 注意:必须保证where的字段都相同,否则会漏删数据
|
|
111
112
|
DELETE FROM 正式表 INNER JOIN 临时表 WHERE 字段1 = 字段1 AND 字段2 = 字段2
|
|
112
113
|
*/
|
|
113
|
-
|
|
114
|
-
})(
|
|
115
|
-
export var
|
|
116
|
-
(function (
|
|
114
|
+
DeleteMode[DeleteMode["TempTable"] = 1] = "TempTable";
|
|
115
|
+
})(DeleteMode || (DeleteMode = {}));
|
|
116
|
+
export var SelectMode;
|
|
117
|
+
(function (SelectMode) {
|
|
117
118
|
/**
|
|
118
119
|
##常规 默认
|
|
119
120
|
### 例一
|
|
@@ -121,46 +122,70 @@ export var SqlSelectMode;
|
|
|
121
122
|
### 例二
|
|
122
123
|
`SELECT * FROM WHERE (id = 1 AND idx = 11) OR (id = 2 AND idx = 22)`
|
|
123
124
|
*/
|
|
124
|
-
|
|
125
|
+
SelectMode[SelectMode["Common"] = 0] = "Common";
|
|
125
126
|
/*
|
|
126
127
|
## 借助临时表
|
|
127
128
|
### 注意:必须保证where的字段都相同,否则会漏删数据
|
|
128
129
|
SELECT * FROM 正式表 INNER JOIN 临时表 WHERE 字段1 = 字段1 AND 字段2 = 字段2
|
|
129
130
|
*/
|
|
130
|
-
|
|
131
|
-
})(
|
|
132
|
-
export var
|
|
133
|
-
(function (
|
|
134
|
-
/**
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
|
|
131
|
+
SelectMode[SelectMode["TempTable"] = 1] = "TempTable";
|
|
132
|
+
})(SelectMode || (SelectMode = {}));
|
|
133
|
+
export var TemplateResult;
|
|
134
|
+
(function (TemplateResult) {
|
|
135
|
+
/** 确定返回一条记录,如果不是一个,将报错,返回类型是T */
|
|
136
|
+
TemplateResult[TemplateResult["AssertOne"] = 0] = "AssertOne";
|
|
137
|
+
/** 可能返回一条记录,返回类型是T|null */
|
|
138
|
+
TemplateResult[TemplateResult["NotSureOne"] = 1] = "NotSureOne";
|
|
139
|
+
/** 返回多条记录 */
|
|
140
|
+
TemplateResult[TemplateResult["Many"] = 2] = "Many";
|
|
140
141
|
/** 仅查询记录数量 */
|
|
141
|
-
|
|
142
|
-
})(
|
|
143
|
-
export var
|
|
144
|
-
(function (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
})(
|
|
142
|
+
TemplateResult[TemplateResult["Count"] = 3] = "Count";
|
|
143
|
+
})(TemplateResult || (TemplateResult = {}));
|
|
144
|
+
export var SelectResult;
|
|
145
|
+
(function (SelectResult) {
|
|
146
|
+
SelectResult[SelectResult["One_Row_One_Column_Assert"] = 0] = "One_Row_One_Column_Assert";
|
|
147
|
+
SelectResult[SelectResult["One_Row_One_Column_NotSure"] = 1] = "One_Row_One_Column_NotSure";
|
|
148
|
+
SelectResult[SelectResult["One_Row_Many_Column_Assert"] = 2] = "One_Row_Many_Column_Assert";
|
|
149
|
+
SelectResult[SelectResult["One_Row_Many_Column_NotSure"] = 3] = "One_Row_Many_Column_NotSure";
|
|
150
|
+
SelectResult[SelectResult["Many_Row_One_Column"] = 4] = "Many_Row_One_Column";
|
|
151
|
+
SelectResult[SelectResult["Many_Row_Many_Column"] = 5] = "Many_Row_Many_Column";
|
|
152
|
+
})(SelectResult || (SelectResult = {}));
|
|
152
153
|
export var SqlType;
|
|
153
154
|
(function (SqlType) {
|
|
154
|
-
SqlType[SqlType["
|
|
155
|
-
SqlType[SqlType["
|
|
156
|
-
SqlType[SqlType["
|
|
155
|
+
SqlType[SqlType["tinyint"] = 0] = "tinyint";
|
|
156
|
+
SqlType[SqlType["smallint"] = 1] = "smallint";
|
|
157
|
+
SqlType[SqlType["mediumint"] = 2] = "mediumint";
|
|
157
158
|
SqlType[SqlType["int"] = 3] = "int";
|
|
158
|
-
SqlType[SqlType["
|
|
159
|
-
SqlType[SqlType["
|
|
160
|
-
SqlType[SqlType["
|
|
161
|
-
SqlType[SqlType["
|
|
162
|
-
SqlType[SqlType["
|
|
163
|
-
SqlType[SqlType["
|
|
159
|
+
SqlType[SqlType["bigint"] = 4] = "bigint";
|
|
160
|
+
SqlType[SqlType["float"] = 5] = "float";
|
|
161
|
+
SqlType[SqlType["double"] = 6] = "double";
|
|
162
|
+
SqlType[SqlType["decimal"] = 7] = "decimal";
|
|
163
|
+
SqlType[SqlType["date"] = 8] = "date";
|
|
164
|
+
SqlType[SqlType["time"] = 9] = "time";
|
|
165
|
+
SqlType[SqlType["year"] = 10] = "year";
|
|
166
|
+
SqlType[SqlType["datetime"] = 11] = "datetime";
|
|
167
|
+
SqlType[SqlType["timestamp"] = 12] = "timestamp";
|
|
168
|
+
SqlType[SqlType["char"] = 13] = "char";
|
|
169
|
+
SqlType[SqlType["varchar"] = 14] = "varchar";
|
|
170
|
+
SqlType[SqlType["tinyblob"] = 15] = "tinyblob";
|
|
171
|
+
SqlType[SqlType["tinytext"] = 16] = "tinytext";
|
|
172
|
+
SqlType[SqlType["blob"] = 17] = "blob";
|
|
173
|
+
SqlType[SqlType["text"] = 18] = "text";
|
|
174
|
+
SqlType[SqlType["mediumblob"] = 19] = "mediumblob";
|
|
175
|
+
SqlType[SqlType["mediumtext"] = 20] = "mediumtext";
|
|
176
|
+
SqlType[SqlType["longblob"] = 21] = "longblob";
|
|
177
|
+
SqlType[SqlType["longtext"] = 22] = "longtext";
|
|
178
|
+
SqlType[SqlType["set"] = 23] = "set";
|
|
179
|
+
SqlType[SqlType["enum"] = 24] = "enum";
|
|
180
|
+
SqlType[SqlType["json"] = 25] = "json";
|
|
181
|
+
SqlType[SqlType["geometry"] = 26] = "geometry";
|
|
182
|
+
SqlType[SqlType["point"] = 27] = "point";
|
|
183
|
+
SqlType[SqlType["linestring"] = 28] = "linestring";
|
|
184
|
+
SqlType[SqlType["polygon"] = 29] = "polygon";
|
|
185
|
+
SqlType[SqlType["multipoint"] = 30] = "multipoint";
|
|
186
|
+
SqlType[SqlType["multilinestring"] = 31] = "multilinestring";
|
|
187
|
+
SqlType[SqlType["multipolygon"] = 32] = "multipolygon";
|
|
188
|
+
SqlType[SqlType["geometrycollection"] = 33] = "geometrycollection";
|
|
164
189
|
})(SqlType || (SqlType = {}));
|
|
165
190
|
export const SqliteMemory = ':memory:';
|
|
166
191
|
const _defOption = {
|
|
@@ -181,7 +206,7 @@ class MysqlConnection {
|
|
|
181
206
|
return { affectedRows: 0, insertId: 0n };
|
|
182
207
|
}
|
|
183
208
|
;
|
|
184
|
-
if (sync ===
|
|
209
|
+
if (sync === SyncMode.Sync) {
|
|
185
210
|
logger.warn('MYSQL not suppouted sync mode');
|
|
186
211
|
return { affectedRows: 0, insertId: 0n };
|
|
187
212
|
}
|
|
@@ -189,7 +214,7 @@ class MysqlConnection {
|
|
|
189
214
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
190
215
|
logger.trace(Sqlstring.format(sql, params));
|
|
191
216
|
}
|
|
192
|
-
return new Promise(async (resolve) => {
|
|
217
|
+
return new Promise(async (resolve, reject) => {
|
|
193
218
|
try {
|
|
194
219
|
const [_result] = await this[_daoConnection].execute(sql, params);
|
|
195
220
|
const result = _result;
|
|
@@ -204,7 +229,7 @@ class MysqlConnection {
|
|
|
204
229
|
sql: ${sql},
|
|
205
230
|
params: ${params}
|
|
206
231
|
`);
|
|
207
|
-
|
|
232
|
+
reject(error);
|
|
208
233
|
}
|
|
209
234
|
});
|
|
210
235
|
}
|
|
@@ -214,7 +239,7 @@ class MysqlConnection {
|
|
|
214
239
|
return null;
|
|
215
240
|
}
|
|
216
241
|
;
|
|
217
|
-
if (sync ===
|
|
242
|
+
if (sync === SyncMode.Sync) {
|
|
218
243
|
logger.warn('MYSQL not suppouted sync mode');
|
|
219
244
|
return null;
|
|
220
245
|
}
|
|
@@ -222,7 +247,7 @@ class MysqlConnection {
|
|
|
222
247
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
223
248
|
logger.trace(Sqlstring.format(sql, params));
|
|
224
249
|
}
|
|
225
|
-
return new Promise(async (resolve) => {
|
|
250
|
+
return new Promise(async (resolve, reject) => {
|
|
226
251
|
try {
|
|
227
252
|
const [result] = await this[_daoConnection].query(sql, params);
|
|
228
253
|
if (result && result[0]) {
|
|
@@ -240,7 +265,7 @@ class MysqlConnection {
|
|
|
240
265
|
sql: ${sql},
|
|
241
266
|
params: ${params}
|
|
242
267
|
`);
|
|
243
|
-
|
|
268
|
+
reject(error);
|
|
244
269
|
}
|
|
245
270
|
});
|
|
246
271
|
}
|
|
@@ -250,7 +275,7 @@ class MysqlConnection {
|
|
|
250
275
|
return null;
|
|
251
276
|
}
|
|
252
277
|
;
|
|
253
|
-
if (sync ===
|
|
278
|
+
if (sync === SyncMode.Sync) {
|
|
254
279
|
logger.warn('MYSQL not suppouted sync mode');
|
|
255
280
|
return null;
|
|
256
281
|
}
|
|
@@ -258,7 +283,7 @@ class MysqlConnection {
|
|
|
258
283
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
259
284
|
logger.trace(Sqlstring.format(sql, params));
|
|
260
285
|
}
|
|
261
|
-
return new Promise(async (resolve) => {
|
|
286
|
+
return new Promise(async (resolve, reject) => {
|
|
262
287
|
try {
|
|
263
288
|
const [result] = await this[_daoConnection].query(sql, params);
|
|
264
289
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
@@ -274,7 +299,7 @@ class MysqlConnection {
|
|
|
274
299
|
sql: ${sql},
|
|
275
300
|
params: ${params}
|
|
276
301
|
`);
|
|
277
|
-
|
|
302
|
+
reject(error);
|
|
278
303
|
}
|
|
279
304
|
});
|
|
280
305
|
}
|
|
@@ -284,7 +309,7 @@ class MysqlConnection {
|
|
|
284
309
|
return [];
|
|
285
310
|
}
|
|
286
311
|
;
|
|
287
|
-
if (sync ===
|
|
312
|
+
if (sync === SyncMode.Sync) {
|
|
288
313
|
logger.warn('MYSQL not suppouted sync mode');
|
|
289
314
|
return [];
|
|
290
315
|
}
|
|
@@ -292,7 +317,7 @@ class MysqlConnection {
|
|
|
292
317
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
293
318
|
logger.trace(Sqlstring.format(sql, params));
|
|
294
319
|
}
|
|
295
|
-
return new Promise(async (resolve) => {
|
|
320
|
+
return new Promise(async (resolve, reject) => {
|
|
296
321
|
try {
|
|
297
322
|
const [result] = await this[_daoConnection].query(sql, params);
|
|
298
323
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
@@ -308,7 +333,7 @@ class MysqlConnection {
|
|
|
308
333
|
sql: ${sql},
|
|
309
334
|
params: ${params}
|
|
310
335
|
`);
|
|
311
|
-
|
|
336
|
+
reject(error);
|
|
312
337
|
}
|
|
313
338
|
});
|
|
314
339
|
}
|
|
@@ -318,7 +343,7 @@ class MysqlConnection {
|
|
|
318
343
|
return [];
|
|
319
344
|
}
|
|
320
345
|
;
|
|
321
|
-
if (sync ===
|
|
346
|
+
if (sync === SyncMode.Sync) {
|
|
322
347
|
logger.warn('MYSQL not suppouted sync mode');
|
|
323
348
|
return [];
|
|
324
349
|
}
|
|
@@ -326,7 +351,7 @@ class MysqlConnection {
|
|
|
326
351
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
327
352
|
logger.trace(Sqlstring.format(sql, params));
|
|
328
353
|
}
|
|
329
|
-
return new Promise(async (resolve) => {
|
|
354
|
+
return new Promise(async (resolve, reject) => {
|
|
330
355
|
try {
|
|
331
356
|
const [result] = await this[_daoConnection].query(sql, params);
|
|
332
357
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
@@ -340,12 +365,12 @@ class MysqlConnection {
|
|
|
340
365
|
sql: ${sql},
|
|
341
366
|
params: ${params}
|
|
342
367
|
`);
|
|
343
|
-
|
|
368
|
+
reject(error);
|
|
344
369
|
}
|
|
345
370
|
});
|
|
346
371
|
}
|
|
347
372
|
realse(sync) {
|
|
348
|
-
if (sync ===
|
|
373
|
+
if (sync === SyncMode.Sync) {
|
|
349
374
|
try {
|
|
350
375
|
this[_daoConnection]?.release();
|
|
351
376
|
}
|
|
@@ -361,28 +386,33 @@ class Mysql {
|
|
|
361
386
|
this[_daoDB] = pool;
|
|
362
387
|
}
|
|
363
388
|
createConnection(sync) {
|
|
364
|
-
if (sync ===
|
|
389
|
+
if (sync === SyncMode.Sync) {
|
|
365
390
|
logger.error('MYSQL not suppouted sync mode');
|
|
366
391
|
return null;
|
|
367
392
|
}
|
|
368
393
|
;
|
|
369
|
-
return new Promise(async (resolve) => {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
394
|
+
return new Promise(async (resolve, reject) => {
|
|
395
|
+
try {
|
|
396
|
+
const connection = await this[_daoDB].getConnection();
|
|
397
|
+
logger.debug('create new!');
|
|
398
|
+
resolve(new MysqlConnection(connection));
|
|
399
|
+
}
|
|
400
|
+
catch (error) {
|
|
401
|
+
reject(error);
|
|
402
|
+
}
|
|
373
403
|
});
|
|
374
404
|
}
|
|
375
405
|
transaction(sync, fn, conn) {
|
|
376
|
-
if (sync ===
|
|
406
|
+
if (sync === SyncMode.Sync) {
|
|
377
407
|
logger.warn('MYSQL not suppouted sync mode');
|
|
378
408
|
return null;
|
|
379
409
|
}
|
|
380
410
|
;
|
|
381
|
-
return new Promise(async (resolve) => {
|
|
411
|
+
return new Promise(async (resolve, reject) => {
|
|
382
412
|
let needCommit = false;
|
|
383
413
|
let newConn = false;
|
|
384
414
|
if (!conn) {
|
|
385
|
-
conn = await this.createConnection(
|
|
415
|
+
conn = await this.createConnection(SyncMode.Async) ?? undefined;
|
|
386
416
|
newConn = true;
|
|
387
417
|
}
|
|
388
418
|
if (conn?.[_inTransaction] !== true) {
|
|
@@ -408,7 +438,7 @@ class Mysql {
|
|
|
408
438
|
logger.debug('rollback end!');
|
|
409
439
|
conn[_inTransaction] = false;
|
|
410
440
|
logger.error(error);
|
|
411
|
-
|
|
441
|
+
reject(error);
|
|
412
442
|
}
|
|
413
443
|
finally {
|
|
414
444
|
try {
|
|
@@ -427,7 +457,7 @@ class Mysql {
|
|
|
427
457
|
});
|
|
428
458
|
}
|
|
429
459
|
close(sync) {
|
|
430
|
-
if (sync ===
|
|
460
|
+
if (sync === SyncMode.Sync) {
|
|
431
461
|
this[_daoDB]?.destroy();
|
|
432
462
|
}
|
|
433
463
|
;
|
|
@@ -451,7 +481,7 @@ class SqliteConnection {
|
|
|
451
481
|
return { affectedRows: 0, insertId: 0n };
|
|
452
482
|
}
|
|
453
483
|
;
|
|
454
|
-
if (sync ===
|
|
484
|
+
if (sync === SyncMode.Async) {
|
|
455
485
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
456
486
|
return { affectedRows: 0, insertId: 0n };
|
|
457
487
|
}
|
|
@@ -482,7 +512,7 @@ class SqliteConnection {
|
|
|
482
512
|
return null;
|
|
483
513
|
}
|
|
484
514
|
;
|
|
485
|
-
if (sync ===
|
|
515
|
+
if (sync === SyncMode.Async) {
|
|
486
516
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
487
517
|
return null;
|
|
488
518
|
}
|
|
@@ -508,7 +538,7 @@ class SqliteConnection {
|
|
|
508
538
|
return null;
|
|
509
539
|
}
|
|
510
540
|
;
|
|
511
|
-
if (sync ===
|
|
541
|
+
if (sync === SyncMode.Async) {
|
|
512
542
|
return null;
|
|
513
543
|
}
|
|
514
544
|
;
|
|
@@ -533,7 +563,7 @@ class SqliteConnection {
|
|
|
533
563
|
return [];
|
|
534
564
|
}
|
|
535
565
|
;
|
|
536
|
-
if (sync ===
|
|
566
|
+
if (sync === SyncMode.Async) {
|
|
537
567
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
538
568
|
return [];
|
|
539
569
|
}
|
|
@@ -559,7 +589,7 @@ class SqliteConnection {
|
|
|
559
589
|
return [];
|
|
560
590
|
}
|
|
561
591
|
;
|
|
562
|
-
if (sync ===
|
|
592
|
+
if (sync === SyncMode.Async) {
|
|
563
593
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
564
594
|
return [];
|
|
565
595
|
}
|
|
@@ -598,7 +628,7 @@ class Sqlite {
|
|
|
598
628
|
`);
|
|
599
629
|
}
|
|
600
630
|
createConnection(sync) {
|
|
601
|
-
if (sync ===
|
|
631
|
+
if (sync === SyncMode.Async) {
|
|
602
632
|
logger.error(`SQLITE not suppoted async mode`);
|
|
603
633
|
return null;
|
|
604
634
|
}
|
|
@@ -606,13 +636,13 @@ class Sqlite {
|
|
|
606
636
|
return new SqliteConnection(this[_daoDB]);
|
|
607
637
|
}
|
|
608
638
|
transaction(sync, fn, conn) {
|
|
609
|
-
if (sync ===
|
|
639
|
+
if (sync === SyncMode.Async) {
|
|
610
640
|
logger.warn(`SQLITE not suppoted async mode`);
|
|
611
641
|
return null;
|
|
612
642
|
}
|
|
613
643
|
;
|
|
614
644
|
if (!conn) {
|
|
615
|
-
conn = this.createConnection(
|
|
645
|
+
conn = this.createConnection(SyncMode.Sync) ?? undefined;
|
|
616
646
|
}
|
|
617
647
|
if (conn[_inTransaction] !== true) {
|
|
618
648
|
return this[_daoDB].transaction(() => {
|
|
@@ -628,13 +658,13 @@ class Sqlite {
|
|
|
628
658
|
}
|
|
629
659
|
}
|
|
630
660
|
close(sync) {
|
|
631
|
-
if (sync ===
|
|
661
|
+
if (sync === SyncMode.Sync) {
|
|
632
662
|
this[_daoDB].close();
|
|
633
663
|
}
|
|
634
664
|
;
|
|
635
665
|
}
|
|
636
666
|
backup(sync, name) {
|
|
637
|
-
if (sync ===
|
|
667
|
+
if (sync === SyncMode.Sync) {
|
|
638
668
|
this[_daoDB].backup(name);
|
|
639
669
|
}
|
|
640
670
|
;
|
|
@@ -656,7 +686,7 @@ class SqliteRemoteConnection {
|
|
|
656
686
|
return { affectedRows: 0, insertId: 0n };
|
|
657
687
|
}
|
|
658
688
|
;
|
|
659
|
-
if (sync ===
|
|
689
|
+
if (sync === SyncMode.Sync) {
|
|
660
690
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
661
691
|
return { affectedRows: 0, insertId: 0n };
|
|
662
692
|
}
|
|
@@ -664,7 +694,7 @@ class SqliteRemoteConnection {
|
|
|
664
694
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
665
695
|
logger.trace(Sqlstring.format(sql, params));
|
|
666
696
|
}
|
|
667
|
-
return new Promise(async (resolve) => {
|
|
697
|
+
return new Promise(async (resolve, reject) => {
|
|
668
698
|
try {
|
|
669
699
|
const { affectedRows, insertId } = await this[_daoConnection].execute(this[_sqliteRemoteName], sql, params);
|
|
670
700
|
resolve({ affectedRows, insertId: insertId ? BigInt(insertId) : 0n });
|
|
@@ -675,7 +705,7 @@ class SqliteRemoteConnection {
|
|
|
675
705
|
sql: ${sql},
|
|
676
706
|
params: ${params}
|
|
677
707
|
`);
|
|
678
|
-
|
|
708
|
+
reject(error);
|
|
679
709
|
}
|
|
680
710
|
});
|
|
681
711
|
}
|
|
@@ -685,7 +715,7 @@ class SqliteRemoteConnection {
|
|
|
685
715
|
return null;
|
|
686
716
|
}
|
|
687
717
|
;
|
|
688
|
-
if (sync ===
|
|
718
|
+
if (sync === SyncMode.Sync) {
|
|
689
719
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
690
720
|
return null;
|
|
691
721
|
}
|
|
@@ -693,7 +723,7 @@ class SqliteRemoteConnection {
|
|
|
693
723
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
694
724
|
logger.trace(Sqlstring.format(sql, params));
|
|
695
725
|
}
|
|
696
|
-
return new Promise(async (resolve) => {
|
|
726
|
+
return new Promise(async (resolve, reject) => {
|
|
697
727
|
try {
|
|
698
728
|
const r = await this[_daoConnection].pluck(this[_sqliteRemoteName], sql, params);
|
|
699
729
|
resolve(r);
|
|
@@ -704,7 +734,7 @@ class SqliteRemoteConnection {
|
|
|
704
734
|
sql: ${sql},
|
|
705
735
|
params: ${params}
|
|
706
736
|
`);
|
|
707
|
-
|
|
737
|
+
reject(error);
|
|
708
738
|
}
|
|
709
739
|
});
|
|
710
740
|
}
|
|
@@ -714,7 +744,7 @@ class SqliteRemoteConnection {
|
|
|
714
744
|
return null;
|
|
715
745
|
}
|
|
716
746
|
;
|
|
717
|
-
if (sync ===
|
|
747
|
+
if (sync === SyncMode.Sync) {
|
|
718
748
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
719
749
|
return null;
|
|
720
750
|
}
|
|
@@ -722,7 +752,7 @@ class SqliteRemoteConnection {
|
|
|
722
752
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
723
753
|
logger.trace(Sqlstring.format(sql, params));
|
|
724
754
|
}
|
|
725
|
-
return new Promise(async (resolve) => {
|
|
755
|
+
return new Promise(async (resolve, reject) => {
|
|
726
756
|
try {
|
|
727
757
|
const r = await this[_daoConnection].get(this[_sqliteRemoteName], sql, params);
|
|
728
758
|
resolve(r);
|
|
@@ -733,7 +763,7 @@ class SqliteRemoteConnection {
|
|
|
733
763
|
sql: ${sql},
|
|
734
764
|
params: ${params}
|
|
735
765
|
`);
|
|
736
|
-
|
|
766
|
+
reject(error);
|
|
737
767
|
}
|
|
738
768
|
});
|
|
739
769
|
}
|
|
@@ -743,7 +773,7 @@ class SqliteRemoteConnection {
|
|
|
743
773
|
return [];
|
|
744
774
|
}
|
|
745
775
|
;
|
|
746
|
-
if (sync ===
|
|
776
|
+
if (sync === SyncMode.Sync) {
|
|
747
777
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
748
778
|
return [];
|
|
749
779
|
}
|
|
@@ -751,7 +781,7 @@ class SqliteRemoteConnection {
|
|
|
751
781
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
752
782
|
logger.trace(Sqlstring.format(sql, params));
|
|
753
783
|
}
|
|
754
|
-
return new Promise(async (resolve) => {
|
|
784
|
+
return new Promise(async (resolve, reject) => {
|
|
755
785
|
try {
|
|
756
786
|
const r = await this[_daoConnection].raw(this[_sqliteRemoteName], sql, params);
|
|
757
787
|
resolve(r);
|
|
@@ -762,7 +792,7 @@ class SqliteRemoteConnection {
|
|
|
762
792
|
sql: ${sql},
|
|
763
793
|
params: ${params}
|
|
764
794
|
`);
|
|
765
|
-
|
|
795
|
+
reject(error);
|
|
766
796
|
}
|
|
767
797
|
});
|
|
768
798
|
}
|
|
@@ -772,7 +802,7 @@ class SqliteRemoteConnection {
|
|
|
772
802
|
return [];
|
|
773
803
|
}
|
|
774
804
|
;
|
|
775
|
-
if (sync ===
|
|
805
|
+
if (sync === SyncMode.Sync) {
|
|
776
806
|
logger.warn('SqliteRemote not suppouted sync mode');
|
|
777
807
|
return [];
|
|
778
808
|
}
|
|
@@ -780,7 +810,7 @@ class SqliteRemoteConnection {
|
|
|
780
810
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
781
811
|
logger.trace(Sqlstring.format(sql, params));
|
|
782
812
|
}
|
|
783
|
-
return new Promise(async (resolve) => {
|
|
813
|
+
return new Promise(async (resolve, reject) => {
|
|
784
814
|
try {
|
|
785
815
|
const r = await this[_daoConnection].query(this[_sqliteRemoteName], sql, params);
|
|
786
816
|
resolve(r);
|
|
@@ -791,7 +821,7 @@ class SqliteRemoteConnection {
|
|
|
791
821
|
sql: ${sql},
|
|
792
822
|
params: ${params}
|
|
793
823
|
`);
|
|
794
|
-
|
|
824
|
+
reject(error);
|
|
795
825
|
}
|
|
796
826
|
});
|
|
797
827
|
}
|
|
@@ -805,7 +835,7 @@ class SqliteRemote {
|
|
|
805
835
|
this[_sqliteRemoteName] = name;
|
|
806
836
|
}
|
|
807
837
|
createConnection(sync) {
|
|
808
|
-
if (sync ===
|
|
838
|
+
if (sync === SyncMode.Sync) {
|
|
809
839
|
logger.error('SQLITEREMOTE not suppouted sync mode');
|
|
810
840
|
return null;
|
|
811
841
|
}
|
|
@@ -819,7 +849,7 @@ class SqliteRemote {
|
|
|
819
849
|
return null;
|
|
820
850
|
}
|
|
821
851
|
close(sync) {
|
|
822
|
-
if (sync ===
|
|
852
|
+
if (sync === SyncMode.Async) {
|
|
823
853
|
return new Promise(async () => {
|
|
824
854
|
await this[_daoConnection].close();
|
|
825
855
|
});
|
|
@@ -827,7 +857,7 @@ class SqliteRemote {
|
|
|
827
857
|
;
|
|
828
858
|
}
|
|
829
859
|
backup(sync, name) {
|
|
830
|
-
if (sync ===
|
|
860
|
+
if (sync === SyncMode.Async) {
|
|
831
861
|
return new Promise(async () => {
|
|
832
862
|
await this[_daoConnection].backup(this[_sqliteRemoteName], name);
|
|
833
863
|
});
|
|
@@ -835,7 +865,7 @@ class SqliteRemote {
|
|
|
835
865
|
;
|
|
836
866
|
}
|
|
837
867
|
remove(sync) {
|
|
838
|
-
if (sync ===
|
|
868
|
+
if (sync === SyncMode.Async) {
|
|
839
869
|
return new Promise(async () => {
|
|
840
870
|
await this[_daoConnection].remove();
|
|
841
871
|
});
|
|
@@ -843,7 +873,7 @@ class SqliteRemote {
|
|
|
843
873
|
;
|
|
844
874
|
}
|
|
845
875
|
restore(sync, name) {
|
|
846
|
-
if (sync ===
|
|
876
|
+
if (sync === SyncMode.Async) {
|
|
847
877
|
return new Promise(async () => {
|
|
848
878
|
await this[_daoConnection].restore(this[_sqliteRemoteName], name);
|
|
849
879
|
});
|
|
@@ -851,34 +881,301 @@ class SqliteRemote {
|
|
|
851
881
|
;
|
|
852
882
|
}
|
|
853
883
|
}
|
|
884
|
+
class Build {
|
|
885
|
+
/**
|
|
886
|
+
*
|
|
887
|
+
* @param count 是否是count查询
|
|
888
|
+
* @param isSum 是否是sum查询
|
|
889
|
+
* @param param
|
|
890
|
+
*/
|
|
891
|
+
constructor(isCount, isSum, param = {}) {
|
|
892
|
+
this.brage = { haveOrderBy: false, haveLimit: false };
|
|
893
|
+
this.isCount = isCount;
|
|
894
|
+
this.isSum = isSum;
|
|
895
|
+
Object.assign(this, param);
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
*
|
|
899
|
+
* 当分页时将函数内包含的内容替换为COUNT(1)
|
|
900
|
+
* @returns
|
|
901
|
+
* @memberof Build
|
|
902
|
+
*/
|
|
903
|
+
pageTag() {
|
|
904
|
+
return (text, render) => {
|
|
905
|
+
if (this.isCount === true) {
|
|
906
|
+
return Build.page;
|
|
907
|
+
}
|
|
908
|
+
else if (this.isSum !== true) {
|
|
909
|
+
return render(text);
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
*
|
|
915
|
+
* 汇总查询专用
|
|
916
|
+
* @returns
|
|
917
|
+
* @memberof Build
|
|
918
|
+
*/
|
|
919
|
+
sumTag() {
|
|
920
|
+
return (text, render) => {
|
|
921
|
+
if (this.isSum !== true) {
|
|
922
|
+
return '';
|
|
923
|
+
}
|
|
924
|
+
else {
|
|
925
|
+
return render(text);
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
*
|
|
931
|
+
* 当分页时、汇总时忽略函数内包含的内容
|
|
932
|
+
* @returns
|
|
933
|
+
* @memberof Build
|
|
934
|
+
*/
|
|
935
|
+
pageIgnoreTag() {
|
|
936
|
+
return (text, render) => {
|
|
937
|
+
if (this.isCount === true || this.isSum === true) {
|
|
938
|
+
return '';
|
|
939
|
+
}
|
|
940
|
+
else {
|
|
941
|
+
return render(text);
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
*
|
|
947
|
+
* 将查询条件包起来,如果条件内容不为空,则自动添加WHERE,同时将第一个条件的and、or替换为空
|
|
948
|
+
* 例如:
|
|
949
|
+
* {{#whereTag}}
|
|
950
|
+
* and name = 1
|
|
951
|
+
* and page = 2
|
|
952
|
+
* {{/whereTag}}
|
|
953
|
+
* 输出
|
|
954
|
+
* where name = 1 and page = 2
|
|
955
|
+
* @returns
|
|
956
|
+
* @memberof Build
|
|
957
|
+
*/
|
|
958
|
+
where() {
|
|
959
|
+
return (text, render) => {
|
|
960
|
+
let data = render(text);
|
|
961
|
+
data = data.trim();
|
|
962
|
+
if (data) {
|
|
963
|
+
data = data.replace(/and|or/i, '');
|
|
964
|
+
return ` WHERE ${data} `;
|
|
965
|
+
}
|
|
966
|
+
else {
|
|
967
|
+
return '';
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* 删除第一个and、or
|
|
973
|
+
* 删除最后一个,
|
|
974
|
+
* 删除最后一个;
|
|
975
|
+
* @memberof Build
|
|
976
|
+
*/
|
|
977
|
+
trim() {
|
|
978
|
+
return (text, render) => {
|
|
979
|
+
let data = render(text);
|
|
980
|
+
data = data.trim();
|
|
981
|
+
if (data) {
|
|
982
|
+
data = data.replace(/(^and\s)|(^or\s)|(,$)|(;$)/i, '');
|
|
983
|
+
return data;
|
|
984
|
+
}
|
|
985
|
+
else {
|
|
986
|
+
return '';
|
|
987
|
+
}
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* 分页时将排序部分代码用此函数包起来,可以自动拼接order by
|
|
992
|
+
* 查询条数时,自动忽略此部分
|
|
993
|
+
* etc
|
|
994
|
+
* {{#orderTag}} name desc, age asc {{/orderTag}}
|
|
995
|
+
* ===
|
|
996
|
+
* ORDER BY name desc, age asc
|
|
997
|
+
* @returns
|
|
998
|
+
* @memberof Build
|
|
999
|
+
*/
|
|
1000
|
+
orderTag() {
|
|
1001
|
+
return (text, render) => {
|
|
1002
|
+
if (this.isCount === true || this.isSum === true) {
|
|
1003
|
+
return '';
|
|
1004
|
+
}
|
|
1005
|
+
else {
|
|
1006
|
+
this.brage.haveOrderBy = true;
|
|
1007
|
+
const orderBy = new Array();
|
|
1008
|
+
const renderOrder = render(text);
|
|
1009
|
+
if (/\S/.test(renderOrder)) {
|
|
1010
|
+
orderBy.push(renderOrder);
|
|
1011
|
+
}
|
|
1012
|
+
return orderBy.length > 0 ? ` ORDER BY ${orderBy.join(',')} ` : '';
|
|
1013
|
+
}
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
limitTag() {
|
|
1017
|
+
return (text, render) => {
|
|
1018
|
+
if (this.isCount === true || this.isSum === true) {
|
|
1019
|
+
return '';
|
|
1020
|
+
}
|
|
1021
|
+
else {
|
|
1022
|
+
this.brage.haveOrderBy = true;
|
|
1023
|
+
const orderBy = new Array();
|
|
1024
|
+
const renderOrder = render(text);
|
|
1025
|
+
if (/\S/.test(renderOrder)) {
|
|
1026
|
+
orderBy.push(renderOrder);
|
|
1027
|
+
}
|
|
1028
|
+
return orderBy.length > 0 ? ` ORDER BY ${orderBy.join(',')} ` : '';
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
*
|
|
1034
|
+
* 分页时将分组部分代码用此函数包起来,可以自动拼接GROUP BY
|
|
1035
|
+
* 当分页时、汇总时,自动忽略此部分
|
|
1036
|
+
* etc
|
|
1037
|
+
* {{#groupTag}} name, age {{/groupTag}}
|
|
1038
|
+
* ===
|
|
1039
|
+
* group by name.age
|
|
1040
|
+
* @returns
|
|
1041
|
+
* @memberof Build
|
|
1042
|
+
*/
|
|
1043
|
+
groupTag() {
|
|
1044
|
+
return (text, render) => {
|
|
1045
|
+
if (this.isCount === true || this.isSum === true) {
|
|
1046
|
+
return '';
|
|
1047
|
+
}
|
|
1048
|
+
else {
|
|
1049
|
+
const groupBy = render(text) || '';
|
|
1050
|
+
return /\S/.test(groupBy) ? ` GROUP BY ${groupBy} ` : '';
|
|
1051
|
+
}
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
*
|
|
1056
|
+
* beetween and
|
|
1057
|
+
* etc.
|
|
1058
|
+
* {{#between}} AND t.createtime | ({{createtime}}) {{/between}}
|
|
1059
|
+
* createtime: 1,2
|
|
1060
|
+
* ===
|
|
1061
|
+
* AND t.createtime BETWEEN 1 AND 2
|
|
1062
|
+
* @returns
|
|
1063
|
+
* @memberof Build
|
|
1064
|
+
*/
|
|
1065
|
+
between() {
|
|
1066
|
+
return (text, render) => {
|
|
1067
|
+
const result = render(text);
|
|
1068
|
+
if (/\(([\w\W]+)\)/.exec(result)) {
|
|
1069
|
+
return render(text).replace(/\(([\w\W]+)\)/, (a, b) => {
|
|
1070
|
+
if (a && b) {
|
|
1071
|
+
const xx = b.split(',');
|
|
1072
|
+
return `'${xx[0]}' AND '${xx[1]}'`;
|
|
1073
|
+
}
|
|
1074
|
+
else {
|
|
1075
|
+
return '';
|
|
1076
|
+
}
|
|
1077
|
+
}).replace(/\|/, ' BETWEEN ');
|
|
1078
|
+
}
|
|
1079
|
+
else {
|
|
1080
|
+
return '';
|
|
1081
|
+
}
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
*
|
|
1086
|
+
* 距离计算,单位米
|
|
1087
|
+
* etc
|
|
1088
|
+
* {{#distanceTag}} (t.longitude, t.latitude), ({{longitude}}, {{latitude}}) {{/distanceTag}}
|
|
1089
|
+
* ===
|
|
1090
|
+
* ROUND(ST_DISTANCE(POINT(longitude1, latitude1), POINT({{longitude}}, {{latitude}}))*111195, 2)
|
|
1091
|
+
* 可根据需求自行将数据转换为千米,例如
|
|
1092
|
+
* {{#distanceTag}} (t.longitude, t.latitude), ({{longitude}}, {{latitude}}) {{/distanceTag}} / 1000
|
|
1093
|
+
* @returns
|
|
1094
|
+
* @memberof Build
|
|
1095
|
+
*/
|
|
1096
|
+
distanceTag() {
|
|
1097
|
+
return (text, render) => {
|
|
1098
|
+
const result = render(text);
|
|
1099
|
+
if (/\(([^()]+)\)/.exec(result)) {
|
|
1100
|
+
let index = 0;
|
|
1101
|
+
return render(text).replace(/\(([^()]+)\)/g, (a, b) => {
|
|
1102
|
+
if (a && b) {
|
|
1103
|
+
const xx = b.split(',');
|
|
1104
|
+
if (index === 0) {
|
|
1105
|
+
index++;
|
|
1106
|
+
return ` ROUND(ST_DISTANCE(POINT(${xx[0]}, ${xx[1]}) `;
|
|
1107
|
+
}
|
|
1108
|
+
else {
|
|
1109
|
+
return ` POINT(${xx[0]}, ${xx[1]}))*111195, 2)`;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
else {
|
|
1113
|
+
return '';
|
|
1114
|
+
}
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
else {
|
|
1118
|
+
return '';
|
|
1119
|
+
}
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
Build.page = 'COUNT(1) zccw1986 ';
|
|
854
1124
|
class SqlCache {
|
|
855
1125
|
constructor() {
|
|
856
|
-
this.
|
|
1126
|
+
this.sqlMap = {};
|
|
1127
|
+
this.sqlFNMap = {};
|
|
857
1128
|
}
|
|
858
1129
|
async init(options) {
|
|
1130
|
+
if (options.sqlMap) {
|
|
1131
|
+
this.sqlMap = options.sqlMap;
|
|
1132
|
+
}
|
|
859
1133
|
if (options.sqlDir) {
|
|
860
1134
|
const sqlFis = globalThis[_fs].readdirSync(options.sqlDir);
|
|
861
1135
|
for (const modeName of sqlFis) {
|
|
862
|
-
const
|
|
863
|
-
const
|
|
864
|
-
|
|
865
|
-
|
|
1136
|
+
const extname = globalThis[_path].extname(modeName);
|
|
1137
|
+
const name = globalThis[_path].basename(modeName, extname);
|
|
1138
|
+
const file = globalThis[_path].join(options.sqlDir, modeName);
|
|
1139
|
+
if (extname === 'mu') {
|
|
1140
|
+
const parser = new MUParser(name, globalThis[_fs].readFileSync(file, { encoding: 'utf-8' }).toString());
|
|
1141
|
+
let source = parser.next();
|
|
1142
|
+
while (source != null) {
|
|
1143
|
+
this.sqlMap[source[0]] = source[1];
|
|
1144
|
+
logger.debug(`sql: ${source[0]} found!`);
|
|
1145
|
+
source = parser.next();
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
else if (extname === '.js') {
|
|
1149
|
+
const obj = (await import(globalThis[_path].join(options.sqlDir, modeName))).default;
|
|
1150
|
+
for (const [key, fn] of Object.entries(obj)) {
|
|
1151
|
+
this.sqlMap[`${name}.${String(key)}`] = fn;
|
|
1152
|
+
}
|
|
866
1153
|
}
|
|
867
1154
|
}
|
|
868
1155
|
}
|
|
869
|
-
|
|
870
|
-
this.
|
|
1156
|
+
if (options.sqlFNMap) {
|
|
1157
|
+
this.sqlFNMap = options.sqlFNMap;
|
|
1158
|
+
}
|
|
1159
|
+
if (options.sqlFNDir) {
|
|
1160
|
+
const sqlFis = globalThis[_fs].readdirSync(options.sqlDir);
|
|
1161
|
+
for (const modeName of sqlFis) {
|
|
1162
|
+
const extname = globalThis[_path].extname(modeName);
|
|
1163
|
+
const name = globalThis[_path].basename(modeName, extname);
|
|
1164
|
+
const file = globalThis[_path].join(options.sqlDir, modeName);
|
|
1165
|
+
if (extname === 'mu') {
|
|
1166
|
+
this.sqlFNMap[name] = globalThis[_fs].readFileSync(file, { encoding: 'utf-8' }).toString();
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
871
1169
|
}
|
|
872
1170
|
}
|
|
873
|
-
load(sqlid,
|
|
874
|
-
const sqlSource = this.
|
|
1171
|
+
load(sqlid, options) {
|
|
1172
|
+
const sqlSource = this.sqlMap[sqlid];
|
|
875
1173
|
Throw.if(!sqlSource, `指定的语句${sqlid}不存在!`);
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
}
|
|
1174
|
+
const _sql = typeof sqlSource === 'function' ? sqlSource(options) : sqlSource;
|
|
1175
|
+
const buildParam = new Build(options.isCount === true, options.isSum === true, options);
|
|
1176
|
+
const sql = mustache.render(_sql, buildParam, this.sqlFNMap);
|
|
1177
|
+
logger.debug(sqlid, sql);
|
|
1178
|
+
return sql;
|
|
882
1179
|
}
|
|
883
1180
|
}
|
|
884
1181
|
// #endregion
|
|
@@ -905,17 +1202,17 @@ function P(skipConn = false) {
|
|
|
905
1202
|
const startTime = +new Date();
|
|
906
1203
|
// option
|
|
907
1204
|
const option = args[0] = Object.assign({}, globalThis[_GlobalSqlOption], this[_SqlOption], args[0]);
|
|
908
|
-
option.sync ?? (option.sync =
|
|
1205
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
909
1206
|
const dbName = option?.dbName ?? this[_daoDBName] ?? _primaryDB;
|
|
910
1207
|
option.dao = globalThis[_dao][this[_dbType]][dbName];
|
|
911
1208
|
Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
|
|
912
1209
|
option.tableName = option?.tableName ?? this[_tableName];
|
|
913
1210
|
const tableES = Sqlstring.escapeId(option.tableName);
|
|
914
1211
|
if (this[_dbType] === DBType.Sqlite) {
|
|
915
|
-
Throw.if(option.sync ===
|
|
1212
|
+
Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
|
|
916
1213
|
// 连接共享
|
|
917
1214
|
if (skipConn === false && !option.conn) {
|
|
918
|
-
option.conn = option.dao.createConnection(
|
|
1215
|
+
option.conn = option.dao.createConnection(SyncMode.Sync);
|
|
919
1216
|
}
|
|
920
1217
|
else {
|
|
921
1218
|
needRealseConn = false;
|
|
@@ -923,20 +1220,20 @@ function P(skipConn = false) {
|
|
|
923
1220
|
if (skipConn === false) {
|
|
924
1221
|
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
925
1222
|
// 检查表
|
|
926
|
-
const tableCheckResult = option.conn.pluck(
|
|
1223
|
+
const tableCheckResult = option.conn.pluck(SyncMode.Sync, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
927
1224
|
if (tableCheckResult) {
|
|
928
1225
|
// 旧版本
|
|
929
|
-
const tableVersion = option.conn.pluck(
|
|
1226
|
+
const tableVersion = option.conn.pluck(SyncMode.Sync, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
930
1227
|
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
931
1228
|
// 更新版本
|
|
932
|
-
const columns = iterare(option.conn.query(
|
|
1229
|
+
const columns = iterare(option.conn.query(SyncMode.Sync, `PRAGMA table_info(${tableES})`))
|
|
933
1230
|
.filter(c => this[_fields].hasOwnProperty(c.name))
|
|
934
1231
|
.map(c => Sqlstring.escapeId(c.name))
|
|
935
1232
|
.join(',');
|
|
936
1233
|
const rtable = Sqlstring.escapeId(`${option.tableName}_${tableVersion.replace(/\./, '_')}`);
|
|
937
|
-
option.conn.execute(
|
|
938
|
-
option.conn.execute(
|
|
939
|
-
option.conn.execute(
|
|
1234
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1235
|
+
option.conn.execute(SyncMode.Sync, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1236
|
+
option.conn.execute(SyncMode.Sync, `
|
|
940
1237
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
941
1238
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
942
1239
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -944,22 +1241,22 @@ function P(skipConn = false) {
|
|
|
944
1241
|
`);
|
|
945
1242
|
if (this[_index] && this[_index].length) {
|
|
946
1243
|
for (const index of this[_index]) {
|
|
947
|
-
option.conn.execute(
|
|
1244
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
948
1245
|
}
|
|
949
1246
|
}
|
|
950
|
-
option.conn.execute(
|
|
951
|
-
option.conn.execute(
|
|
952
|
-
option.conn.execute(
|
|
1247
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1248
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1249
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
953
1250
|
// 更新完毕,保存版本号
|
|
954
|
-
option.conn.execute(
|
|
1251
|
+
option.conn.execute(SyncMode.Sync, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
955
1252
|
}
|
|
956
1253
|
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
957
|
-
option.conn.execute(
|
|
1254
|
+
option.conn.execute(SyncMode.Sync, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
958
1255
|
}
|
|
959
1256
|
}
|
|
960
1257
|
else { // 表不存在
|
|
961
1258
|
// 创建表
|
|
962
|
-
option.conn.execute(
|
|
1259
|
+
option.conn.execute(SyncMode.Sync, `
|
|
963
1260
|
CREATE TABLE IF NOT EXISTS ${tableES} (
|
|
964
1261
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
965
1262
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -968,10 +1265,10 @@ function P(skipConn = false) {
|
|
|
968
1265
|
`);
|
|
969
1266
|
if (this[_index] && this[_index].length) {
|
|
970
1267
|
for (const index of this[_index]) {
|
|
971
|
-
option.conn.execute(
|
|
1268
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
972
1269
|
}
|
|
973
1270
|
}
|
|
974
|
-
option.conn.execute(
|
|
1271
|
+
option.conn.execute(SyncMode.Sync, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
975
1272
|
}
|
|
976
1273
|
}
|
|
977
1274
|
try {
|
|
@@ -990,7 +1287,7 @@ function P(skipConn = false) {
|
|
|
990
1287
|
finally {
|
|
991
1288
|
if (needRealseConn && option && option.conn) {
|
|
992
1289
|
try {
|
|
993
|
-
option.conn.realse(
|
|
1290
|
+
option.conn.realse(SyncMode.Sync);
|
|
994
1291
|
}
|
|
995
1292
|
catch (error) {
|
|
996
1293
|
}
|
|
@@ -998,11 +1295,11 @@ function P(skipConn = false) {
|
|
|
998
1295
|
}
|
|
999
1296
|
}
|
|
1000
1297
|
else if (this[_dbType] === DBType.SqliteRemote) {
|
|
1001
|
-
Throw.if(option.sync ===
|
|
1002
|
-
return new Promise(async (resolve) => {
|
|
1298
|
+
Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
|
|
1299
|
+
return new Promise(async (resolve, reject) => {
|
|
1003
1300
|
// 连接共享
|
|
1004
1301
|
if (skipConn === false && !option.conn) {
|
|
1005
|
-
(option).conn = await option.dao.createConnection(
|
|
1302
|
+
(option).conn = await option.dao.createConnection(SyncMode.Async);
|
|
1006
1303
|
}
|
|
1007
1304
|
else {
|
|
1008
1305
|
needRealseConn = false;
|
|
@@ -1010,20 +1307,20 @@ function P(skipConn = false) {
|
|
|
1010
1307
|
if (skipConn === false) {
|
|
1011
1308
|
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
1012
1309
|
// 检查表
|
|
1013
|
-
const tableCheckResult = await option.conn.pluck(
|
|
1310
|
+
const tableCheckResult = await option.conn.pluck(SyncMode.Async, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
1014
1311
|
if (tableCheckResult) {
|
|
1015
1312
|
// 旧版本
|
|
1016
|
-
const tableVersion = await option.conn.pluck(
|
|
1313
|
+
const tableVersion = await option.conn.pluck(SyncMode.Async, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
1017
1314
|
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
1018
1315
|
// 更新版本
|
|
1019
|
-
const columns = iterare(await option.conn.query(
|
|
1316
|
+
const columns = iterare(await option.conn.query(SyncMode.Async, `PRAGMA table_info(${tableES})`))
|
|
1020
1317
|
.filter(c => this[_fields].hasOwnProperty(c.name))
|
|
1021
1318
|
.map(c => Sqlstring.escapeId(c.name))
|
|
1022
1319
|
.join(',');
|
|
1023
1320
|
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
1024
|
-
await option.conn.execute(
|
|
1025
|
-
await option.conn.execute(
|
|
1026
|
-
await option.conn.execute(
|
|
1321
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1322
|
+
await option.conn.execute(SyncMode.Async, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1323
|
+
await option.conn.execute(SyncMode.Async, `
|
|
1027
1324
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1028
1325
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1029
1326
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -1031,22 +1328,22 @@ function P(skipConn = false) {
|
|
|
1031
1328
|
`);
|
|
1032
1329
|
if (this[_index] && this[_index].length) {
|
|
1033
1330
|
for (const index of this[_index]) {
|
|
1034
|
-
await option.conn.execute(
|
|
1331
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
1035
1332
|
}
|
|
1036
1333
|
}
|
|
1037
|
-
await option.conn.execute(
|
|
1038
|
-
await option.conn.execute(
|
|
1039
|
-
await option.conn.execute(
|
|
1334
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1335
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1336
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1040
1337
|
// 更新完毕,保存版本号
|
|
1041
|
-
await option.conn.execute(
|
|
1338
|
+
await option.conn.execute(SyncMode.Async, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
1042
1339
|
}
|
|
1043
1340
|
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
1044
|
-
await option.conn.execute(
|
|
1341
|
+
await option.conn.execute(SyncMode.Async, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1045
1342
|
}
|
|
1046
1343
|
}
|
|
1047
1344
|
else { // 表不存在
|
|
1048
1345
|
// 创建表
|
|
1049
|
-
await option.conn.execute(
|
|
1346
|
+
await option.conn.execute(SyncMode.Async, `
|
|
1050
1347
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1051
1348
|
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1052
1349
|
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.esName).join(',')})` : ''}
|
|
@@ -1054,10 +1351,10 @@ function P(skipConn = false) {
|
|
|
1054
1351
|
`);
|
|
1055
1352
|
if (this[_index] && this[_index].length) {
|
|
1056
1353
|
for (const index of this[_index]) {
|
|
1057
|
-
await option.conn.execute(
|
|
1354
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${Sqlstring.escapeId(option.tableName)} ("${index}");`);
|
|
1058
1355
|
}
|
|
1059
1356
|
}
|
|
1060
|
-
await option.conn.execute(
|
|
1357
|
+
await option.conn.execute(SyncMode.Async, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1061
1358
|
}
|
|
1062
1359
|
}
|
|
1063
1360
|
try {
|
|
@@ -1067,12 +1364,12 @@ function P(skipConn = false) {
|
|
|
1067
1364
|
}
|
|
1068
1365
|
catch (error) {
|
|
1069
1366
|
console.error(`service ${propertyKey} have an error:${error}, it's argumens: ${JSON.stringify(args.filter(i => typeof i !== 'object' || (typeof i === 'object' && !i.insert)))}`);
|
|
1070
|
-
|
|
1367
|
+
reject(error);
|
|
1071
1368
|
}
|
|
1072
1369
|
finally {
|
|
1073
1370
|
if (needRealseConn && option && option.conn) {
|
|
1074
1371
|
try {
|
|
1075
|
-
option.conn.realse(
|
|
1372
|
+
option.conn.realse(SyncMode.Sync);
|
|
1076
1373
|
}
|
|
1077
1374
|
catch (error) {
|
|
1078
1375
|
}
|
|
@@ -1081,11 +1378,11 @@ function P(skipConn = false) {
|
|
|
1081
1378
|
});
|
|
1082
1379
|
}
|
|
1083
1380
|
else if (this[_dbType] === DBType.Mysql) {
|
|
1084
|
-
return new Promise(async (resolve) => {
|
|
1381
|
+
return new Promise(async (resolve, reject) => {
|
|
1085
1382
|
try {
|
|
1086
1383
|
// 连接共享
|
|
1087
1384
|
if (skipConn === false && !option.conn) {
|
|
1088
|
-
(option).conn = await option.dao.createConnection(
|
|
1385
|
+
(option).conn = await option.dao.createConnection(SyncMode.Async);
|
|
1089
1386
|
}
|
|
1090
1387
|
else {
|
|
1091
1388
|
needRealseConn = false;
|
|
@@ -1096,12 +1393,12 @@ function P(skipConn = false) {
|
|
|
1096
1393
|
}
|
|
1097
1394
|
catch (error) {
|
|
1098
1395
|
console.error(`service ${propertyKey} have an error:${error}, it's argumens: ${JSON.stringify(args.filter(i => typeof i !== 'object' || (typeof i === 'object' && !i.insert)))}`);
|
|
1099
|
-
|
|
1396
|
+
reject(error);
|
|
1100
1397
|
}
|
|
1101
1398
|
finally {
|
|
1102
1399
|
if (needRealseConn && option && option.conn) {
|
|
1103
1400
|
try {
|
|
1104
|
-
option.conn.realse(
|
|
1401
|
+
option.conn.realse(SyncMode.Sync);
|
|
1105
1402
|
}
|
|
1106
1403
|
catch (error) {
|
|
1107
1404
|
}
|
|
@@ -1150,19 +1447,19 @@ export const Field = (config) => {
|
|
|
1150
1447
|
field.esName = Sqlstring.escapeId(propertyName);
|
|
1151
1448
|
const hasDef = field.hasOwnProperty('def') === true;
|
|
1152
1449
|
switch (field.type) {
|
|
1153
|
-
case SqlType.
|
|
1154
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1450
|
+
case SqlType.tinyint: {
|
|
1451
|
+
field[DBType.Mysql] = `${field.esName} tinyint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''}`;
|
|
1155
1452
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1156
1453
|
break;
|
|
1157
1454
|
}
|
|
1158
|
-
case SqlType.
|
|
1159
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1160
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName}
|
|
1455
|
+
case SqlType.smallint: {
|
|
1456
|
+
field[DBType.Mysql] = `${field.esName} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''}`;
|
|
1457
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1161
1458
|
break;
|
|
1162
1459
|
}
|
|
1163
|
-
case SqlType.
|
|
1164
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1165
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName}
|
|
1460
|
+
case SqlType.mediumint: {
|
|
1461
|
+
field[DBType.Mysql] = `${field.esName} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''}`;
|
|
1462
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1166
1463
|
break;
|
|
1167
1464
|
}
|
|
1168
1465
|
case SqlType.int: {
|
|
@@ -1170,6 +1467,26 @@ export const Field = (config) => {
|
|
|
1170
1467
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1171
1468
|
break;
|
|
1172
1469
|
}
|
|
1470
|
+
case SqlType.bigint: {
|
|
1471
|
+
field[DBType.Mysql] = `${field.esName} bigint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1472
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1473
|
+
break;
|
|
1474
|
+
}
|
|
1475
|
+
case SqlType.float: {
|
|
1476
|
+
field[DBType.Mysql] = `${field.esName} float(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1477
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1478
|
+
break;
|
|
1479
|
+
}
|
|
1480
|
+
case SqlType.double: {
|
|
1481
|
+
field[DBType.Mysql] = `${field.esName} double(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1482
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1483
|
+
break;
|
|
1484
|
+
}
|
|
1485
|
+
case SqlType.decimal: {
|
|
1486
|
+
field[DBType.Mysql] = `${field.esName} decimal(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1487
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1488
|
+
break;
|
|
1489
|
+
}
|
|
1173
1490
|
case SqlType.longtext: {
|
|
1174
1491
|
field[DBType.Mysql] = `${field.esName} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1175
1492
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
@@ -1180,8 +1497,58 @@ export const Field = (config) => {
|
|
|
1180
1497
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1181
1498
|
break;
|
|
1182
1499
|
}
|
|
1183
|
-
case SqlType.
|
|
1184
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1500
|
+
case SqlType.text: {
|
|
1501
|
+
field[DBType.Mysql] = `${field.esName} text ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1502
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1503
|
+
break;
|
|
1504
|
+
}
|
|
1505
|
+
case SqlType.date: {
|
|
1506
|
+
field[DBType.Mysql] = `${field.esName} date ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1507
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1508
|
+
break;
|
|
1509
|
+
}
|
|
1510
|
+
case SqlType.time: {
|
|
1511
|
+
field[DBType.Mysql] = `${field.esName} time ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1512
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1513
|
+
break;
|
|
1514
|
+
}
|
|
1515
|
+
case SqlType.year: {
|
|
1516
|
+
field[DBType.Mysql] = `${field.esName} year ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1517
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1518
|
+
break;
|
|
1519
|
+
}
|
|
1520
|
+
case SqlType.datetime: {
|
|
1521
|
+
field[DBType.Mysql] = `${field.esName} datetime ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1522
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1523
|
+
break;
|
|
1524
|
+
}
|
|
1525
|
+
case SqlType.timestamp: {
|
|
1526
|
+
field[DBType.Mysql] = `${field.esName} timestamp ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1527
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} integer`;
|
|
1528
|
+
break;
|
|
1529
|
+
}
|
|
1530
|
+
case SqlType.char: {
|
|
1531
|
+
field[DBType.Mysql] = `${field.esName} char(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1532
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1533
|
+
break;
|
|
1534
|
+
}
|
|
1535
|
+
case SqlType.varchar: {
|
|
1536
|
+
field[DBType.Mysql] = `${field.esName} varchar(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1537
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1538
|
+
break;
|
|
1539
|
+
}
|
|
1540
|
+
case SqlType.tinyblob: {
|
|
1541
|
+
field[DBType.Mysql] = `${field.esName} tinyblob ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1542
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1543
|
+
break;
|
|
1544
|
+
}
|
|
1545
|
+
case SqlType.tinytext: {
|
|
1546
|
+
field[DBType.Mysql] = `${field.esName} tinytext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1547
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1548
|
+
break;
|
|
1549
|
+
}
|
|
1550
|
+
case SqlType.blob: {
|
|
1551
|
+
field[DBType.Mysql] = `${field.esName} binary ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1185
1552
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1186
1553
|
break;
|
|
1187
1554
|
}
|
|
@@ -1190,13 +1557,78 @@ export const Field = (config) => {
|
|
|
1190
1557
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1191
1558
|
break;
|
|
1192
1559
|
}
|
|
1193
|
-
case SqlType.
|
|
1194
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1560
|
+
case SqlType.mediumblob: {
|
|
1561
|
+
field[DBType.Mysql] = `${field.esName} mediumblob ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1195
1562
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1196
1563
|
break;
|
|
1197
1564
|
}
|
|
1198
|
-
case SqlType.
|
|
1199
|
-
field[DBType.Mysql] = `${field.esName}
|
|
1565
|
+
case SqlType.mediumtext: {
|
|
1566
|
+
field[DBType.Mysql] = `${field.esName} mediumtext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1567
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1568
|
+
break;
|
|
1569
|
+
}
|
|
1570
|
+
case SqlType.longblob: {
|
|
1571
|
+
field[DBType.Mysql] = `${field.esName} longblob ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1572
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1573
|
+
break;
|
|
1574
|
+
}
|
|
1575
|
+
case SqlType.longtext: {
|
|
1576
|
+
field[DBType.Mysql] = `${field.esName} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1577
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1578
|
+
break;
|
|
1579
|
+
}
|
|
1580
|
+
case SqlType.set: {
|
|
1581
|
+
field[DBType.Mysql] = `${field.esName} set ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1582
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1583
|
+
break;
|
|
1584
|
+
}
|
|
1585
|
+
case SqlType.enum: {
|
|
1586
|
+
field[DBType.Mysql] = `${field.esName} enum ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1587
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1588
|
+
break;
|
|
1589
|
+
}
|
|
1590
|
+
case SqlType.json: {
|
|
1591
|
+
field[DBType.Mysql] = `${field.esName} json ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1592
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1593
|
+
break;
|
|
1594
|
+
}
|
|
1595
|
+
case SqlType.geometry: {
|
|
1596
|
+
field[DBType.Mysql] = `${field.esName} geometry ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1597
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1598
|
+
break;
|
|
1599
|
+
}
|
|
1600
|
+
case SqlType.point: {
|
|
1601
|
+
field[DBType.Mysql] = `${field.esName} point ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1602
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1603
|
+
break;
|
|
1604
|
+
}
|
|
1605
|
+
case SqlType.linestring: {
|
|
1606
|
+
field[DBType.Mysql] = `${field.esName} linestring ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1607
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1608
|
+
break;
|
|
1609
|
+
}
|
|
1610
|
+
case SqlType.polygon: {
|
|
1611
|
+
field[DBType.Mysql] = `${field.esName} polygon ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1612
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1613
|
+
break;
|
|
1614
|
+
}
|
|
1615
|
+
case SqlType.multipoint: {
|
|
1616
|
+
field[DBType.Mysql] = `${field.esName} multipoint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1617
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1618
|
+
break;
|
|
1619
|
+
}
|
|
1620
|
+
case SqlType.multilinestring: {
|
|
1621
|
+
field[DBType.Mysql] = `${field.esName} multilinestring ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1622
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1623
|
+
break;
|
|
1624
|
+
}
|
|
1625
|
+
case SqlType.multipolygon: {
|
|
1626
|
+
field[DBType.Mysql] = `${field.esName} multipolygon ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1627
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1628
|
+
break;
|
|
1629
|
+
}
|
|
1630
|
+
case SqlType.geometrycollection: {
|
|
1631
|
+
field[DBType.Mysql] = `${field.esName} geometrycollection ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1200
1632
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1201
1633
|
break;
|
|
1202
1634
|
}
|
|
@@ -1352,7 +1784,7 @@ export class SqlService {
|
|
|
1352
1784
|
const sqls = [];
|
|
1353
1785
|
const tableName = Sqlstring.escapeId(option.tableName);
|
|
1354
1786
|
switch (option?.mode) {
|
|
1355
|
-
case
|
|
1787
|
+
case InsertMode.InsertIfNotExists: {
|
|
1356
1788
|
const conditions = option.existConditionOtherThanIds || this[_ids];
|
|
1357
1789
|
Throw.if(!conditions, 'not found where condition for insertIfNotExists!');
|
|
1358
1790
|
Throw.if(conditions.length === 0, 'insertIfNotExists must have not null where!');
|
|
@@ -1391,7 +1823,7 @@ export class SqlService {
|
|
|
1391
1823
|
${selects};`;
|
|
1392
1824
|
sqls.push({ sql, params });
|
|
1393
1825
|
}
|
|
1394
|
-
case
|
|
1826
|
+
case InsertMode.Replace: {
|
|
1395
1827
|
const finalColumns = new Set();
|
|
1396
1828
|
const params = datas
|
|
1397
1829
|
.map(data => this[_transformer](data, { ...option, finalColumns, def: true }))
|
|
@@ -1419,7 +1851,7 @@ export class SqlService {
|
|
|
1419
1851
|
`;
|
|
1420
1852
|
sqls.push({ sql, params });
|
|
1421
1853
|
}
|
|
1422
|
-
case
|
|
1854
|
+
case InsertMode.Insert: {
|
|
1423
1855
|
const finalColumns = new Set();
|
|
1424
1856
|
const params = datas
|
|
1425
1857
|
.map(data => this[_transformer](data, { ...option, finalColumns, def: true }))
|
|
@@ -1447,7 +1879,7 @@ export class SqlService {
|
|
|
1447
1879
|
`;
|
|
1448
1880
|
sqls.push({ sql, params });
|
|
1449
1881
|
}
|
|
1450
|
-
case
|
|
1882
|
+
case InsertMode.InsertWithTempTable: {
|
|
1451
1883
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1452
1884
|
const tableTempESC = Sqlstring.escapeId(tableTemp);
|
|
1453
1885
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
@@ -1490,16 +1922,16 @@ export class SqlService {
|
|
|
1490
1922
|
return sqls;
|
|
1491
1923
|
}
|
|
1492
1924
|
insert(option) {
|
|
1493
|
-
option.mode ?? (option.mode =
|
|
1925
|
+
option.mode ?? (option.mode = InsertMode.Insert);
|
|
1494
1926
|
const isArray = option.data instanceof Array;
|
|
1495
1927
|
const datas = option.data instanceof Array ? option.data : [option.data];
|
|
1496
|
-
if (option.sync ===
|
|
1928
|
+
if (option.sync === SyncMode.Sync) {
|
|
1497
1929
|
const fn = () => {
|
|
1498
1930
|
const result = excuteSplit(ExcuteSplitMode.SyncTrust, datas, _data => {
|
|
1499
1931
|
const sqls = this._insert(_data, option);
|
|
1500
1932
|
let result = 0n;
|
|
1501
1933
|
for (const { sql, params } of sqls) {
|
|
1502
|
-
const dd = option.conn.execute(
|
|
1934
|
+
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1503
1935
|
if (dd.insertId) {
|
|
1504
1936
|
result += dd.insertId;
|
|
1505
1937
|
}
|
|
@@ -1515,17 +1947,17 @@ export class SqlService {
|
|
|
1515
1947
|
return fn();
|
|
1516
1948
|
}
|
|
1517
1949
|
else {
|
|
1518
|
-
return option?.dao?.transaction(
|
|
1950
|
+
return option?.dao?.transaction(SyncMode.Sync, fn, option?.conn);
|
|
1519
1951
|
}
|
|
1520
1952
|
}
|
|
1521
1953
|
else if (isArray) {
|
|
1522
1954
|
const fn = async () => {
|
|
1523
|
-
return await option?.dao?.transaction(
|
|
1955
|
+
return await option?.dao?.transaction(SyncMode.Async, async () => {
|
|
1524
1956
|
const result = await excuteSplit(ExcuteSplitMode.AsyncTrust, datas, async (_data) => {
|
|
1525
1957
|
const sqls = this._insert(_data, option);
|
|
1526
1958
|
let result = 0n;
|
|
1527
1959
|
for (const { sql, params } of sqls) {
|
|
1528
|
-
const dd = await option?.conn.execute(
|
|
1960
|
+
const dd = await option?.conn.execute(SyncMode.Async, sql, params);
|
|
1529
1961
|
if (dd.insertId) {
|
|
1530
1962
|
result += dd.insertId;
|
|
1531
1963
|
}
|
|
@@ -1540,7 +1972,7 @@ export class SqlService {
|
|
|
1540
1972
|
resolve((await fn()));
|
|
1541
1973
|
}
|
|
1542
1974
|
else {
|
|
1543
|
-
await option?.dao?.transaction(
|
|
1975
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1544
1976
|
}
|
|
1545
1977
|
});
|
|
1546
1978
|
}
|
|
@@ -1550,7 +1982,7 @@ export class SqlService {
|
|
|
1550
1982
|
const sqls = this._insert(_data, option);
|
|
1551
1983
|
let result = 0n;
|
|
1552
1984
|
for (const { sql, params } of sqls) {
|
|
1553
|
-
const dd = await option.conn.execute(
|
|
1985
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1554
1986
|
if (dd.insertId) {
|
|
1555
1987
|
result += dd.insertId;
|
|
1556
1988
|
}
|
|
@@ -1564,7 +1996,7 @@ export class SqlService {
|
|
|
1564
1996
|
resolve((await fn()));
|
|
1565
1997
|
}
|
|
1566
1998
|
else {
|
|
1567
|
-
await option?.dao?.transaction(
|
|
1999
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1568
2000
|
}
|
|
1569
2001
|
});
|
|
1570
2002
|
}
|
|
@@ -1605,13 +2037,13 @@ export class SqlService {
|
|
|
1605
2037
|
update(option) {
|
|
1606
2038
|
Throw.if(!this[_ids] || this[_ids].length === 0, 'not found id');
|
|
1607
2039
|
const datas = option.data instanceof Array ? option.data : [option.data];
|
|
1608
|
-
if (option.sync ===
|
|
2040
|
+
if (option.sync === SyncMode.Sync) {
|
|
1609
2041
|
const fn = () => {
|
|
1610
2042
|
const result = excuteSplit(ExcuteSplitMode.SyncTrust, datas, _data => {
|
|
1611
2043
|
const sqls = this._update(_data, option);
|
|
1612
2044
|
let result = 0;
|
|
1613
2045
|
for (const { sql, params } of sqls) {
|
|
1614
|
-
const dd = option.conn.execute(
|
|
2046
|
+
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1615
2047
|
if (dd.affectedRows) {
|
|
1616
2048
|
result += dd.affectedRows;
|
|
1617
2049
|
}
|
|
@@ -1624,7 +2056,7 @@ export class SqlService {
|
|
|
1624
2056
|
return fn();
|
|
1625
2057
|
}
|
|
1626
2058
|
else {
|
|
1627
|
-
return option?.dao?.transaction(
|
|
2059
|
+
return option?.dao?.transaction(SyncMode.Sync, fn, option?.conn);
|
|
1628
2060
|
}
|
|
1629
2061
|
}
|
|
1630
2062
|
else {
|
|
@@ -1633,7 +2065,7 @@ export class SqlService {
|
|
|
1633
2065
|
const sqls = this._update(_data, option);
|
|
1634
2066
|
let result = 0;
|
|
1635
2067
|
for (const { sql, params } of sqls) {
|
|
1636
|
-
const dd = await option.conn.execute(
|
|
2068
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1637
2069
|
if (dd.affectedRows) {
|
|
1638
2070
|
result += dd.affectedRows;
|
|
1639
2071
|
}
|
|
@@ -1647,7 +2079,7 @@ export class SqlService {
|
|
|
1647
2079
|
resolve((await fn()));
|
|
1648
2080
|
}
|
|
1649
2081
|
else {
|
|
1650
|
-
await option?.dao?.transaction(
|
|
2082
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1651
2083
|
}
|
|
1652
2084
|
});
|
|
1653
2085
|
}
|
|
@@ -1658,7 +2090,7 @@ export class SqlService {
|
|
|
1658
2090
|
Throw.if(!option.id && !option.where, 'not found id or where!');
|
|
1659
2091
|
Throw.if(!!option.id && !!this[_ids] && this[_ids].length > 1, 'muit id must set where!');
|
|
1660
2092
|
Throw.if(!!option.id && !!option.where, 'id and where only one can set!');
|
|
1661
|
-
option.mode ?? (option.mode =
|
|
2093
|
+
option.mode ?? (option.mode = DeleteMode.Common);
|
|
1662
2094
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1663
2095
|
const tableTempESC = Sqlstring.escapeId(tableTemp);
|
|
1664
2096
|
const tableNameESC = Sqlstring.escapeId(option?.tableName);
|
|
@@ -1669,7 +2101,7 @@ export class SqlService {
|
|
|
1669
2101
|
}
|
|
1670
2102
|
const wheres = option.where instanceof Array ? option.where : [option.where];
|
|
1671
2103
|
const sqls = [];
|
|
1672
|
-
if (option.mode ===
|
|
2104
|
+
if (option.mode === DeleteMode.Common) {
|
|
1673
2105
|
const params = new Array();
|
|
1674
2106
|
const whereSql = iterare(wheres).map(where => {
|
|
1675
2107
|
return `(
|
|
@@ -1726,11 +2158,11 @@ export class SqlService {
|
|
|
1726
2158
|
}
|
|
1727
2159
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
1728
2160
|
}
|
|
1729
|
-
if (option.sync ===
|
|
2161
|
+
if (option.sync === SyncMode.Sync) {
|
|
1730
2162
|
const fn = () => {
|
|
1731
2163
|
let result = 0;
|
|
1732
2164
|
for (const { sql, params } of sqls) {
|
|
1733
|
-
const dd = option.conn.execute(
|
|
2165
|
+
const dd = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1734
2166
|
result += dd.affectedRows;
|
|
1735
2167
|
}
|
|
1736
2168
|
return result;
|
|
@@ -1739,14 +2171,14 @@ export class SqlService {
|
|
|
1739
2171
|
return fn();
|
|
1740
2172
|
}
|
|
1741
2173
|
else {
|
|
1742
|
-
return option?.dao?.transaction(
|
|
2174
|
+
return option?.dao?.transaction(SyncMode.Sync, fn, option?.conn);
|
|
1743
2175
|
}
|
|
1744
2176
|
}
|
|
1745
2177
|
else {
|
|
1746
2178
|
const fn = async () => {
|
|
1747
2179
|
let result = 0;
|
|
1748
2180
|
for (const { sql, params } of sqls) {
|
|
1749
|
-
const dd = await option.conn.execute(
|
|
2181
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1750
2182
|
result += dd.affectedRows;
|
|
1751
2183
|
}
|
|
1752
2184
|
return result;
|
|
@@ -1756,25 +2188,25 @@ export class SqlService {
|
|
|
1756
2188
|
resolve((await fn()));
|
|
1757
2189
|
}
|
|
1758
2190
|
else {
|
|
1759
|
-
await option?.dao?.transaction(
|
|
2191
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
1760
2192
|
}
|
|
1761
2193
|
});
|
|
1762
2194
|
}
|
|
1763
2195
|
}
|
|
1764
|
-
|
|
1765
|
-
switch (
|
|
1766
|
-
case
|
|
2196
|
+
_template(templateResult, result, error) {
|
|
2197
|
+
switch (templateResult) {
|
|
2198
|
+
case TemplateResult.AssertOne: {
|
|
1767
2199
|
Throw.if(!result || result.length !== 1, error);
|
|
1768
2200
|
return result[0];
|
|
1769
2201
|
}
|
|
1770
|
-
case
|
|
2202
|
+
case TemplateResult.NotSureOne: {
|
|
1771
2203
|
Throw.if(!result, error);
|
|
1772
2204
|
return result[0] ?? null;
|
|
1773
2205
|
}
|
|
1774
|
-
case
|
|
2206
|
+
case TemplateResult.Many: {
|
|
1775
2207
|
return result;
|
|
1776
2208
|
}
|
|
1777
|
-
case
|
|
2209
|
+
case TemplateResult.Count: {
|
|
1778
2210
|
return result[0].ct;
|
|
1779
2211
|
}
|
|
1780
2212
|
}
|
|
@@ -1785,8 +2217,8 @@ export class SqlService {
|
|
|
1785
2217
|
Throw.if(!option.id && !option.where, 'not found id or where!');
|
|
1786
2218
|
Throw.if(!!option.id && !!this[_ids] && this[_ids].length > 1, 'muit id must set where!');
|
|
1787
2219
|
Throw.if(!!option.id && !!option.where, 'id and where only one can set!');
|
|
1788
|
-
option.mode ?? (option.mode =
|
|
1789
|
-
option.
|
|
2220
|
+
option.mode ?? (option.mode = SelectMode.Common);
|
|
2221
|
+
option.templateResult ?? (option.templateResult = TemplateResult.AssertOne);
|
|
1790
2222
|
option.error ?? (option.error = 'error data!');
|
|
1791
2223
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1792
2224
|
const tableTempESC = Sqlstring.escapeId(tableTemp);
|
|
@@ -1796,11 +2228,11 @@ export class SqlService {
|
|
|
1796
2228
|
const ids = option.id instanceof Array ? option.id : [option.id];
|
|
1797
2229
|
option.where = ids.map(i => ({ [idName]: i }));
|
|
1798
2230
|
}
|
|
1799
|
-
const columns = option.
|
|
2231
|
+
const columns = option.templateResult === TemplateResult.Count ? 'COUNT(1) ct' : iterare((option.columns ?? this[_columns])).map((K) => `a.${this[_fields][K]?.esName}`).join(',');
|
|
1800
2232
|
const wheres = option.where instanceof Array ? option.where : [option.where];
|
|
1801
2233
|
const sqls = [];
|
|
1802
2234
|
let resultIndex = -1;
|
|
1803
|
-
if (option.mode ===
|
|
2235
|
+
if (option.mode === SelectMode.Common) {
|
|
1804
2236
|
const params = new Array();
|
|
1805
2237
|
const whereSql = iterare(wheres).map(where => {
|
|
1806
2238
|
return `SELECT ${columns} FROM ${tableNameESC} a WHERE
|
|
@@ -1821,79 +2253,79 @@ export class SqlService {
|
|
|
1821
2253
|
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
2254
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
1823
2255
|
}
|
|
1824
|
-
if (option.sync ===
|
|
2256
|
+
if (option.sync === SyncMode.Sync) {
|
|
1825
2257
|
let result;
|
|
1826
2258
|
for (let i = 0; i < sqls.length; i++) {
|
|
1827
2259
|
if (i === resultIndex) {
|
|
1828
|
-
result = option.conn.query(
|
|
2260
|
+
result = option.conn.query(SyncMode.Sync, sqls[i]?.sql, sqls[i]?.params);
|
|
1829
2261
|
}
|
|
1830
2262
|
else {
|
|
1831
|
-
option.conn.execute(
|
|
2263
|
+
option.conn.execute(SyncMode.Sync, sqls[i]?.sql, sqls[i]?.params);
|
|
1832
2264
|
}
|
|
1833
2265
|
}
|
|
1834
|
-
return this.
|
|
2266
|
+
return this._template(option.templateResult, result, option.error);
|
|
1835
2267
|
}
|
|
1836
2268
|
else {
|
|
1837
2269
|
return new Promise(async (resolve) => {
|
|
1838
2270
|
let result;
|
|
1839
2271
|
for (let i = 0; i < sqls.length; i++) {
|
|
1840
2272
|
if (i === resultIndex) {
|
|
1841
|
-
result = await option.conn.query(
|
|
2273
|
+
result = await option.conn.query(SyncMode.Async, sqls[i]?.sql, sqls[i]?.params);
|
|
1842
2274
|
}
|
|
1843
2275
|
else {
|
|
1844
|
-
await option.conn.execute(
|
|
2276
|
+
await option.conn.execute(SyncMode.Async, sqls[i]?.sql, sqls[i]?.params);
|
|
1845
2277
|
}
|
|
1846
2278
|
}
|
|
1847
|
-
resolve(this.
|
|
2279
|
+
resolve(this._template(option.templateResult, result, option.error));
|
|
1848
2280
|
});
|
|
1849
2281
|
}
|
|
1850
2282
|
}
|
|
1851
|
-
|
|
2283
|
+
_select(templateResult, result, def, errorMsg, multiple) {
|
|
1852
2284
|
if (multiple === true) {
|
|
1853
|
-
switch (
|
|
1854
|
-
case
|
|
2285
|
+
switch (templateResult) {
|
|
2286
|
+
case SelectResult.One_Row_One_Column_NotSure: {
|
|
1855
2287
|
try {
|
|
1856
2288
|
return result.map((r) => Object.values(r)[0]);
|
|
1857
2289
|
}
|
|
1858
2290
|
catch (error) {
|
|
1859
2291
|
}
|
|
1860
2292
|
}
|
|
1861
|
-
case
|
|
2293
|
+
case SelectResult.One_Row_One_Column_Assert: {
|
|
1862
2294
|
try {
|
|
1863
2295
|
return iterare(result).map((r) => Object.values(r)[0]).filter((r) => r !== null).toArray();
|
|
1864
2296
|
}
|
|
1865
2297
|
catch (error) {
|
|
1866
2298
|
}
|
|
1867
2299
|
}
|
|
1868
|
-
case
|
|
2300
|
+
case SelectResult.One_Row_Many_Column_NotSure: {
|
|
1869
2301
|
try {
|
|
1870
2302
|
return result.map((r) => r[0]);
|
|
1871
2303
|
}
|
|
1872
2304
|
catch (error) {
|
|
1873
2305
|
}
|
|
1874
2306
|
}
|
|
1875
|
-
case
|
|
2307
|
+
case SelectResult.One_Row_Many_Column_Assert: {
|
|
1876
2308
|
try {
|
|
1877
2309
|
return iterare(result).map((r) => r[0]).filter((r) => r !== null).toArray();
|
|
1878
2310
|
}
|
|
1879
2311
|
catch (error) {
|
|
1880
2312
|
}
|
|
1881
2313
|
}
|
|
1882
|
-
case
|
|
2314
|
+
case SelectResult.Many_Row_One_Column: {
|
|
1883
2315
|
try {
|
|
1884
2316
|
return result.map((rx) => rx.map((r) => Object.values(r)[0]));
|
|
1885
2317
|
}
|
|
1886
2318
|
catch (error) {
|
|
1887
2319
|
}
|
|
1888
2320
|
}
|
|
1889
|
-
case
|
|
2321
|
+
case SelectResult.Many_Row_Many_Column: {
|
|
1890
2322
|
return result;
|
|
1891
2323
|
}
|
|
1892
2324
|
}
|
|
1893
2325
|
}
|
|
1894
2326
|
else {
|
|
1895
|
-
switch (
|
|
1896
|
-
case
|
|
2327
|
+
switch (templateResult) {
|
|
2328
|
+
case SelectResult.One_Row_One_Column_NotSure: {
|
|
1897
2329
|
try {
|
|
1898
2330
|
return Object.values(result[0])[0];
|
|
1899
2331
|
}
|
|
@@ -1901,7 +2333,7 @@ export class SqlService {
|
|
|
1901
2333
|
return def;
|
|
1902
2334
|
}
|
|
1903
2335
|
}
|
|
1904
|
-
case
|
|
2336
|
+
case SelectResult.One_Row_One_Column_Assert: {
|
|
1905
2337
|
try {
|
|
1906
2338
|
return Object.values(result[0])[0];
|
|
1907
2339
|
}
|
|
@@ -1911,15 +2343,15 @@ export class SqlService {
|
|
|
1911
2343
|
Throw.now(errorMsg ?? 'not found data!');
|
|
1912
2344
|
}
|
|
1913
2345
|
}
|
|
1914
|
-
case
|
|
2346
|
+
case SelectResult.One_Row_Many_Column_NotSure: {
|
|
1915
2347
|
return result[0] ?? null;
|
|
1916
2348
|
}
|
|
1917
|
-
case
|
|
2349
|
+
case SelectResult.One_Row_Many_Column_Assert: {
|
|
1918
2350
|
const data = result[0];
|
|
1919
2351
|
Throw.if(data === null, errorMsg ?? 'not found data!');
|
|
1920
2352
|
return data ?? null;
|
|
1921
2353
|
}
|
|
1922
|
-
case
|
|
2354
|
+
case SelectResult.Many_Row_One_Column: {
|
|
1923
2355
|
try {
|
|
1924
2356
|
return result.map((r) => Object.values(r)[0]);
|
|
1925
2357
|
}
|
|
@@ -1927,7 +2359,7 @@ export class SqlService {
|
|
|
1927
2359
|
return def;
|
|
1928
2360
|
}
|
|
1929
2361
|
}
|
|
1930
|
-
case
|
|
2362
|
+
case SelectResult.Many_Row_Many_Column: {
|
|
1931
2363
|
return result;
|
|
1932
2364
|
}
|
|
1933
2365
|
}
|
|
@@ -1935,11 +2367,11 @@ export class SqlService {
|
|
|
1935
2367
|
}
|
|
1936
2368
|
select(option) {
|
|
1937
2369
|
Throw.if(!option.sqlId && !option.sql, 'not found sql!');
|
|
1938
|
-
option.
|
|
1939
|
-
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, option.context, option.isPage));
|
|
2370
|
+
option.selectResult ?? (option.selectResult = SelectResult.Many_Row_Many_Column);
|
|
1940
2371
|
option.defValue ?? (option.defValue = null);
|
|
1941
|
-
logger.debug(option.sql);
|
|
1942
2372
|
const _params = Object.assign({}, option.context, option.params);
|
|
2373
|
+
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, { ctx: option.context, isCount: option.isCount, ..._params }));
|
|
2374
|
+
logger.debug(option.sql);
|
|
1943
2375
|
const params = [];
|
|
1944
2376
|
const sql = option.sql?.replace(/\:(\w+)/g, (txt, key) => {
|
|
1945
2377
|
if (_params.hasOwnProperty(key)) {
|
|
@@ -1949,22 +2381,22 @@ export class SqlService {
|
|
|
1949
2381
|
}
|
|
1950
2382
|
return txt;
|
|
1951
2383
|
});
|
|
1952
|
-
if (option.sync ===
|
|
1953
|
-
const result = option.conn.query(
|
|
1954
|
-
return this.
|
|
2384
|
+
if (option.sync === SyncMode.Sync) {
|
|
2385
|
+
const result = option.conn.query(SyncMode.Sync, sql, params);
|
|
2386
|
+
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.multiple);
|
|
1955
2387
|
}
|
|
1956
2388
|
else {
|
|
1957
2389
|
return new Promise(async (resolve) => {
|
|
1958
|
-
const result = await option.conn.query(
|
|
1959
|
-
resolve(this.
|
|
2390
|
+
const result = await option.conn.query(SyncMode.Async, sql, params);
|
|
2391
|
+
resolve(this._select(option.selectResult, result, option.defValue, option.errorMsg, option.multiple));
|
|
1960
2392
|
});
|
|
1961
2393
|
}
|
|
1962
2394
|
}
|
|
1963
2395
|
excute(option) {
|
|
1964
2396
|
Throw.if(!option.sqlId && !option.sql, 'not found sql!');
|
|
1965
|
-
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, option.context));
|
|
1966
|
-
logger.debug(option.sql);
|
|
1967
2397
|
const _params = Object.assign({}, option.context, option.params);
|
|
2398
|
+
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, { ctx: option.context, ..._params }));
|
|
2399
|
+
logger.debug(option.sql);
|
|
1968
2400
|
const params = [];
|
|
1969
2401
|
const sql = option.sql?.replace(/\:(\w+)/g, (txt, key) => {
|
|
1970
2402
|
if (_params.hasOwnProperty(key)) {
|
|
@@ -1974,24 +2406,24 @@ export class SqlService {
|
|
|
1974
2406
|
}
|
|
1975
2407
|
return txt;
|
|
1976
2408
|
});
|
|
1977
|
-
if (option.sync ===
|
|
1978
|
-
const result = option.conn.execute(
|
|
2409
|
+
if (option.sync === SyncMode.Sync) {
|
|
2410
|
+
const result = option.conn.execute(SyncMode.Sync, sql, params);
|
|
1979
2411
|
return result.affectedRows;
|
|
1980
2412
|
}
|
|
1981
2413
|
else {
|
|
1982
2414
|
return new Promise(async (resolve) => {
|
|
1983
|
-
const result = await option.conn.execute(
|
|
2415
|
+
const result = await option.conn.execute(SyncMode.Async, sql, params);
|
|
1984
2416
|
resolve(result.affectedRows);
|
|
1985
2417
|
});
|
|
1986
2418
|
}
|
|
1987
2419
|
}
|
|
1988
2420
|
transaction(option) {
|
|
1989
|
-
if (option.sync ===
|
|
1990
|
-
return option.dao.transaction(
|
|
2421
|
+
if (option.sync === SyncMode.Sync) {
|
|
2422
|
+
return option.dao.transaction(SyncMode.Sync, option.fn);
|
|
1991
2423
|
}
|
|
1992
2424
|
else {
|
|
1993
2425
|
return new Promise(async (resolve) => {
|
|
1994
|
-
const rt = await option.dao.transaction(
|
|
2426
|
+
const rt = await option.dao.transaction(SyncMode.Async, option.fn);
|
|
1995
2427
|
resolve(rt);
|
|
1996
2428
|
});
|
|
1997
2429
|
}
|
|
@@ -2930,7 +3362,7 @@ class StreamQuery extends StreamBuild {
|
|
|
2930
3362
|
this._service = service;
|
|
2931
3363
|
}
|
|
2932
3364
|
select(option) {
|
|
2933
|
-
option.sync ?? (option.sync =
|
|
3365
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
2934
3366
|
const { where, params } = this.where();
|
|
2935
3367
|
const sql = `
|
|
2936
3368
|
SELECT
|
|
@@ -2938,29 +3370,29 @@ class StreamQuery extends StreamBuild {
|
|
|
2938
3370
|
FROM ${this._table}
|
|
2939
3371
|
${where ? ' WHERE ' : ''}
|
|
2940
3372
|
${where}`;
|
|
2941
|
-
if (option.sync ===
|
|
2942
|
-
switch (option.
|
|
2943
|
-
case
|
|
2944
|
-
case
|
|
2945
|
-
case
|
|
2946
|
-
case
|
|
2947
|
-
case
|
|
2948
|
-
case
|
|
3373
|
+
if (option.sync === SyncMode.Async) {
|
|
3374
|
+
switch (option.selectResult) {
|
|
3375
|
+
case SelectResult.Many_Row_Many_Column: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.Many_Row_Many_Column, errorMsg: option.errorMsg, sql, params });
|
|
3376
|
+
case SelectResult.Many_Row_One_Column: return this._service.select({ sync: SyncMode.Async, selectResult: SelectResult.Many_Row_One_Column, errorMsg: option.errorMsg, sql, params });
|
|
3377
|
+
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 });
|
|
3378
|
+
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 });
|
|
3379
|
+
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 });
|
|
3380
|
+
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
3381
|
}
|
|
2950
3382
|
}
|
|
2951
3383
|
else {
|
|
2952
|
-
switch (option.
|
|
2953
|
-
case
|
|
2954
|
-
case
|
|
2955
|
-
case
|
|
2956
|
-
case
|
|
2957
|
-
case
|
|
2958
|
-
case
|
|
3384
|
+
switch (option.selectResult) {
|
|
3385
|
+
case SelectResult.Many_Row_Many_Column: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.Many_Row_Many_Column, errorMsg: option.errorMsg, sql, params });
|
|
3386
|
+
case SelectResult.Many_Row_One_Column: return this._service.select({ sync: SyncMode.Sync, selectResult: SelectResult.Many_Row_One_Column, errorMsg: option.errorMsg, sql, params });
|
|
3387
|
+
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 });
|
|
3388
|
+
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 });
|
|
3389
|
+
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 });
|
|
3390
|
+
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
3391
|
}
|
|
2960
3392
|
}
|
|
2961
3393
|
}
|
|
2962
3394
|
update(option) {
|
|
2963
|
-
option.sync ?? (option.sync =
|
|
3395
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
2964
3396
|
const { where, params } = this.where();
|
|
2965
3397
|
const sets = new Array(...this._updateColumns);
|
|
2966
3398
|
if (this._updates) {
|
|
@@ -2972,11 +3404,11 @@ class StreamQuery extends StreamBuild {
|
|
|
2972
3404
|
}
|
|
2973
3405
|
if (sets.length > 0) {
|
|
2974
3406
|
const sql = `UPDATE ${this._table} SET ${sets.join(',')} ${where}`;
|
|
2975
|
-
if (option.sync ===
|
|
2976
|
-
return this._service.excute({ sync:
|
|
3407
|
+
if (option.sync === SyncMode.Async) {
|
|
3408
|
+
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
2977
3409
|
}
|
|
2978
3410
|
else {
|
|
2979
|
-
return this._service.excute({ sync:
|
|
3411
|
+
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
2980
3412
|
}
|
|
2981
3413
|
}
|
|
2982
3414
|
else {
|
|
@@ -2984,14 +3416,14 @@ class StreamQuery extends StreamBuild {
|
|
|
2984
3416
|
}
|
|
2985
3417
|
}
|
|
2986
3418
|
delete(option) {
|
|
2987
|
-
option.sync ?? (option.sync =
|
|
3419
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
2988
3420
|
const { where, params } = this.where();
|
|
2989
3421
|
const sql = `DELETE FROM ${this._table} ${where}`;
|
|
2990
|
-
if (option.sync ===
|
|
2991
|
-
return this._service.excute({ sync:
|
|
3422
|
+
if (option.sync === SyncMode.Async) {
|
|
3423
|
+
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
2992
3424
|
}
|
|
2993
3425
|
else {
|
|
2994
|
-
return this._service.excute({ sync:
|
|
3426
|
+
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
2995
3427
|
}
|
|
2996
3428
|
}
|
|
2997
3429
|
}
|
|
@@ -3213,15 +3645,137 @@ export function MethodCache(config) {
|
|
|
3213
3645
|
};
|
|
3214
3646
|
};
|
|
3215
3647
|
}
|
|
3216
|
-
|
|
3648
|
+
class MUParser {
|
|
3649
|
+
constructor(modelName, file) {
|
|
3650
|
+
this.linNumber = 0;
|
|
3651
|
+
this.status = 0;
|
|
3652
|
+
this.lineSeparator = '\n';
|
|
3653
|
+
this.modelName = modelName;
|
|
3654
|
+
this.files = file.replace(/\r/g, '').split(this.lineSeparator);
|
|
3655
|
+
this.skipHeader();
|
|
3656
|
+
}
|
|
3657
|
+
next() {
|
|
3658
|
+
let sqlId = this.readSqlId();
|
|
3659
|
+
if (this.status === MUParser.END || !sqlId) {
|
|
3660
|
+
return null;
|
|
3661
|
+
}
|
|
3662
|
+
// 去掉可能的尾部空格
|
|
3663
|
+
sqlId = sqlId.trim();
|
|
3664
|
+
this.skipComment();
|
|
3665
|
+
if (this.status === MUParser.END) {
|
|
3666
|
+
return null;
|
|
3667
|
+
}
|
|
3668
|
+
const sql = this.readSql();
|
|
3669
|
+
return [`${this.modelName}.${sqlId}`, sql];
|
|
3670
|
+
}
|
|
3671
|
+
skipHeader() {
|
|
3672
|
+
while (true) {
|
|
3673
|
+
const line = this.nextLine();
|
|
3674
|
+
if (!line) {
|
|
3675
|
+
return;
|
|
3676
|
+
}
|
|
3677
|
+
if (this.status === MUParser.END) {
|
|
3678
|
+
return;
|
|
3679
|
+
}
|
|
3680
|
+
if (line.startsWith('===')) {
|
|
3681
|
+
return;
|
|
3682
|
+
}
|
|
3683
|
+
}
|
|
3684
|
+
}
|
|
3685
|
+
nextLine() {
|
|
3686
|
+
const line = this.files[this.linNumber];
|
|
3687
|
+
this.linNumber++;
|
|
3688
|
+
if (line === undefined) {
|
|
3689
|
+
this.status = MUParser.END;
|
|
3690
|
+
}
|
|
3691
|
+
// 保存最后读的俩行
|
|
3692
|
+
this.lastlastLine = this.lastLine;
|
|
3693
|
+
this.lastLine = line;
|
|
3694
|
+
return line;
|
|
3695
|
+
}
|
|
3696
|
+
readSqlId() {
|
|
3697
|
+
return this.lastlastLine;
|
|
3698
|
+
}
|
|
3699
|
+
skipComment() {
|
|
3700
|
+
let findComment = false;
|
|
3701
|
+
while (true) {
|
|
3702
|
+
let line = this.nextLine();
|
|
3703
|
+
if (this.status === MUParser.END || !line) {
|
|
3704
|
+
return;
|
|
3705
|
+
}
|
|
3706
|
+
line = line.trim();
|
|
3707
|
+
if (!findComment && line.length === 0) {
|
|
3708
|
+
continue;
|
|
3709
|
+
}
|
|
3710
|
+
if (line.startsWith('*')) {
|
|
3711
|
+
// 注释符号
|
|
3712
|
+
findComment = true;
|
|
3713
|
+
continue;
|
|
3714
|
+
}
|
|
3715
|
+
else {
|
|
3716
|
+
if (line.length === 0) {
|
|
3717
|
+
continue;
|
|
3718
|
+
}
|
|
3719
|
+
else if (line.startsWith('```') || line.startsWith('~~~')) {
|
|
3720
|
+
// 忽略以code block开头的符号
|
|
3721
|
+
continue;
|
|
3722
|
+
}
|
|
3723
|
+
else {
|
|
3724
|
+
// 注释结束
|
|
3725
|
+
return;
|
|
3726
|
+
}
|
|
3727
|
+
}
|
|
3728
|
+
}
|
|
3729
|
+
}
|
|
3730
|
+
readSql() {
|
|
3731
|
+
const list = [];
|
|
3732
|
+
if (this.lastLine) {
|
|
3733
|
+
list.push(this.lastLine);
|
|
3734
|
+
while (true) {
|
|
3735
|
+
const line = this.nextLine();
|
|
3736
|
+
if (line) {
|
|
3737
|
+
if (this.status === MUParser.END) {
|
|
3738
|
+
return this.getBuildSql(list);
|
|
3739
|
+
}
|
|
3740
|
+
if (line.startsWith('===')) {
|
|
3741
|
+
// 删除下一个sqlId表示
|
|
3742
|
+
list.pop();
|
|
3743
|
+
return this.getBuildSql(list);
|
|
3744
|
+
}
|
|
3745
|
+
list.push(line);
|
|
3746
|
+
}
|
|
3747
|
+
else {
|
|
3748
|
+
return '';
|
|
3749
|
+
}
|
|
3750
|
+
}
|
|
3751
|
+
}
|
|
3752
|
+
else {
|
|
3753
|
+
return '';
|
|
3754
|
+
}
|
|
3755
|
+
}
|
|
3756
|
+
getBuildSql(list) {
|
|
3757
|
+
const sb = [];
|
|
3758
|
+
for (const str of list) {
|
|
3759
|
+
const s = str.trim();
|
|
3760
|
+
if (s.startsWith('```') || s.startsWith('~~~')) {
|
|
3761
|
+
// 忽略以code block开头的符号
|
|
3762
|
+
continue;
|
|
3763
|
+
}
|
|
3764
|
+
sb.push(str);
|
|
3765
|
+
}
|
|
3766
|
+
return sb.join(this.lineSeparator);
|
|
3767
|
+
}
|
|
3768
|
+
}
|
|
3769
|
+
MUParser.END = 1;
|
|
3770
|
+
export const Boot = async function (options) {
|
|
3217
3771
|
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption, options);
|
|
3218
3772
|
if (options.sqlDir) {
|
|
3219
|
-
globalThis[_path] = import('path');
|
|
3220
|
-
globalThis[_fs] = import('fs');
|
|
3773
|
+
globalThis[_path] = await import('path');
|
|
3774
|
+
globalThis[_fs] = await import('fs');
|
|
3221
3775
|
}
|
|
3222
3776
|
logger.level = options.log ?? 'info';
|
|
3223
3777
|
globalThis[_sqlCache] = new SqlCache();
|
|
3224
|
-
if (options.
|
|
3778
|
+
if (options.sqlMap || options.sqlDir) {
|
|
3225
3779
|
await globalThis[_sqlCache].init(options);
|
|
3226
3780
|
}
|
|
3227
3781
|
globalThis[_dao] = {
|