baja-lite 1.0.2 → 1.0.4
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 +1 -0
- package/cjs/code.js +3 -0
- package/cjs/index.d.ts +0 -1
- package/cjs/index.js +0 -1
- package/cjs/object.js +3 -7
- package/cjs/sql.d.ts +44 -18
- package/cjs/sql.js +453 -58
- package/cjs/string.d.ts +0 -1
- package/cjs/string.js +1 -6
- package/cjs/test-mysql.js +3 -3
- package/es/code.d.ts +1 -0
- package/es/code.js +3 -0
- package/es/index.d.ts +0 -1
- package/es/index.js +0 -1
- package/es/object.js +3 -4
- package/es/sql.d.ts +44 -18
- package/es/sql.js +453 -58
- package/es/string.d.ts +0 -1
- package/es/string.js +0 -4
- package/es/test-mysql.js +3 -3
- package/package.json +19 -20
- package/src/code.ts +2 -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/cjs/sql.js
CHANGED
|
@@ -47,6 +47,7 @@ const string_1 = require("./string");
|
|
|
47
47
|
const pino_1 = __importDefault(require("pino"));
|
|
48
48
|
const fn_1 = require("./fn");
|
|
49
49
|
const math_1 = require("./math");
|
|
50
|
+
const mustache_1 = __importDefault(require("mustache"));
|
|
50
51
|
// #region 常量
|
|
51
52
|
const _daoDBName = Symbol('dbName');
|
|
52
53
|
const _tableName = Symbol('tableName');
|
|
@@ -59,7 +60,7 @@ const _deleteState = Symbol('deleteState');
|
|
|
59
60
|
const _transformer = Symbol('transformer');
|
|
60
61
|
const _index = Symbol('index');
|
|
61
62
|
const _def = Symbol('def');
|
|
62
|
-
const _sqlCache = Symbol('
|
|
63
|
+
const _sqlCache = Symbol('sqlMap');
|
|
63
64
|
const _dao = Symbol('dao');
|
|
64
65
|
const _primaryDB = Symbol('primaryDB');
|
|
65
66
|
const _dbType = Symbol('dbType');
|
|
@@ -242,7 +243,7 @@ class MysqlConnection {
|
|
|
242
243
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
243
244
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
244
245
|
}
|
|
245
|
-
return new Promise(async (resolve) => {
|
|
246
|
+
return new Promise(async (resolve, reject) => {
|
|
246
247
|
try {
|
|
247
248
|
const [_result] = await this[_daoConnection].execute(sql, params);
|
|
248
249
|
const result = _result;
|
|
@@ -257,7 +258,7 @@ class MysqlConnection {
|
|
|
257
258
|
sql: ${sql},
|
|
258
259
|
params: ${params}
|
|
259
260
|
`);
|
|
260
|
-
|
|
261
|
+
reject(error);
|
|
261
262
|
}
|
|
262
263
|
});
|
|
263
264
|
}
|
|
@@ -275,7 +276,7 @@ class MysqlConnection {
|
|
|
275
276
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
276
277
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
277
278
|
}
|
|
278
|
-
return new Promise(async (resolve) => {
|
|
279
|
+
return new Promise(async (resolve, reject) => {
|
|
279
280
|
try {
|
|
280
281
|
const [result] = await this[_daoConnection].query(sql, params);
|
|
281
282
|
if (result && result[0]) {
|
|
@@ -293,7 +294,7 @@ class MysqlConnection {
|
|
|
293
294
|
sql: ${sql},
|
|
294
295
|
params: ${params}
|
|
295
296
|
`);
|
|
296
|
-
|
|
297
|
+
reject(error);
|
|
297
298
|
}
|
|
298
299
|
});
|
|
299
300
|
}
|
|
@@ -311,7 +312,7 @@ class MysqlConnection {
|
|
|
311
312
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
312
313
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
313
314
|
}
|
|
314
|
-
return new Promise(async (resolve) => {
|
|
315
|
+
return new Promise(async (resolve, reject) => {
|
|
315
316
|
try {
|
|
316
317
|
const [result] = await this[_daoConnection].query(sql, params);
|
|
317
318
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
@@ -327,7 +328,7 @@ class MysqlConnection {
|
|
|
327
328
|
sql: ${sql},
|
|
328
329
|
params: ${params}
|
|
329
330
|
`);
|
|
330
|
-
|
|
331
|
+
reject(error);
|
|
331
332
|
}
|
|
332
333
|
});
|
|
333
334
|
}
|
|
@@ -345,7 +346,7 @@ class MysqlConnection {
|
|
|
345
346
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
346
347
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
347
348
|
}
|
|
348
|
-
return new Promise(async (resolve) => {
|
|
349
|
+
return new Promise(async (resolve, reject) => {
|
|
349
350
|
try {
|
|
350
351
|
const [result] = await this[_daoConnection].query(sql, params);
|
|
351
352
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
@@ -361,7 +362,7 @@ class MysqlConnection {
|
|
|
361
362
|
sql: ${sql},
|
|
362
363
|
params: ${params}
|
|
363
364
|
`);
|
|
364
|
-
|
|
365
|
+
reject(error);
|
|
365
366
|
}
|
|
366
367
|
});
|
|
367
368
|
}
|
|
@@ -379,7 +380,7 @@ class MysqlConnection {
|
|
|
379
380
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
380
381
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
381
382
|
}
|
|
382
|
-
return new Promise(async (resolve) => {
|
|
383
|
+
return new Promise(async (resolve, reject) => {
|
|
383
384
|
try {
|
|
384
385
|
const [result] = await this[_daoConnection].query(sql, params);
|
|
385
386
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
@@ -393,7 +394,7 @@ class MysqlConnection {
|
|
|
393
394
|
sql: ${sql},
|
|
394
395
|
params: ${params}
|
|
395
396
|
`);
|
|
396
|
-
|
|
397
|
+
reject(error);
|
|
397
398
|
}
|
|
398
399
|
});
|
|
399
400
|
}
|
|
@@ -419,10 +420,15 @@ class Mysql {
|
|
|
419
420
|
return null;
|
|
420
421
|
}
|
|
421
422
|
;
|
|
422
|
-
return new Promise(async (resolve) => {
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
423
|
+
return new Promise(async (resolve, reject) => {
|
|
424
|
+
try {
|
|
425
|
+
const connection = await this[_daoDB].getConnection();
|
|
426
|
+
logger.debug('create new!');
|
|
427
|
+
resolve(new MysqlConnection(connection));
|
|
428
|
+
}
|
|
429
|
+
catch (error) {
|
|
430
|
+
reject(error);
|
|
431
|
+
}
|
|
426
432
|
});
|
|
427
433
|
}
|
|
428
434
|
transaction(sync, fn, conn) {
|
|
@@ -431,7 +437,7 @@ class Mysql {
|
|
|
431
437
|
return null;
|
|
432
438
|
}
|
|
433
439
|
;
|
|
434
|
-
return new Promise(async (resolve) => {
|
|
440
|
+
return new Promise(async (resolve, reject) => {
|
|
435
441
|
let needCommit = false;
|
|
436
442
|
let newConn = false;
|
|
437
443
|
if (!conn) {
|
|
@@ -461,7 +467,7 @@ class Mysql {
|
|
|
461
467
|
logger.debug('rollback end!');
|
|
462
468
|
conn[_inTransaction] = false;
|
|
463
469
|
logger.error(error);
|
|
464
|
-
|
|
470
|
+
reject(error);
|
|
465
471
|
}
|
|
466
472
|
finally {
|
|
467
473
|
try {
|
|
@@ -717,7 +723,7 @@ class SqliteRemoteConnection {
|
|
|
717
723
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
718
724
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
719
725
|
}
|
|
720
|
-
return new Promise(async (resolve) => {
|
|
726
|
+
return new Promise(async (resolve, reject) => {
|
|
721
727
|
try {
|
|
722
728
|
const { affectedRows, insertId } = await this[_daoConnection].execute(this[_sqliteRemoteName], sql, params);
|
|
723
729
|
resolve({ affectedRows, insertId: insertId ? BigInt(insertId) : 0n });
|
|
@@ -728,7 +734,7 @@ class SqliteRemoteConnection {
|
|
|
728
734
|
sql: ${sql},
|
|
729
735
|
params: ${params}
|
|
730
736
|
`);
|
|
731
|
-
|
|
737
|
+
reject(error);
|
|
732
738
|
}
|
|
733
739
|
});
|
|
734
740
|
}
|
|
@@ -746,7 +752,7 @@ class SqliteRemoteConnection {
|
|
|
746
752
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
747
753
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
748
754
|
}
|
|
749
|
-
return new Promise(async (resolve) => {
|
|
755
|
+
return new Promise(async (resolve, reject) => {
|
|
750
756
|
try {
|
|
751
757
|
const r = await this[_daoConnection].pluck(this[_sqliteRemoteName], sql, params);
|
|
752
758
|
resolve(r);
|
|
@@ -757,7 +763,7 @@ class SqliteRemoteConnection {
|
|
|
757
763
|
sql: ${sql},
|
|
758
764
|
params: ${params}
|
|
759
765
|
`);
|
|
760
|
-
|
|
766
|
+
reject(error);
|
|
761
767
|
}
|
|
762
768
|
});
|
|
763
769
|
}
|
|
@@ -775,7 +781,7 @@ class SqliteRemoteConnection {
|
|
|
775
781
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
776
782
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
777
783
|
}
|
|
778
|
-
return new Promise(async (resolve) => {
|
|
784
|
+
return new Promise(async (resolve, reject) => {
|
|
779
785
|
try {
|
|
780
786
|
const r = await this[_daoConnection].get(this[_sqliteRemoteName], sql, params);
|
|
781
787
|
resolve(r);
|
|
@@ -786,7 +792,7 @@ class SqliteRemoteConnection {
|
|
|
786
792
|
sql: ${sql},
|
|
787
793
|
params: ${params}
|
|
788
794
|
`);
|
|
789
|
-
|
|
795
|
+
reject(error);
|
|
790
796
|
}
|
|
791
797
|
});
|
|
792
798
|
}
|
|
@@ -804,7 +810,7 @@ class SqliteRemoteConnection {
|
|
|
804
810
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
805
811
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
806
812
|
}
|
|
807
|
-
return new Promise(async (resolve) => {
|
|
813
|
+
return new Promise(async (resolve, reject) => {
|
|
808
814
|
try {
|
|
809
815
|
const r = await this[_daoConnection].raw(this[_sqliteRemoteName], sql, params);
|
|
810
816
|
resolve(r);
|
|
@@ -815,7 +821,7 @@ class SqliteRemoteConnection {
|
|
|
815
821
|
sql: ${sql},
|
|
816
822
|
params: ${params}
|
|
817
823
|
`);
|
|
818
|
-
|
|
824
|
+
reject(error);
|
|
819
825
|
}
|
|
820
826
|
});
|
|
821
827
|
}
|
|
@@ -833,7 +839,7 @@ class SqliteRemoteConnection {
|
|
|
833
839
|
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
834
840
|
logger.trace(sqlstring_1.default.format(sql, params));
|
|
835
841
|
}
|
|
836
|
-
return new Promise(async (resolve) => {
|
|
842
|
+
return new Promise(async (resolve, reject) => {
|
|
837
843
|
try {
|
|
838
844
|
const r = await this[_daoConnection].query(this[_sqliteRemoteName], sql, params);
|
|
839
845
|
resolve(r);
|
|
@@ -844,7 +850,7 @@ class SqliteRemoteConnection {
|
|
|
844
850
|
sql: ${sql},
|
|
845
851
|
params: ${params}
|
|
846
852
|
`);
|
|
847
|
-
|
|
853
|
+
reject(error);
|
|
848
854
|
}
|
|
849
855
|
});
|
|
850
856
|
}
|
|
@@ -904,34 +910,301 @@ class SqliteRemote {
|
|
|
904
910
|
;
|
|
905
911
|
}
|
|
906
912
|
}
|
|
913
|
+
class Build {
|
|
914
|
+
/**
|
|
915
|
+
*
|
|
916
|
+
* @param count 是否是count查询
|
|
917
|
+
* @param isSum 是否是sum查询
|
|
918
|
+
* @param param
|
|
919
|
+
*/
|
|
920
|
+
constructor(isCount, isSum, param = {}) {
|
|
921
|
+
this.brage = { haveOrderBy: false, haveLimit: false };
|
|
922
|
+
this.isCount = isCount;
|
|
923
|
+
this.isSum = isSum;
|
|
924
|
+
Object.assign(this, param);
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
*
|
|
928
|
+
* 当分页时将函数内包含的内容替换为COUNT(1)
|
|
929
|
+
* @returns
|
|
930
|
+
* @memberof Build
|
|
931
|
+
*/
|
|
932
|
+
pageTag() {
|
|
933
|
+
return (text, render) => {
|
|
934
|
+
if (this.isCount === true) {
|
|
935
|
+
return Build.page;
|
|
936
|
+
}
|
|
937
|
+
else if (this.isSum !== true) {
|
|
938
|
+
return render(text);
|
|
939
|
+
}
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
*
|
|
944
|
+
* 汇总查询专用
|
|
945
|
+
* @returns
|
|
946
|
+
* @memberof Build
|
|
947
|
+
*/
|
|
948
|
+
sumTag() {
|
|
949
|
+
return (text, render) => {
|
|
950
|
+
if (this.isSum !== true) {
|
|
951
|
+
return '';
|
|
952
|
+
}
|
|
953
|
+
else {
|
|
954
|
+
return render(text);
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
*
|
|
960
|
+
* 当分页时、汇总时忽略函数内包含的内容
|
|
961
|
+
* @returns
|
|
962
|
+
* @memberof Build
|
|
963
|
+
*/
|
|
964
|
+
pageIgnoreTag() {
|
|
965
|
+
return (text, render) => {
|
|
966
|
+
if (this.isCount === true || this.isSum === true) {
|
|
967
|
+
return '';
|
|
968
|
+
}
|
|
969
|
+
else {
|
|
970
|
+
return render(text);
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
*
|
|
976
|
+
* 将查询条件包起来,如果条件内容不为空,则自动添加WHERE,同时将第一个条件的and、or替换为空
|
|
977
|
+
* 例如:
|
|
978
|
+
* {{#whereTag}}
|
|
979
|
+
* and name = 1
|
|
980
|
+
* and page = 2
|
|
981
|
+
* {{/whereTag}}
|
|
982
|
+
* 输出
|
|
983
|
+
* where name = 1 and page = 2
|
|
984
|
+
* @returns
|
|
985
|
+
* @memberof Build
|
|
986
|
+
*/
|
|
987
|
+
where() {
|
|
988
|
+
return (text, render) => {
|
|
989
|
+
let data = render(text);
|
|
990
|
+
data = data.trim();
|
|
991
|
+
if (data) {
|
|
992
|
+
data = data.replace(/and|or/i, '');
|
|
993
|
+
return ` WHERE ${data} `;
|
|
994
|
+
}
|
|
995
|
+
else {
|
|
996
|
+
return '';
|
|
997
|
+
}
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* 删除第一个and、or
|
|
1002
|
+
* 删除最后一个,
|
|
1003
|
+
* 删除最后一个;
|
|
1004
|
+
* @memberof Build
|
|
1005
|
+
*/
|
|
1006
|
+
trim() {
|
|
1007
|
+
return (text, render) => {
|
|
1008
|
+
let data = render(text);
|
|
1009
|
+
data = data.trim();
|
|
1010
|
+
if (data) {
|
|
1011
|
+
data = data.replace(/(^and\s)|(^or\s)|(,$)|(;$)/i, '');
|
|
1012
|
+
return data;
|
|
1013
|
+
}
|
|
1014
|
+
else {
|
|
1015
|
+
return '';
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* 分页时将排序部分代码用此函数包起来,可以自动拼接order by
|
|
1021
|
+
* 查询条数时,自动忽略此部分
|
|
1022
|
+
* etc
|
|
1023
|
+
* {{#orderTag}} name desc, age asc {{/orderTag}}
|
|
1024
|
+
* ===
|
|
1025
|
+
* ORDER BY name desc, age asc
|
|
1026
|
+
* @returns
|
|
1027
|
+
* @memberof Build
|
|
1028
|
+
*/
|
|
1029
|
+
orderTag() {
|
|
1030
|
+
return (text, render) => {
|
|
1031
|
+
if (this.isCount === true || this.isSum === true) {
|
|
1032
|
+
return '';
|
|
1033
|
+
}
|
|
1034
|
+
else {
|
|
1035
|
+
this.brage.haveOrderBy = true;
|
|
1036
|
+
const orderBy = new Array();
|
|
1037
|
+
const renderOrder = render(text);
|
|
1038
|
+
if (/\S/.test(renderOrder)) {
|
|
1039
|
+
orderBy.push(renderOrder);
|
|
1040
|
+
}
|
|
1041
|
+
return orderBy.length > 0 ? ` ORDER BY ${orderBy.join(',')} ` : '';
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
}
|
|
1045
|
+
limitTag() {
|
|
1046
|
+
return (text, render) => {
|
|
1047
|
+
if (this.isCount === true || this.isSum === true) {
|
|
1048
|
+
return '';
|
|
1049
|
+
}
|
|
1050
|
+
else {
|
|
1051
|
+
this.brage.haveOrderBy = true;
|
|
1052
|
+
const orderBy = new Array();
|
|
1053
|
+
const renderOrder = render(text);
|
|
1054
|
+
if (/\S/.test(renderOrder)) {
|
|
1055
|
+
orderBy.push(renderOrder);
|
|
1056
|
+
}
|
|
1057
|
+
return orderBy.length > 0 ? ` ORDER BY ${orderBy.join(',')} ` : '';
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
*
|
|
1063
|
+
* 分页时将分组部分代码用此函数包起来,可以自动拼接GROUP BY
|
|
1064
|
+
* 当分页时、汇总时,自动忽略此部分
|
|
1065
|
+
* etc
|
|
1066
|
+
* {{#groupTag}} name, age {{/groupTag}}
|
|
1067
|
+
* ===
|
|
1068
|
+
* group by name.age
|
|
1069
|
+
* @returns
|
|
1070
|
+
* @memberof Build
|
|
1071
|
+
*/
|
|
1072
|
+
groupTag() {
|
|
1073
|
+
return (text, render) => {
|
|
1074
|
+
if (this.isCount === true || this.isSum === true) {
|
|
1075
|
+
return '';
|
|
1076
|
+
}
|
|
1077
|
+
else {
|
|
1078
|
+
const groupBy = render(text) || '';
|
|
1079
|
+
return /\S/.test(groupBy) ? ` GROUP BY ${groupBy} ` : '';
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
*
|
|
1085
|
+
* beetween and
|
|
1086
|
+
* etc.
|
|
1087
|
+
* {{#between}} AND t.createtime | ({{createtime}}) {{/between}}
|
|
1088
|
+
* createtime: 1,2
|
|
1089
|
+
* ===
|
|
1090
|
+
* AND t.createtime BETWEEN 1 AND 2
|
|
1091
|
+
* @returns
|
|
1092
|
+
* @memberof Build
|
|
1093
|
+
*/
|
|
1094
|
+
between() {
|
|
1095
|
+
return (text, render) => {
|
|
1096
|
+
const result = render(text);
|
|
1097
|
+
if (/\(([\w\W]+)\)/.exec(result)) {
|
|
1098
|
+
return render(text).replace(/\(([\w\W]+)\)/, (a, b) => {
|
|
1099
|
+
if (a && b) {
|
|
1100
|
+
const xx = b.split(',');
|
|
1101
|
+
return `'${xx[0]}' AND '${xx[1]}'`;
|
|
1102
|
+
}
|
|
1103
|
+
else {
|
|
1104
|
+
return '';
|
|
1105
|
+
}
|
|
1106
|
+
}).replace(/\|/, ' BETWEEN ');
|
|
1107
|
+
}
|
|
1108
|
+
else {
|
|
1109
|
+
return '';
|
|
1110
|
+
}
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
*
|
|
1115
|
+
* 距离计算,单位米
|
|
1116
|
+
* etc
|
|
1117
|
+
* {{#distanceTag}} (t.longitude, t.latitude), ({{longitude}}, {{latitude}}) {{/distanceTag}}
|
|
1118
|
+
* ===
|
|
1119
|
+
* ROUND(ST_DISTANCE(POINT(longitude1, latitude1), POINT({{longitude}}, {{latitude}}))*111195, 2)
|
|
1120
|
+
* 可根据需求自行将数据转换为千米,例如
|
|
1121
|
+
* {{#distanceTag}} (t.longitude, t.latitude), ({{longitude}}, {{latitude}}) {{/distanceTag}} / 1000
|
|
1122
|
+
* @returns
|
|
1123
|
+
* @memberof Build
|
|
1124
|
+
*/
|
|
1125
|
+
distanceTag() {
|
|
1126
|
+
return (text, render) => {
|
|
1127
|
+
const result = render(text);
|
|
1128
|
+
if (/\(([^()]+)\)/.exec(result)) {
|
|
1129
|
+
let index = 0;
|
|
1130
|
+
return render(text).replace(/\(([^()]+)\)/g, (a, b) => {
|
|
1131
|
+
if (a && b) {
|
|
1132
|
+
const xx = b.split(',');
|
|
1133
|
+
if (index === 0) {
|
|
1134
|
+
index++;
|
|
1135
|
+
return ` ROUND(ST_DISTANCE(POINT(${xx[0]}, ${xx[1]}) `;
|
|
1136
|
+
}
|
|
1137
|
+
else {
|
|
1138
|
+
return ` POINT(${xx[0]}, ${xx[1]}))*111195, 2)`;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
else {
|
|
1142
|
+
return '';
|
|
1143
|
+
}
|
|
1144
|
+
});
|
|
1145
|
+
}
|
|
1146
|
+
else {
|
|
1147
|
+
return '';
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
Build.page = 'COUNT(1) zccw1986 ';
|
|
907
1153
|
class SqlCache {
|
|
908
1154
|
constructor() {
|
|
909
|
-
this.
|
|
1155
|
+
this.sqlMap = {};
|
|
1156
|
+
this.sqlFNMap = {};
|
|
910
1157
|
}
|
|
911
1158
|
async init(options) {
|
|
1159
|
+
if (options.sqlMap) {
|
|
1160
|
+
this.sqlMap = options.sqlMap;
|
|
1161
|
+
}
|
|
912
1162
|
if (options.sqlDir) {
|
|
913
1163
|
const sqlFis = globalThis[_fs].readdirSync(options.sqlDir);
|
|
914
1164
|
for (const modeName of sqlFis) {
|
|
915
|
-
const
|
|
916
|
-
const
|
|
917
|
-
|
|
918
|
-
|
|
1165
|
+
const extname = globalThis[_path].extname(modeName);
|
|
1166
|
+
const name = globalThis[_path].basename(modeName, extname);
|
|
1167
|
+
const file = globalThis[_path].join(options.sqlDir, modeName);
|
|
1168
|
+
if (extname === 'mu') {
|
|
1169
|
+
const parser = new MUParser(name, globalThis[_fs].readFileSync(file, { encoding: 'utf-8' }).toString());
|
|
1170
|
+
let source = parser.next();
|
|
1171
|
+
while (source != null) {
|
|
1172
|
+
this.sqlMap[source[0]] = source[1];
|
|
1173
|
+
logger.debug(`sql: ${source[0]} found!`);
|
|
1174
|
+
source = parser.next();
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
else if (extname === '.js') {
|
|
1178
|
+
const obj = (await Promise.resolve(`${globalThis[_path].join(options.sqlDir, modeName)}`).then(s => __importStar(require(s)))).default;
|
|
1179
|
+
for (const [key, fn] of Object.entries(obj)) {
|
|
1180
|
+
this.sqlMap[`${name}.${String(key)}`] = fn;
|
|
1181
|
+
}
|
|
919
1182
|
}
|
|
920
1183
|
}
|
|
921
1184
|
}
|
|
922
|
-
|
|
923
|
-
this.
|
|
1185
|
+
if (options.sqlFNMap) {
|
|
1186
|
+
this.sqlFNMap = options.sqlFNMap;
|
|
1187
|
+
}
|
|
1188
|
+
if (options.sqlFNDir) {
|
|
1189
|
+
const sqlFis = globalThis[_fs].readdirSync(options.sqlDir);
|
|
1190
|
+
for (const modeName of sqlFis) {
|
|
1191
|
+
const extname = globalThis[_path].extname(modeName);
|
|
1192
|
+
const name = globalThis[_path].basename(modeName, extname);
|
|
1193
|
+
const file = globalThis[_path].join(options.sqlDir, modeName);
|
|
1194
|
+
if (extname === 'mu') {
|
|
1195
|
+
this.sqlFNMap[name] = globalThis[_fs].readFileSync(file, { encoding: 'utf-8' }).toString();
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
924
1198
|
}
|
|
925
1199
|
}
|
|
926
|
-
load(sqlid,
|
|
927
|
-
const sqlSource = this.
|
|
1200
|
+
load(sqlid, options) {
|
|
1201
|
+
const sqlSource = this.sqlMap[sqlid];
|
|
928
1202
|
error_1.Throw.if(!sqlSource, `指定的语句${sqlid}不存在!`);
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
}
|
|
1203
|
+
const _sql = typeof sqlSource === 'function' ? sqlSource(options) : sqlSource;
|
|
1204
|
+
const buildParam = new Build(options.isCount === true, options.isSum === true, options);
|
|
1205
|
+
const sql = mustache_1.default.render(_sql, buildParam, this.sqlFNMap);
|
|
1206
|
+
logger.debug(sqlid, sql);
|
|
1207
|
+
return sql;
|
|
935
1208
|
}
|
|
936
1209
|
}
|
|
937
1210
|
// #endregion
|
|
@@ -1052,7 +1325,7 @@ function P(skipConn = false) {
|
|
|
1052
1325
|
}
|
|
1053
1326
|
else if (this[_dbType] === DBType.SqliteRemote) {
|
|
1054
1327
|
error_1.Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
|
|
1055
|
-
return new Promise(async (resolve) => {
|
|
1328
|
+
return new Promise(async (resolve, reject) => {
|
|
1056
1329
|
// 连接共享
|
|
1057
1330
|
if (skipConn === false && !option.conn) {
|
|
1058
1331
|
(option).conn = await option.dao.createConnection(SyncMode.Async);
|
|
@@ -1120,7 +1393,7 @@ function P(skipConn = false) {
|
|
|
1120
1393
|
}
|
|
1121
1394
|
catch (error) {
|
|
1122
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)))}`);
|
|
1123
|
-
|
|
1396
|
+
reject(error);
|
|
1124
1397
|
}
|
|
1125
1398
|
finally {
|
|
1126
1399
|
if (needRealseConn && option && option.conn) {
|
|
@@ -1134,7 +1407,7 @@ function P(skipConn = false) {
|
|
|
1134
1407
|
});
|
|
1135
1408
|
}
|
|
1136
1409
|
else if (this[_dbType] === DBType.Mysql) {
|
|
1137
|
-
return new Promise(async (resolve) => {
|
|
1410
|
+
return new Promise(async (resolve, reject) => {
|
|
1138
1411
|
try {
|
|
1139
1412
|
// 连接共享
|
|
1140
1413
|
if (skipConn === false && !option.conn) {
|
|
@@ -1149,7 +1422,7 @@ function P(skipConn = false) {
|
|
|
1149
1422
|
}
|
|
1150
1423
|
catch (error) {
|
|
1151
1424
|
console.error(`service ${propertyKey} have an error:${error}, it's argumens: ${JSON.stringify(args.filter(i => typeof i !== 'object' || (typeof i === 'object' && !i.insert)))}`);
|
|
1152
|
-
|
|
1425
|
+
reject(error);
|
|
1153
1426
|
}
|
|
1154
1427
|
finally {
|
|
1155
1428
|
if (needRealseConn && option && option.conn) {
|
|
@@ -1229,17 +1502,17 @@ const Field = (config) => {
|
|
|
1229
1502
|
break;
|
|
1230
1503
|
}
|
|
1231
1504
|
case SqlType.float: {
|
|
1232
|
-
field[DBType.Mysql] = `${field.esName} float(${config.
|
|
1505
|
+
field[DBType.Mysql] = `${field.esName} float(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1233
1506
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1234
1507
|
break;
|
|
1235
1508
|
}
|
|
1236
1509
|
case SqlType.double: {
|
|
1237
|
-
field[DBType.Mysql] = `${field.esName} double(${config.
|
|
1510
|
+
field[DBType.Mysql] = `${field.esName} double(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1238
1511
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1239
1512
|
break;
|
|
1240
1513
|
}
|
|
1241
1514
|
case SqlType.decimal: {
|
|
1242
|
-
field[DBType.Mysql] = `${field.esName} decimal(${config.
|
|
1515
|
+
field[DBType.Mysql] = `${field.esName} decimal(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} `;
|
|
1243
1516
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} real`;
|
|
1244
1517
|
break;
|
|
1245
1518
|
}
|
|
@@ -1284,12 +1557,12 @@ const Field = (config) => {
|
|
|
1284
1557
|
break;
|
|
1285
1558
|
}
|
|
1286
1559
|
case SqlType.char: {
|
|
1287
|
-
field[DBType.Mysql] = `${field.esName} char(${config.
|
|
1560
|
+
field[DBType.Mysql] = `${field.esName} char(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1288
1561
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1289
1562
|
break;
|
|
1290
1563
|
}
|
|
1291
1564
|
case SqlType.varchar: {
|
|
1292
|
-
field[DBType.Mysql] = `${field.esName} varchar(${config.
|
|
1565
|
+
field[DBType.Mysql] = `${field.esName} varchar(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? field.def : ''} ${MYSQLCHARSET}`;
|
|
1293
1566
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.esName} text`;
|
|
1294
1567
|
break;
|
|
1295
1568
|
}
|
|
@@ -2128,10 +2401,10 @@ class SqlService {
|
|
|
2128
2401
|
select(option) {
|
|
2129
2402
|
error_1.Throw.if(!option.sqlId && !option.sql, 'not found sql!');
|
|
2130
2403
|
option.selectResult ?? (option.selectResult = SelectResult.Many_Row_Many_Column);
|
|
2131
|
-
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, option.context, option.isPage));
|
|
2132
2404
|
option.defValue ?? (option.defValue = null);
|
|
2133
|
-
logger.debug(option.sql);
|
|
2134
2405
|
const _params = Object.assign({}, option.context, option.params);
|
|
2406
|
+
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, { ctx: option.context, isCount: option.isCount, ..._params }));
|
|
2407
|
+
logger.debug(option.sql);
|
|
2135
2408
|
const params = [];
|
|
2136
2409
|
const sql = option.sql?.replace(/\:(\w+)/g, (txt, key) => {
|
|
2137
2410
|
if (_params.hasOwnProperty(key)) {
|
|
@@ -2154,9 +2427,9 @@ class SqlService {
|
|
|
2154
2427
|
}
|
|
2155
2428
|
excute(option) {
|
|
2156
2429
|
error_1.Throw.if(!option.sqlId && !option.sql, 'not found sql!');
|
|
2157
|
-
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, option.context));
|
|
2158
|
-
logger.debug(option.sql);
|
|
2159
2430
|
const _params = Object.assign({}, option.context, option.params);
|
|
2431
|
+
option.sql ?? (option.sql = globalThis[_sqlCache].load(option.sqlId, { ctx: option.context, ..._params }));
|
|
2432
|
+
logger.debug(option.sql);
|
|
2160
2433
|
const params = [];
|
|
2161
2434
|
const sql = option.sql?.replace(/\:(\w+)/g, (txt, key) => {
|
|
2162
2435
|
if (_params.hasOwnProperty(key)) {
|
|
@@ -3413,15 +3686,137 @@ function MethodCache(config) {
|
|
|
3413
3686
|
};
|
|
3414
3687
|
}
|
|
3415
3688
|
exports.MethodCache = MethodCache;
|
|
3689
|
+
class MUParser {
|
|
3690
|
+
constructor(modelName, file) {
|
|
3691
|
+
this.linNumber = 0;
|
|
3692
|
+
this.status = 0;
|
|
3693
|
+
this.lineSeparator = '\n';
|
|
3694
|
+
this.modelName = modelName;
|
|
3695
|
+
this.files = file.replace(/\r/g, '').split(this.lineSeparator);
|
|
3696
|
+
this.skipHeader();
|
|
3697
|
+
}
|
|
3698
|
+
next() {
|
|
3699
|
+
let sqlId = this.readSqlId();
|
|
3700
|
+
if (this.status === MUParser.END || !sqlId) {
|
|
3701
|
+
return null;
|
|
3702
|
+
}
|
|
3703
|
+
// 去掉可能的尾部空格
|
|
3704
|
+
sqlId = sqlId.trim();
|
|
3705
|
+
this.skipComment();
|
|
3706
|
+
if (this.status === MUParser.END) {
|
|
3707
|
+
return null;
|
|
3708
|
+
}
|
|
3709
|
+
const sql = this.readSql();
|
|
3710
|
+
return [`${this.modelName}.${sqlId}`, sql];
|
|
3711
|
+
}
|
|
3712
|
+
skipHeader() {
|
|
3713
|
+
while (true) {
|
|
3714
|
+
const line = this.nextLine();
|
|
3715
|
+
if (!line) {
|
|
3716
|
+
return;
|
|
3717
|
+
}
|
|
3718
|
+
if (this.status === MUParser.END) {
|
|
3719
|
+
return;
|
|
3720
|
+
}
|
|
3721
|
+
if (line.startsWith('===')) {
|
|
3722
|
+
return;
|
|
3723
|
+
}
|
|
3724
|
+
}
|
|
3725
|
+
}
|
|
3726
|
+
nextLine() {
|
|
3727
|
+
const line = this.files[this.linNumber];
|
|
3728
|
+
this.linNumber++;
|
|
3729
|
+
if (line === undefined) {
|
|
3730
|
+
this.status = MUParser.END;
|
|
3731
|
+
}
|
|
3732
|
+
// 保存最后读的俩行
|
|
3733
|
+
this.lastlastLine = this.lastLine;
|
|
3734
|
+
this.lastLine = line;
|
|
3735
|
+
return line;
|
|
3736
|
+
}
|
|
3737
|
+
readSqlId() {
|
|
3738
|
+
return this.lastlastLine;
|
|
3739
|
+
}
|
|
3740
|
+
skipComment() {
|
|
3741
|
+
let findComment = false;
|
|
3742
|
+
while (true) {
|
|
3743
|
+
let line = this.nextLine();
|
|
3744
|
+
if (this.status === MUParser.END || !line) {
|
|
3745
|
+
return;
|
|
3746
|
+
}
|
|
3747
|
+
line = line.trim();
|
|
3748
|
+
if (!findComment && line.length === 0) {
|
|
3749
|
+
continue;
|
|
3750
|
+
}
|
|
3751
|
+
if (line.startsWith('*')) {
|
|
3752
|
+
// 注释符号
|
|
3753
|
+
findComment = true;
|
|
3754
|
+
continue;
|
|
3755
|
+
}
|
|
3756
|
+
else {
|
|
3757
|
+
if (line.length === 0) {
|
|
3758
|
+
continue;
|
|
3759
|
+
}
|
|
3760
|
+
else if (line.startsWith('```') || line.startsWith('~~~')) {
|
|
3761
|
+
// 忽略以code block开头的符号
|
|
3762
|
+
continue;
|
|
3763
|
+
}
|
|
3764
|
+
else {
|
|
3765
|
+
// 注释结束
|
|
3766
|
+
return;
|
|
3767
|
+
}
|
|
3768
|
+
}
|
|
3769
|
+
}
|
|
3770
|
+
}
|
|
3771
|
+
readSql() {
|
|
3772
|
+
const list = [];
|
|
3773
|
+
if (this.lastLine) {
|
|
3774
|
+
list.push(this.lastLine);
|
|
3775
|
+
while (true) {
|
|
3776
|
+
const line = this.nextLine();
|
|
3777
|
+
if (line) {
|
|
3778
|
+
if (this.status === MUParser.END) {
|
|
3779
|
+
return this.getBuildSql(list);
|
|
3780
|
+
}
|
|
3781
|
+
if (line.startsWith('===')) {
|
|
3782
|
+
// 删除下一个sqlId表示
|
|
3783
|
+
list.pop();
|
|
3784
|
+
return this.getBuildSql(list);
|
|
3785
|
+
}
|
|
3786
|
+
list.push(line);
|
|
3787
|
+
}
|
|
3788
|
+
else {
|
|
3789
|
+
return '';
|
|
3790
|
+
}
|
|
3791
|
+
}
|
|
3792
|
+
}
|
|
3793
|
+
else {
|
|
3794
|
+
return '';
|
|
3795
|
+
}
|
|
3796
|
+
}
|
|
3797
|
+
getBuildSql(list) {
|
|
3798
|
+
const sb = [];
|
|
3799
|
+
for (const str of list) {
|
|
3800
|
+
const s = str.trim();
|
|
3801
|
+
if (s.startsWith('```') || s.startsWith('~~~')) {
|
|
3802
|
+
// 忽略以code block开头的符号
|
|
3803
|
+
continue;
|
|
3804
|
+
}
|
|
3805
|
+
sb.push(str);
|
|
3806
|
+
}
|
|
3807
|
+
return sb.join(this.lineSeparator);
|
|
3808
|
+
}
|
|
3809
|
+
}
|
|
3810
|
+
MUParser.END = 1;
|
|
3416
3811
|
const Boot = async function (options) {
|
|
3417
3812
|
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption, options);
|
|
3418
3813
|
if (options.sqlDir) {
|
|
3419
|
-
globalThis[_path] = Promise.resolve().then(() => __importStar(require('path')));
|
|
3420
|
-
globalThis[_fs] = Promise.resolve().then(() => __importStar(require('fs')));
|
|
3814
|
+
globalThis[_path] = await Promise.resolve().then(() => __importStar(require('path')));
|
|
3815
|
+
globalThis[_fs] = await Promise.resolve().then(() => __importStar(require('fs')));
|
|
3421
3816
|
}
|
|
3422
3817
|
logger.level = options.log ?? 'info';
|
|
3423
3818
|
globalThis[_sqlCache] = new SqlCache();
|
|
3424
|
-
if (options.
|
|
3819
|
+
if (options.sqlMap || options.sqlDir) {
|
|
3425
3820
|
await globalThis[_sqlCache].init(options);
|
|
3426
3821
|
}
|
|
3427
3822
|
globalThis[_dao] = {
|