baja-lite 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/boot-remote.d.ts +2 -0
- package/cjs/boot-remote.js +35 -0
- package/cjs/boot.d.ts +2 -0
- package/cjs/boot.js +152 -0
- package/cjs/code.d.ts +1 -0
- package/cjs/code.js +359 -1
- package/cjs/convert-xml.d.ts +10 -0
- package/cjs/convert-xml.js +414 -0
- package/cjs/enum.d.ts +10 -0
- package/cjs/enum.js +32 -0
- package/cjs/error.js +1 -1
- package/cjs/fn.js +3 -3
- package/cjs/index.d.ts +3 -0
- package/cjs/index.js +3 -0
- package/cjs/list.d.ts +10 -0
- package/cjs/list.js +36 -0
- package/cjs/object.d.ts +7 -1
- package/cjs/object.js +36 -2
- package/cjs/set-ex.d.ts +41 -15
- package/cjs/set-ex.js +68 -52
- package/cjs/sql.d.ts +760 -305
- package/cjs/sql.js +1702 -1041
- package/cjs/sqlite.d.ts +38 -0
- package/cjs/sqlite.js +194 -0
- package/cjs/test-mysql.d.ts +2 -1
- package/cjs/test-mysql.js +85 -63
- package/cjs/test-sqlite.d.ts +1 -1
- package/cjs/test-sqlite.js +3 -1
- package/cjs/test-xml.d.ts +1 -0
- package/cjs/test-xml.js +75 -0
- package/es/boot-remote.d.ts +2 -0
- package/es/boot-remote.js +31 -0
- package/es/boot.d.ts +2 -0
- package/es/boot.js +125 -0
- package/es/code.d.ts +1 -0
- package/es/code.js +355 -2
- package/es/convert-xml.d.ts +10 -0
- package/es/convert-xml.js +410 -0
- package/es/enum.d.ts +10 -0
- package/es/enum.js +28 -0
- package/es/error.js +1 -1
- package/es/index.d.ts +3 -0
- package/es/index.js +3 -0
- package/es/list.d.ts +10 -0
- package/es/list.js +32 -0
- package/es/object.d.ts +7 -1
- package/es/object.js +28 -1
- package/es/set-ex.d.ts +41 -15
- package/es/set-ex.js +68 -52
- package/es/sql.d.ts +760 -305
- package/es/sql.js +1573 -917
- package/es/sqlite.d.ts +38 -0
- package/es/sqlite.js +164 -0
- package/es/test-mysql.d.ts +2 -1
- package/es/test-mysql.js +85 -64
- package/es/test-sqlite.d.ts +1 -1
- package/es/test-sqlite.js +3 -1
- package/es/test-xml.d.ts +1 -0
- package/es/test-xml.js +70 -0
- package/package.json +15 -10
- package/src/boot-remote.ts +31 -0
- package/src/boot.ts +129 -0
- package/src/code.ts +342 -1
- package/src/convert-xml.ts +462 -0
- package/src/enum.ts +31 -0
- package/src/error.ts +1 -1
- package/src/index.ts +4 -1
- package/src/list.ts +31 -0
- package/src/object.ts +48 -14
- package/src/set-ex.ts +91 -70
- package/src/sql.ts +1652 -965
- package/src/sqlite.ts +161 -0
- package/src/test-mysql.ts +93 -65
- package/src/test-sqlite.ts +3 -1
- package/src/test-xml.ts +70 -0
- package/cjs/constant.d.ts +0 -13
- package/cjs/constant.js +0 -19
- package/cjs/redis.d.ts +0 -0
- package/cjs/redis.js +0 -1
- package/es/constant.d.ts +0 -13
- package/es/constant.js +0 -16
- package/es/redis.d.ts +0 -0
- package/es/redis.js +0 -1
- package/src/constant.ts +0 -14
- package/src/redis.ts +0 -0
package/es/sql.js
CHANGED
|
@@ -8,8 +8,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
var _a, _b, _c;
|
|
11
|
-
var _d, _e, _f;
|
|
12
|
-
import 'reflect-metadata';
|
|
11
|
+
var _d, _e, _f, _g;
|
|
13
12
|
import { Throw } from './error';
|
|
14
13
|
import tslib from 'tslib';
|
|
15
14
|
import Sqlstring from 'sqlstring';
|
|
@@ -17,11 +16,20 @@ import iterare from 'iterare';
|
|
|
17
16
|
import { emptyString } from './string';
|
|
18
17
|
import pino from 'pino';
|
|
19
18
|
import { excuteSplit, ExcuteSplitMode, sleep } from './fn';
|
|
20
|
-
import { add } from './math';
|
|
19
|
+
import { add, calc } from './math';
|
|
21
20
|
import mustache from 'mustache';
|
|
21
|
+
import { C2P, C2P2, P2C } from './object';
|
|
22
|
+
import { format } from 'sql-formatter';
|
|
23
|
+
import HTML from 'html-parse-stringify';
|
|
24
|
+
import { convert } from './convert-xml';
|
|
25
|
+
import { ArrayList } from './list';
|
|
26
|
+
import LGet from 'lodash.get';
|
|
22
27
|
// #region 常量
|
|
23
28
|
const _daoDBName = Symbol('dbName');
|
|
24
29
|
const _tableName = Symbol('tableName');
|
|
30
|
+
const _className = Symbol('className');
|
|
31
|
+
const _ClassName = Symbol('ClassName');
|
|
32
|
+
const _vueName = Symbol('vueName');
|
|
25
33
|
const _ids = Symbol('ids');
|
|
26
34
|
const _columns = Symbol('columns');
|
|
27
35
|
const _columnsNoId = Symbol('columnsNoId');
|
|
@@ -31,9 +39,9 @@ const _deleteState = Symbol('deleteState');
|
|
|
31
39
|
const _transformer = Symbol('transformer');
|
|
32
40
|
const _index = Symbol('index');
|
|
33
41
|
const _def = Symbol('def');
|
|
34
|
-
const _sqlCache = Symbol('sqlMap');
|
|
35
|
-
const _dao = Symbol('dao');
|
|
36
|
-
const _primaryDB = Symbol('primaryDB');
|
|
42
|
+
export const _sqlCache = Symbol('sqlMap');
|
|
43
|
+
export const _dao = Symbol('dao');
|
|
44
|
+
export const _primaryDB = Symbol('primaryDB');
|
|
37
45
|
const _dbType = Symbol('dbType');
|
|
38
46
|
const _sqlite_version = Symbol('sqlite_version');
|
|
39
47
|
const _daoConnection = Symbol('daoConnection');
|
|
@@ -41,16 +49,20 @@ const _inTransaction = Symbol('inTransaction');
|
|
|
41
49
|
const _daoDB = Symbol('daoDB');
|
|
42
50
|
const _sqliteRemoteName = Symbol('sqliteRemoteName');
|
|
43
51
|
const _SqlOption = Symbol('SqlOption');
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const
|
|
52
|
+
const _resultMap = Symbol('resultMap');
|
|
53
|
+
const _resultMap_SQLID = Symbol('resultMap_SQLID');
|
|
54
|
+
export const _Hump = Symbol('Hump');
|
|
55
|
+
export const _GlobalSqlOption = Symbol('GlobalSqlOption');
|
|
56
|
+
export const _EventBus = Symbol('EventBus');
|
|
57
|
+
export const _path = Symbol('path');
|
|
58
|
+
export const _fs = Symbol('fs');
|
|
59
|
+
export const logger = process.env['NODE_ENV'] !== 'production' ? pino({
|
|
49
60
|
name: 'sql',
|
|
50
61
|
transport: {
|
|
51
62
|
target: 'pino-pretty'
|
|
52
63
|
}
|
|
53
|
-
});
|
|
64
|
+
}) : pino({ name: 'sql' });
|
|
65
|
+
globalThis[_resultMap_SQLID] = {};
|
|
54
66
|
// #endregion
|
|
55
67
|
// #region 可选配置
|
|
56
68
|
export var DBType;
|
|
@@ -62,6 +74,15 @@ export var DBType;
|
|
|
62
74
|
DBType[DBType["Redis"] = 4] = "Redis";
|
|
63
75
|
DBType[DBType["RedisLock"] = 5] = "RedisLock";
|
|
64
76
|
})(DBType || (DBType = {}));
|
|
77
|
+
;
|
|
78
|
+
export var MapperIfUndefined;
|
|
79
|
+
(function (MapperIfUndefined) {
|
|
80
|
+
MapperIfUndefined[MapperIfUndefined["Null"] = 0] = "Null";
|
|
81
|
+
MapperIfUndefined[MapperIfUndefined["Skip"] = 1] = "Skip";
|
|
82
|
+
MapperIfUndefined[MapperIfUndefined["Zero"] = 2] = "Zero";
|
|
83
|
+
MapperIfUndefined[MapperIfUndefined["EmptyString"] = 3] = "EmptyString";
|
|
84
|
+
})(MapperIfUndefined || (MapperIfUndefined = {}));
|
|
85
|
+
;
|
|
65
86
|
export var SyncMode;
|
|
66
87
|
(function (SyncMode) {
|
|
67
88
|
/** 同步执行 */
|
|
@@ -138,17 +159,29 @@ export var TemplateResult;
|
|
|
138
159
|
TemplateResult[TemplateResult["NotSureOne"] = 1] = "NotSureOne";
|
|
139
160
|
/** 返回多条记录 */
|
|
140
161
|
TemplateResult[TemplateResult["Many"] = 2] = "Many";
|
|
162
|
+
/** 返回多条记录并封装ArrayList */
|
|
163
|
+
TemplateResult[TemplateResult["ManyList"] = 3] = "ManyList";
|
|
141
164
|
/** 仅查询记录数量 */
|
|
142
|
-
TemplateResult[TemplateResult["Count"] =
|
|
165
|
+
TemplateResult[TemplateResult["Count"] = 4] = "Count";
|
|
143
166
|
})(TemplateResult || (TemplateResult = {}));
|
|
144
167
|
export var SelectResult;
|
|
145
168
|
(function (SelectResult) {
|
|
146
|
-
|
|
147
|
-
SelectResult[SelectResult["
|
|
148
|
-
|
|
149
|
-
SelectResult[SelectResult["
|
|
150
|
-
|
|
151
|
-
SelectResult[SelectResult["
|
|
169
|
+
/** 一行一列 确定非空 */
|
|
170
|
+
SelectResult[SelectResult["R_C_Assert"] = 0] = "R_C_Assert";
|
|
171
|
+
/** 一行一列 可能空 */
|
|
172
|
+
SelectResult[SelectResult["R_C_NotSure"] = 1] = "R_C_NotSure";
|
|
173
|
+
/** 一行多列 确定非空 */
|
|
174
|
+
SelectResult[SelectResult["R_CS_Assert"] = 2] = "R_CS_Assert";
|
|
175
|
+
/** 一行多列 可能空 */
|
|
176
|
+
SelectResult[SelectResult["R_CS_NotSure"] = 3] = "R_CS_NotSure";
|
|
177
|
+
/** 多行一列 */
|
|
178
|
+
SelectResult[SelectResult["RS_C"] = 4] = "RS_C";
|
|
179
|
+
/** 多行一列并封装ArrayList */
|
|
180
|
+
SelectResult[SelectResult["RS_C_List"] = 5] = "RS_C_List";
|
|
181
|
+
/** 多行多列 */
|
|
182
|
+
SelectResult[SelectResult["RS_CS"] = 6] = "RS_CS";
|
|
183
|
+
/** 多行多列并封装ArrayList */
|
|
184
|
+
SelectResult[SelectResult["RS_CS_List"] = 7] = "RS_CS_List";
|
|
152
185
|
})(SelectResult || (SelectResult = {}));
|
|
153
186
|
export var SqlType;
|
|
154
187
|
(function (SqlType) {
|
|
@@ -187,8 +220,13 @@ export var SqlType;
|
|
|
187
220
|
SqlType[SqlType["multipolygon"] = 32] = "multipolygon";
|
|
188
221
|
SqlType[SqlType["geometrycollection"] = 33] = "geometrycollection";
|
|
189
222
|
})(SqlType || (SqlType = {}));
|
|
223
|
+
export var ColumnMode;
|
|
224
|
+
(function (ColumnMode) {
|
|
225
|
+
ColumnMode[ColumnMode["NONE"] = 0] = "NONE";
|
|
226
|
+
ColumnMode[ColumnMode["HUMP"] = 1] = "HUMP";
|
|
227
|
+
})(ColumnMode || (ColumnMode = {}));
|
|
190
228
|
export const SqliteMemory = ':memory:';
|
|
191
|
-
const _defOption = {
|
|
229
|
+
export const _defOption = {
|
|
192
230
|
maxDeal: 500,
|
|
193
231
|
skipUndefined: true,
|
|
194
232
|
skipNull: true,
|
|
@@ -381,7 +419,7 @@ class MysqlConnection {
|
|
|
381
419
|
}
|
|
382
420
|
}
|
|
383
421
|
_a = _inTransaction;
|
|
384
|
-
class Mysql {
|
|
422
|
+
export class Mysql {
|
|
385
423
|
constructor(pool) {
|
|
386
424
|
this[_daoDB] = pool;
|
|
387
425
|
}
|
|
@@ -394,7 +432,7 @@ class Mysql {
|
|
|
394
432
|
return new Promise(async (resolve, reject) => {
|
|
395
433
|
try {
|
|
396
434
|
const connection = await this[_daoDB].getConnection();
|
|
397
|
-
logger.debug('create new!');
|
|
435
|
+
logger.debug('create new connection!');
|
|
398
436
|
resolve(new MysqlConnection(connection));
|
|
399
437
|
}
|
|
400
438
|
catch (error) {
|
|
@@ -612,7 +650,7 @@ class SqliteConnection {
|
|
|
612
650
|
}
|
|
613
651
|
}
|
|
614
652
|
_b = _inTransaction;
|
|
615
|
-
class Sqlite {
|
|
653
|
+
export class Sqlite {
|
|
616
654
|
constructor(db) {
|
|
617
655
|
this[_daoDB] = db;
|
|
618
656
|
this[_daoDB].pragma('journal_mode = WAL');
|
|
@@ -674,7 +712,7 @@ class Sqlite {
|
|
|
674
712
|
restore(sync, name) {
|
|
675
713
|
}
|
|
676
714
|
}
|
|
677
|
-
class SqliteRemoteConnection {
|
|
715
|
+
export class SqliteRemoteConnection {
|
|
678
716
|
constructor(conn, name) {
|
|
679
717
|
this[_c] = false;
|
|
680
718
|
this[_daoConnection] = conn;
|
|
@@ -829,7 +867,7 @@ class SqliteRemoteConnection {
|
|
|
829
867
|
}
|
|
830
868
|
}
|
|
831
869
|
_c = _inTransaction;
|
|
832
|
-
class SqliteRemote {
|
|
870
|
+
export class SqliteRemote {
|
|
833
871
|
constructor(db, name) {
|
|
834
872
|
this[_daoDB] = db;
|
|
835
873
|
this[_sqliteRemoteName] = name;
|
|
@@ -840,8 +878,13 @@ class SqliteRemote {
|
|
|
840
878
|
return null;
|
|
841
879
|
}
|
|
842
880
|
;
|
|
843
|
-
return new Promise(async (resolve) => {
|
|
844
|
-
|
|
881
|
+
return new Promise(async (resolve, reject) => {
|
|
882
|
+
try {
|
|
883
|
+
resolve(new SqliteRemoteConnection(this[_daoDB], this[_sqliteRemoteName]));
|
|
884
|
+
}
|
|
885
|
+
catch (error) {
|
|
886
|
+
reject(error);
|
|
887
|
+
}
|
|
845
888
|
});
|
|
846
889
|
}
|
|
847
890
|
transaction(sync, fn, conn) {
|
|
@@ -850,32 +893,52 @@ class SqliteRemote {
|
|
|
850
893
|
}
|
|
851
894
|
close(sync) {
|
|
852
895
|
if (sync === SyncMode.Async) {
|
|
853
|
-
return new Promise(async () => {
|
|
854
|
-
|
|
896
|
+
return new Promise(async (resolve, reject) => {
|
|
897
|
+
try {
|
|
898
|
+
await this[_daoConnection].close();
|
|
899
|
+
}
|
|
900
|
+
catch (error) {
|
|
901
|
+
reject(error);
|
|
902
|
+
}
|
|
855
903
|
});
|
|
856
904
|
}
|
|
857
905
|
;
|
|
858
906
|
}
|
|
859
907
|
backup(sync, name) {
|
|
860
908
|
if (sync === SyncMode.Async) {
|
|
861
|
-
return new Promise(async () => {
|
|
862
|
-
|
|
909
|
+
return new Promise(async (resolve, reject) => {
|
|
910
|
+
try {
|
|
911
|
+
await this[_daoConnection].backup(this[_sqliteRemoteName], name);
|
|
912
|
+
}
|
|
913
|
+
catch (error) {
|
|
914
|
+
reject(error);
|
|
915
|
+
}
|
|
863
916
|
});
|
|
864
917
|
}
|
|
865
918
|
;
|
|
866
919
|
}
|
|
867
920
|
remove(sync) {
|
|
868
921
|
if (sync === SyncMode.Async) {
|
|
869
|
-
return new Promise(async () => {
|
|
870
|
-
|
|
922
|
+
return new Promise(async (resolve, reject) => {
|
|
923
|
+
try {
|
|
924
|
+
await this[_daoConnection].remove();
|
|
925
|
+
}
|
|
926
|
+
catch (error) {
|
|
927
|
+
reject(error);
|
|
928
|
+
}
|
|
871
929
|
});
|
|
872
930
|
}
|
|
873
931
|
;
|
|
874
932
|
}
|
|
875
933
|
restore(sync, name) {
|
|
876
934
|
if (sync === SyncMode.Async) {
|
|
877
|
-
return new Promise(async () => {
|
|
878
|
-
|
|
935
|
+
return new Promise(async (resolve, reject) => {
|
|
936
|
+
try {
|
|
937
|
+
await this[_daoConnection].restore(this[_sqliteRemoteName], name);
|
|
938
|
+
}
|
|
939
|
+
catch (error) {
|
|
940
|
+
reject(error);
|
|
941
|
+
}
|
|
879
942
|
});
|
|
880
943
|
}
|
|
881
944
|
;
|
|
@@ -900,7 +963,7 @@ class Build {
|
|
|
900
963
|
* @returns
|
|
901
964
|
* @memberof Build
|
|
902
965
|
*/
|
|
903
|
-
|
|
966
|
+
page() {
|
|
904
967
|
return (text, render) => {
|
|
905
968
|
if (this.isCount === true) {
|
|
906
969
|
return Build.page;
|
|
@@ -912,11 +975,11 @@ class Build {
|
|
|
912
975
|
}
|
|
913
976
|
/**
|
|
914
977
|
*
|
|
915
|
-
*
|
|
978
|
+
* 包含的内容只在汇总查询时有效,否则是空白
|
|
916
979
|
* @returns
|
|
917
980
|
* @memberof Build
|
|
918
981
|
*/
|
|
919
|
-
|
|
982
|
+
sum() {
|
|
920
983
|
return (text, render) => {
|
|
921
984
|
if (this.isSum !== true) {
|
|
922
985
|
return '';
|
|
@@ -932,7 +995,7 @@ class Build {
|
|
|
932
995
|
* @returns
|
|
933
996
|
* @memberof Build
|
|
934
997
|
*/
|
|
935
|
-
|
|
998
|
+
notPage() {
|
|
936
999
|
return (text, render) => {
|
|
937
1000
|
if (this.isCount === true || this.isSum === true) {
|
|
938
1001
|
return '';
|
|
@@ -957,8 +1020,7 @@ class Build {
|
|
|
957
1020
|
*/
|
|
958
1021
|
where() {
|
|
959
1022
|
return (text, render) => {
|
|
960
|
-
let data = render(text);
|
|
961
|
-
data = data.trim();
|
|
1023
|
+
let data = render(text).trim();
|
|
962
1024
|
if (data) {
|
|
963
1025
|
data = data.replace(/and|or/i, '');
|
|
964
1026
|
return ` WHERE ${data} `;
|
|
@@ -968,6 +1030,33 @@ class Build {
|
|
|
968
1030
|
}
|
|
969
1031
|
};
|
|
970
1032
|
}
|
|
1033
|
+
/**
|
|
1034
|
+
* ```
|
|
1035
|
+
* SELECT
|
|
1036
|
+
* {{#hump}}
|
|
1037
|
+
* a.event_id, a.event_name eventName
|
|
1038
|
+
* {{/hump}}
|
|
1039
|
+
* FROM...
|
|
1040
|
+
* ```
|
|
1041
|
+
* 编译后:
|
|
1042
|
+
* ```
|
|
1043
|
+
* SELECT
|
|
1044
|
+
* a.event_id eventId, a.event_name eventName
|
|
1045
|
+
* FROM...
|
|
1046
|
+
* ```
|
|
1047
|
+
*/
|
|
1048
|
+
hump() {
|
|
1049
|
+
return (text, render) => {
|
|
1050
|
+
let data = render(text).trim();
|
|
1051
|
+
const datas = data.split(',');
|
|
1052
|
+
for (let i = 0; i < datas.length; i++) {
|
|
1053
|
+
if (datas[i]?.match(/\s|\t/) === null) {
|
|
1054
|
+
datas[i] = `${datas[i]} ${datas[i].replace(/[a-zA-Z0-9]+\./, '').replace(/_([a-z])/g, (a, b, c) => b.toUpperCase())}`;
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
return datas.join(',');
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
971
1060
|
/**
|
|
972
1061
|
* 删除第一个and、or
|
|
973
1062
|
* 删除最后一个,
|
|
@@ -991,13 +1080,13 @@ class Build {
|
|
|
991
1080
|
* 分页时将排序部分代码用此函数包起来,可以自动拼接order by
|
|
992
1081
|
* 查询条数时,自动忽略此部分
|
|
993
1082
|
* etc
|
|
994
|
-
* {{#
|
|
1083
|
+
* {{#order}} name desc, age asc {{/order}}
|
|
995
1084
|
* ===
|
|
996
1085
|
* ORDER BY name desc, age asc
|
|
997
1086
|
* @returns
|
|
998
1087
|
* @memberof Build
|
|
999
1088
|
*/
|
|
1000
|
-
|
|
1089
|
+
order() {
|
|
1001
1090
|
return (text, render) => {
|
|
1002
1091
|
if (this.isCount === true || this.isSum === true) {
|
|
1003
1092
|
return '';
|
|
@@ -1013,7 +1102,7 @@ class Build {
|
|
|
1013
1102
|
}
|
|
1014
1103
|
};
|
|
1015
1104
|
}
|
|
1016
|
-
|
|
1105
|
+
limit() {
|
|
1017
1106
|
return (text, render) => {
|
|
1018
1107
|
if (this.isCount === true || this.isSum === true) {
|
|
1019
1108
|
return '';
|
|
@@ -1034,13 +1123,13 @@ class Build {
|
|
|
1034
1123
|
* 分页时将分组部分代码用此函数包起来,可以自动拼接GROUP BY
|
|
1035
1124
|
* 当分页时、汇总时,自动忽略此部分
|
|
1036
1125
|
* etc
|
|
1037
|
-
* {{#
|
|
1126
|
+
* {{#between}} name, age {{/between}}
|
|
1038
1127
|
* ===
|
|
1039
1128
|
* group by name.age
|
|
1040
1129
|
* @returns
|
|
1041
1130
|
* @memberof Build
|
|
1042
1131
|
*/
|
|
1043
|
-
|
|
1132
|
+
group() {
|
|
1044
1133
|
return (text, render) => {
|
|
1045
1134
|
if (this.isCount === true || this.isSum === true) {
|
|
1046
1135
|
return '';
|
|
@@ -1085,15 +1174,15 @@ class Build {
|
|
|
1085
1174
|
*
|
|
1086
1175
|
* 距离计算,单位米
|
|
1087
1176
|
* etc
|
|
1088
|
-
* {{#
|
|
1177
|
+
* {{#distance}} (t.longitude, t.latitude), ({{longitude}}, {{latitude}}) {{/distance}}
|
|
1089
1178
|
* ===
|
|
1090
1179
|
* ROUND(ST_DISTANCE(POINT(longitude1, latitude1), POINT({{longitude}}, {{latitude}}))*111195, 2)
|
|
1091
1180
|
* 可根据需求自行将数据转换为千米,例如
|
|
1092
|
-
* {{#
|
|
1181
|
+
* {{#distance}} (t.longitude, t.latitude), ({{longitude}}, {{latitude}}) {{/distance}} / 1000
|
|
1093
1182
|
* @returns
|
|
1094
1183
|
* @memberof Build
|
|
1095
1184
|
*/
|
|
1096
|
-
|
|
1185
|
+
distance() {
|
|
1097
1186
|
return (text, render) => {
|
|
1098
1187
|
const result = render(text);
|
|
1099
1188
|
if (/\(([^()]+)\)/.exec(result)) {
|
|
@@ -1121,38 +1210,179 @@ class Build {
|
|
|
1121
1210
|
}
|
|
1122
1211
|
}
|
|
1123
1212
|
Build.page = 'COUNT(1) zccw1986 ';
|
|
1124
|
-
|
|
1213
|
+
function replaceCdata(rawText) {
|
|
1214
|
+
var cdataRegex = new RegExp('(<!\\[CDATA\\[)([\\s\\S]*?)(\\]\\]>)', 'g');
|
|
1215
|
+
var matches = rawText.match(cdataRegex);
|
|
1216
|
+
if (matches != null && matches.length > 0) {
|
|
1217
|
+
for (var z = 0; z < matches.length; z++) {
|
|
1218
|
+
var regex = new RegExp('(<!\\[CDATA\\[)([\\s\\S]*?)(\\]\\]>)', 'g');
|
|
1219
|
+
var m = regex.exec(matches[z]);
|
|
1220
|
+
var cdataText = m[2];
|
|
1221
|
+
cdataText = cdataText.replace(/\&/g, '&');
|
|
1222
|
+
cdataText = cdataText.replace(/\</g, '<');
|
|
1223
|
+
cdataText = cdataText.replace(/\>/g, '>');
|
|
1224
|
+
cdataText = cdataText.replace(/\"/g, '"');
|
|
1225
|
+
rawText = rawText.replace(m[0], cdataText);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
return rawText;
|
|
1229
|
+
}
|
|
1230
|
+
function _flatData(result, i, length, keys, V) {
|
|
1231
|
+
var _d;
|
|
1232
|
+
const key = keys[i];
|
|
1233
|
+
if (i < length) {
|
|
1234
|
+
result[_d = key] ?? (result[_d] = {});
|
|
1235
|
+
i++;
|
|
1236
|
+
_flatData(result[key], i, length, keys, V);
|
|
1237
|
+
}
|
|
1238
|
+
else {
|
|
1239
|
+
result[key] = V;
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
/**
|
|
1243
|
+
* ifUndefined默认是MapperIfUndefined.Skip
|
|
1244
|
+
*/
|
|
1245
|
+
export function flatData(options) {
|
|
1246
|
+
if (typeof options.mapper === 'string') {
|
|
1247
|
+
const name = options.mapper;
|
|
1248
|
+
options.mapper = globalThis[_resultMap][name];
|
|
1249
|
+
Throw.if(!options.mapper, `not found mapper!${name}`);
|
|
1250
|
+
}
|
|
1251
|
+
options.mapperIfUndefined ?? (options.mapperIfUndefined = MapperIfUndefined.Skip);
|
|
1252
|
+
options.mapper = options.mapper;
|
|
1253
|
+
const result = {};
|
|
1254
|
+
for (const [columnName, keys, def] of options.mapper) {
|
|
1255
|
+
let V = options.data[columnName];
|
|
1256
|
+
if (V === undefined) {
|
|
1257
|
+
if (options.mapperIfUndefined === MapperIfUndefined.Null) {
|
|
1258
|
+
V = null;
|
|
1259
|
+
}
|
|
1260
|
+
else if (options.mapperIfUndefined === MapperIfUndefined.Zero) {
|
|
1261
|
+
V = 0;
|
|
1262
|
+
}
|
|
1263
|
+
else if (options.mapperIfUndefined === MapperIfUndefined.EmptyString) {
|
|
1264
|
+
V = '';
|
|
1265
|
+
}
|
|
1266
|
+
else if (def !== undefined) {
|
|
1267
|
+
V = def;
|
|
1268
|
+
}
|
|
1269
|
+
else {
|
|
1270
|
+
continue;
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
_flatData(result, 0, keys.length - 1, keys, V);
|
|
1274
|
+
}
|
|
1275
|
+
return result;
|
|
1276
|
+
}
|
|
1277
|
+
export class SqlCache {
|
|
1125
1278
|
constructor() {
|
|
1126
1279
|
this.sqlMap = {};
|
|
1127
1280
|
this.sqlFNMap = {};
|
|
1128
1281
|
}
|
|
1129
|
-
async
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1282
|
+
async _read(jsMode, sqlDir, queryTypes, rootName) {
|
|
1283
|
+
const sqlFis = globalThis[_fs].readdirSync(sqlDir);
|
|
1284
|
+
for (const modeName of sqlFis) {
|
|
1285
|
+
const file = globalThis[_path].join(sqlDir, modeName);
|
|
1286
|
+
const stat = globalThis[_fs].statSync(file);
|
|
1287
|
+
if (stat.isDirectory()) {
|
|
1288
|
+
await this._read(jsMode, file, queryTypes, modeName);
|
|
1289
|
+
}
|
|
1290
|
+
else {
|
|
1136
1291
|
const extname = globalThis[_path].extname(modeName);
|
|
1137
1292
|
const name = globalThis[_path].basename(modeName, extname);
|
|
1138
|
-
|
|
1139
|
-
if (extname === 'mu') {
|
|
1140
|
-
|
|
1293
|
+
let ct = 0;
|
|
1294
|
+
if (extname === '.mu') {
|
|
1295
|
+
logger.debug(`sql: ${file} start explain!`);
|
|
1296
|
+
const parser = new MUParser(rootName || name, globalThis[_fs].readFileSync(file, { encoding: 'utf-8' }).toString());
|
|
1141
1297
|
let source = parser.next();
|
|
1142
1298
|
while (source != null) {
|
|
1299
|
+
ct++;
|
|
1143
1300
|
this.sqlMap[source[0]] = source[1];
|
|
1144
1301
|
logger.debug(`sql: ${source[0]} found!`);
|
|
1145
1302
|
source = parser.next();
|
|
1146
1303
|
}
|
|
1304
|
+
logger.debug(`sql: ${file} explain over[${ct}]!`);
|
|
1147
1305
|
}
|
|
1148
|
-
else if (extname === '.js') {
|
|
1149
|
-
|
|
1306
|
+
else if (jsMode === true && extname === '.js') {
|
|
1307
|
+
logger.debug(`sql: ${file} start explain!`);
|
|
1308
|
+
const obj = (await import(globalThis[_path].join(sqlDir, modeName))).default;
|
|
1150
1309
|
for (const [key, fn] of Object.entries(obj)) {
|
|
1151
|
-
|
|
1310
|
+
ct++;
|
|
1311
|
+
this.sqlMap[`${rootName || name}.${String(key)}`] = fn;
|
|
1152
1312
|
}
|
|
1313
|
+
logger.debug(`sql: ${file} explain over[${ct}]!`);
|
|
1314
|
+
}
|
|
1315
|
+
else if (extname === '.xml') {
|
|
1316
|
+
logger.debug(`sql: ${file} start explain!`);
|
|
1317
|
+
const root = HTML.parse(replaceCdata(globalThis[_fs].readFileSync(file, { encoding: 'utf-8' }).toString()))[0];
|
|
1318
|
+
if (root) {
|
|
1319
|
+
const mappers = root.children;
|
|
1320
|
+
for (const mapper of mappers) {
|
|
1321
|
+
if (mapper.type === 'tag' && mapper.name === 'mapper') {
|
|
1322
|
+
for (const am of mapper.children) {
|
|
1323
|
+
if (am.type === 'tag') {
|
|
1324
|
+
Throw.if(!queryTypes.includes(am.name), `${rootName} ${name}错误,${am.name}不支持!`);
|
|
1325
|
+
am.id = am.attrs['id'];
|
|
1326
|
+
Throw.if(!am.id, `${rootName} ${name}错误,没有为此块设置id:${am}`);
|
|
1327
|
+
if (am.name === 'resultMap') {
|
|
1328
|
+
ct++;
|
|
1329
|
+
globalThis[_resultMap] ?? (globalThis[_resultMap] = {});
|
|
1330
|
+
const keys = [];
|
|
1331
|
+
this.readResultMap(am.children, keys, []);
|
|
1332
|
+
globalThis[_resultMap][am.id] = keys;
|
|
1333
|
+
logger.debug(`sql_resultMap: ${am.id} found!`);
|
|
1334
|
+
}
|
|
1335
|
+
else {
|
|
1336
|
+
this.sqlMap[`${rootName || name}.${am.id}`] = am.children;
|
|
1337
|
+
if (am.attrs['resultMap']) {
|
|
1338
|
+
globalThis[_resultMap_SQLID][`${rootName || name}.${am.id}`] = am.attrs['resultMap'];
|
|
1339
|
+
logger.debug(`sql: autoMapper: ${rootName || name}.${am.id}-${am.attrs['resultMap']}`);
|
|
1340
|
+
}
|
|
1341
|
+
logger.debug(`sql: ${rootName || name}.${am.id} found!`);
|
|
1342
|
+
ct++;
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
logger.debug(`sql: ${file} explain over[${ct}]!`);
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
/**
|
|
1355
|
+
*
|
|
1356
|
+
* ```
|
|
1357
|
+
// 第一个元素=列名,第二个元素是属性路径,
|
|
1358
|
+
[
|
|
1359
|
+
['dit_id', ['id']], // 列名ditid,对应属性id
|
|
1360
|
+
['event_id', ['eventMainInfo', 'id']] // 列名event_id对应属性eventMainInfo.id
|
|
1361
|
+
]
|
|
1362
|
+
* ```
|
|
1363
|
+
* @param am
|
|
1364
|
+
* @param keys
|
|
1365
|
+
*/
|
|
1366
|
+
readResultMap(ams, keys, key) {
|
|
1367
|
+
for (const am of ams) {
|
|
1368
|
+
if (am.type === 'tag') {
|
|
1369
|
+
if (am.name === 'result' || am.name === 'id') {
|
|
1370
|
+
keys.push([am.attrs['column'], [...key, am.attrs['property']]]);
|
|
1371
|
+
}
|
|
1372
|
+
else {
|
|
1373
|
+
this.readResultMap(am.children, keys, [...key, am.attrs['property']]);
|
|
1153
1374
|
}
|
|
1154
1375
|
}
|
|
1155
1376
|
}
|
|
1377
|
+
}
|
|
1378
|
+
async init(options) {
|
|
1379
|
+
if (options.sqlMap) {
|
|
1380
|
+
this.sqlMap = options.sqlMap;
|
|
1381
|
+
}
|
|
1382
|
+
const queryTypes = ['sql', 'select', 'insert', 'update', 'delete', 'resultMap'];
|
|
1383
|
+
if (options.sqlDir) {
|
|
1384
|
+
await this._read(options.jsMode === true, options.sqlDir, queryTypes, '');
|
|
1385
|
+
}
|
|
1156
1386
|
if (options.sqlFNMap) {
|
|
1157
1387
|
this.sqlFNMap = options.sqlFNMap;
|
|
1158
1388
|
}
|
|
@@ -1167,15 +1397,50 @@ class SqlCache {
|
|
|
1167
1397
|
}
|
|
1168
1398
|
}
|
|
1169
1399
|
}
|
|
1400
|
+
if (options.sqlMapperMap) {
|
|
1401
|
+
globalThis[_resultMap] = options.sqlFNMap;
|
|
1402
|
+
}
|
|
1403
|
+
if (options.sqlMapperDir) {
|
|
1404
|
+
const sqlFis = globalThis[_fs].readdirSync(options.sqlDir);
|
|
1405
|
+
globalThis[_resultMap] ?? (globalThis[_resultMap] = {});
|
|
1406
|
+
for (const modeName of sqlFis) {
|
|
1407
|
+
const extname = globalThis[_path].extname(modeName);
|
|
1408
|
+
const name = globalThis[_path].basename(modeName, extname);
|
|
1409
|
+
const file = globalThis[_path].join(options.sqlDir, modeName);
|
|
1410
|
+
if (extname === 'json') {
|
|
1411
|
+
globalThis[_resultMap][name] = JSON.parse(globalThis[_fs].readFileSync(file, { encoding: 'utf-8' }).toString());
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1170
1415
|
}
|
|
1171
|
-
load(
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1416
|
+
load(sqlids, options) {
|
|
1417
|
+
let sqlSource;
|
|
1418
|
+
for (const sqlid of sqlids) {
|
|
1419
|
+
sqlSource = this.sqlMap[sqlid];
|
|
1420
|
+
if (sqlSource) {
|
|
1421
|
+
break;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
const matchSqlid = sqlids.map(i => i.split('.')[0]);
|
|
1425
|
+
Throw.if(!sqlSource, `指定的语句${sqlids.join('|')}不存在!`);
|
|
1175
1426
|
const buildParam = new Build(options.isCount === true, options.isSum === true, options);
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1427
|
+
if (typeof sqlSource === 'function') {
|
|
1428
|
+
const _sql = sqlSource(options);
|
|
1429
|
+
const sql = mustache.render(_sql, buildParam, this.sqlFNMap);
|
|
1430
|
+
return format(sql);
|
|
1431
|
+
}
|
|
1432
|
+
else if (typeof sqlSource === 'string') {
|
|
1433
|
+
const sql = mustache.render(sqlSource, buildParam, this.sqlFNMap);
|
|
1434
|
+
return format(sql);
|
|
1435
|
+
}
|
|
1436
|
+
else if (typeof sqlSource === 'object') {
|
|
1437
|
+
const _sql = convert(sqlSource, options, matchSqlid, this.sqlMap);
|
|
1438
|
+
console.log(_sql);
|
|
1439
|
+
const sql = mustache.render(_sql, buildParam, this.sqlFNMap);
|
|
1440
|
+
console.log(sql);
|
|
1441
|
+
return format(sql);
|
|
1442
|
+
}
|
|
1443
|
+
return '';
|
|
1179
1444
|
}
|
|
1180
1445
|
}
|
|
1181
1446
|
// #endregion
|
|
@@ -1204,6 +1469,8 @@ function P(skipConn = false) {
|
|
|
1204
1469
|
const option = args[0] = Object.assign({}, globalThis[_GlobalSqlOption], this[_SqlOption], args[0]);
|
|
1205
1470
|
option.sync ?? (option.sync = SyncMode.Async);
|
|
1206
1471
|
const dbName = option?.dbName ?? this[_daoDBName] ?? _primaryDB;
|
|
1472
|
+
const dddx = this[_dbType];
|
|
1473
|
+
logger.info(dddx);
|
|
1207
1474
|
option.dao = globalThis[_dao][this[_dbType]][dbName];
|
|
1208
1475
|
Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
|
|
1209
1476
|
option.tableName = option?.tableName ?? this[_tableName];
|
|
@@ -1227,7 +1494,7 @@ function P(skipConn = false) {
|
|
|
1227
1494
|
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
1228
1495
|
// 更新版本
|
|
1229
1496
|
const columns = iterare(option.conn.query(SyncMode.Sync, `PRAGMA table_info(${tableES})`))
|
|
1230
|
-
.filter(c => this[_fields].hasOwnProperty(c.name))
|
|
1497
|
+
.filter(c => this[_fields].hasOwnProperty(C2P(c.name, globalThis[_Hump])))
|
|
1231
1498
|
.map(c => Sqlstring.escapeId(c.name))
|
|
1232
1499
|
.join(',');
|
|
1233
1500
|
const rtable = Sqlstring.escapeId(`${option.tableName}_${tableVersion.replace(/\./, '_')}`);
|
|
@@ -1235,13 +1502,13 @@ function P(skipConn = false) {
|
|
|
1235
1502
|
option.conn.execute(SyncMode.Sync, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1236
1503
|
option.conn.execute(SyncMode.Sync, `
|
|
1237
1504
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1238
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1239
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.
|
|
1505
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1506
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1240
1507
|
);
|
|
1241
1508
|
`);
|
|
1242
1509
|
if (this[_index] && this[_index].length) {
|
|
1243
1510
|
for (const index of this[_index]) {
|
|
1244
|
-
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
1511
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
1245
1512
|
}
|
|
1246
1513
|
}
|
|
1247
1514
|
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
@@ -1258,14 +1525,14 @@ function P(skipConn = false) {
|
|
|
1258
1525
|
// 创建表
|
|
1259
1526
|
option.conn.execute(SyncMode.Sync, `
|
|
1260
1527
|
CREATE TABLE IF NOT EXISTS ${tableES} (
|
|
1261
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1262
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.
|
|
1528
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1529
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1263
1530
|
|
|
1264
1531
|
);
|
|
1265
1532
|
`);
|
|
1266
1533
|
if (this[_index] && this[_index].length) {
|
|
1267
1534
|
for (const index of this[_index]) {
|
|
1268
|
-
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
1535
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
1269
1536
|
}
|
|
1270
1537
|
}
|
|
1271
1538
|
option.conn.execute(SyncMode.Sync, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
@@ -1314,7 +1581,7 @@ function P(skipConn = false) {
|
|
|
1314
1581
|
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
1315
1582
|
// 更新版本
|
|
1316
1583
|
const columns = iterare(await option.conn.query(SyncMode.Async, `PRAGMA table_info(${tableES})`))
|
|
1317
|
-
.filter(c => this[_fields].hasOwnProperty(c.name))
|
|
1584
|
+
.filter(c => this[_fields].hasOwnProperty(C2P(c.name, globalThis[_Hump])))
|
|
1318
1585
|
.map(c => Sqlstring.escapeId(c.name))
|
|
1319
1586
|
.join(',');
|
|
1320
1587
|
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
@@ -1322,13 +1589,13 @@ function P(skipConn = false) {
|
|
|
1322
1589
|
await option.conn.execute(SyncMode.Async, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1323
1590
|
await option.conn.execute(SyncMode.Async, `
|
|
1324
1591
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1325
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1326
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.
|
|
1592
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1593
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1327
1594
|
);
|
|
1328
1595
|
`);
|
|
1329
1596
|
if (this[_index] && this[_index].length) {
|
|
1330
1597
|
for (const index of this[_index]) {
|
|
1331
|
-
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${index}");`);
|
|
1598
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
1332
1599
|
}
|
|
1333
1600
|
}
|
|
1334
1601
|
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
@@ -1345,13 +1612,13 @@ function P(skipConn = false) {
|
|
|
1345
1612
|
// 创建表
|
|
1346
1613
|
await option.conn.execute(SyncMode.Async, `
|
|
1347
1614
|
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1348
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]).join(',')}
|
|
1349
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.
|
|
1615
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1616
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1350
1617
|
);
|
|
1351
1618
|
`);
|
|
1352
1619
|
if (this[_index] && this[_index].length) {
|
|
1353
1620
|
for (const index of this[_index]) {
|
|
1354
|
-
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${Sqlstring.escapeId(option.tableName)} ("${index}");`);
|
|
1621
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${Sqlstring.escapeId(option.tableName)} ("${this[_fields][index]?.C2()}");`);
|
|
1355
1622
|
}
|
|
1356
1623
|
}
|
|
1357
1624
|
await option.conn.execute(SyncMode.Async, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
@@ -1409,28 +1676,44 @@ function P(skipConn = false) {
|
|
|
1409
1676
|
};
|
|
1410
1677
|
};
|
|
1411
1678
|
}
|
|
1412
|
-
const FieldFilter = (K, V, def, option) => {
|
|
1679
|
+
const FieldFilter = (K, V, def, uuidColumn, option) => {
|
|
1413
1680
|
let ret = 0;
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1681
|
+
// 如果是插入操作且字段是UUID,则不进行空值检查
|
|
1682
|
+
// 只有在非插入或者非UUID时,进行空置检查
|
|
1683
|
+
if (option?.insert === true && uuidColumn === true) {
|
|
1684
|
+
ret = 1;
|
|
1685
|
+
if (V === undefined) {
|
|
1686
|
+
V = null;
|
|
1418
1687
|
}
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
if (option?.skipUndefined !== true) {
|
|
1422
|
-
ret = 1;
|
|
1423
|
-
V = option?.def === true && def && def.hasOwnProperty(K) ? def[K] : null;
|
|
1688
|
+
else if (emptyString(`${V ?? ''}`)) {
|
|
1689
|
+
V = '';
|
|
1424
1690
|
}
|
|
1425
|
-
|
|
1426
|
-
else if (emptyString(`${V ?? ''}`)) {
|
|
1427
|
-
if (option?.skipEmptyString !== true) {
|
|
1691
|
+
else {
|
|
1428
1692
|
ret = 1;
|
|
1429
|
-
V = option?.def === true && def && def.hasOwnProperty(K) ? def[K] : '';
|
|
1430
1693
|
}
|
|
1431
1694
|
}
|
|
1432
1695
|
else {
|
|
1433
|
-
|
|
1696
|
+
if (V === null) {
|
|
1697
|
+
if (option?.skipNull !== true) {
|
|
1698
|
+
ret = 1;
|
|
1699
|
+
V = option?.insert === true && def && def.hasOwnProperty(K) ? def[K] : null;
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
else if (V === undefined) {
|
|
1703
|
+
if (option?.skipUndefined !== true) {
|
|
1704
|
+
ret = 1;
|
|
1705
|
+
V = option?.insert === true && def && def.hasOwnProperty(K) ? def[K] : null;
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
else if (emptyString(`${V ?? ''}`)) {
|
|
1709
|
+
if (option?.skipEmptyString !== true) {
|
|
1710
|
+
ret = 1;
|
|
1711
|
+
V = option?.insert === true && def && def.hasOwnProperty(K) ? def[K] : '';
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
else {
|
|
1715
|
+
ret = 1;
|
|
1716
|
+
}
|
|
1434
1717
|
}
|
|
1435
1718
|
if (ret === 1) {
|
|
1436
1719
|
option?.finalColumns?.add(K);
|
|
@@ -1443,193 +1726,195 @@ export const Field = (config) => {
|
|
|
1443
1726
|
config.type ?? (config.type = SqlType.varchar);
|
|
1444
1727
|
return (object, propertyName) => {
|
|
1445
1728
|
const field = config;
|
|
1446
|
-
field.
|
|
1447
|
-
field.
|
|
1729
|
+
field.P = propertyName;
|
|
1730
|
+
field.C = () => P2C(propertyName, globalThis[_Hump]);
|
|
1731
|
+
field.C2 = () => Sqlstring.escapeId(P2C(propertyName, globalThis[_Hump]));
|
|
1732
|
+
field.C3 = () => `${Sqlstring.escapeId(P2C(propertyName, globalThis[_Hump]))} ${propertyName}`;
|
|
1448
1733
|
const hasDef = field.hasOwnProperty('def') === true;
|
|
1449
1734
|
switch (field.type) {
|
|
1450
1735
|
case SqlType.tinyint: {
|
|
1451
|
-
field[DBType.Mysql] = `${field.
|
|
1452
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1736
|
+
field[DBType.Mysql] = () => `${field.C2()} tinyint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1737
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1453
1738
|
break;
|
|
1454
1739
|
}
|
|
1455
1740
|
case SqlType.smallint: {
|
|
1456
|
-
field[DBType.Mysql] = `${field.
|
|
1457
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1741
|
+
field[DBType.Mysql] = () => `${field.C2()} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1742
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1458
1743
|
break;
|
|
1459
1744
|
}
|
|
1460
1745
|
case SqlType.mediumint: {
|
|
1461
|
-
field[DBType.Mysql] = `${field.
|
|
1462
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1746
|
+
field[DBType.Mysql] = () => `${field.C2()} smallint ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1747
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1463
1748
|
break;
|
|
1464
1749
|
}
|
|
1465
1750
|
case SqlType.int: {
|
|
1466
|
-
field[DBType.Mysql] = `${field.
|
|
1467
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1751
|
+
field[DBType.Mysql] = () => `${field.C2()} int ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1752
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1468
1753
|
break;
|
|
1469
1754
|
}
|
|
1470
1755
|
case SqlType.bigint: {
|
|
1471
|
-
field[DBType.Mysql] = `${field.
|
|
1472
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1756
|
+
field[DBType.Mysql] = () => `${field.C2()} bigint ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1757
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1473
1758
|
break;
|
|
1474
1759
|
}
|
|
1475
1760
|
case SqlType.float: {
|
|
1476
|
-
field[DBType.Mysql] = `${field.
|
|
1477
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1761
|
+
field[DBType.Mysql] = () => `${field.C2()} float(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''} `;
|
|
1762
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} real`;
|
|
1478
1763
|
break;
|
|
1479
1764
|
}
|
|
1480
1765
|
case SqlType.double: {
|
|
1481
|
-
field[DBType.Mysql] = `${field.
|
|
1482
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1766
|
+
field[DBType.Mysql] = () => `${field.C2()} double(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''} `;
|
|
1767
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} real`;
|
|
1483
1768
|
break;
|
|
1484
1769
|
}
|
|
1485
1770
|
case SqlType.decimal: {
|
|
1486
|
-
field[DBType.Mysql] = `${field.
|
|
1487
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1771
|
+
field[DBType.Mysql] = () => `${field.C2()} decimal(${config.length ?? 1}, ${config.scale ?? 2}) ${config.notNull === true ? 'NOT NULL' : ''} ${hasDef ? `DEFAULT '${field.def}'` : ''} `;
|
|
1772
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} real`;
|
|
1488
1773
|
break;
|
|
1489
1774
|
}
|
|
1490
1775
|
case SqlType.longtext: {
|
|
1491
|
-
field[DBType.Mysql] = `${field.
|
|
1492
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1776
|
+
field[DBType.Mysql] = () => `${field.C2()} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1777
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1493
1778
|
break;
|
|
1494
1779
|
}
|
|
1495
1780
|
case SqlType.mediumtext: {
|
|
1496
|
-
field[DBType.Mysql] = `${field.
|
|
1497
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1781
|
+
field[DBType.Mysql] = () => `${field.C2()} mediumtext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1782
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1498
1783
|
break;
|
|
1499
1784
|
}
|
|
1500
1785
|
case SqlType.text: {
|
|
1501
|
-
field[DBType.Mysql] = `${field.
|
|
1502
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1786
|
+
field[DBType.Mysql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1787
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1503
1788
|
break;
|
|
1504
1789
|
}
|
|
1505
1790
|
case SqlType.date: {
|
|
1506
|
-
field[DBType.Mysql] = `${field.
|
|
1507
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1791
|
+
field[DBType.Mysql] = () => `${field.C2()} date ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1792
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1508
1793
|
break;
|
|
1509
1794
|
}
|
|
1510
1795
|
case SqlType.time: {
|
|
1511
|
-
field[DBType.Mysql] = `${field.
|
|
1512
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1796
|
+
field[DBType.Mysql] = () => `${field.C2()} time ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1797
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1513
1798
|
break;
|
|
1514
1799
|
}
|
|
1515
1800
|
case SqlType.year: {
|
|
1516
|
-
field[DBType.Mysql] = `${field.
|
|
1517
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1801
|
+
field[DBType.Mysql] = () => `${field.C2()} year ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1802
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1518
1803
|
break;
|
|
1519
1804
|
}
|
|
1520
1805
|
case SqlType.datetime: {
|
|
1521
|
-
field[DBType.Mysql] = `${field.
|
|
1522
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1806
|
+
field[DBType.Mysql] = () => `${field.C2()} datetime ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1807
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1523
1808
|
break;
|
|
1524
1809
|
}
|
|
1525
1810
|
case SqlType.timestamp: {
|
|
1526
|
-
field[DBType.Mysql] = `${field.
|
|
1527
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1811
|
+
field[DBType.Mysql] = () => `${field.C2()} timestamp ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1812
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1528
1813
|
break;
|
|
1529
1814
|
}
|
|
1530
1815
|
case SqlType.char: {
|
|
1531
|
-
field[DBType.Mysql] = `${field.
|
|
1532
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1816
|
+
field[DBType.Mysql] = () => `${field.C2()} char(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1817
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1533
1818
|
break;
|
|
1534
1819
|
}
|
|
1535
1820
|
case SqlType.varchar: {
|
|
1536
|
-
field[DBType.Mysql] = `${field.
|
|
1537
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1821
|
+
field[DBType.Mysql] = () => `${field.C2()} varchar(${config.length ?? 1}) ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1822
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1538
1823
|
break;
|
|
1539
1824
|
}
|
|
1540
1825
|
case SqlType.tinyblob: {
|
|
1541
|
-
field[DBType.Mysql] = `${field.
|
|
1542
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1826
|
+
field[DBType.Mysql] = () => `${field.C2()} tinyblob ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1827
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1543
1828
|
break;
|
|
1544
1829
|
}
|
|
1545
1830
|
case SqlType.tinytext: {
|
|
1546
|
-
field[DBType.Mysql] = `${field.
|
|
1547
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1831
|
+
field[DBType.Mysql] = () => `${field.C2()} tinytext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1832
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1548
1833
|
break;
|
|
1549
1834
|
}
|
|
1550
1835
|
case SqlType.blob: {
|
|
1551
|
-
field[DBType.Mysql] = `${field.
|
|
1552
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1836
|
+
field[DBType.Mysql] = () => `${field.C2()} binary ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1837
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1553
1838
|
break;
|
|
1554
1839
|
}
|
|
1555
1840
|
case SqlType.text: {
|
|
1556
|
-
field[DBType.Mysql] = `${field.
|
|
1557
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1841
|
+
field[DBType.Mysql] = () => `${field.C2()} text ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1842
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1558
1843
|
break;
|
|
1559
1844
|
}
|
|
1560
1845
|
case SqlType.mediumblob: {
|
|
1561
|
-
field[DBType.Mysql] = `${field.
|
|
1562
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1846
|
+
field[DBType.Mysql] = () => `${field.C2()} mediumblob ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1847
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1563
1848
|
break;
|
|
1564
1849
|
}
|
|
1565
1850
|
case SqlType.mediumtext: {
|
|
1566
|
-
field[DBType.Mysql] = `${field.
|
|
1567
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1851
|
+
field[DBType.Mysql] = () => `${field.C2()} mediumtext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1852
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1568
1853
|
break;
|
|
1569
1854
|
}
|
|
1570
1855
|
case SqlType.longblob: {
|
|
1571
|
-
field[DBType.Mysql] = `${field.
|
|
1572
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1856
|
+
field[DBType.Mysql] = () => `${field.C2()} longblob ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1857
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1573
1858
|
break;
|
|
1574
1859
|
}
|
|
1575
1860
|
case SqlType.longtext: {
|
|
1576
|
-
field[DBType.Mysql] = `${field.
|
|
1577
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1861
|
+
field[DBType.Mysql] = () => `${field.C2()} longtext ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1862
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1578
1863
|
break;
|
|
1579
1864
|
}
|
|
1580
1865
|
case SqlType.set: {
|
|
1581
|
-
field[DBType.Mysql] = `${field.
|
|
1582
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1866
|
+
field[DBType.Mysql] = () => `${field.C2()} set ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1867
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1583
1868
|
break;
|
|
1584
1869
|
}
|
|
1585
1870
|
case SqlType.enum: {
|
|
1586
|
-
field[DBType.Mysql] = `${field.
|
|
1587
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1871
|
+
field[DBType.Mysql] = () => `${field.C2()} enum ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1872
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1588
1873
|
break;
|
|
1589
1874
|
}
|
|
1590
1875
|
case SqlType.json: {
|
|
1591
|
-
field[DBType.Mysql] = `${field.
|
|
1592
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1876
|
+
field[DBType.Mysql] = () => `${field.C2()} json ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1877
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1593
1878
|
break;
|
|
1594
1879
|
}
|
|
1595
1880
|
case SqlType.geometry: {
|
|
1596
|
-
field[DBType.Mysql] = `${field.
|
|
1597
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1881
|
+
field[DBType.Mysql] = () => `${field.C2()} geometry ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1882
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1598
1883
|
break;
|
|
1599
1884
|
}
|
|
1600
1885
|
case SqlType.point: {
|
|
1601
|
-
field[DBType.Mysql] = `${field.
|
|
1602
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1886
|
+
field[DBType.Mysql] = () => `${field.C2()} point ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1887
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1603
1888
|
break;
|
|
1604
1889
|
}
|
|
1605
1890
|
case SqlType.linestring: {
|
|
1606
|
-
field[DBType.Mysql] = `${field.
|
|
1607
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1891
|
+
field[DBType.Mysql] = () => `${field.C2()} linestring ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1892
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1608
1893
|
break;
|
|
1609
1894
|
}
|
|
1610
1895
|
case SqlType.polygon: {
|
|
1611
|
-
field[DBType.Mysql] = `${field.
|
|
1612
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1896
|
+
field[DBType.Mysql] = () => `${field.C2()} polygon ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1897
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1613
1898
|
break;
|
|
1614
1899
|
}
|
|
1615
1900
|
case SqlType.multipoint: {
|
|
1616
|
-
field[DBType.Mysql] = `${field.
|
|
1617
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1901
|
+
field[DBType.Mysql] = () => `${field.C2()} multipoint ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1902
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1618
1903
|
break;
|
|
1619
1904
|
}
|
|
1620
1905
|
case SqlType.multilinestring: {
|
|
1621
|
-
field[DBType.Mysql] = `${field.
|
|
1622
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1906
|
+
field[DBType.Mysql] = () => `${field.C2()} multilinestring ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1907
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1623
1908
|
break;
|
|
1624
1909
|
}
|
|
1625
1910
|
case SqlType.multipolygon: {
|
|
1626
|
-
field[DBType.Mysql] = `${field.
|
|
1627
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1911
|
+
field[DBType.Mysql] = () => `${field.C2()} multipolygon ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1912
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1628
1913
|
break;
|
|
1629
1914
|
}
|
|
1630
1915
|
case SqlType.geometrycollection: {
|
|
1631
|
-
field[DBType.Mysql] = `${field.
|
|
1632
|
-
field[DBType.SqliteRemote] = field[DBType.Sqlite] = `${field.
|
|
1916
|
+
field[DBType.Mysql] = () => `${field.C2()} geometrycollection ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1917
|
+
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1633
1918
|
break;
|
|
1634
1919
|
}
|
|
1635
1920
|
}
|
|
@@ -1676,7 +1961,7 @@ export const Field = (config) => {
|
|
|
1676
1961
|
};
|
|
1677
1962
|
export const DB = (config) => {
|
|
1678
1963
|
return function (constructor) {
|
|
1679
|
-
var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
|
|
1964
|
+
var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
1680
1965
|
const __ids = Reflect.getMetadata(_ids, config.clz.prototype) || new Array;
|
|
1681
1966
|
const __fields = Reflect.getMetadata(_fields, config.clz.prototype);
|
|
1682
1967
|
const __columns = Reflect.getMetadata(_columns, config.clz.prototype);
|
|
@@ -1685,25 +1970,31 @@ export const DB = (config) => {
|
|
|
1685
1970
|
const __deleteState = Reflect.getMetadata(_deleteState, config.clz.prototype);
|
|
1686
1971
|
const __index = Reflect.getMetadata(_index, config.clz.prototype);
|
|
1687
1972
|
const __def = Reflect.getMetadata(_def, config.clz.prototype);
|
|
1688
|
-
|
|
1973
|
+
const className = config.tableName?.replace(/_(\w)/g, (a, b) => b.toUpperCase());
|
|
1974
|
+
const ClassName = className?.replace(/\w/, (v) => v.toUpperCase());
|
|
1975
|
+
const vueName = config.tableName?.replace(/_/g, '-');
|
|
1976
|
+
return _w = class extends constructor {
|
|
1689
1977
|
constructor() {
|
|
1690
1978
|
super(...arguments);
|
|
1691
1979
|
this[_d] = config.tableName;
|
|
1692
|
-
this[_e] =
|
|
1693
|
-
this[_f] =
|
|
1694
|
-
this[_g] =
|
|
1695
|
-
this[_h] =
|
|
1696
|
-
this[_j] =
|
|
1697
|
-
this[_k] =
|
|
1698
|
-
this[_l] =
|
|
1699
|
-
this[_m] =
|
|
1700
|
-
this[_o] =
|
|
1701
|
-
this[_p] =
|
|
1702
|
-
this[_q] =
|
|
1703
|
-
this[_r] =
|
|
1704
|
-
this[_s] =
|
|
1980
|
+
this[_e] = className;
|
|
1981
|
+
this[_f] = ClassName;
|
|
1982
|
+
this[_g] = vueName;
|
|
1983
|
+
this[_h] = config.dbName;
|
|
1984
|
+
this[_j] = config.dbType ?? DBType.Mysql;
|
|
1985
|
+
this[_k] = config.sqliteVersion;
|
|
1986
|
+
this[_l] = Object.assign({}, _defOption, config);
|
|
1987
|
+
this[_m] = __ids;
|
|
1988
|
+
this[_o] = __fields;
|
|
1989
|
+
this[_p] = __columns;
|
|
1990
|
+
this[_q] = __columnsNoId;
|
|
1991
|
+
this[_r] = __index;
|
|
1992
|
+
this[_s] = __def;
|
|
1993
|
+
this[_t] = __stateFileName;
|
|
1994
|
+
this[_u] = __deleteState;
|
|
1995
|
+
this[_v] = (data, option) => {
|
|
1705
1996
|
return Object.fromEntries(iterare(option?.skipId === true ? __columnsNoId : __columns)
|
|
1706
|
-
.map(K => [K, FieldFilter(K, data[K], __def, option)])
|
|
1997
|
+
.map(K => [K, FieldFilter(K, data[K], __def, __fields[K].uuid === true || __fields[K].uuidShort === true, option)])
|
|
1707
1998
|
.filter(data => {
|
|
1708
1999
|
if (data[1][0] === 1) {
|
|
1709
2000
|
if (option?.onFieldExists) {
|
|
@@ -1721,20 +2012,23 @@ export const DB = (config) => {
|
|
|
1721
2012
|
}
|
|
1722
2013
|
},
|
|
1723
2014
|
_d = _tableName,
|
|
1724
|
-
_e =
|
|
1725
|
-
_f =
|
|
1726
|
-
_g =
|
|
1727
|
-
_h =
|
|
1728
|
-
_j =
|
|
1729
|
-
_k =
|
|
1730
|
-
_l =
|
|
1731
|
-
_m =
|
|
1732
|
-
_o =
|
|
1733
|
-
_p =
|
|
1734
|
-
_q =
|
|
1735
|
-
_r =
|
|
1736
|
-
_s =
|
|
1737
|
-
_t
|
|
2015
|
+
_e = _className,
|
|
2016
|
+
_f = _ClassName,
|
|
2017
|
+
_g = _vueName,
|
|
2018
|
+
_h = _daoDBName,
|
|
2019
|
+
_j = _dbType,
|
|
2020
|
+
_k = _sqlite_version,
|
|
2021
|
+
_l = _SqlOption,
|
|
2022
|
+
_m = _ids,
|
|
2023
|
+
_o = _fields,
|
|
2024
|
+
_p = _columns,
|
|
2025
|
+
_q = _columnsNoId,
|
|
2026
|
+
_r = _index,
|
|
2027
|
+
_s = _def,
|
|
2028
|
+
_t = _stateFileName,
|
|
2029
|
+
_u = _deleteState,
|
|
2030
|
+
_v = _transformer,
|
|
2031
|
+
_w;
|
|
1738
2032
|
};
|
|
1739
2033
|
};
|
|
1740
2034
|
/**
|
|
@@ -1751,7 +2045,7 @@ export const DB = (config) => {
|
|
|
1751
2045
|
*/
|
|
1752
2046
|
export function DeclareClass(clz, FieldOptions) {
|
|
1753
2047
|
for (const item of FieldOptions) {
|
|
1754
|
-
tslib.__decorate([Field(item)], clz.prototype, item.
|
|
2048
|
+
tslib.__decorate([Field(item)], clz.prototype, item.P, void 0);
|
|
1755
2049
|
}
|
|
1756
2050
|
}
|
|
1757
2051
|
/**
|
|
@@ -1788,135 +2082,194 @@ export class SqlService {
|
|
|
1788
2082
|
const conditions = option.existConditionOtherThanIds || this[_ids];
|
|
1789
2083
|
Throw.if(!conditions, 'not found where condition for insertIfNotExists!');
|
|
1790
2084
|
Throw.if(conditions.length === 0, 'insertIfNotExists must have not null where!');
|
|
1791
|
-
const where = iterare(conditions).map(c => `${this[_fields][c]?.
|
|
2085
|
+
const where = iterare(conditions).map(c => `${this[_fields][c]?.C2()} = ?`).join(' AND ');
|
|
1792
2086
|
const finalColumns = new Set();
|
|
1793
2087
|
const whereColumns = conditions;
|
|
1794
|
-
const params =
|
|
1795
|
-
|
|
1796
|
-
.
|
|
1797
|
-
|
|
2088
|
+
const params = [];
|
|
2089
|
+
const questMarks = datas
|
|
2090
|
+
.map(data => this[_transformer](data, { ...option, finalColumns, insert: true }))
|
|
2091
|
+
.map(data => {
|
|
1798
2092
|
const questMark = new Array();
|
|
1799
2093
|
for (const column of finalColumns) {
|
|
1800
|
-
|
|
1801
|
-
result.push(data.hasOwnProperty(column)
|
|
2094
|
+
const V = data.hasOwnProperty(column)
|
|
1802
2095
|
? data[column]
|
|
1803
2096
|
: this[_def] && this[_def].hasOwnProperty(column)
|
|
1804
2097
|
? this[_def][column]
|
|
1805
|
-
: null
|
|
2098
|
+
: null;
|
|
2099
|
+
if (V === null) {
|
|
2100
|
+
const field = this[_fields][column];
|
|
2101
|
+
if (field?.uuid) {
|
|
2102
|
+
questMark.push('UUID()');
|
|
2103
|
+
}
|
|
2104
|
+
else if (field?.uuidShort && this[_dbType] === DBType.Mysql) {
|
|
2105
|
+
questMark.push('UUID_SHORT()');
|
|
2106
|
+
}
|
|
2107
|
+
else {
|
|
2108
|
+
questMark.push('?');
|
|
2109
|
+
params.push(V);
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
else {
|
|
2113
|
+
questMark.push('?');
|
|
2114
|
+
params.push(V);
|
|
2115
|
+
}
|
|
1806
2116
|
}
|
|
1807
2117
|
for (const column of whereColumns) {
|
|
1808
|
-
|
|
1809
|
-
result.push(data.hasOwnProperty(column)
|
|
2118
|
+
params.push(data.hasOwnProperty(column)
|
|
1810
2119
|
? data[column]
|
|
1811
2120
|
: this[_def] && this[_def].hasOwnProperty(column)
|
|
1812
2121
|
? this[_def][column]
|
|
1813
2122
|
: null);
|
|
1814
2123
|
}
|
|
1815
|
-
return
|
|
2124
|
+
return `SELECT ${questMark.join(',')} FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM ${tableName} WHERE ${where})`;
|
|
1816
2125
|
});
|
|
1817
|
-
const
|
|
1818
|
-
const
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
(${columnNames})
|
|
1823
|
-
${selects};`;
|
|
2126
|
+
const columnNames = iterare(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2127
|
+
const sql = format(`INSERT INTO
|
|
2128
|
+
${tableName}
|
|
2129
|
+
(${columnNames})
|
|
2130
|
+
${questMarks.join(' UNION ALL ')};`);
|
|
1824
2131
|
sqls.push({ sql, params });
|
|
2132
|
+
break;
|
|
1825
2133
|
}
|
|
1826
2134
|
case InsertMode.Replace: {
|
|
1827
2135
|
const finalColumns = new Set();
|
|
1828
|
-
const params =
|
|
1829
|
-
|
|
1830
|
-
.
|
|
1831
|
-
|
|
2136
|
+
const params = [];
|
|
2137
|
+
const questMarks = datas
|
|
2138
|
+
.map(data => this[_transformer](data, { ...option, finalColumns, insert: true }))
|
|
2139
|
+
.map(data => {
|
|
1832
2140
|
const questMark = new Array();
|
|
1833
2141
|
for (const column of finalColumns) {
|
|
1834
|
-
|
|
1835
|
-
result.push(data.hasOwnProperty(column)
|
|
2142
|
+
const V = data.hasOwnProperty(column)
|
|
1836
2143
|
? data[column]
|
|
1837
2144
|
: this[_def] && this[_def].hasOwnProperty(column)
|
|
1838
2145
|
? this[_def][column]
|
|
1839
|
-
: null
|
|
2146
|
+
: null;
|
|
2147
|
+
if (V === null) {
|
|
2148
|
+
const field = this[_fields][column];
|
|
2149
|
+
if (field?.uuid) {
|
|
2150
|
+
questMark.push('UUID()');
|
|
2151
|
+
}
|
|
2152
|
+
else if (field?.uuidShort && this[_dbType] === DBType.Mysql) {
|
|
2153
|
+
questMark.push('UUID_SHORT()');
|
|
2154
|
+
}
|
|
2155
|
+
else {
|
|
2156
|
+
questMark.push('?');
|
|
2157
|
+
params.push(V);
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
else {
|
|
2161
|
+
questMark.push('?');
|
|
2162
|
+
params.push(V);
|
|
2163
|
+
}
|
|
1840
2164
|
}
|
|
1841
|
-
return
|
|
2165
|
+
return `(${questMark.join(',')})`;
|
|
1842
2166
|
});
|
|
1843
|
-
const
|
|
1844
|
-
const
|
|
1845
|
-
const questMarks = iterare(new Array(datas.length)).map(() => `(${quests})`).join(',');
|
|
1846
|
-
const sql = `
|
|
2167
|
+
const columnNames = iterare(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2168
|
+
const sql = format(`
|
|
1847
2169
|
${this[_dbType] === DBType.Mysql ? '' : 'INSERT OR'} REPLACE INTO
|
|
1848
2170
|
${tableName}
|
|
1849
2171
|
(${columnNames})
|
|
1850
2172
|
VALUES ${questMarks};
|
|
1851
|
-
|
|
2173
|
+
`);
|
|
1852
2174
|
sqls.push({ sql, params });
|
|
2175
|
+
break;
|
|
1853
2176
|
}
|
|
1854
2177
|
case InsertMode.Insert: {
|
|
1855
2178
|
const finalColumns = new Set();
|
|
1856
|
-
const params =
|
|
1857
|
-
|
|
1858
|
-
.
|
|
1859
|
-
|
|
2179
|
+
const params = [];
|
|
2180
|
+
const questMarks = datas
|
|
2181
|
+
.map(data => this[_transformer](data, { ...option, finalColumns, insert: true }))
|
|
2182
|
+
.map(data => {
|
|
1860
2183
|
const questMark = new Array();
|
|
1861
2184
|
for (const column of finalColumns) {
|
|
1862
|
-
|
|
1863
|
-
result.push(data.hasOwnProperty(column)
|
|
2185
|
+
const V = data.hasOwnProperty(column)
|
|
1864
2186
|
? data[column]
|
|
1865
2187
|
: this[_def] && this[_def].hasOwnProperty(column)
|
|
1866
2188
|
? this[_def][column]
|
|
1867
|
-
: null
|
|
2189
|
+
: null;
|
|
2190
|
+
if (V === null) {
|
|
2191
|
+
const field = this[_fields][column];
|
|
2192
|
+
if (field?.uuid) {
|
|
2193
|
+
questMark.push('UUID()');
|
|
2194
|
+
}
|
|
2195
|
+
else if (field?.uuidShort && this[_dbType] === DBType.Mysql) {
|
|
2196
|
+
questMark.push('UUID_SHORT()');
|
|
2197
|
+
}
|
|
2198
|
+
else {
|
|
2199
|
+
questMark.push('?');
|
|
2200
|
+
params.push(V);
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
else {
|
|
2204
|
+
questMark.push('?');
|
|
2205
|
+
params.push(V);
|
|
2206
|
+
}
|
|
1868
2207
|
}
|
|
1869
|
-
return
|
|
2208
|
+
return `(${questMark.join(',')})`;
|
|
1870
2209
|
});
|
|
1871
|
-
const
|
|
1872
|
-
const
|
|
1873
|
-
const questMarks = iterare(new Array(datas.length)).map(() => `(${quests})`).join(',');
|
|
1874
|
-
const sql = `
|
|
2210
|
+
const columnNames = iterare(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2211
|
+
const sql = format(`
|
|
1875
2212
|
INSERT INTO
|
|
1876
2213
|
${tableName}
|
|
1877
2214
|
(${columnNames})
|
|
1878
2215
|
VALUES ${questMarks};
|
|
1879
|
-
|
|
2216
|
+
`);
|
|
1880
2217
|
sqls.push({ sql, params });
|
|
2218
|
+
break;
|
|
1881
2219
|
}
|
|
1882
2220
|
case InsertMode.InsertWithTempTable: {
|
|
1883
2221
|
const tableTemp = `${option?.tableName}_${Math.random()}`.replace(/\./, '');
|
|
1884
2222
|
const tableTempESC = Sqlstring.escapeId(tableTemp);
|
|
1885
2223
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
1886
2224
|
const finalColumns = new Set();
|
|
1887
|
-
const params =
|
|
1888
|
-
|
|
1889
|
-
.
|
|
1890
|
-
|
|
2225
|
+
const params = [];
|
|
2226
|
+
const questMarks = datas
|
|
2227
|
+
.map(data => this[_transformer](data, { ...option, finalColumns, insert: true }))
|
|
2228
|
+
.map(data => {
|
|
1891
2229
|
const questMark = new Array();
|
|
1892
2230
|
for (const column of finalColumns) {
|
|
1893
|
-
|
|
1894
|
-
result.push(data.hasOwnProperty(column)
|
|
2231
|
+
const V = data.hasOwnProperty(column)
|
|
1895
2232
|
? data[column]
|
|
1896
2233
|
: this[_def] && this[_def].hasOwnProperty(column)
|
|
1897
2234
|
? this[_def][column]
|
|
1898
|
-
: null
|
|
2235
|
+
: null;
|
|
2236
|
+
if (V === null) {
|
|
2237
|
+
const field = this[_fields][column];
|
|
2238
|
+
if (field?.uuid) {
|
|
2239
|
+
questMark.push('UUID()');
|
|
2240
|
+
}
|
|
2241
|
+
else if (field?.uuidShort && this[_dbType] === DBType.Mysql) {
|
|
2242
|
+
questMark.push('UUID_SHORT()');
|
|
2243
|
+
}
|
|
2244
|
+
else {
|
|
2245
|
+
questMark.push('?');
|
|
2246
|
+
params.push(V);
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
else {
|
|
2250
|
+
questMark.push('?');
|
|
2251
|
+
params.push(V);
|
|
2252
|
+
}
|
|
1899
2253
|
}
|
|
1900
|
-
return
|
|
2254
|
+
return `(${questMark.join(',')})`;
|
|
1901
2255
|
});
|
|
1902
2256
|
const _sqls = this._createTable({ tableName: tableTemp, temp: true, columns: Array.from(finalColumns) });
|
|
1903
2257
|
sqls.push(..._sqls);
|
|
1904
|
-
const
|
|
1905
|
-
const columnNames = iterare(finalColumns).map(i => this[_fields][i]?.esName).join(',');
|
|
1906
|
-
const questMarks = iterare(new Array(datas.length)).map(() => `(${quests})`).join(',');
|
|
2258
|
+
const columnNames = iterare(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
1907
2259
|
sqls.push({
|
|
1908
|
-
sql: `
|
|
2260
|
+
sql: format(`
|
|
1909
2261
|
INSERT INTO
|
|
1910
2262
|
${tableTemp}
|
|
1911
2263
|
(${columnNames})
|
|
1912
2264
|
VALUES ${questMarks};
|
|
1913
|
-
|
|
2265
|
+
`), params
|
|
1914
2266
|
});
|
|
1915
2267
|
sqls.push({
|
|
1916
|
-
sql: `INSERT INTO ${Sqlstring.escapeId(option.tableName)} (${columnNames})
|
|
1917
|
-
SELECT ${columnNames} FROM ${tableTemp};`
|
|
2268
|
+
sql: format(`INSERT INTO ${Sqlstring.escapeId(option.tableName)} (${columnNames})
|
|
2269
|
+
SELECT ${columnNames} FROM ${tableTemp};`)
|
|
1918
2270
|
});
|
|
1919
2271
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
2272
|
+
break;
|
|
1920
2273
|
}
|
|
1921
2274
|
}
|
|
1922
2275
|
return sqls;
|
|
@@ -1943,7 +2296,7 @@ export class SqlService {
|
|
|
1943
2296
|
else
|
|
1944
2297
|
return result[0];
|
|
1945
2298
|
};
|
|
1946
|
-
if (option?.conn?.[_inTransaction] === true) {
|
|
2299
|
+
if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
1947
2300
|
return fn();
|
|
1948
2301
|
}
|
|
1949
2302
|
else {
|
|
@@ -1967,12 +2320,17 @@ export class SqlService {
|
|
|
1967
2320
|
return result;
|
|
1968
2321
|
}, option?.conn);
|
|
1969
2322
|
};
|
|
1970
|
-
return new Promise(async (resolve) => {
|
|
1971
|
-
|
|
1972
|
-
|
|
2323
|
+
return new Promise(async (resolve, reject) => {
|
|
2324
|
+
try {
|
|
2325
|
+
if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2326
|
+
resolve((await fn()));
|
|
2327
|
+
}
|
|
2328
|
+
else {
|
|
2329
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
2330
|
+
}
|
|
1973
2331
|
}
|
|
1974
|
-
|
|
1975
|
-
|
|
2332
|
+
catch (error) {
|
|
2333
|
+
reject(error);
|
|
1976
2334
|
}
|
|
1977
2335
|
});
|
|
1978
2336
|
}
|
|
@@ -1991,12 +2349,17 @@ export class SqlService {
|
|
|
1991
2349
|
}, { everyLength: 1 });
|
|
1992
2350
|
return result[0];
|
|
1993
2351
|
};
|
|
1994
|
-
return new Promise(async (resolve) => {
|
|
1995
|
-
|
|
1996
|
-
|
|
2352
|
+
return new Promise(async (resolve, reject) => {
|
|
2353
|
+
try {
|
|
2354
|
+
if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2355
|
+
resolve((await fn()));
|
|
2356
|
+
}
|
|
2357
|
+
else {
|
|
2358
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
2359
|
+
}
|
|
1997
2360
|
}
|
|
1998
|
-
|
|
1999
|
-
|
|
2361
|
+
catch (error) {
|
|
2362
|
+
reject(error);
|
|
2000
2363
|
}
|
|
2001
2364
|
});
|
|
2002
2365
|
}
|
|
@@ -2004,7 +2367,7 @@ export class SqlService {
|
|
|
2004
2367
|
_update(datas, option) {
|
|
2005
2368
|
const sqls = [];
|
|
2006
2369
|
const tableName = Sqlstring.escapeId(option?.tableName);
|
|
2007
|
-
const where = `WHEN ${iterare(this[_ids]).map(c => `${this[_fields][c]?.
|
|
2370
|
+
const where = `WHEN ${iterare(this[_ids]).map(c => `${this[_fields][c]?.C2()} = ?`).join(' AND ')} THEN ?`;
|
|
2008
2371
|
const columnMaps = Object.fromEntries(this[_columnsNoId].map(c => [c, {
|
|
2009
2372
|
where: new Array(),
|
|
2010
2373
|
params: []
|
|
@@ -2024,13 +2387,13 @@ export class SqlService {
|
|
|
2024
2387
|
}
|
|
2025
2388
|
});
|
|
2026
2389
|
}
|
|
2027
|
-
const sql = `UPDATE ${tableName} SET ${iterare(this[_columnsNoId])
|
|
2390
|
+
const sql = format(`UPDATE ${tableName} SET ${iterare(this[_columnsNoId])
|
|
2028
2391
|
.filter(K => columnMaps[K].where.length > 0)
|
|
2029
2392
|
.map(K => {
|
|
2030
2393
|
params.push(...columnMaps[K].params);
|
|
2031
|
-
return `${this[_fields][K]?.
|
|
2394
|
+
return `${this[_fields][K]?.C2()} = CASE ${columnMaps[K].where.join(' ')} ELSE ${this[_fields][K]?.C2()} END`;
|
|
2032
2395
|
})
|
|
2033
|
-
.join(',')}
|
|
2396
|
+
.join(',')};`);
|
|
2034
2397
|
sqls.push({ sql, params });
|
|
2035
2398
|
return sqls;
|
|
2036
2399
|
}
|
|
@@ -2052,7 +2415,7 @@ export class SqlService {
|
|
|
2052
2415
|
}, { everyLength: option?.maxDeal });
|
|
2053
2416
|
return result.reduce((a, b) => a + b);
|
|
2054
2417
|
};
|
|
2055
|
-
if (option?.conn?.[_inTransaction] === true) {
|
|
2418
|
+
if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2056
2419
|
return fn();
|
|
2057
2420
|
}
|
|
2058
2421
|
else {
|
|
@@ -2074,12 +2437,17 @@ export class SqlService {
|
|
|
2074
2437
|
}, { everyLength: option?.maxDeal });
|
|
2075
2438
|
return result.reduce((a, b) => a + b);
|
|
2076
2439
|
};
|
|
2077
|
-
return new Promise(async (resolve) => {
|
|
2078
|
-
|
|
2079
|
-
|
|
2440
|
+
return new Promise(async (resolve, reject) => {
|
|
2441
|
+
try {
|
|
2442
|
+
if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2443
|
+
resolve((await fn()));
|
|
2444
|
+
}
|
|
2445
|
+
else {
|
|
2446
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
2447
|
+
}
|
|
2080
2448
|
}
|
|
2081
|
-
|
|
2082
|
-
|
|
2449
|
+
catch (error) {
|
|
2450
|
+
reject(error);
|
|
2083
2451
|
}
|
|
2084
2452
|
});
|
|
2085
2453
|
}
|
|
@@ -2107,20 +2475,21 @@ export class SqlService {
|
|
|
2107
2475
|
return `(
|
|
2108
2476
|
${Object.entries(where).map(([K, V]) => {
|
|
2109
2477
|
params.push(V);
|
|
2110
|
-
return `${K} = ?`;
|
|
2478
|
+
return `${this[_fields][K]?.C2()} = ?`;
|
|
2111
2479
|
}).join(' AND ')}
|
|
2112
2480
|
)`;
|
|
2113
2481
|
}).join(' OR ');
|
|
2114
2482
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2483
|
+
params.unshift(this[_deleteState]);
|
|
2115
2484
|
sqls.push({
|
|
2116
|
-
sql: `
|
|
2117
|
-
UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.
|
|
2485
|
+
sql: format(`
|
|
2486
|
+
UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.C2()} = ?
|
|
2118
2487
|
WHERE ${whereSql};
|
|
2119
|
-
|
|
2488
|
+
`), params
|
|
2120
2489
|
});
|
|
2121
2490
|
}
|
|
2122
2491
|
else {
|
|
2123
|
-
sqls.push({ sql: `DELETE FROM ${tableNameESC} WHERE ${whereSql}
|
|
2492
|
+
sqls.push({ sql: format(`DELETE FROM ${tableNameESC} WHERE ${whereSql};`), params });
|
|
2124
2493
|
}
|
|
2125
2494
|
}
|
|
2126
2495
|
else {
|
|
@@ -2132,28 +2501,32 @@ export class SqlService {
|
|
|
2132
2501
|
case DBType.Mysql: {
|
|
2133
2502
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2134
2503
|
sqls.push({
|
|
2135
|
-
sql: `UPDATE ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.
|
|
2136
|
-
SET a.${this[_fields][this[_stateFileName]]?.
|
|
2504
|
+
sql: format(`UPDATE ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')}
|
|
2505
|
+
SET a.${this[_fields][this[_stateFileName]]?.C2()} = ?;`),
|
|
2506
|
+
params: [this[_deleteState]]
|
|
2137
2507
|
});
|
|
2138
2508
|
}
|
|
2139
2509
|
else {
|
|
2140
2510
|
sqls.push({
|
|
2141
|
-
sql: `DELETE a.* FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.
|
|
2511
|
+
sql: format(`DELETE a.* FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')};`)
|
|
2142
2512
|
});
|
|
2143
2513
|
}
|
|
2514
|
+
break;
|
|
2144
2515
|
}
|
|
2145
2516
|
case DBType.Sqlite:
|
|
2146
2517
|
case DBType.SqliteRemote: {
|
|
2147
|
-
const columnNames = iterare(delWhere).map(K => this[_fields][K]?.
|
|
2518
|
+
const columnNames = iterare(delWhere).map(K => this[_fields][K]?.C2()).join(',');
|
|
2148
2519
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2149
2520
|
sqls.push({
|
|
2150
|
-
sql: `UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.
|
|
2151
|
-
WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`
|
|
2521
|
+
sql: format(`UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.C2()} = ?
|
|
2522
|
+
WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`),
|
|
2523
|
+
params: [this[_deleteState]]
|
|
2152
2524
|
});
|
|
2153
2525
|
}
|
|
2154
2526
|
else {
|
|
2155
|
-
sqls.push({ sql: `DELETE FROM ${tableNameESC} WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});` });
|
|
2527
|
+
sqls.push({ sql: format(`DELETE FROM ${tableNameESC} WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`) });
|
|
2156
2528
|
}
|
|
2529
|
+
break;
|
|
2157
2530
|
}
|
|
2158
2531
|
}
|
|
2159
2532
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
@@ -2167,7 +2540,7 @@ export class SqlService {
|
|
|
2167
2540
|
}
|
|
2168
2541
|
return result;
|
|
2169
2542
|
};
|
|
2170
|
-
if (option?.conn?.[_inTransaction] === true) {
|
|
2543
|
+
if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2171
2544
|
return fn();
|
|
2172
2545
|
}
|
|
2173
2546
|
else {
|
|
@@ -2183,12 +2556,17 @@ export class SqlService {
|
|
|
2183
2556
|
}
|
|
2184
2557
|
return result;
|
|
2185
2558
|
};
|
|
2186
|
-
return new Promise(async (resolve) => {
|
|
2187
|
-
|
|
2188
|
-
|
|
2559
|
+
return new Promise(async (resolve, reject) => {
|
|
2560
|
+
try {
|
|
2561
|
+
if (this[_dbType] === DBType.SqliteRemote || option?.conn?.[_inTransaction] === true) {
|
|
2562
|
+
resolve((await fn()));
|
|
2563
|
+
}
|
|
2564
|
+
else {
|
|
2565
|
+
await option?.dao?.transaction(SyncMode.Async, async () => resolve((await fn())), option?.conn);
|
|
2566
|
+
}
|
|
2189
2567
|
}
|
|
2190
|
-
|
|
2191
|
-
|
|
2568
|
+
catch (error) {
|
|
2569
|
+
reject(error);
|
|
2192
2570
|
}
|
|
2193
2571
|
});
|
|
2194
2572
|
}
|
|
@@ -2209,6 +2587,9 @@ export class SqlService {
|
|
|
2209
2587
|
case TemplateResult.Count: {
|
|
2210
2588
|
return result[0].ct;
|
|
2211
2589
|
}
|
|
2590
|
+
case TemplateResult.ManyList: {
|
|
2591
|
+
return new ArrayList(result[0]);
|
|
2592
|
+
}
|
|
2212
2593
|
}
|
|
2213
2594
|
}
|
|
2214
2595
|
template(option) {
|
|
@@ -2228,19 +2609,19 @@ export class SqlService {
|
|
|
2228
2609
|
const ids = option.id instanceof Array ? option.id : [option.id];
|
|
2229
2610
|
option.where = ids.map(i => ({ [idName]: i }));
|
|
2230
2611
|
}
|
|
2231
|
-
const columns = option.templateResult === TemplateResult.Count ? 'COUNT(1) ct' : iterare((option.columns ?? this[_columns])).map((K) => `a.${this[_fields][K]?.
|
|
2612
|
+
const columns = option.templateResult === TemplateResult.Count ? 'COUNT(1) ct' : iterare((option.columns ?? this[_columns])).map((K) => `a.${this[_fields][K]?.C3()}`).join(',');
|
|
2232
2613
|
const wheres = option.where instanceof Array ? option.where : [option.where];
|
|
2233
2614
|
const sqls = [];
|
|
2234
2615
|
let resultIndex = -1;
|
|
2235
2616
|
if (option.mode === SelectMode.Common) {
|
|
2236
2617
|
const params = new Array();
|
|
2237
|
-
const whereSql = iterare(wheres).map(where => {
|
|
2618
|
+
const whereSql = format(iterare(wheres).map(where => this[_transformer](where, option)).map(where => {
|
|
2238
2619
|
return `SELECT ${columns} FROM ${tableNameESC} a WHERE
|
|
2239
2620
|
${Object.entries(where).map(([K, V]) => {
|
|
2240
2621
|
params.push(V);
|
|
2241
|
-
return `${K} = ?`;
|
|
2622
|
+
return `${this[_fields][K]?.C2()} = ?`;
|
|
2242
2623
|
}).join(' AND ')}`;
|
|
2243
|
-
}).join(' UNION ALL ');
|
|
2624
|
+
}).join(' UNION ALL '));
|
|
2244
2625
|
sqls.push({ sql: whereSql, params });
|
|
2245
2626
|
resultIndex = 0;
|
|
2246
2627
|
}
|
|
@@ -2250,7 +2631,7 @@ export class SqlService {
|
|
|
2250
2631
|
const _sqls = this._createTable({ tableName: tableTemp, temp: true, columns: delWhere, data: wheres, index: 'all', id: 'none' });
|
|
2251
2632
|
sqls.push(..._sqls);
|
|
2252
2633
|
resultIndex = sqls.length;
|
|
2253
|
-
sqls.push({ sql: `SELECT ${columns} FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.
|
|
2634
|
+
sqls.push({ sql: format(`SELECT ${columns} FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')};`) });
|
|
2254
2635
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
2255
2636
|
}
|
|
2256
2637
|
if (option.sync === SyncMode.Sync) {
|
|
@@ -2266,143 +2647,189 @@ export class SqlService {
|
|
|
2266
2647
|
return this._template(option.templateResult, result, option.error);
|
|
2267
2648
|
}
|
|
2268
2649
|
else {
|
|
2269
|
-
return new Promise(async (resolve) => {
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2650
|
+
return new Promise(async (resolve, reject) => {
|
|
2651
|
+
try {
|
|
2652
|
+
let result;
|
|
2653
|
+
for (let i = 0; i < sqls.length; i++) {
|
|
2654
|
+
if (i === resultIndex) {
|
|
2655
|
+
result = await option.conn.query(SyncMode.Async, sqls[i]?.sql, sqls[i]?.params);
|
|
2656
|
+
}
|
|
2657
|
+
else {
|
|
2658
|
+
await option.conn.execute(SyncMode.Async, sqls[i]?.sql, sqls[i]?.params);
|
|
2659
|
+
}
|
|
2277
2660
|
}
|
|
2661
|
+
resolve(this._template(option.templateResult, result, option.error));
|
|
2662
|
+
}
|
|
2663
|
+
catch (error) {
|
|
2664
|
+
reject(error);
|
|
2278
2665
|
}
|
|
2279
|
-
resolve(this._template(option.templateResult, result, option.error));
|
|
2280
2666
|
});
|
|
2281
2667
|
}
|
|
2282
2668
|
}
|
|
2283
|
-
_select(templateResult, result, def, errorMsg,
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
return result.map((r) => Object.values(r)[0]);
|
|
2289
|
-
}
|
|
2290
|
-
catch (error) {
|
|
2291
|
-
}
|
|
2292
|
-
}
|
|
2293
|
-
case SelectResult.One_Row_One_Column_Assert: {
|
|
2294
|
-
try {
|
|
2295
|
-
return iterare(result).map((r) => Object.values(r)[0]).filter((r) => r !== null).toArray();
|
|
2296
|
-
}
|
|
2297
|
-
catch (error) {
|
|
2298
|
-
}
|
|
2299
|
-
}
|
|
2300
|
-
case SelectResult.One_Row_Many_Column_NotSure: {
|
|
2301
|
-
try {
|
|
2302
|
-
return result.map((r) => r[0]);
|
|
2303
|
-
}
|
|
2304
|
-
catch (error) {
|
|
2305
|
-
}
|
|
2669
|
+
_select(templateResult, result, def, errorMsg, hump, mapper, mapperIfUndefined) {
|
|
2670
|
+
switch (templateResult) {
|
|
2671
|
+
case SelectResult.R_C_NotSure: {
|
|
2672
|
+
try {
|
|
2673
|
+
return Object.values(result[0])[0];
|
|
2306
2674
|
}
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
return iterare(result).map((r) => r[0]).filter((r) => r !== null).toArray();
|
|
2310
|
-
}
|
|
2311
|
-
catch (error) {
|
|
2312
|
-
}
|
|
2675
|
+
catch (error) {
|
|
2676
|
+
return def;
|
|
2313
2677
|
}
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
catch (error) {
|
|
2319
|
-
}
|
|
2678
|
+
}
|
|
2679
|
+
case SelectResult.R_C_Assert: {
|
|
2680
|
+
try {
|
|
2681
|
+
return Object.values(result[0])[0];
|
|
2320
2682
|
}
|
|
2321
|
-
|
|
2322
|
-
|
|
2683
|
+
catch (error) {
|
|
2684
|
+
if (def !== undefined)
|
|
2685
|
+
return def;
|
|
2686
|
+
Throw.now(errorMsg ?? 'not found data!');
|
|
2323
2687
|
}
|
|
2324
2688
|
}
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
case SelectResult.One_Row_One_Column_NotSure: {
|
|
2329
|
-
try {
|
|
2330
|
-
return Object.values(result[0])[0];
|
|
2331
|
-
}
|
|
2332
|
-
catch (error) {
|
|
2333
|
-
return def;
|
|
2334
|
-
}
|
|
2689
|
+
case SelectResult.R_CS_NotSure: {
|
|
2690
|
+
if (mapper) {
|
|
2691
|
+
return flatData({ data: result[0], mapper, mapperIfUndefined });
|
|
2335
2692
|
}
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
return Object.values(result[0])[0];
|
|
2339
|
-
}
|
|
2340
|
-
catch (error) {
|
|
2341
|
-
if (def !== undefined)
|
|
2342
|
-
return def;
|
|
2343
|
-
Throw.now(errorMsg ?? 'not found data!');
|
|
2344
|
-
}
|
|
2693
|
+
else if (hump === true || (hump === undefined && globalThis[_Hump] === true)) {
|
|
2694
|
+
return C2P2(result[0]) ?? null;
|
|
2345
2695
|
}
|
|
2346
|
-
|
|
2696
|
+
else {
|
|
2347
2697
|
return result[0] ?? null;
|
|
2348
2698
|
}
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2699
|
+
}
|
|
2700
|
+
case SelectResult.R_CS_Assert: {
|
|
2701
|
+
const data = result[0];
|
|
2702
|
+
Throw.if(data === null || data === undefined, errorMsg ?? 'not found data!');
|
|
2703
|
+
if (mapper) {
|
|
2704
|
+
return flatData({ data, mapper, mapperIfUndefined });
|
|
2705
|
+
}
|
|
2706
|
+
else if (hump === true || (hump === undefined && globalThis[_Hump] === true)) {
|
|
2707
|
+
return C2P2(data) ?? null;
|
|
2708
|
+
}
|
|
2709
|
+
else {
|
|
2352
2710
|
return data ?? null;
|
|
2353
2711
|
}
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
catch (error) {
|
|
2359
|
-
return def;
|
|
2360
|
-
}
|
|
2712
|
+
}
|
|
2713
|
+
case SelectResult.RS_C: {
|
|
2714
|
+
try {
|
|
2715
|
+
return result.map((r) => Object.values(r)[0]);
|
|
2361
2716
|
}
|
|
2362
|
-
|
|
2717
|
+
catch (error) {
|
|
2363
2718
|
return result;
|
|
2364
2719
|
}
|
|
2365
2720
|
}
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2721
|
+
case SelectResult.RS_CS: {
|
|
2722
|
+
if (mapper) {
|
|
2723
|
+
return iterare(result).map((data) => flatData({ data, mapper, mapperIfUndefined })).toArray();
|
|
2724
|
+
}
|
|
2725
|
+
else if (hump === true || (hump === undefined && globalThis[_Hump] === true)) {
|
|
2726
|
+
return iterare(result).map((r) => C2P2(r)).toArray();
|
|
2727
|
+
}
|
|
2728
|
+
else {
|
|
2729
|
+
return result;
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
case SelectResult.RS_C_List: {
|
|
2733
|
+
try {
|
|
2734
|
+
return new ArrayList(result.map((r) => Object.values(r)[0]));
|
|
2735
|
+
}
|
|
2736
|
+
catch (error) {
|
|
2737
|
+
return new ArrayList();
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
case SelectResult.RS_CS_List: {
|
|
2741
|
+
if (mapper) {
|
|
2742
|
+
return new ArrayList(iterare(result).map((data) => flatData({ data, mapper, mapperIfUndefined })).toArray());
|
|
2743
|
+
}
|
|
2744
|
+
else if (hump === true || (hump === undefined && globalThis[_Hump] === true)) {
|
|
2745
|
+
return new ArrayList(iterare(result).map((r) => C2P2(r)).toArray());
|
|
2746
|
+
}
|
|
2747
|
+
else {
|
|
2748
|
+
return new ArrayList();
|
|
2749
|
+
}
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
select(option) {
|
|
2754
|
+
Throw.if(!option.sqlId && !option.sql, 'not found sql!');
|
|
2755
|
+
option.selectResult ?? (option.selectResult = SelectResult.RS_CS);
|
|
2756
|
+
option.defValue ?? (option.defValue = null);
|
|
2757
|
+
if (option.sqlId && globalThis[_resultMap_SQLID][option.sqlId] && !option.mapper) {
|
|
2758
|
+
option.mapper = globalThis[_resultMap_SQLID][option.sqlId];
|
|
2759
|
+
}
|
|
2760
|
+
const _params = Object.assign({}, option.context, option.params);
|
|
2761
|
+
option.sql ?? (option.sql = globalThis[_sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, isCount: option.isCount, ..._params }));
|
|
2762
|
+
const params = [];
|
|
2763
|
+
const sql = option.sql?.replace(/\:([A-Za-z0-9._]+)/g, (txt, key) => {
|
|
2764
|
+
let V = LGet(_params, key);
|
|
2765
|
+
if (V) {
|
|
2766
|
+
if (V !== undefined) {
|
|
2767
|
+
params.push(V);
|
|
2768
|
+
}
|
|
2769
|
+
return '?';
|
|
2770
|
+
}
|
|
2771
|
+
const _key = C2P(key);
|
|
2772
|
+
V = LGet(_params, _key);
|
|
2773
|
+
if (V) {
|
|
2774
|
+
if (V !== undefined) {
|
|
2775
|
+
params.push(V);
|
|
2776
|
+
}
|
|
2777
|
+
return '?';
|
|
2778
|
+
}
|
|
2779
|
+
const __key = P2C(key);
|
|
2780
|
+
V = LGet(_params, __key);
|
|
2781
|
+
if (V) {
|
|
2782
|
+
if (V !== undefined) {
|
|
2783
|
+
params.push(V);
|
|
2784
|
+
}
|
|
2785
|
+
return '?';
|
|
2381
2786
|
}
|
|
2382
2787
|
return txt;
|
|
2383
2788
|
});
|
|
2384
2789
|
if (option.sync === SyncMode.Sync) {
|
|
2385
2790
|
const result = option.conn.query(SyncMode.Sync, sql, params);
|
|
2386
|
-
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.
|
|
2791
|
+
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.hump, option.mapper, option.mapperIfUndefined);
|
|
2387
2792
|
}
|
|
2388
2793
|
else {
|
|
2389
|
-
return new Promise(async (resolve) => {
|
|
2390
|
-
|
|
2391
|
-
|
|
2794
|
+
return new Promise(async (resolve, reject) => {
|
|
2795
|
+
try {
|
|
2796
|
+
const result = await option.conn.query(SyncMode.Async, sql, params);
|
|
2797
|
+
resolve(this._select(option.selectResult, result, option.defValue, option.errorMsg, option.hump, option.mapper, option.mapperIfUndefined));
|
|
2798
|
+
}
|
|
2799
|
+
catch (error) {
|
|
2800
|
+
reject(error);
|
|
2801
|
+
}
|
|
2392
2802
|
});
|
|
2393
2803
|
}
|
|
2394
2804
|
}
|
|
2395
2805
|
excute(option) {
|
|
2396
2806
|
Throw.if(!option.sqlId && !option.sql, 'not found sql!');
|
|
2397
2807
|
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);
|
|
2808
|
+
option.sql ?? (option.sql = globalThis[_sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, ..._params }));
|
|
2400
2809
|
const params = [];
|
|
2401
2810
|
const sql = option.sql?.replace(/\:(\w+)/g, (txt, key) => {
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2811
|
+
let V = LGet(_params, key);
|
|
2812
|
+
if (V) {
|
|
2813
|
+
if (V !== undefined) {
|
|
2814
|
+
params.push(V);
|
|
2815
|
+
}
|
|
2816
|
+
return '?';
|
|
2817
|
+
}
|
|
2818
|
+
const _key = C2P(key);
|
|
2819
|
+
V = LGet(_params, _key);
|
|
2820
|
+
if (V) {
|
|
2821
|
+
if (V !== undefined) {
|
|
2822
|
+
params.push(V);
|
|
2823
|
+
}
|
|
2824
|
+
return '?';
|
|
2825
|
+
}
|
|
2826
|
+
const __key = P2C(key);
|
|
2827
|
+
V = LGet(_params, __key);
|
|
2828
|
+
if (V) {
|
|
2829
|
+
if (V !== undefined) {
|
|
2830
|
+
params.push(V);
|
|
2831
|
+
}
|
|
2832
|
+
return '?';
|
|
2406
2833
|
}
|
|
2407
2834
|
return txt;
|
|
2408
2835
|
});
|
|
@@ -2411,9 +2838,14 @@ export class SqlService {
|
|
|
2411
2838
|
return result.affectedRows;
|
|
2412
2839
|
}
|
|
2413
2840
|
else {
|
|
2414
|
-
return new Promise(async (resolve) => {
|
|
2415
|
-
|
|
2416
|
-
|
|
2841
|
+
return new Promise(async (resolve, reject) => {
|
|
2842
|
+
try {
|
|
2843
|
+
const result = await option.conn.execute(SyncMode.Async, sql, params);
|
|
2844
|
+
resolve(result.affectedRows);
|
|
2845
|
+
}
|
|
2846
|
+
catch (error) {
|
|
2847
|
+
reject(error);
|
|
2848
|
+
}
|
|
2417
2849
|
});
|
|
2418
2850
|
}
|
|
2419
2851
|
}
|
|
@@ -2422,14 +2854,127 @@ export class SqlService {
|
|
|
2422
2854
|
return option.dao.transaction(SyncMode.Sync, option.fn);
|
|
2423
2855
|
}
|
|
2424
2856
|
else {
|
|
2425
|
-
return new Promise(async (resolve) => {
|
|
2426
|
-
|
|
2427
|
-
|
|
2857
|
+
return new Promise(async (resolve, reject) => {
|
|
2858
|
+
try {
|
|
2859
|
+
const rt = await option.dao.transaction(SyncMode.Async, option.fn);
|
|
2860
|
+
resolve(rt);
|
|
2861
|
+
}
|
|
2862
|
+
catch (error) {
|
|
2863
|
+
reject(error);
|
|
2864
|
+
}
|
|
2428
2865
|
});
|
|
2429
2866
|
}
|
|
2430
2867
|
}
|
|
2431
2868
|
stream(option) {
|
|
2432
|
-
return new StreamQuery(option?.tableName ?? this[_tableName], this);
|
|
2869
|
+
return new StreamQuery(option?.tableName ?? this[_tableName], this, this[_fields], this[_columns]);
|
|
2870
|
+
}
|
|
2871
|
+
page(option) {
|
|
2872
|
+
const result = {
|
|
2873
|
+
sum: {},
|
|
2874
|
+
records: [],
|
|
2875
|
+
size: 0,
|
|
2876
|
+
total: 0
|
|
2877
|
+
};
|
|
2878
|
+
Object.assign(option.params, {
|
|
2879
|
+
limitStart: calc(option.pageNumber).sub(1).mul(option.pageSize).over(),
|
|
2880
|
+
limitEnd: calc(option.pageSize).over(),
|
|
2881
|
+
orderBy: option.orderBy
|
|
2882
|
+
});
|
|
2883
|
+
let sql = globalThis[_sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, isCount: false, ...option.params });
|
|
2884
|
+
let sqlSum = '';
|
|
2885
|
+
let sqlCount = '';
|
|
2886
|
+
if (option.sumSelf === true) {
|
|
2887
|
+
sqlSum = globalThis[_sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, isCount: false, isSum: true, ...option.params });
|
|
2888
|
+
}
|
|
2889
|
+
if (option.limitSelf !== true) {
|
|
2890
|
+
if (option.countSelf !== true) {
|
|
2891
|
+
}
|
|
2892
|
+
if (option.orderBy) {
|
|
2893
|
+
sql = `${sql} ORDER BY ${option.orderBy}`;
|
|
2894
|
+
}
|
|
2895
|
+
if (option.pageSize > 0) {
|
|
2896
|
+
sql = `${sql} LIMIT ${option.params['limitStart']}, ${option.pageSize}`;
|
|
2897
|
+
}
|
|
2898
|
+
}
|
|
2899
|
+
if (option.pageSize > 0) {
|
|
2900
|
+
if (option.countSelf === true) {
|
|
2901
|
+
sqlCount = globalThis[_sqlCache].load(this._matchSqlid(`${option.sqlId}_count`), { ctx: option.context, isCount: true, isSum: false, ...option.params });
|
|
2902
|
+
}
|
|
2903
|
+
else {
|
|
2904
|
+
sqlCount = globalThis[_sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, isCount: true, isSum: false, ...option.params });
|
|
2905
|
+
}
|
|
2906
|
+
}
|
|
2907
|
+
if (option.sync === SyncMode.Sync) {
|
|
2908
|
+
if (sqlCount) {
|
|
2909
|
+
result.total = this.select({
|
|
2910
|
+
...option,
|
|
2911
|
+
sql: sqlCount,
|
|
2912
|
+
sync: SyncMode.Sync,
|
|
2913
|
+
selectResult: SelectResult.R_C_Assert
|
|
2914
|
+
});
|
|
2915
|
+
result.size = calc(result.total)
|
|
2916
|
+
.add(option.pageSize - 1)
|
|
2917
|
+
.div(option.pageSize)
|
|
2918
|
+
.round(0, 2)
|
|
2919
|
+
.over();
|
|
2920
|
+
}
|
|
2921
|
+
if (sqlSum) {
|
|
2922
|
+
result.sum = this.select({
|
|
2923
|
+
...option,
|
|
2924
|
+
sql: sqlSum,
|
|
2925
|
+
sync: SyncMode.Sync,
|
|
2926
|
+
selectResult: SelectResult.R_CS_Assert
|
|
2927
|
+
});
|
|
2928
|
+
}
|
|
2929
|
+
if (sql) {
|
|
2930
|
+
result.records = this.select({
|
|
2931
|
+
...option,
|
|
2932
|
+
sql,
|
|
2933
|
+
sync: SyncMode.Sync,
|
|
2934
|
+
selectResult: SelectResult.RS_CS
|
|
2935
|
+
});
|
|
2936
|
+
}
|
|
2937
|
+
return result;
|
|
2938
|
+
}
|
|
2939
|
+
else {
|
|
2940
|
+
return new Promise(async (resolve, reject) => {
|
|
2941
|
+
try {
|
|
2942
|
+
if (sqlCount) {
|
|
2943
|
+
result.total = await this.select({
|
|
2944
|
+
...option,
|
|
2945
|
+
sql: sqlCount,
|
|
2946
|
+
sync: SyncMode.Async,
|
|
2947
|
+
selectResult: SelectResult.R_C_Assert
|
|
2948
|
+
});
|
|
2949
|
+
result.size = calc(result.total)
|
|
2950
|
+
.add(option.pageSize - 1)
|
|
2951
|
+
.div(option.pageSize)
|
|
2952
|
+
.round(0, 2)
|
|
2953
|
+
.over();
|
|
2954
|
+
}
|
|
2955
|
+
if (sqlSum) {
|
|
2956
|
+
result.sum = await this.select({
|
|
2957
|
+
...option,
|
|
2958
|
+
sql: sqlSum,
|
|
2959
|
+
sync: SyncMode.Async,
|
|
2960
|
+
selectResult: SelectResult.R_CS_Assert
|
|
2961
|
+
});
|
|
2962
|
+
}
|
|
2963
|
+
if (sql) {
|
|
2964
|
+
result.records = await this.select({
|
|
2965
|
+
...option,
|
|
2966
|
+
sql,
|
|
2967
|
+
sync: SyncMode.Async,
|
|
2968
|
+
selectResult: SelectResult.RS_CS
|
|
2969
|
+
});
|
|
2970
|
+
}
|
|
2971
|
+
resolve(result);
|
|
2972
|
+
}
|
|
2973
|
+
catch (error) {
|
|
2974
|
+
reject(error);
|
|
2975
|
+
}
|
|
2976
|
+
});
|
|
2977
|
+
}
|
|
2433
2978
|
}
|
|
2434
2979
|
/**
|
|
2435
2980
|
#创建表
|
|
@@ -2475,59 +3020,67 @@ export class SqlService {
|
|
|
2475
3020
|
tableName = Sqlstring.escapeId(tableName ?? this[_tableName]);
|
|
2476
3021
|
switch (this[_dbType]) {
|
|
2477
3022
|
case DBType.Mysql: {
|
|
2478
|
-
let sql = `CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
2479
|
-
${columns.map(K => this[_fields][K][DBType.Mysql]).join(',')}
|
|
2480
|
-
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.
|
|
2481
|
-
${indexs && indexs.length ? `,${indexs.map(i => `KEY ${this[_fields][i]?.
|
|
2482
|
-
) ENGINE=MEMORY
|
|
3023
|
+
let sql = format(`CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3024
|
+
${columns.map(K => this[_fields][K][DBType.Mysql]()).join(',')}
|
|
3025
|
+
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) USING BTREE ` : ''}
|
|
3026
|
+
${indexs && indexs.length ? `,${indexs.map(i => `KEY ${this[_fields][i]?.C2()} (${this[_fields][i]?.C2()})`).join(',')} ` : ''}
|
|
3027
|
+
) ENGINE=MEMORY;`);
|
|
2483
3028
|
sqls.push({ sql });
|
|
2484
3029
|
if (data && data.length > 0) {
|
|
2485
3030
|
const params = [];
|
|
2486
3031
|
let first = true;
|
|
2487
|
-
sql = `INSERT INTO ${tableName} (${columns.map(c =>
|
|
3032
|
+
sql = format(`INSERT INTO ${tableName} (${columns.map(c => this[_fields][c]?.C2()).join(',')})
|
|
2488
3033
|
${(data).map(d => {
|
|
2489
3034
|
const r = `SELECT ${Object.entries(d).map(([K, V]) => {
|
|
2490
3035
|
params.push(V);
|
|
2491
|
-
return `? ${first ? this[_fields][K]?.
|
|
3036
|
+
return `? ${first ? this[_fields][K]?.C2() : ''}`;
|
|
2492
3037
|
}).join(',')}`;
|
|
2493
3038
|
first = false;
|
|
2494
3039
|
return r;
|
|
2495
|
-
}).join(' UNION ALL ')}
|
|
3040
|
+
}).join(' UNION ALL ')}`);
|
|
2496
3041
|
sqls.push({ sql, params });
|
|
2497
3042
|
}
|
|
2498
3043
|
break;
|
|
2499
3044
|
}
|
|
2500
3045
|
case DBType.Sqlite:
|
|
2501
3046
|
case DBType.SqliteRemote: {
|
|
2502
|
-
let sql = `CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
2503
|
-
${columns.map(K => this[_fields][K][DBType.Sqlite]).join(',')}
|
|
2504
|
-
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.
|
|
2505
|
-
)
|
|
3047
|
+
let sql = format(`CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3048
|
+
${columns.map(K => this[_fields][K][DBType.Sqlite]()).join(',')}
|
|
3049
|
+
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) ` : ''}
|
|
3050
|
+
);`);
|
|
2506
3051
|
sqls.push({ sql });
|
|
2507
3052
|
if (indexs) {
|
|
2508
3053
|
for (const index of indexs) {
|
|
2509
|
-
sql = `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableName} (${this[_fields][index]?.
|
|
3054
|
+
sql = format(`CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableName} (${this[_fields][index]?.C2()});`);
|
|
2510
3055
|
sqls.push({ sql });
|
|
2511
3056
|
}
|
|
2512
3057
|
}
|
|
2513
3058
|
if (data && data.length > 0) {
|
|
2514
3059
|
const params = [];
|
|
2515
3060
|
let first = true;
|
|
2516
|
-
sql = `INSERT INTO ${tableName} (${columns.map(c =>
|
|
3061
|
+
sql = format(`INSERT INTO ${tableName} (${columns.map(c => this[_fields][c]?.C2()).join(',')})
|
|
2517
3062
|
${(data).map(d => {
|
|
2518
3063
|
const r = `SELECT ${Object.entries(d).map(([K, V]) => {
|
|
2519
3064
|
params.push(V);
|
|
2520
|
-
return `? ${first ? this[_fields][K]?.
|
|
3065
|
+
return `? ${first ? this[_fields][K]?.C2() : ''}`;
|
|
2521
3066
|
}).join(',')}`;
|
|
2522
3067
|
first = false;
|
|
2523
3068
|
return r;
|
|
2524
|
-
}).join(' UNION ALL ')}
|
|
3069
|
+
}).join(' UNION ALL ')}`);
|
|
2525
3070
|
sqls.push({ sql, params });
|
|
2526
3071
|
}
|
|
3072
|
+
break;
|
|
2527
3073
|
}
|
|
2528
3074
|
}
|
|
2529
3075
|
return sqls;
|
|
2530
3076
|
}
|
|
3077
|
+
_matchSqlid(sqlid) {
|
|
3078
|
+
sqlid ?? (sqlid = '');
|
|
3079
|
+
if (sqlid.includes('.'))
|
|
3080
|
+
return [sqlid];
|
|
3081
|
+
else
|
|
3082
|
+
return [`${this[_tableName]}.${sqlid}`, `${this[_className]}.${sqlid}`, `${this[_ClassName]}.${sqlid}`, `${this[_vueName]}.${sqlid}`];
|
|
3083
|
+
}
|
|
2531
3084
|
}
|
|
2532
3085
|
__decorate([
|
|
2533
3086
|
P(),
|
|
@@ -2571,6 +3124,12 @@ __decorate([
|
|
|
2571
3124
|
__metadata("design:paramtypes", [Object]),
|
|
2572
3125
|
__metadata("design:returntype", Object)
|
|
2573
3126
|
], SqlService.prototype, "transaction", null);
|
|
3127
|
+
__decorate([
|
|
3128
|
+
P(),
|
|
3129
|
+
__metadata("design:type", Function),
|
|
3130
|
+
__metadata("design:paramtypes", [Object]),
|
|
3131
|
+
__metadata("design:returntype", Object)
|
|
3132
|
+
], SqlService.prototype, "page", null);
|
|
2574
3133
|
/** 是否进行下一个动作 */
|
|
2575
3134
|
const IF_PROCEED = function () {
|
|
2576
3135
|
return function (_target, _propertyKey, descriptor) {
|
|
@@ -2604,8 +3163,8 @@ const IF_EXEC = function (def) {
|
|
|
2604
3163
|
};
|
|
2605
3164
|
};
|
|
2606
3165
|
};
|
|
2607
|
-
class
|
|
2608
|
-
constructor() {
|
|
3166
|
+
class StreamQuery {
|
|
3167
|
+
constructor(table, service, __fields, __columns) {
|
|
2609
3168
|
this._prefix = 0;
|
|
2610
3169
|
this._index = 0;
|
|
2611
3170
|
this._wheres = [];
|
|
@@ -2615,7 +3174,18 @@ class StreamCondition {
|
|
|
2615
3174
|
this._param = {};
|
|
2616
3175
|
this.if_proceed = true;
|
|
2617
3176
|
this.if_exec = true;
|
|
3177
|
+
this._distinct = false;
|
|
3178
|
+
this._columns = [];
|
|
3179
|
+
this._updateColumns = [];
|
|
3180
|
+
this._groups = [];
|
|
3181
|
+
this._orders = [];
|
|
3182
|
+
this._startRow = 0;
|
|
3183
|
+
this._pageSize = 0;
|
|
2618
3184
|
this._prefix = parseInt(`${Math.random() * 1000}`);
|
|
3185
|
+
this._table = table;
|
|
3186
|
+
this._service = service;
|
|
3187
|
+
this[_fields] = __fields;
|
|
3188
|
+
this[_columns] = __columns;
|
|
2619
3189
|
}
|
|
2620
3190
|
/** 将当前stream重置 */
|
|
2621
3191
|
reset() {
|
|
@@ -2623,108 +3193,353 @@ class StreamCondition {
|
|
|
2623
3193
|
this._wheres.length = 0;
|
|
2624
3194
|
this._param = {};
|
|
2625
3195
|
this._paramKeys = {};
|
|
3196
|
+
this._pageSize = 0;
|
|
3197
|
+
this._startRow = 0;
|
|
3198
|
+
this._orders.length = 0;
|
|
3199
|
+
this._groups.length = 0;
|
|
3200
|
+
this._columns.length = 0;
|
|
3201
|
+
this._updateColumns.length = 0;
|
|
2626
3202
|
return this;
|
|
2627
3203
|
}
|
|
3204
|
+
// #region 条件
|
|
2628
3205
|
/** 为下次链条执行提供条件判断:非异步方法跳过,异步方法不执行并返回默认值 */
|
|
2629
3206
|
if(condition) {
|
|
2630
3207
|
this.if_proceed = condition;
|
|
2631
3208
|
return this;
|
|
2632
3209
|
}
|
|
2633
|
-
eq(key, value, {
|
|
2634
|
-
eqT(t, { name } = {}) {
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
this.
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
3210
|
+
eq(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._(key, value, '=', { paramName, skipEmptyString, breakExcuteIfSkip }); }
|
|
3211
|
+
eqT(t, { name: paramName = '', breakExcuteIfSkip = false } = {}) {
|
|
3212
|
+
let exe = false;
|
|
3213
|
+
if (t) {
|
|
3214
|
+
t = this._service[_transformer](t, {
|
|
3215
|
+
skipNull: true,
|
|
3216
|
+
skipUndefined: true,
|
|
3217
|
+
skipEmptyString: true
|
|
3218
|
+
});
|
|
3219
|
+
const keys = Object.keys(t);
|
|
3220
|
+
if (keys.length > 0) {
|
|
3221
|
+
if (paramName && this._paramKeys[paramName]) {
|
|
3222
|
+
for (const [key, pname] of Object.entries(this._paramKeys[paramName])) {
|
|
3223
|
+
this._param[pname] = t[key];
|
|
3224
|
+
}
|
|
2648
3225
|
}
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
3226
|
+
else {
|
|
3227
|
+
const paramKeys = {};
|
|
3228
|
+
for (const [key, value] of Object.entries(t)) {
|
|
3229
|
+
const pkey = `p${this._prefix}${this._index++}`;
|
|
3230
|
+
this._wheres.push(`AND ${this[_fields][String(key)]?.C2()} = :${pkey} `);
|
|
3231
|
+
this._param[pkey] = value;
|
|
3232
|
+
if (paramName) {
|
|
3233
|
+
paramKeys[key] = pkey;
|
|
3234
|
+
}
|
|
3235
|
+
}
|
|
3236
|
+
if (paramName) {
|
|
3237
|
+
this._paramKeys[paramName] = paramKeys;
|
|
3238
|
+
}
|
|
3239
|
+
}
|
|
3240
|
+
exe = true;
|
|
2652
3241
|
}
|
|
2653
3242
|
}
|
|
3243
|
+
if (breakExcuteIfSkip === true && exe === false) {
|
|
3244
|
+
this.if_exec = false;
|
|
3245
|
+
}
|
|
2654
3246
|
return this;
|
|
2655
3247
|
}
|
|
2656
|
-
notEq(key, value, {
|
|
3248
|
+
notEq(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._(key, value, '<>', { paramName, skipEmptyString, breakExcuteIfSkip }); }
|
|
2657
3249
|
eqWith(key1, key2) { return this._key(key1, key2, '='); }
|
|
2658
3250
|
notEqWith(key1, key2) { return this._key(key1, key2, '<>'); }
|
|
2659
|
-
regexp(key, regexp, {
|
|
2660
|
-
notRegexp(key, regexp, {
|
|
3251
|
+
regexp(key, regexp, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._(key, regexp, 'REGEXP', { paramName, skipEmptyString: true, breakExcuteIfSkip }); }
|
|
3252
|
+
notRegexp(key, regexp, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._(key, regexp, 'REGEXP', { paramName, skipEmptyString: true, not: 'NOT', breakExcuteIfSkip }); }
|
|
2661
3253
|
/** (key1 << 8) + key2 = value */
|
|
2662
|
-
shiftEq(key1, key2, value, {
|
|
3254
|
+
shiftEq(key1, key2, value, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._shift(key1, key2, value, '=', { paramName, breakExcuteIfSkip }); }
|
|
2663
3255
|
/** (key1 << 8) + key2 <> value */
|
|
2664
|
-
shiftNotEq(key1, key2, value, {
|
|
2665
|
-
grate(key, value, {
|
|
2666
|
-
grateEq(key, value, {
|
|
3256
|
+
shiftNotEq(key1, key2, value, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._shift(key1, key2, value, '<>', { paramName, breakExcuteIfSkip }); }
|
|
3257
|
+
grate(key, value, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._(key, value, '>', { paramName, skipEmptyString: true, breakExcuteIfSkip }); }
|
|
3258
|
+
grateEq(key, value, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._(key, value, '>=', { paramName, skipEmptyString: true, breakExcuteIfSkip }); }
|
|
2667
3259
|
grateWith(key1, key2) { return this._key(key1, key2, '>'); }
|
|
2668
3260
|
grateEqWith(key1, key2) { return this._key(key1, key2, '>='); }
|
|
2669
|
-
less(key, value, {
|
|
2670
|
-
lessEq(key, value, {
|
|
3261
|
+
less(key, value, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._(key, value, '<', { paramName, skipEmptyString: true, breakExcuteIfSkip }); }
|
|
3262
|
+
lessEq(key, value, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._(key, value, '<=', { paramName, skipEmptyString: true, breakExcuteIfSkip }); }
|
|
2671
3263
|
lessWith(key1, key2) { return this._key(key1, key2, '<'); }
|
|
2672
3264
|
lessEqWith(key1, key2) { return this._key(key1, key2, '<='); }
|
|
2673
|
-
like(key, value, {
|
|
2674
|
-
notLike(key, value, {
|
|
2675
|
-
leftLike(key, value, {
|
|
2676
|
-
notLeftLike(key, value, {
|
|
2677
|
-
rightLike(key, value, {
|
|
2678
|
-
notRightLike(key, value, {
|
|
2679
|
-
PreciseLike(key, value, {
|
|
2680
|
-
notPreciseLike(key, value, {
|
|
2681
|
-
glob(key, value, {
|
|
2682
|
-
notGlob(key, value, {
|
|
2683
|
-
leftGlob(key, value, {
|
|
2684
|
-
notLeftGlob(key, value, {
|
|
2685
|
-
rightGlob(key, value, {
|
|
2686
|
-
notRightGlob(key, value, {
|
|
2687
|
-
PreciseGlob(key, value, {
|
|
2688
|
-
notPreciseGlob(key, value, {
|
|
2689
|
-
in(key, value, {
|
|
2690
|
-
notIn(key, value, {
|
|
3265
|
+
like(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, left: '%', right: '%', breakExcuteIfSkip }); }
|
|
3266
|
+
notLike(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, left: '%', right: '%', not: 'NOT', breakExcuteIfSkip }); }
|
|
3267
|
+
leftLike(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, left: '%', breakExcuteIfSkip }); }
|
|
3268
|
+
notLeftLike(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, left: '%', not: 'NOT', breakExcuteIfSkip }); }
|
|
3269
|
+
rightLike(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, right: '%', breakExcuteIfSkip }); }
|
|
3270
|
+
notRightLike(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, right: '%', not: 'NOT', breakExcuteIfSkip }); }
|
|
3271
|
+
PreciseLike(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, breakExcuteIfSkip }); }
|
|
3272
|
+
notPreciseLike(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, not: 'NOT', breakExcuteIfSkip }); }
|
|
3273
|
+
glob(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, left: '%', right: '%', breakExcuteIfSkip, op: 'GLOB' }); }
|
|
3274
|
+
notGlob(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, left: '%', right: '%', not: 'NOT', breakExcuteIfSkip, op: 'GLOB' }); }
|
|
3275
|
+
leftGlob(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, left: '%', breakExcuteIfSkip, op: 'GLOB' }); }
|
|
3276
|
+
notLeftGlob(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, left: '%', not: 'NOT', breakExcuteIfSkip, op: 'GLOB' }); }
|
|
3277
|
+
rightGlob(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, right: '%', breakExcuteIfSkip, op: 'GLOB' }); }
|
|
3278
|
+
notRightGlob(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, right: '%', not: 'NOT', breakExcuteIfSkip, op: 'GLOB' }); }
|
|
3279
|
+
PreciseGlob(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, breakExcuteIfSkip, op: 'GLOB' }); }
|
|
3280
|
+
notPreciseGlob(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._like(key, value, { paramName, skipEmptyString, not: 'NOT', breakExcuteIfSkip, op: 'GLOB' }); }
|
|
3281
|
+
in(key, value, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._in(key, value, { paramName, breakExcuteIfSkip }); }
|
|
3282
|
+
notIn(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._in(key, value, { paramName, skipEmptyString, not: 'NOT', breakExcuteIfSkip }); }
|
|
2691
3283
|
isNULL(key) { return this._null(key); }
|
|
2692
3284
|
isNotNULL(key) { return this._null(key, 'NOT'); }
|
|
2693
|
-
between(key, value1, value2, {
|
|
2694
|
-
notBetween(key, value1, value2, {
|
|
2695
|
-
pow(key, value, {
|
|
2696
|
-
notPow(key, value, {
|
|
2697
|
-
powWith(key, values, {
|
|
2698
|
-
notPowWith(key, values, {
|
|
3285
|
+
between(key, value1, value2, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._between(key, value1, value2, { paramName, skipEmptyString, breakExcuteIfSkip }); }
|
|
3286
|
+
notBetween(key, value1, value2, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._between(key, value1, value2, { paramName, skipEmptyString, not: 'NOT', breakExcuteIfSkip }); }
|
|
3287
|
+
pow(key, value, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._pow(key, value, { paramName, breakExcuteIfSkip }); }
|
|
3288
|
+
notPow(key, value, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._pow(key, value, { paramName, not: 'NOT', breakExcuteIfSkip }); }
|
|
3289
|
+
powWith(key, values, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._pow(key, add(...values.map(value => Math.pow(2, +value))), { paramName, breakExcuteIfSkip }); }
|
|
3290
|
+
notPowWith(key, values, { paramName = '', breakExcuteIfSkip = false } = {}) { return this._pow(key, add(...values.map(value => Math.pow(2, +value))), { paramName, not: 'NOT', breakExcuteIfSkip }); }
|
|
2699
3291
|
/** MATCH(key1, key2, key3) AGAINST (value) */
|
|
2700
|
-
match(value, keys, {
|
|
3292
|
+
match(value, keys, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._match(value, keys, { paramName, skipEmptyString, breakExcuteIfSkip }); }
|
|
2701
3293
|
/** NOT MATCH(key1, key2, key3) AGAINST (value) */
|
|
2702
|
-
notMatch(value, keys, {
|
|
3294
|
+
notMatch(value, keys, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._match(value, keys, { paramName, skipEmptyString, not: 'NOT', breakExcuteIfSkip }); }
|
|
2703
3295
|
/** MATCH(key1, key2, key3) AGAINST (value) IN BOOLEAN MODE*/
|
|
2704
|
-
matchBoolean(value, keys, {
|
|
3296
|
+
matchBoolean(value, keys, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._match(value, keys, { paramName, skipEmptyString, breakExcuteIfSkip, append: 'IN BOOLEAN MODE' }); }
|
|
2705
3297
|
/** NOT MATCH(key1, key2, key3) AGAINST (value) IN BOOLEAN MODE */
|
|
2706
|
-
notMatchBoolean(value, keys, {
|
|
3298
|
+
notMatchBoolean(value, keys, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._match(value, keys, { paramName, skipEmptyString, breakExcuteIfSkip, not: 'NOT', append: 'IN BOOLEAN MODE' }); }
|
|
2707
3299
|
/** MATCH(key1, key2, key3) AGAINST (value) WITH QUERY EXPANSION*/
|
|
2708
|
-
matchQuery(value, keys, {
|
|
3300
|
+
matchQuery(value, keys, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._match(value, keys, { paramName, skipEmptyString, breakExcuteIfSkip, append: 'WITH QUERY EXPANSION' }); }
|
|
2709
3301
|
/** NOT MATCH(key1, key2, key3) AGAINST (value) WITH QUERY EXPANSION*/
|
|
2710
|
-
notMatchQuery(value, keys, {
|
|
2711
|
-
includes(key, value, {
|
|
2712
|
-
notIncludes(key, value, {
|
|
2713
|
-
and(fn) {
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
3302
|
+
notMatchQuery(value, keys, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._match(value, keys, { paramName, skipEmptyString, breakExcuteIfSkip, not: 'NOT', append: 'WITH QUERY EXPANSION' }); }
|
|
3303
|
+
includes(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._includes(key, value, { paramName, skipEmptyString, breakExcuteIfSkip }); }
|
|
3304
|
+
notIncludes(key, value, { paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) { return this._includes(key, value, { paramName, skipEmptyString, not: 'NOT', breakExcuteIfSkip }); }
|
|
3305
|
+
and(fn) {
|
|
3306
|
+
if (fn instanceof StreamQuery) {
|
|
3307
|
+
this._andQuerys.push(fn);
|
|
3308
|
+
}
|
|
3309
|
+
else {
|
|
3310
|
+
const stream = new StreamQuery(this._table, this._service, this[_fields], this[_columns]);
|
|
3311
|
+
const ret = fn(stream);
|
|
3312
|
+
if (ret !== false) {
|
|
3313
|
+
this._andQuerys.push(stream);
|
|
3314
|
+
}
|
|
3315
|
+
}
|
|
3316
|
+
return this;
|
|
3317
|
+
}
|
|
3318
|
+
or(fn) {
|
|
3319
|
+
if (fn instanceof StreamQuery) {
|
|
3320
|
+
this._andQuerys.push(fn);
|
|
3321
|
+
}
|
|
3322
|
+
else {
|
|
3323
|
+
const stream = new StreamQuery(this._table, this._service, this[_fields], this[_columns]);
|
|
3324
|
+
const ret = fn(stream);
|
|
3325
|
+
if (ret !== false) {
|
|
3326
|
+
this._orQuerys.push(stream);
|
|
3327
|
+
}
|
|
3328
|
+
}
|
|
3329
|
+
return this;
|
|
3330
|
+
}
|
|
3331
|
+
groupBy(...keys) { this._groups.push(...keys.map(key => `${this[_fields][String(key)]?.C2()}`)); return this; }
|
|
3332
|
+
groupBy2(...keys) { this._groups.push(...keys); return this; }
|
|
3333
|
+
asc(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} ASC`)); return this; }
|
|
3334
|
+
asc2(...keys) { this._orders.push(...keys.map(key => `${key} ASC`)); return this; }
|
|
3335
|
+
desc(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} DESC`)); return this; }
|
|
3336
|
+
desc2(...keys) { this._orders.push(...keys.map(key => `${key} ASC`)); return this; }
|
|
3337
|
+
limit(startRow, pageSize) { this._startRow = startRow; this._pageSize = pageSize; return this; }
|
|
3338
|
+
page(pageNumber, pageSize) { this._startRow = ((pageNumber || 1) - 1) * pageSize; this._pageSize = pageSize; return this; }
|
|
3339
|
+
table(_table) { this._table = _table; return this; }
|
|
3340
|
+
distinct(on = true) { this._distinct = on; return this; }
|
|
3341
|
+
countDistinct(key, countName) { this._columns.push(`COUNT(DISTINCT ${this[_fields][String(key)]?.C2()}) ${countName || `${this[_fields][String(key)]?.C2()}`}`); return this; }
|
|
3342
|
+
count(countName) { this._columns.push(`COUNT(1) ${countName ?? 'ct'}`); return this; }
|
|
3343
|
+
sum(key, legName, distinct) { this._columns.push(`SUM(${distinct ? 'DISTINCT' : ''} ${this[_fields][String(key)]?.C2()}) ${legName || `${this[_fields][String(key)]?.C2()}`}`); return this; }
|
|
3344
|
+
avg(key, legName, distinct) { this._columns.push(`AVG(${distinct ? 'DISTINCT' : ''} ${this[_fields][String(key)]?.C2()}) ${legName || `${this[_fields][String(key)]?.C2()}`}`); return this; }
|
|
3345
|
+
max(key, legName, distinct) { this._columns.push(`MAX(${distinct ? 'DISTINCT' : ''} ${this[_fields][String(key)]?.C2()}) ${legName || `${this[_fields][String(key)]?.C2()}`}`); return this; }
|
|
3346
|
+
min(key, legName, distinct) { this._columns.push(`MIN(${distinct ? 'DISTINCT' : ''} ${this[_fields][String(key)]?.C2()}) ${legName || `${this[_fields][String(key)]?.C2()}`}`); return this; }
|
|
3347
|
+
groupConcat(key, param) {
|
|
3348
|
+
this._columns.push(`GROUP_CONCAT(
|
|
3349
|
+
${param && param.distinct ? 'DISTINCT' : ''} ${this[_fields][String(key)]?.C2()}
|
|
3350
|
+
${param && param.asc && param.asc.length > 0 ? `ORDER BY ${param.asc.map(i => `${this[_fields][String(i)]?.C2()} ASC`)} ` : ''}
|
|
3351
|
+
${param && param.desc && param.desc.length > 0 ? `${param && param.asc && param.asc.length > 0 ? '' : 'ORDER BY'} ${param.desc.map(i => `${this[_fields][String(i)]?.C2()} DESC`)} ` : ''}
|
|
3352
|
+
SEPARATOR '${param && param.separator || ','}'
|
|
3353
|
+
) ${param && param.groupName || `${this[_fields][String(key)]?.C2()}`}`);
|
|
3354
|
+
return this;
|
|
3355
|
+
}
|
|
3356
|
+
select(...key) { this._columns.push(...(key.map(k => this[_fields][String(k)].C3()))); return this; }
|
|
3357
|
+
select2(sql, param) { this._columns.push(`${sql}`); Object.assign(this._param, param); return this; }
|
|
3358
|
+
update(key, value) { this._updates ?? (this._updates = {}); this._updates[key] = value; return this; }
|
|
3359
|
+
update2(sql, param) { this._updateColumns.push(sql); Object.assign(this._param, param); return this; }
|
|
3360
|
+
updateT(t) { this._updates ?? (this._updates = {}); Object.assign(this._updates, t); return this; }
|
|
3361
|
+
replace(key, valueToFind, valueToReplace) {
|
|
3362
|
+
const [pkey1, pkey2] = [`p${this._prefix}${this._index++}`, `p${this._prefix}${this._index++}`];
|
|
3363
|
+
this._updateColumns.push(` ${this[_fields][String(key)]?.C2()} = REPLACE(${this[_fields][String(key)]?.C2()}, :${pkey1}, :${pkey2}) `);
|
|
3364
|
+
this._param[pkey1] = valueToFind;
|
|
3365
|
+
this._param[pkey2] = valueToReplace;
|
|
3366
|
+
return this;
|
|
3367
|
+
}
|
|
3368
|
+
excuteSelect(option) {
|
|
3369
|
+
option ?? (option = {});
|
|
3370
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
3371
|
+
option.selectResult ?? (option.selectResult = SelectResult.RS_CS);
|
|
3372
|
+
const { where, params } = this._where();
|
|
3373
|
+
let sql = `
|
|
3374
|
+
SELECT
|
|
3375
|
+
${this._distinct ? 'DISTINCT' : ''} ${this._columns && this._columns.length > 0 ? this._columns.join(',') : this[_columns].map(key => this[_fields][String(key)]?.C3()).join(',')}
|
|
3376
|
+
FROM ${this._table}
|
|
3377
|
+
${where ? ' WHERE ' : ''}
|
|
3378
|
+
${where}
|
|
3379
|
+
${this._groups.length > 0 ? `GROUP BY ${this._groups.join(',')} ` : ''}
|
|
3380
|
+
${this._orders.length > 0 ? `ORDER BY ${this._orders.join(',')} ` : ''}
|
|
3381
|
+
`;
|
|
3382
|
+
if (this._startRow && this._pageSize) {
|
|
3383
|
+
sql += `LIMIT ${this._startRow}, ${this._pageSize}`;
|
|
3384
|
+
}
|
|
3385
|
+
else if (this._startRow) {
|
|
3386
|
+
sql += `LIMIT ${this._startRow}`;
|
|
3387
|
+
}
|
|
3388
|
+
if (option.sync === SyncMode.Async) {
|
|
3389
|
+
switch (option.selectResult) {
|
|
3390
|
+
case SelectResult.RS_CS: return this._service.select({ ...option, sync: SyncMode.Async, selectResult: SelectResult.RS_CS, sql, params });
|
|
3391
|
+
case SelectResult.RS_C: return this._service.select({ ...option, sync: SyncMode.Async, selectResult: SelectResult.RS_C, sql, params });
|
|
3392
|
+
case SelectResult.RS_CS_List: return this._service.select({ ...option, sync: SyncMode.Async, selectResult: SelectResult.RS_CS_List, sql, params });
|
|
3393
|
+
case SelectResult.RS_C_List: return this._service.select({ ...option, sync: SyncMode.Async, selectResult: SelectResult.RS_C_List, sql, params });
|
|
3394
|
+
case SelectResult.R_CS_Assert: return this._service.select({ ...option, sync: SyncMode.Async, selectResult: SelectResult.R_CS_Assert, sql, params });
|
|
3395
|
+
case SelectResult.R_C_Assert: return this._service.select({ ...option, sync: SyncMode.Async, selectResult: SelectResult.R_C_Assert, sql, params });
|
|
3396
|
+
case SelectResult.R_CS_NotSure: return this._service.select({ ...option, sync: SyncMode.Async, selectResult: SelectResult.R_CS_NotSure, sql, params });
|
|
3397
|
+
case SelectResult.R_C_NotSure: return this._service.select({ ...option, sync: SyncMode.Async, selectResult: SelectResult.R_C_NotSure, sql, params });
|
|
3398
|
+
}
|
|
3399
|
+
}
|
|
3400
|
+
else {
|
|
3401
|
+
switch (option.selectResult) {
|
|
3402
|
+
case SelectResult.RS_CS: return this._service.select({ ...option, sync: SyncMode.Sync, selectResult: SelectResult.RS_CS, sql, params });
|
|
3403
|
+
case SelectResult.RS_C: return this._service.select({ ...option, sync: SyncMode.Sync, selectResult: SelectResult.RS_C, sql, params });
|
|
3404
|
+
case SelectResult.RS_CS_List: return this._service.select({ ...option, sync: SyncMode.Sync, selectResult: SelectResult.RS_CS_List, sql, params });
|
|
3405
|
+
case SelectResult.RS_C_List: return this._service.select({ ...option, sync: SyncMode.Sync, selectResult: SelectResult.RS_C_List, sql, params });
|
|
3406
|
+
case SelectResult.R_CS_Assert: return this._service.select({ ...option, sync: SyncMode.Sync, selectResult: SelectResult.R_CS_Assert, sql, params });
|
|
3407
|
+
case SelectResult.R_C_Assert: return this._service.select({ ...option, sync: SyncMode.Sync, selectResult: SelectResult.R_C_Assert, sql, params });
|
|
3408
|
+
case SelectResult.R_CS_NotSure: return this._service.select({ ...option, sync: SyncMode.Sync, selectResult: SelectResult.R_CS_NotSure, sql, params });
|
|
3409
|
+
case SelectResult.R_C_NotSure: return this._service.select({ ...option, sync: SyncMode.Sync, selectResult: SelectResult.R_C_NotSure, sql, params });
|
|
3410
|
+
}
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
excutePage(option) {
|
|
3414
|
+
option ?? (option = {});
|
|
3415
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
3416
|
+
const { where, params } = this._where();
|
|
3417
|
+
const result = {
|
|
3418
|
+
records: [],
|
|
3419
|
+
size: 0,
|
|
3420
|
+
total: 0
|
|
3421
|
+
};
|
|
3422
|
+
let sql = `
|
|
3423
|
+
SELECT
|
|
3424
|
+
${this._distinct ? 'DISTINCT' : ''} ${this._columns && this._columns.length > 0 ? this._columns.join(',') : this[_columns].map(key => this[_fields][String(key)]?.C3()).join(',')}
|
|
3425
|
+
FROM ${this._table}
|
|
3426
|
+
${where ? ' WHERE ' : ''}
|
|
3427
|
+
${where}
|
|
3428
|
+
${this._groups.length > 0 ? `GROUP BY ${this._groups.join(',')} ` : ''}
|
|
3429
|
+
${this._orders.length > 0 ? `ORDER BY ${this._orders.join(',')} ` : ''}
|
|
3430
|
+
`;
|
|
3431
|
+
if (this._startRow && this._pageSize) {
|
|
3432
|
+
sql += `LIMIT ${this._startRow}, ${this._pageSize}`;
|
|
3433
|
+
}
|
|
3434
|
+
else if (this._startRow) {
|
|
3435
|
+
sql += `LIMIT ${this._startRow}`;
|
|
3436
|
+
}
|
|
3437
|
+
const sqlCount = `
|
|
3438
|
+
SELECT COUNT(1)
|
|
3439
|
+
FROM ${this._table}
|
|
3440
|
+
${where ? ' WHERE ' : ''}
|
|
3441
|
+
${where}
|
|
3442
|
+
${this._groups.length > 0 ? `GROUP BY ${this._groups.join(',')} ` : ''}
|
|
3443
|
+
${this._orders.length > 0 ? `ORDER BY ${this._orders.join(',')} ` : ''}
|
|
3444
|
+
`;
|
|
3445
|
+
if (option.sync === SyncMode.Sync) {
|
|
3446
|
+
result.total = this._service.select({
|
|
3447
|
+
...option,
|
|
3448
|
+
params,
|
|
3449
|
+
sql: sqlCount,
|
|
3450
|
+
sync: SyncMode.Sync,
|
|
3451
|
+
selectResult: SelectResult.R_C_Assert
|
|
3452
|
+
});
|
|
3453
|
+
result.size = calc(result.total)
|
|
3454
|
+
.add(this._pageSize - 1)
|
|
3455
|
+
.div(this._pageSize)
|
|
3456
|
+
.round(0, 2)
|
|
3457
|
+
.over();
|
|
3458
|
+
result.records = this._service.select({
|
|
3459
|
+
...option,
|
|
3460
|
+
params,
|
|
3461
|
+
sql,
|
|
3462
|
+
sync: SyncMode.Sync,
|
|
3463
|
+
selectResult: SelectResult.RS_CS
|
|
3464
|
+
});
|
|
3465
|
+
return result;
|
|
3466
|
+
}
|
|
3467
|
+
else {
|
|
3468
|
+
return new Promise(async (resolve, reject) => {
|
|
3469
|
+
try {
|
|
3470
|
+
result.total = await this._service.select({
|
|
3471
|
+
...option,
|
|
3472
|
+
params,
|
|
3473
|
+
sql: sqlCount,
|
|
3474
|
+
sync: SyncMode.Async,
|
|
3475
|
+
selectResult: SelectResult.R_C_Assert
|
|
3476
|
+
});
|
|
3477
|
+
result.size = calc(result.total)
|
|
3478
|
+
.add(this._pageSize - 1)
|
|
3479
|
+
.div(this._pageSize)
|
|
3480
|
+
.round(0, 2)
|
|
3481
|
+
.over();
|
|
3482
|
+
result.records = await this._service.select({
|
|
3483
|
+
...option,
|
|
3484
|
+
params,
|
|
3485
|
+
sql,
|
|
3486
|
+
sync: SyncMode.Async,
|
|
3487
|
+
selectResult: SelectResult.RS_CS
|
|
3488
|
+
});
|
|
3489
|
+
resolve(result);
|
|
3490
|
+
}
|
|
3491
|
+
catch (error) {
|
|
3492
|
+
reject(error);
|
|
3493
|
+
}
|
|
3494
|
+
});
|
|
3495
|
+
}
|
|
3496
|
+
}
|
|
3497
|
+
excuteUpdate(option) {
|
|
3498
|
+
option ?? (option = {});
|
|
3499
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
3500
|
+
const { where, params } = this._where();
|
|
3501
|
+
const sets = new Array(...this._updateColumns);
|
|
3502
|
+
if (this._updates) {
|
|
3503
|
+
for (const [K, V] of Object.entries(this._updates)) {
|
|
3504
|
+
const pkey = `p${this._prefix}${this._index++}`;
|
|
3505
|
+
sets.push(` ${K} = :${pkey} `);
|
|
3506
|
+
params[pkey] = V;
|
|
3507
|
+
}
|
|
3508
|
+
}
|
|
3509
|
+
if (sets.length > 0) {
|
|
3510
|
+
const sql = `UPDATE ${this._table} SET ${sets.join(',')} ${where}`;
|
|
3511
|
+
if (option.sync === SyncMode.Async) {
|
|
3512
|
+
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3513
|
+
}
|
|
3514
|
+
else {
|
|
3515
|
+
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
3516
|
+
}
|
|
3517
|
+
}
|
|
3518
|
+
else {
|
|
3519
|
+
return 0;
|
|
3520
|
+
}
|
|
3521
|
+
}
|
|
3522
|
+
excuteDelete(option) {
|
|
3523
|
+
option ?? (option = {});
|
|
3524
|
+
option.sync ?? (option.sync = SyncMode.Async);
|
|
3525
|
+
const { where, params } = this._where();
|
|
3526
|
+
const sql = `DELETE FROM ${this._table} ${where}`;
|
|
3527
|
+
if (option.sync === SyncMode.Async) {
|
|
3528
|
+
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3529
|
+
}
|
|
3530
|
+
else {
|
|
3531
|
+
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
3532
|
+
}
|
|
3533
|
+
}
|
|
3534
|
+
_where() {
|
|
2720
3535
|
const wheres = new Array();
|
|
2721
3536
|
const sql = this._wheres.join(' ');
|
|
2722
3537
|
if (sql) {
|
|
2723
|
-
wheres.push(`(${sql})`);
|
|
3538
|
+
wheres.push(`(${sql.replace(/^and|^or/i, '')})`);
|
|
2724
3539
|
}
|
|
2725
3540
|
if (this._orQuerys.length > 0) {
|
|
2726
3541
|
for (const query of this._orQuerys) {
|
|
2727
|
-
const { where, params } = query.
|
|
3542
|
+
const { where, params } = query._where();
|
|
2728
3543
|
if (where) {
|
|
2729
3544
|
wheres.push(` OR (${where}) `);
|
|
2730
3545
|
}
|
|
@@ -2733,7 +3548,7 @@ class StreamCondition {
|
|
|
2733
3548
|
}
|
|
2734
3549
|
if (this._andQuerys.length > 0) {
|
|
2735
3550
|
for (const query of this._andQuerys) {
|
|
2736
|
-
const { where, params } = query.
|
|
3551
|
+
const { where, params } = query._where();
|
|
2737
3552
|
if (where) {
|
|
2738
3553
|
wheres.push(` AND (${where}) `);
|
|
2739
3554
|
}
|
|
@@ -2742,134 +3557,189 @@ class StreamCondition {
|
|
|
2742
3557
|
}
|
|
2743
3558
|
return { where: wheres.join(' '), params: this._param };
|
|
2744
3559
|
}
|
|
2745
|
-
_(key, value, op, { not = '',
|
|
2746
|
-
if (
|
|
2747
|
-
|
|
3560
|
+
_(key, value, op, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
3561
|
+
if (value === null
|
|
3562
|
+
|| value === undefined
|
|
3563
|
+
|| (emptyString(`${value ?? ''}`) && skipEmptyString === true)) {
|
|
3564
|
+
if (breakExcuteIfSkip === true) {
|
|
3565
|
+
this.if_exec = false;
|
|
3566
|
+
}
|
|
3567
|
+
return this;
|
|
3568
|
+
}
|
|
3569
|
+
if (paramName !== undefined && this._paramKeys.hasOwnProperty(paramName)) {
|
|
3570
|
+
this._param[this._paramKeys[paramName]] = value;
|
|
2748
3571
|
}
|
|
2749
3572
|
else {
|
|
3573
|
+
paramName;
|
|
2750
3574
|
const pkey = `p${this._prefix}${this._index++}`;
|
|
2751
|
-
this._wheres.push(`AND ${String(key)} ${not} ${op} :${pkey} `);
|
|
3575
|
+
this._wheres.push(`AND ${this[_fields][String(key)]?.C2()} ${not} ${op} :${pkey} `);
|
|
2752
3576
|
this._param[pkey] = value;
|
|
2753
|
-
if (
|
|
2754
|
-
this._paramKeys[
|
|
3577
|
+
if (paramName) {
|
|
3578
|
+
this._paramKeys[paramName] = pkey;
|
|
2755
3579
|
}
|
|
2756
3580
|
}
|
|
2757
3581
|
return this;
|
|
2758
3582
|
}
|
|
2759
3583
|
_null(key, not = '') {
|
|
2760
|
-
this._wheres.push(`AND ${String(key)}
|
|
3584
|
+
this._wheres.push(`AND ${this[_fields][String(key)]?.C2()} IS ${not} NULL`);
|
|
2761
3585
|
return this;
|
|
2762
3586
|
}
|
|
2763
3587
|
_key(key1, key2, op, not = '') {
|
|
2764
|
-
this._wheres.push(`AND ${String(key1)} ${not} ${op} ${String(key2)} `);
|
|
3588
|
+
this._wheres.push(`AND ${this[_fields][String(key1)]?.C2()} ${not} ${op} ${this[_fields][String(key2)]?.C2()} `);
|
|
2765
3589
|
return this;
|
|
2766
3590
|
}
|
|
2767
|
-
_between(key, value1, value2, { not = '',
|
|
2768
|
-
if (
|
|
2769
|
-
|
|
2770
|
-
|
|
3591
|
+
_between(key, value1, value2, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
3592
|
+
if (value1 === null
|
|
3593
|
+
|| value1 === undefined
|
|
3594
|
+
|| (emptyString(`${value1 ?? ''}`) && skipEmptyString === true)
|
|
3595
|
+
|| value2 === null
|
|
3596
|
+
|| value2 === undefined
|
|
3597
|
+
|| (emptyString(`${value2 ?? ''}`) && skipEmptyString === true)) {
|
|
3598
|
+
if (breakExcuteIfSkip === true) {
|
|
3599
|
+
this.if_exec = false;
|
|
3600
|
+
}
|
|
3601
|
+
return this;
|
|
3602
|
+
}
|
|
3603
|
+
if (paramName && this._paramKeys[paramName]) {
|
|
3604
|
+
this._param[this._paramKeys[paramName][0]] = value1;
|
|
3605
|
+
this._param[this._paramKeys[paramName][1]] = value2;
|
|
2771
3606
|
}
|
|
2772
3607
|
else {
|
|
2773
3608
|
const [pkey1, pkey2] = [`p${this._prefix}${this._index++}`, `p${this._prefix}${this._index++}`];
|
|
2774
|
-
this._wheres.push(`AND ${String(key)} ${not} BETWEEN :${pkey1} AND :${pkey2}`);
|
|
3609
|
+
this._wheres.push(`AND ${this[_fields][String(key)]?.C2()} ${not} BETWEEN :${pkey1} AND :${pkey2}`);
|
|
2775
3610
|
this._param[pkey1] = value1;
|
|
2776
3611
|
this._param[pkey2] = value2;
|
|
2777
|
-
if (
|
|
2778
|
-
this._paramKeys[
|
|
3612
|
+
if (paramName) {
|
|
3613
|
+
this._paramKeys[paramName] = [pkey1, pkey2];
|
|
2779
3614
|
}
|
|
2780
3615
|
}
|
|
2781
3616
|
return this;
|
|
2782
3617
|
}
|
|
2783
|
-
_in(key, value, { not = '',
|
|
3618
|
+
_in(key, value, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
2784
3619
|
if (value && value.length > 0) {
|
|
2785
|
-
if (
|
|
2786
|
-
this._param[this._paramKeys[
|
|
3620
|
+
if (paramName !== undefined && this._paramKeys.hasOwnProperty(paramName)) {
|
|
3621
|
+
this._param[this._paramKeys[paramName]] = value;
|
|
2787
3622
|
}
|
|
2788
3623
|
else {
|
|
2789
3624
|
const pkey = `p${this._prefix}${this._index++}`;
|
|
2790
|
-
this._wheres.push(`AND ${String(key)} ${not} IN (:${pkey}) `);
|
|
3625
|
+
this._wheres.push(`AND ${this[_fields][String(key)]?.C2()} ${not} IN (:${pkey}) `);
|
|
2791
3626
|
this._param[pkey] = value;
|
|
2792
|
-
if (
|
|
2793
|
-
this._paramKeys[
|
|
3627
|
+
if (paramName) {
|
|
3628
|
+
this._paramKeys[paramName] = pkey;
|
|
2794
3629
|
}
|
|
2795
3630
|
}
|
|
2796
3631
|
}
|
|
2797
|
-
else if (
|
|
3632
|
+
else if (breakExcuteIfSkip !== true) {
|
|
2798
3633
|
this.if_exec = false;
|
|
2799
3634
|
}
|
|
2800
3635
|
return this;
|
|
2801
3636
|
}
|
|
2802
|
-
_shift(key1, key2, value, op, { not = '',
|
|
2803
|
-
if (
|
|
2804
|
-
|
|
3637
|
+
_shift(key1, key2, value, op, { not = '', paramName = '', breakExcuteIfSkip = false } = {}) {
|
|
3638
|
+
if (value === null
|
|
3639
|
+
|| value === undefined
|
|
3640
|
+
|| emptyString(`${value ?? ''}`)) {
|
|
3641
|
+
if (breakExcuteIfSkip === true) {
|
|
3642
|
+
this.if_exec = false;
|
|
3643
|
+
}
|
|
3644
|
+
return this;
|
|
3645
|
+
}
|
|
3646
|
+
if (paramName !== undefined && this._paramKeys.hasOwnProperty(paramName)) {
|
|
3647
|
+
this._param[this._paramKeys[paramName]] = value;
|
|
2805
3648
|
}
|
|
2806
3649
|
else {
|
|
2807
3650
|
const pkey = `p${this._prefix}${this._index++}`;
|
|
2808
|
-
this._wheres.push(`AND (${String(key1)} << 8) + ${String(key2)} ${not} ${op} :${pkey} `);
|
|
3651
|
+
this._wheres.push(`AND (${this[_fields][String(key1)]?.C2()} << 8) + ${this[_fields][String(key2)]?.C2()} ${not} ${op} :${pkey} `);
|
|
2809
3652
|
this._param[pkey] = value;
|
|
2810
|
-
if (
|
|
2811
|
-
this._paramKeys[
|
|
3653
|
+
if (paramName) {
|
|
3654
|
+
this._paramKeys[paramName] = pkey;
|
|
2812
3655
|
}
|
|
2813
3656
|
}
|
|
2814
3657
|
return this;
|
|
2815
3658
|
}
|
|
2816
|
-
_match(value, keys, {
|
|
2817
|
-
if (
|
|
2818
|
-
|
|
3659
|
+
_match(value, keys, { paramName = '', not = '', append = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
3660
|
+
if (value === null
|
|
3661
|
+
|| value === undefined
|
|
3662
|
+
|| emptyString(`${value ?? ''}`)) {
|
|
3663
|
+
if (breakExcuteIfSkip === true) {
|
|
3664
|
+
this.if_exec = false;
|
|
3665
|
+
}
|
|
3666
|
+
return this;
|
|
3667
|
+
}
|
|
3668
|
+
if (paramName !== undefined && this._paramKeys.hasOwnProperty(paramName)) {
|
|
3669
|
+
this._param[this._paramKeys[paramName]] = value;
|
|
2819
3670
|
}
|
|
2820
3671
|
else {
|
|
2821
3672
|
const pkey = `p${this._prefix}${this._index++}`;
|
|
2822
|
-
this._wheres.push(`AND MATCH(${keys.join(',')}) AGAINST (:${pkey} ${append ?? ''})`);
|
|
3673
|
+
this._wheres.push(`AND ${not} MATCH(${keys.map(key => this[_fields][String(key)]?.C2()).join(',')}) AGAINST (:${pkey} ${append ?? ''})`);
|
|
2823
3674
|
this._param[pkey] = value;
|
|
2824
|
-
if (
|
|
2825
|
-
this._paramKeys[
|
|
3675
|
+
if (paramName) {
|
|
3676
|
+
this._paramKeys[paramName] = pkey;
|
|
2826
3677
|
}
|
|
2827
3678
|
}
|
|
2828
3679
|
return this;
|
|
2829
3680
|
}
|
|
2830
|
-
_pow(key, value, {
|
|
2831
|
-
if (
|
|
2832
|
-
|
|
3681
|
+
_pow(key, value, { not = '', paramName = '', breakExcuteIfSkip = false } = {}) {
|
|
3682
|
+
if (value === null
|
|
3683
|
+
|| value === undefined
|
|
3684
|
+
|| emptyString(`${value ?? ''}`)) {
|
|
3685
|
+
if (breakExcuteIfSkip === true) {
|
|
3686
|
+
this.if_exec = false;
|
|
3687
|
+
}
|
|
3688
|
+
return this;
|
|
3689
|
+
}
|
|
3690
|
+
if (paramName !== undefined && this._paramKeys.hasOwnProperty(paramName)) {
|
|
3691
|
+
this._param[this._paramKeys[paramName]] = value;
|
|
2833
3692
|
}
|
|
2834
3693
|
else {
|
|
2835
3694
|
const pkey = `p${this._prefix}${this._index++}`;
|
|
2836
|
-
this._wheres.push(`AND
|
|
3695
|
+
this._wheres.push(`AND ${not} POW(2, ${this[_fields][String(key)]?.C2()}) & :${pkey}`);
|
|
2837
3696
|
this._param[pkey] = value;
|
|
2838
|
-
if (
|
|
2839
|
-
this._paramKeys[
|
|
3697
|
+
if (paramName) {
|
|
3698
|
+
this._paramKeys[paramName] = pkey;
|
|
2840
3699
|
}
|
|
2841
3700
|
}
|
|
2842
3701
|
return this;
|
|
2843
3702
|
}
|
|
2844
|
-
_like(key, value, { not = '', left = '', right = '',
|
|
2845
|
-
if (value
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
const pkey = `p${this._prefix}${this._index++}`;
|
|
2851
|
-
this._wheres.push(`AND ${String(key)} ${not} ${op} CONCAT('${left}', :${pkey}, '${right}') `);
|
|
2852
|
-
this._param[pkey] = value;
|
|
2853
|
-
if (name) {
|
|
2854
|
-
this._paramKeys[name] = pkey;
|
|
2855
|
-
}
|
|
3703
|
+
_like(key, value, { not = '', left = '', right = '', paramName = '', op = 'LIKE', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
3704
|
+
if (value === null
|
|
3705
|
+
|| value === undefined
|
|
3706
|
+
|| (emptyString(`${value ?? ''}`) && skipEmptyString === true)) {
|
|
3707
|
+
if (breakExcuteIfSkip === true) {
|
|
3708
|
+
this.if_exec = false;
|
|
2856
3709
|
}
|
|
3710
|
+
return this;
|
|
2857
3711
|
}
|
|
2858
|
-
|
|
2859
|
-
this.
|
|
3712
|
+
if (paramName !== undefined && this._paramKeys.hasOwnProperty(paramName)) {
|
|
3713
|
+
this._param[this._paramKeys[paramName]] = value;
|
|
3714
|
+
}
|
|
3715
|
+
else {
|
|
3716
|
+
const pkey = `p${this._prefix}${this._index++}`;
|
|
3717
|
+
this._wheres.push(`AND ${this[_fields][String(key)]?.C2()} ${not} ${op} CONCAT('${left}', :${pkey}, '${right}') `);
|
|
3718
|
+
this._param[pkey] = value;
|
|
3719
|
+
if (paramName) {
|
|
3720
|
+
this._paramKeys[paramName] = pkey;
|
|
3721
|
+
}
|
|
2860
3722
|
}
|
|
2861
3723
|
return this;
|
|
2862
3724
|
}
|
|
2863
|
-
_includes(key, value, { not = '',
|
|
2864
|
-
if (
|
|
2865
|
-
|
|
3725
|
+
_includes(key, value, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = true } = {}) {
|
|
3726
|
+
if (value === null
|
|
3727
|
+
|| value === undefined
|
|
3728
|
+
|| (emptyString(`${value ?? ''}`) && skipEmptyString === true)) {
|
|
3729
|
+
if (breakExcuteIfSkip === true) {
|
|
3730
|
+
this.if_exec = false;
|
|
3731
|
+
}
|
|
3732
|
+
return this;
|
|
3733
|
+
}
|
|
3734
|
+
if (paramName !== undefined && this._paramKeys.hasOwnProperty(paramName)) {
|
|
3735
|
+
this._param[this._paramKeys[paramName]] = value;
|
|
2866
3736
|
}
|
|
2867
3737
|
else {
|
|
2868
3738
|
const pkey = `p${this._prefix}${this._index++}`;
|
|
2869
|
-
this._wheres.push(`AND LOCATE(${String(key)}, :${pkey}) ${not ? '=' : ''} 0`);
|
|
3739
|
+
this._wheres.push(`AND LOCATE(${this[_fields][String(key)]?.C2()}, :${pkey}) ${not ? '=' : '>'} 0`);
|
|
2870
3740
|
this._param[pkey] = value;
|
|
2871
|
-
if (
|
|
2872
|
-
this._paramKeys[
|
|
3741
|
+
if (paramName) {
|
|
3742
|
+
this._paramKeys[paramName] = pkey;
|
|
2873
3743
|
}
|
|
2874
3744
|
}
|
|
2875
3745
|
return this;
|
|
@@ -2880,576 +3750,496 @@ __decorate([
|
|
|
2880
3750
|
__metadata("design:type", Function),
|
|
2881
3751
|
__metadata("design:paramtypes", [Boolean]),
|
|
2882
3752
|
__metadata("design:returntype", void 0)
|
|
2883
|
-
],
|
|
3753
|
+
], StreamQuery.prototype, "if", null);
|
|
2884
3754
|
__decorate([
|
|
2885
3755
|
IF_PROCEED(),
|
|
2886
3756
|
__metadata("design:type", Function),
|
|
2887
3757
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
2888
3758
|
__metadata("design:returntype", void 0)
|
|
2889
|
-
],
|
|
3759
|
+
], StreamQuery.prototype, "eq", null);
|
|
2890
3760
|
__decorate([
|
|
2891
3761
|
IF_PROCEED(),
|
|
2892
3762
|
__metadata("design:type", Function),
|
|
2893
3763
|
__metadata("design:paramtypes", [Object, Object]),
|
|
2894
3764
|
__metadata("design:returntype", void 0)
|
|
2895
|
-
],
|
|
3765
|
+
], StreamQuery.prototype, "eqT", null);
|
|
2896
3766
|
__decorate([
|
|
2897
3767
|
IF_PROCEED(),
|
|
2898
3768
|
__metadata("design:type", Function),
|
|
2899
3769
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
2900
3770
|
__metadata("design:returntype", void 0)
|
|
2901
|
-
],
|
|
3771
|
+
], StreamQuery.prototype, "notEq", null);
|
|
2902
3772
|
__decorate([
|
|
2903
3773
|
IF_PROCEED(),
|
|
2904
3774
|
__metadata("design:type", Function),
|
|
2905
3775
|
__metadata("design:paramtypes", [Object, Object]),
|
|
2906
3776
|
__metadata("design:returntype", void 0)
|
|
2907
|
-
],
|
|
3777
|
+
], StreamQuery.prototype, "eqWith", null);
|
|
2908
3778
|
__decorate([
|
|
2909
3779
|
IF_PROCEED(),
|
|
2910
3780
|
__metadata("design:type", Function),
|
|
2911
3781
|
__metadata("design:paramtypes", [Object, Object]),
|
|
2912
3782
|
__metadata("design:returntype", void 0)
|
|
2913
|
-
],
|
|
3783
|
+
], StreamQuery.prototype, "notEqWith", null);
|
|
2914
3784
|
__decorate([
|
|
2915
3785
|
IF_PROCEED(),
|
|
2916
3786
|
__metadata("design:type", Function),
|
|
2917
3787
|
__metadata("design:paramtypes", [Object, String, Object]),
|
|
2918
3788
|
__metadata("design:returntype", void 0)
|
|
2919
|
-
],
|
|
3789
|
+
], StreamQuery.prototype, "regexp", null);
|
|
2920
3790
|
__decorate([
|
|
2921
3791
|
IF_PROCEED(),
|
|
2922
3792
|
__metadata("design:type", Function),
|
|
2923
3793
|
__metadata("design:paramtypes", [Object, String, Object]),
|
|
2924
3794
|
__metadata("design:returntype", void 0)
|
|
2925
|
-
],
|
|
3795
|
+
], StreamQuery.prototype, "notRegexp", null);
|
|
2926
3796
|
__decorate([
|
|
2927
3797
|
IF_PROCEED(),
|
|
2928
3798
|
__metadata("design:type", Function),
|
|
2929
3799
|
__metadata("design:paramtypes", [Object, Object, Number, Object]),
|
|
2930
3800
|
__metadata("design:returntype", void 0)
|
|
2931
|
-
],
|
|
3801
|
+
], StreamQuery.prototype, "shiftEq", null);
|
|
2932
3802
|
__decorate([
|
|
2933
3803
|
IF_PROCEED(),
|
|
2934
3804
|
__metadata("design:type", Function),
|
|
2935
3805
|
__metadata("design:paramtypes", [Object, Object, Number, Object]),
|
|
2936
3806
|
__metadata("design:returntype", void 0)
|
|
2937
|
-
],
|
|
3807
|
+
], StreamQuery.prototype, "shiftNotEq", null);
|
|
2938
3808
|
__decorate([
|
|
2939
3809
|
IF_PROCEED(),
|
|
2940
3810
|
__metadata("design:type", Function),
|
|
2941
3811
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
2942
3812
|
__metadata("design:returntype", void 0)
|
|
2943
|
-
],
|
|
3813
|
+
], StreamQuery.prototype, "grate", null);
|
|
2944
3814
|
__decorate([
|
|
2945
3815
|
IF_PROCEED(),
|
|
2946
3816
|
__metadata("design:type", Function),
|
|
2947
3817
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
2948
3818
|
__metadata("design:returntype", void 0)
|
|
2949
|
-
],
|
|
3819
|
+
], StreamQuery.prototype, "grateEq", null);
|
|
2950
3820
|
__decorate([
|
|
2951
3821
|
IF_PROCEED(),
|
|
2952
3822
|
__metadata("design:type", Function),
|
|
2953
3823
|
__metadata("design:paramtypes", [Object, Object]),
|
|
2954
3824
|
__metadata("design:returntype", void 0)
|
|
2955
|
-
],
|
|
3825
|
+
], StreamQuery.prototype, "grateWith", null);
|
|
2956
3826
|
__decorate([
|
|
2957
3827
|
IF_PROCEED(),
|
|
2958
3828
|
__metadata("design:type", Function),
|
|
2959
3829
|
__metadata("design:paramtypes", [Object, Object]),
|
|
2960
3830
|
__metadata("design:returntype", void 0)
|
|
2961
|
-
],
|
|
3831
|
+
], StreamQuery.prototype, "grateEqWith", null);
|
|
2962
3832
|
__decorate([
|
|
2963
3833
|
IF_PROCEED(),
|
|
2964
3834
|
__metadata("design:type", Function),
|
|
2965
3835
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
2966
3836
|
__metadata("design:returntype", void 0)
|
|
2967
|
-
],
|
|
3837
|
+
], StreamQuery.prototype, "less", null);
|
|
2968
3838
|
__decorate([
|
|
2969
3839
|
IF_PROCEED(),
|
|
2970
3840
|
__metadata("design:type", Function),
|
|
2971
3841
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
2972
3842
|
__metadata("design:returntype", void 0)
|
|
2973
|
-
],
|
|
3843
|
+
], StreamQuery.prototype, "lessEq", null);
|
|
2974
3844
|
__decorate([
|
|
2975
3845
|
IF_PROCEED(),
|
|
2976
3846
|
__metadata("design:type", Function),
|
|
2977
3847
|
__metadata("design:paramtypes", [Object, Object]),
|
|
2978
3848
|
__metadata("design:returntype", void 0)
|
|
2979
|
-
],
|
|
3849
|
+
], StreamQuery.prototype, "lessWith", null);
|
|
2980
3850
|
__decorate([
|
|
2981
3851
|
IF_PROCEED(),
|
|
2982
3852
|
__metadata("design:type", Function),
|
|
2983
3853
|
__metadata("design:paramtypes", [Object, Object]),
|
|
2984
3854
|
__metadata("design:returntype", void 0)
|
|
2985
|
-
],
|
|
3855
|
+
], StreamQuery.prototype, "lessEqWith", null);
|
|
2986
3856
|
__decorate([
|
|
2987
3857
|
IF_PROCEED(),
|
|
2988
3858
|
__metadata("design:type", Function),
|
|
2989
3859
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
2990
3860
|
__metadata("design:returntype", void 0)
|
|
2991
|
-
],
|
|
3861
|
+
], StreamQuery.prototype, "like", null);
|
|
2992
3862
|
__decorate([
|
|
2993
3863
|
IF_PROCEED(),
|
|
2994
3864
|
__metadata("design:type", Function),
|
|
2995
3865
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
2996
3866
|
__metadata("design:returntype", void 0)
|
|
2997
|
-
],
|
|
3867
|
+
], StreamQuery.prototype, "notLike", null);
|
|
2998
3868
|
__decorate([
|
|
2999
3869
|
IF_PROCEED(),
|
|
3000
3870
|
__metadata("design:type", Function),
|
|
3001
3871
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3002
3872
|
__metadata("design:returntype", void 0)
|
|
3003
|
-
],
|
|
3873
|
+
], StreamQuery.prototype, "leftLike", null);
|
|
3004
3874
|
__decorate([
|
|
3005
3875
|
IF_PROCEED(),
|
|
3006
3876
|
__metadata("design:type", Function),
|
|
3007
3877
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3008
3878
|
__metadata("design:returntype", void 0)
|
|
3009
|
-
],
|
|
3879
|
+
], StreamQuery.prototype, "notLeftLike", null);
|
|
3010
3880
|
__decorate([
|
|
3011
3881
|
IF_PROCEED(),
|
|
3012
3882
|
__metadata("design:type", Function),
|
|
3013
3883
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3014
3884
|
__metadata("design:returntype", void 0)
|
|
3015
|
-
],
|
|
3885
|
+
], StreamQuery.prototype, "rightLike", null);
|
|
3016
3886
|
__decorate([
|
|
3017
3887
|
IF_PROCEED(),
|
|
3018
3888
|
__metadata("design:type", Function),
|
|
3019
3889
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3020
3890
|
__metadata("design:returntype", void 0)
|
|
3021
|
-
],
|
|
3891
|
+
], StreamQuery.prototype, "notRightLike", null);
|
|
3022
3892
|
__decorate([
|
|
3023
3893
|
IF_PROCEED(),
|
|
3024
3894
|
__metadata("design:type", Function),
|
|
3025
3895
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3026
3896
|
__metadata("design:returntype", void 0)
|
|
3027
|
-
],
|
|
3897
|
+
], StreamQuery.prototype, "PreciseLike", null);
|
|
3028
3898
|
__decorate([
|
|
3029
3899
|
IF_PROCEED(),
|
|
3030
3900
|
__metadata("design:type", Function),
|
|
3031
3901
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3032
3902
|
__metadata("design:returntype", void 0)
|
|
3033
|
-
],
|
|
3903
|
+
], StreamQuery.prototype, "notPreciseLike", null);
|
|
3034
3904
|
__decorate([
|
|
3035
3905
|
IF_PROCEED(),
|
|
3036
3906
|
__metadata("design:type", Function),
|
|
3037
3907
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3038
3908
|
__metadata("design:returntype", void 0)
|
|
3039
|
-
],
|
|
3909
|
+
], StreamQuery.prototype, "glob", null);
|
|
3040
3910
|
__decorate([
|
|
3041
3911
|
IF_PROCEED(),
|
|
3042
3912
|
__metadata("design:type", Function),
|
|
3043
3913
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3044
3914
|
__metadata("design:returntype", void 0)
|
|
3045
|
-
],
|
|
3915
|
+
], StreamQuery.prototype, "notGlob", null);
|
|
3046
3916
|
__decorate([
|
|
3047
3917
|
IF_PROCEED(),
|
|
3048
3918
|
__metadata("design:type", Function),
|
|
3049
3919
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3050
3920
|
__metadata("design:returntype", void 0)
|
|
3051
|
-
],
|
|
3921
|
+
], StreamQuery.prototype, "leftGlob", null);
|
|
3052
3922
|
__decorate([
|
|
3053
3923
|
IF_PROCEED(),
|
|
3054
3924
|
__metadata("design:type", Function),
|
|
3055
3925
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3056
3926
|
__metadata("design:returntype", void 0)
|
|
3057
|
-
],
|
|
3927
|
+
], StreamQuery.prototype, "notLeftGlob", null);
|
|
3058
3928
|
__decorate([
|
|
3059
3929
|
IF_PROCEED(),
|
|
3060
3930
|
__metadata("design:type", Function),
|
|
3061
3931
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3062
3932
|
__metadata("design:returntype", void 0)
|
|
3063
|
-
],
|
|
3933
|
+
], StreamQuery.prototype, "rightGlob", null);
|
|
3064
3934
|
__decorate([
|
|
3065
3935
|
IF_PROCEED(),
|
|
3066
3936
|
__metadata("design:type", Function),
|
|
3067
3937
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3068
3938
|
__metadata("design:returntype", void 0)
|
|
3069
|
-
],
|
|
3939
|
+
], StreamQuery.prototype, "notRightGlob", null);
|
|
3070
3940
|
__decorate([
|
|
3071
3941
|
IF_PROCEED(),
|
|
3072
3942
|
__metadata("design:type", Function),
|
|
3073
3943
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3074
3944
|
__metadata("design:returntype", void 0)
|
|
3075
|
-
],
|
|
3945
|
+
], StreamQuery.prototype, "PreciseGlob", null);
|
|
3076
3946
|
__decorate([
|
|
3077
3947
|
IF_PROCEED(),
|
|
3078
3948
|
__metadata("design:type", Function),
|
|
3079
3949
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3080
3950
|
__metadata("design:returntype", void 0)
|
|
3081
|
-
],
|
|
3951
|
+
], StreamQuery.prototype, "notPreciseGlob", null);
|
|
3082
3952
|
__decorate([
|
|
3083
3953
|
IF_PROCEED(),
|
|
3084
3954
|
__metadata("design:type", Function),
|
|
3085
3955
|
__metadata("design:paramtypes", [Object, Array, Object]),
|
|
3086
3956
|
__metadata("design:returntype", void 0)
|
|
3087
|
-
],
|
|
3957
|
+
], StreamQuery.prototype, "in", null);
|
|
3088
3958
|
__decorate([
|
|
3089
3959
|
IF_PROCEED(),
|
|
3090
3960
|
__metadata("design:type", Function),
|
|
3091
3961
|
__metadata("design:paramtypes", [Object, Array, Object]),
|
|
3092
3962
|
__metadata("design:returntype", void 0)
|
|
3093
|
-
],
|
|
3963
|
+
], StreamQuery.prototype, "notIn", null);
|
|
3094
3964
|
__decorate([
|
|
3095
3965
|
IF_PROCEED(),
|
|
3096
3966
|
__metadata("design:type", Function),
|
|
3097
3967
|
__metadata("design:paramtypes", [Object]),
|
|
3098
3968
|
__metadata("design:returntype", void 0)
|
|
3099
|
-
],
|
|
3969
|
+
], StreamQuery.prototype, "isNULL", null);
|
|
3100
3970
|
__decorate([
|
|
3101
3971
|
IF_PROCEED(),
|
|
3102
3972
|
__metadata("design:type", Function),
|
|
3103
3973
|
__metadata("design:paramtypes", [Object]),
|
|
3104
3974
|
__metadata("design:returntype", void 0)
|
|
3105
|
-
],
|
|
3975
|
+
], StreamQuery.prototype, "isNotNULL", null);
|
|
3106
3976
|
__decorate([
|
|
3107
3977
|
IF_PROCEED(),
|
|
3108
3978
|
__metadata("design:type", Function),
|
|
3109
3979
|
__metadata("design:paramtypes", [Object, Object, Object, Object]),
|
|
3110
3980
|
__metadata("design:returntype", void 0)
|
|
3111
|
-
],
|
|
3981
|
+
], StreamQuery.prototype, "between", null);
|
|
3112
3982
|
__decorate([
|
|
3113
3983
|
IF_PROCEED(),
|
|
3114
3984
|
__metadata("design:type", Function),
|
|
3115
3985
|
__metadata("design:paramtypes", [Object, Object, Object, Object]),
|
|
3116
3986
|
__metadata("design:returntype", void 0)
|
|
3117
|
-
],
|
|
3987
|
+
], StreamQuery.prototype, "notBetween", null);
|
|
3118
3988
|
__decorate([
|
|
3119
3989
|
IF_PROCEED(),
|
|
3120
3990
|
__metadata("design:type", Function),
|
|
3121
3991
|
__metadata("design:paramtypes", [Object, Number, Object]),
|
|
3122
3992
|
__metadata("design:returntype", void 0)
|
|
3123
|
-
],
|
|
3993
|
+
], StreamQuery.prototype, "pow", null);
|
|
3124
3994
|
__decorate([
|
|
3125
3995
|
IF_PROCEED(),
|
|
3126
3996
|
__metadata("design:type", Function),
|
|
3127
3997
|
__metadata("design:paramtypes", [Object, Number, Object]),
|
|
3128
3998
|
__metadata("design:returntype", void 0)
|
|
3129
|
-
],
|
|
3999
|
+
], StreamQuery.prototype, "notPow", null);
|
|
3130
4000
|
__decorate([
|
|
3131
4001
|
IF_PROCEED(),
|
|
3132
4002
|
__metadata("design:type", Function),
|
|
3133
4003
|
__metadata("design:paramtypes", [Object, Array, Object]),
|
|
3134
4004
|
__metadata("design:returntype", void 0)
|
|
3135
|
-
],
|
|
4005
|
+
], StreamQuery.prototype, "powWith", null);
|
|
3136
4006
|
__decorate([
|
|
3137
4007
|
IF_PROCEED(),
|
|
3138
4008
|
__metadata("design:type", Function),
|
|
3139
4009
|
__metadata("design:paramtypes", [Object, Array, Object]),
|
|
3140
4010
|
__metadata("design:returntype", void 0)
|
|
3141
|
-
],
|
|
4011
|
+
], StreamQuery.prototype, "notPowWith", null);
|
|
3142
4012
|
__decorate([
|
|
3143
4013
|
IF_PROCEED(),
|
|
3144
4014
|
__metadata("design:type", Function),
|
|
3145
4015
|
__metadata("design:paramtypes", [String, Array, Object]),
|
|
3146
4016
|
__metadata("design:returntype", void 0)
|
|
3147
|
-
],
|
|
4017
|
+
], StreamQuery.prototype, "match", null);
|
|
3148
4018
|
__decorate([
|
|
3149
4019
|
IF_PROCEED(),
|
|
3150
4020
|
__metadata("design:type", Function),
|
|
3151
4021
|
__metadata("design:paramtypes", [String, Array, Object]),
|
|
3152
4022
|
__metadata("design:returntype", void 0)
|
|
3153
|
-
],
|
|
4023
|
+
], StreamQuery.prototype, "notMatch", null);
|
|
3154
4024
|
__decorate([
|
|
3155
4025
|
IF_PROCEED(),
|
|
3156
4026
|
__metadata("design:type", Function),
|
|
3157
4027
|
__metadata("design:paramtypes", [String, Array, Object]),
|
|
3158
4028
|
__metadata("design:returntype", void 0)
|
|
3159
|
-
],
|
|
4029
|
+
], StreamQuery.prototype, "matchBoolean", null);
|
|
3160
4030
|
__decorate([
|
|
3161
4031
|
IF_PROCEED(),
|
|
3162
4032
|
__metadata("design:type", Function),
|
|
3163
4033
|
__metadata("design:paramtypes", [String, Array, Object]),
|
|
3164
4034
|
__metadata("design:returntype", void 0)
|
|
3165
|
-
],
|
|
4035
|
+
], StreamQuery.prototype, "notMatchBoolean", null);
|
|
3166
4036
|
__decorate([
|
|
3167
4037
|
IF_PROCEED(),
|
|
3168
4038
|
__metadata("design:type", Function),
|
|
3169
4039
|
__metadata("design:paramtypes", [String, Array, Object]),
|
|
3170
4040
|
__metadata("design:returntype", void 0)
|
|
3171
|
-
],
|
|
4041
|
+
], StreamQuery.prototype, "matchQuery", null);
|
|
3172
4042
|
__decorate([
|
|
3173
4043
|
IF_PROCEED(),
|
|
3174
4044
|
__metadata("design:type", Function),
|
|
3175
4045
|
__metadata("design:paramtypes", [String, Array, Object]),
|
|
3176
4046
|
__metadata("design:returntype", void 0)
|
|
3177
|
-
],
|
|
4047
|
+
], StreamQuery.prototype, "notMatchQuery", null);
|
|
3178
4048
|
__decorate([
|
|
3179
4049
|
IF_PROCEED(),
|
|
3180
4050
|
__metadata("design:type", Function),
|
|
3181
4051
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3182
4052
|
__metadata("design:returntype", void 0)
|
|
3183
|
-
],
|
|
4053
|
+
], StreamQuery.prototype, "includes", null);
|
|
3184
4054
|
__decorate([
|
|
3185
4055
|
IF_PROCEED(),
|
|
3186
4056
|
__metadata("design:type", Function),
|
|
3187
4057
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3188
4058
|
__metadata("design:returntype", void 0)
|
|
3189
|
-
],
|
|
4059
|
+
], StreamQuery.prototype, "notIncludes", null);
|
|
3190
4060
|
__decorate([
|
|
3191
4061
|
IF_PROCEED(),
|
|
3192
4062
|
__metadata("design:type", Function),
|
|
3193
|
-
__metadata("design:paramtypes", [
|
|
4063
|
+
__metadata("design:paramtypes", [Object]),
|
|
3194
4064
|
__metadata("design:returntype", void 0)
|
|
3195
|
-
],
|
|
4065
|
+
], StreamQuery.prototype, "and", null);
|
|
3196
4066
|
__decorate([
|
|
3197
4067
|
IF_PROCEED(),
|
|
3198
4068
|
__metadata("design:type", Function),
|
|
3199
|
-
__metadata("design:paramtypes", [
|
|
4069
|
+
__metadata("design:paramtypes", [Object]),
|
|
3200
4070
|
__metadata("design:returntype", void 0)
|
|
3201
|
-
],
|
|
3202
|
-
class StreamBuild extends StreamCondition {
|
|
3203
|
-
constructor(table) {
|
|
3204
|
-
super();
|
|
3205
|
-
this._distinct = false;
|
|
3206
|
-
this._columns = [];
|
|
3207
|
-
this._updateColumns = [];
|
|
3208
|
-
this._groups = [];
|
|
3209
|
-
this._orders = [];
|
|
3210
|
-
this._startRow = 0;
|
|
3211
|
-
this._pageSize = 0;
|
|
3212
|
-
this._table = table;
|
|
3213
|
-
}
|
|
3214
|
-
/** 将当前stream重置 */
|
|
3215
|
-
reset() {
|
|
3216
|
-
super.reset();
|
|
3217
|
-
this._pageSize = 0;
|
|
3218
|
-
this._startRow = 0;
|
|
3219
|
-
this._orders.length = 0;
|
|
3220
|
-
this._groups.length = 0;
|
|
3221
|
-
this._columns.length = 0;
|
|
3222
|
-
this._updateColumns.length = 0;
|
|
3223
|
-
return this;
|
|
3224
|
-
}
|
|
3225
|
-
groupBy(key) { this._groups.push(key); return this; }
|
|
3226
|
-
asc(...keys) { this._orders.push(...keys.map(key => `${String(key)} ASC`)); return this; }
|
|
3227
|
-
desc(...keys) { this._orders.push(...keys.map(key => `${String(key)} DESC`)); return this; }
|
|
3228
|
-
limit(startRow, pageSize) { this._startRow = startRow; this._pageSize = pageSize; return this; }
|
|
3229
|
-
page(pageNumber, pageSize) { this._startRow = ((pageNumber || 1) - 1) * pageSize; this._pageSize = pageSize; return this; }
|
|
3230
|
-
table(_table) { this._table = _table; return this; }
|
|
3231
|
-
distinct(on = true) { this._distinct = on; return this; }
|
|
3232
|
-
count(key, countName, distinct) { this._columns.push(`COUNT(${distinct ? 'DISTINCT' : ''} ${String(key)}) ${countName || `${String(key)}`}`); return this; }
|
|
3233
|
-
sum(key, legName, distinct) { this._columns.push(`SUM(${distinct ? 'DISTINCT' : ''} ${String(key)}) ${legName || `${String(key)}`}`); return this; }
|
|
3234
|
-
avg(key, legName, distinct) { this._columns.push(`AVG(${distinct ? 'DISTINCT' : ''} ${String(key)}) ${legName || `${String(key)}`}`); return this; }
|
|
3235
|
-
max(key, legName, distinct) { this._columns.push(`MAX(${distinct ? 'DISTINCT' : ''} ${String(key)}) ${legName || `${String(key)}`}`); return this; }
|
|
3236
|
-
min(key, legName, distinct) { this._columns.push(`MIN(${distinct ? 'DISTINCT' : ''} ${String(key)}) ${legName || `${String(key)}`}`); return this; }
|
|
3237
|
-
groupConcat(key, param) {
|
|
3238
|
-
this._columns.push(`GROUP_CONCAT(
|
|
3239
|
-
${param && param.distinct ? 'DISTINCT' : ''} ${String(key)}
|
|
3240
|
-
${param && param.asc && param.asc.length > 0 ? `ORDER BY ${param.asc.map(i => `${String(i)} ASC`)} ` : ''}
|
|
3241
|
-
${param && param.desc && param.desc.length > 0 ? `${param && param.asc && param.asc.length > 0 ? '' : 'ORDER BY'} ${param.desc.map(i => `${String(i)} DESC`)} ` : ''}
|
|
3242
|
-
SEPARATOR '${param && param.separator || ','}'
|
|
3243
|
-
) ${param && param.groupName || `${String(key)}`}`);
|
|
3244
|
-
return this;
|
|
3245
|
-
}
|
|
3246
|
-
selectColumn(...key) { this._columns.push(...(key.map(k => k))); return this; }
|
|
3247
|
-
updateColumn(key, value) { this._updates ?? (this._updates = {}); this._updates[key] = value; return this; }
|
|
3248
|
-
updateT(t) { this._updates ?? (this._updates = {}); Object.assign(this._updates, t); return this; }
|
|
3249
|
-
replace(key, valueToFind, valueToReplace) {
|
|
3250
|
-
const [pkey1, pkey2] = [`p${this._prefix}${this._index++}`, `p${this._prefix}${this._index++}`];
|
|
3251
|
-
this._updateColumns.push(` ${String(key)} = REPLACE(${String(key)}, :${pkey1}, :${pkey2}) `);
|
|
3252
|
-
this._param[pkey1] = valueToFind;
|
|
3253
|
-
this._param[pkey2] = valueToReplace;
|
|
3254
|
-
return this;
|
|
3255
|
-
}
|
|
3256
|
-
}
|
|
4071
|
+
], StreamQuery.prototype, "or", null);
|
|
3257
4072
|
__decorate([
|
|
3258
4073
|
IF_PROCEED(),
|
|
3259
4074
|
__metadata("design:type", Function),
|
|
3260
4075
|
__metadata("design:paramtypes", [Object]),
|
|
3261
4076
|
__metadata("design:returntype", void 0)
|
|
3262
|
-
],
|
|
4077
|
+
], StreamQuery.prototype, "groupBy", null);
|
|
4078
|
+
__decorate([
|
|
4079
|
+
IF_PROCEED(),
|
|
4080
|
+
__metadata("design:type", Function),
|
|
4081
|
+
__metadata("design:paramtypes", [String]),
|
|
4082
|
+
__metadata("design:returntype", void 0)
|
|
4083
|
+
], StreamQuery.prototype, "groupBy2", null);
|
|
3263
4084
|
__decorate([
|
|
3264
4085
|
IF_PROCEED(),
|
|
3265
4086
|
__metadata("design:type", Function),
|
|
3266
4087
|
__metadata("design:paramtypes", [Object]),
|
|
3267
4088
|
__metadata("design:returntype", void 0)
|
|
3268
|
-
],
|
|
4089
|
+
], StreamQuery.prototype, "asc", null);
|
|
4090
|
+
__decorate([
|
|
4091
|
+
IF_PROCEED(),
|
|
4092
|
+
__metadata("design:type", Function),
|
|
4093
|
+
__metadata("design:paramtypes", [String]),
|
|
4094
|
+
__metadata("design:returntype", void 0)
|
|
4095
|
+
], StreamQuery.prototype, "asc2", null);
|
|
3269
4096
|
__decorate([
|
|
3270
4097
|
IF_PROCEED(),
|
|
3271
4098
|
__metadata("design:type", Function),
|
|
3272
4099
|
__metadata("design:paramtypes", [Object]),
|
|
3273
4100
|
__metadata("design:returntype", void 0)
|
|
3274
|
-
],
|
|
4101
|
+
], StreamQuery.prototype, "desc", null);
|
|
4102
|
+
__decorate([
|
|
4103
|
+
IF_PROCEED(),
|
|
4104
|
+
__metadata("design:type", Function),
|
|
4105
|
+
__metadata("design:paramtypes", [String]),
|
|
4106
|
+
__metadata("design:returntype", void 0)
|
|
4107
|
+
], StreamQuery.prototype, "desc2", null);
|
|
3275
4108
|
__decorate([
|
|
3276
4109
|
IF_PROCEED(),
|
|
3277
4110
|
__metadata("design:type", Function),
|
|
3278
4111
|
__metadata("design:paramtypes", [Number, Number]),
|
|
3279
4112
|
__metadata("design:returntype", void 0)
|
|
3280
|
-
],
|
|
4113
|
+
], StreamQuery.prototype, "limit", null);
|
|
3281
4114
|
__decorate([
|
|
3282
4115
|
IF_PROCEED(),
|
|
3283
4116
|
__metadata("design:type", Function),
|
|
3284
4117
|
__metadata("design:paramtypes", [Number, Number]),
|
|
3285
4118
|
__metadata("design:returntype", void 0)
|
|
3286
|
-
],
|
|
4119
|
+
], StreamQuery.prototype, "page", null);
|
|
3287
4120
|
__decorate([
|
|
3288
4121
|
IF_PROCEED(),
|
|
3289
4122
|
__metadata("design:type", Function),
|
|
3290
4123
|
__metadata("design:paramtypes", [String]),
|
|
3291
4124
|
__metadata("design:returntype", void 0)
|
|
3292
|
-
],
|
|
4125
|
+
], StreamQuery.prototype, "table", null);
|
|
3293
4126
|
__decorate([
|
|
3294
4127
|
IF_PROCEED(),
|
|
3295
4128
|
__metadata("design:type", Function),
|
|
3296
4129
|
__metadata("design:paramtypes", [Object]),
|
|
3297
4130
|
__metadata("design:returntype", void 0)
|
|
3298
|
-
],
|
|
4131
|
+
], StreamQuery.prototype, "distinct", null);
|
|
3299
4132
|
__decorate([
|
|
3300
4133
|
IF_PROCEED(),
|
|
3301
4134
|
__metadata("design:type", Function),
|
|
3302
|
-
__metadata("design:paramtypes", [Object, String
|
|
4135
|
+
__metadata("design:paramtypes", [Object, String]),
|
|
4136
|
+
__metadata("design:returntype", void 0)
|
|
4137
|
+
], StreamQuery.prototype, "countDistinct", null);
|
|
4138
|
+
__decorate([
|
|
4139
|
+
IF_PROCEED(),
|
|
4140
|
+
__metadata("design:type", Function),
|
|
4141
|
+
__metadata("design:paramtypes", [String]),
|
|
3303
4142
|
__metadata("design:returntype", void 0)
|
|
3304
|
-
],
|
|
4143
|
+
], StreamQuery.prototype, "count", null);
|
|
3305
4144
|
__decorate([
|
|
3306
4145
|
IF_PROCEED(),
|
|
3307
4146
|
__metadata("design:type", Function),
|
|
3308
4147
|
__metadata("design:paramtypes", [Object, String, Boolean]),
|
|
3309
4148
|
__metadata("design:returntype", void 0)
|
|
3310
|
-
],
|
|
4149
|
+
], StreamQuery.prototype, "sum", null);
|
|
3311
4150
|
__decorate([
|
|
3312
4151
|
IF_PROCEED(),
|
|
3313
4152
|
__metadata("design:type", Function),
|
|
3314
4153
|
__metadata("design:paramtypes", [Object, String, Boolean]),
|
|
3315
4154
|
__metadata("design:returntype", void 0)
|
|
3316
|
-
],
|
|
4155
|
+
], StreamQuery.prototype, "avg", null);
|
|
3317
4156
|
__decorate([
|
|
3318
4157
|
IF_PROCEED(),
|
|
3319
4158
|
__metadata("design:type", Function),
|
|
3320
4159
|
__metadata("design:paramtypes", [Object, String, Boolean]),
|
|
3321
4160
|
__metadata("design:returntype", void 0)
|
|
3322
|
-
],
|
|
4161
|
+
], StreamQuery.prototype, "max", null);
|
|
3323
4162
|
__decorate([
|
|
3324
4163
|
IF_PROCEED(),
|
|
3325
4164
|
__metadata("design:type", Function),
|
|
3326
4165
|
__metadata("design:paramtypes", [Object, String, Boolean]),
|
|
3327
4166
|
__metadata("design:returntype", void 0)
|
|
3328
|
-
],
|
|
4167
|
+
], StreamQuery.prototype, "min", null);
|
|
3329
4168
|
__decorate([
|
|
3330
4169
|
IF_PROCEED(),
|
|
3331
4170
|
__metadata("design:type", Function),
|
|
3332
4171
|
__metadata("design:paramtypes", [Object, Object]),
|
|
3333
4172
|
__metadata("design:returntype", Object)
|
|
3334
|
-
],
|
|
4173
|
+
], StreamQuery.prototype, "groupConcat", null);
|
|
3335
4174
|
__decorate([
|
|
3336
4175
|
IF_PROCEED(),
|
|
3337
4176
|
__metadata("design:type", Function),
|
|
3338
4177
|
__metadata("design:paramtypes", [Object]),
|
|
3339
4178
|
__metadata("design:returntype", void 0)
|
|
3340
|
-
],
|
|
4179
|
+
], StreamQuery.prototype, "select", null);
|
|
4180
|
+
__decorate([
|
|
4181
|
+
IF_PROCEED(),
|
|
4182
|
+
__metadata("design:type", Function),
|
|
4183
|
+
__metadata("design:paramtypes", [String, Object]),
|
|
4184
|
+
__metadata("design:returntype", void 0)
|
|
4185
|
+
], StreamQuery.prototype, "select2", null);
|
|
3341
4186
|
__decorate([
|
|
3342
4187
|
IF_PROCEED(),
|
|
3343
4188
|
__metadata("design:type", Function),
|
|
3344
4189
|
__metadata("design:paramtypes", [Object, Object]),
|
|
3345
4190
|
__metadata("design:returntype", void 0)
|
|
3346
|
-
],
|
|
4191
|
+
], StreamQuery.prototype, "update", null);
|
|
4192
|
+
__decorate([
|
|
4193
|
+
IF_PROCEED(),
|
|
4194
|
+
__metadata("design:type", Function),
|
|
4195
|
+
__metadata("design:paramtypes", [String, Object]),
|
|
4196
|
+
__metadata("design:returntype", void 0)
|
|
4197
|
+
], StreamQuery.prototype, "update2", null);
|
|
3347
4198
|
__decorate([
|
|
3348
4199
|
IF_PROCEED(),
|
|
3349
4200
|
__metadata("design:type", Function),
|
|
3350
4201
|
__metadata("design:paramtypes", [Object]),
|
|
3351
4202
|
__metadata("design:returntype", void 0)
|
|
3352
|
-
],
|
|
4203
|
+
], StreamQuery.prototype, "updateT", null);
|
|
3353
4204
|
__decorate([
|
|
3354
4205
|
IF_PROCEED(),
|
|
3355
4206
|
__metadata("design:type", Function),
|
|
3356
4207
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
3357
4208
|
__metadata("design:returntype", void 0)
|
|
3358
|
-
],
|
|
3359
|
-
class StreamQuery extends StreamBuild {
|
|
3360
|
-
constructor(table, service) {
|
|
3361
|
-
super(table);
|
|
3362
|
-
this._service = service;
|
|
3363
|
-
}
|
|
3364
|
-
select(option) {
|
|
3365
|
-
option.sync ?? (option.sync = SyncMode.Async);
|
|
3366
|
-
const { where, params } = this.where();
|
|
3367
|
-
const sql = `
|
|
3368
|
-
SELECT
|
|
3369
|
-
${this._columns && this._columns.length > 0 ? this._columns.join(',') : '*'}
|
|
3370
|
-
FROM ${this._table}
|
|
3371
|
-
${where ? ' WHERE ' : ''}
|
|
3372
|
-
${where}`;
|
|
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 });
|
|
3381
|
-
}
|
|
3382
|
-
}
|
|
3383
|
-
else {
|
|
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 });
|
|
3391
|
-
}
|
|
3392
|
-
}
|
|
3393
|
-
}
|
|
3394
|
-
update(option) {
|
|
3395
|
-
option.sync ?? (option.sync = SyncMode.Async);
|
|
3396
|
-
const { where, params } = this.where();
|
|
3397
|
-
const sets = new Array(...this._updateColumns);
|
|
3398
|
-
if (this._updates) {
|
|
3399
|
-
for (const [K, V] of Object.entries(this._updates)) {
|
|
3400
|
-
const pkey = `p${this._prefix}${this._index++}`;
|
|
3401
|
-
sets.push(` ${K} = :${pkey} `);
|
|
3402
|
-
params[pkey] = V;
|
|
3403
|
-
}
|
|
3404
|
-
}
|
|
3405
|
-
if (sets.length > 0) {
|
|
3406
|
-
const sql = `UPDATE ${this._table} SET ${sets.join(',')} ${where}`;
|
|
3407
|
-
if (option.sync === SyncMode.Async) {
|
|
3408
|
-
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3409
|
-
}
|
|
3410
|
-
else {
|
|
3411
|
-
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
3412
|
-
}
|
|
3413
|
-
}
|
|
3414
|
-
else {
|
|
3415
|
-
return 0;
|
|
3416
|
-
}
|
|
3417
|
-
}
|
|
3418
|
-
delete(option) {
|
|
3419
|
-
option.sync ?? (option.sync = SyncMode.Async);
|
|
3420
|
-
const { where, params } = this.where();
|
|
3421
|
-
const sql = `DELETE FROM ${this._table} ${where}`;
|
|
3422
|
-
if (option.sync === SyncMode.Async) {
|
|
3423
|
-
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3424
|
-
}
|
|
3425
|
-
else {
|
|
3426
|
-
return this._service.excute({ sync: SyncMode.Sync, sql, params });
|
|
3427
|
-
}
|
|
3428
|
-
}
|
|
3429
|
-
}
|
|
4209
|
+
], StreamQuery.prototype, "replace", null);
|
|
3430
4210
|
__decorate([
|
|
3431
4211
|
IF_EXEC(null),
|
|
3432
4212
|
__metadata("design:type", Function),
|
|
3433
4213
|
__metadata("design:paramtypes", [Object]),
|
|
3434
4214
|
__metadata("design:returntype", Object)
|
|
3435
|
-
], StreamQuery.prototype, "
|
|
4215
|
+
], StreamQuery.prototype, "excuteSelect", null);
|
|
4216
|
+
__decorate([
|
|
4217
|
+
IF_EXEC(null),
|
|
4218
|
+
__metadata("design:type", Function),
|
|
4219
|
+
__metadata("design:paramtypes", [Object]),
|
|
4220
|
+
__metadata("design:returntype", Object)
|
|
4221
|
+
], StreamQuery.prototype, "excutePage", null);
|
|
3436
4222
|
__decorate([
|
|
3437
4223
|
IF_EXEC(0),
|
|
3438
4224
|
__metadata("design:type", Function),
|
|
3439
4225
|
__metadata("design:paramtypes", [Object]),
|
|
3440
4226
|
__metadata("design:returntype", Object)
|
|
3441
|
-
], StreamQuery.prototype, "
|
|
4227
|
+
], StreamQuery.prototype, "excuteUpdate", null);
|
|
3442
4228
|
__decorate([
|
|
3443
4229
|
IF_EXEC(0),
|
|
3444
4230
|
__metadata("design:type", Function),
|
|
3445
4231
|
__metadata("design:paramtypes", [Object]),
|
|
3446
4232
|
__metadata("design:returntype", Object)
|
|
3447
|
-
], StreamQuery.prototype, "
|
|
4233
|
+
], StreamQuery.prototype, "excuteDelete", null);
|
|
3448
4234
|
/**
|
|
3449
4235
|
获取REDIS客户端,
|
|
3450
4236
|
# [查看库的API](https://github.com/redis/ioredis?tab=readme-ov-file)
|
|
3451
4237
|
# [REDIS API](http://doc.redisfans.com/)
|
|
3452
4238
|
REDIS 的API 可以直接用,将方法名转为小写
|
|
4239
|
+
```
|
|
4240
|
+
// 设置<Redis>来获得代码提示
|
|
4241
|
+
getRedisDB<Redis>('').exists(?)
|
|
4242
|
+
```
|
|
3453
4243
|
*/
|
|
3454
4244
|
export function getRedisDB(db) {
|
|
3455
4245
|
const rd = globalThis[_dao][DBType.Redis][db ?? _primaryDB];
|
|
@@ -3648,6 +4438,8 @@ export function MethodCache(config) {
|
|
|
3648
4438
|
class MUParser {
|
|
3649
4439
|
constructor(modelName, file) {
|
|
3650
4440
|
this.linNumber = 0;
|
|
4441
|
+
this.lastLine = '';
|
|
4442
|
+
this.lastlastLine = '';
|
|
3651
4443
|
this.status = 0;
|
|
3652
4444
|
this.lineSeparator = '\n';
|
|
3653
4445
|
this.modelName = modelName;
|
|
@@ -3656,7 +4448,7 @@ class MUParser {
|
|
|
3656
4448
|
}
|
|
3657
4449
|
next() {
|
|
3658
4450
|
let sqlId = this.readSqlId();
|
|
3659
|
-
if (this.status === MUParser.END
|
|
4451
|
+
if (this.status === MUParser.END) {
|
|
3660
4452
|
return null;
|
|
3661
4453
|
}
|
|
3662
4454
|
// 去掉可能的尾部空格
|
|
@@ -3671,9 +4463,6 @@ class MUParser {
|
|
|
3671
4463
|
skipHeader() {
|
|
3672
4464
|
while (true) {
|
|
3673
4465
|
const line = this.nextLine();
|
|
3674
|
-
if (!line) {
|
|
3675
|
-
return;
|
|
3676
|
-
}
|
|
3677
4466
|
if (this.status === MUParser.END) {
|
|
3678
4467
|
return;
|
|
3679
4468
|
}
|
|
@@ -3700,7 +4489,7 @@ class MUParser {
|
|
|
3700
4489
|
let findComment = false;
|
|
3701
4490
|
while (true) {
|
|
3702
4491
|
let line = this.nextLine();
|
|
3703
|
-
if (this.status === MUParser.END
|
|
4492
|
+
if (this.status === MUParser.END) {
|
|
3704
4493
|
return;
|
|
3705
4494
|
}
|
|
3706
4495
|
line = line.trim();
|
|
@@ -3729,28 +4518,18 @@ class MUParser {
|
|
|
3729
4518
|
}
|
|
3730
4519
|
readSql() {
|
|
3731
4520
|
const list = [];
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
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
|
-
}
|
|
4521
|
+
list.push(this.lastLine);
|
|
4522
|
+
while (true) {
|
|
4523
|
+
const line = this.nextLine();
|
|
4524
|
+
if (this.status === MUParser.END) {
|
|
4525
|
+
return this.getBuildSql(list);
|
|
3750
4526
|
}
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
4527
|
+
if (line.startsWith('===')) {
|
|
4528
|
+
// 删除下一个sqlId表示
|
|
4529
|
+
list.pop();
|
|
4530
|
+
return this.getBuildSql(list);
|
|
4531
|
+
}
|
|
4532
|
+
list.push(line);
|
|
3754
4533
|
}
|
|
3755
4534
|
}
|
|
3756
4535
|
getBuildSql(list) {
|
|
@@ -3767,126 +4546,3 @@ class MUParser {
|
|
|
3767
4546
|
}
|
|
3768
4547
|
}
|
|
3769
4548
|
MUParser.END = 1;
|
|
3770
|
-
export const Boot = async function (options) {
|
|
3771
|
-
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption, options);
|
|
3772
|
-
if (options.sqlDir) {
|
|
3773
|
-
globalThis[_path] = await import('path');
|
|
3774
|
-
globalThis[_fs] = await import('fs');
|
|
3775
|
-
}
|
|
3776
|
-
logger.level = options.log ?? 'info';
|
|
3777
|
-
globalThis[_sqlCache] = new SqlCache();
|
|
3778
|
-
if (options.sqlMap || options.sqlDir) {
|
|
3779
|
-
await globalThis[_sqlCache].init(options);
|
|
3780
|
-
}
|
|
3781
|
-
globalThis[_dao] = {
|
|
3782
|
-
[DBType.Mongo]: {},
|
|
3783
|
-
[DBType.Mysql]: {},
|
|
3784
|
-
[DBType.Sqlite]: {},
|
|
3785
|
-
[DBType.SqliteRemote]: {},
|
|
3786
|
-
[DBType.Redis]: {}
|
|
3787
|
-
};
|
|
3788
|
-
if (options.Mysql) {
|
|
3789
|
-
const { createPool } = await import('mysql2/promise');
|
|
3790
|
-
if (options.Mysql['host']) {
|
|
3791
|
-
globalThis[_dao][DBType.Mysql][_primaryDB] = new Mysql(createPool({
|
|
3792
|
-
...options.Mysql,
|
|
3793
|
-
multipleStatements: true,
|
|
3794
|
-
decimalNumbers: true,
|
|
3795
|
-
supportBigNumbers: true
|
|
3796
|
-
}));
|
|
3797
|
-
}
|
|
3798
|
-
else {
|
|
3799
|
-
let flag = false;
|
|
3800
|
-
for (const [key, option] of Object.entries(options.Mysql)) {
|
|
3801
|
-
const db = new Mysql(createPool({
|
|
3802
|
-
...option,
|
|
3803
|
-
multipleStatements: true,
|
|
3804
|
-
decimalNumbers: true,
|
|
3805
|
-
supportBigNumbers: true
|
|
3806
|
-
}));
|
|
3807
|
-
if (flag === false) {
|
|
3808
|
-
globalThis[_dao][DBType.Mysql][_primaryDB] = db;
|
|
3809
|
-
flag = true;
|
|
3810
|
-
}
|
|
3811
|
-
globalThis[_dao][DBType.Mysql][key] = db;
|
|
3812
|
-
}
|
|
3813
|
-
}
|
|
3814
|
-
}
|
|
3815
|
-
if (options.Sqlite) {
|
|
3816
|
-
const BetterSqlite3 = await import('better-sqlite3');
|
|
3817
|
-
if (typeof options.Sqlite === 'string') {
|
|
3818
|
-
globalThis[_dao][DBType.Sqlite][_primaryDB] = new Sqlite(new BetterSqlite3.default(options.Sqlite, { fileMustExist: false }));
|
|
3819
|
-
}
|
|
3820
|
-
else {
|
|
3821
|
-
let flag = false;
|
|
3822
|
-
for (const [key, fileName] of Object.entries(options.Sqlite)) {
|
|
3823
|
-
const db = new Sqlite(new BetterSqlite3.default(fileName, { fileMustExist: false }));
|
|
3824
|
-
if (flag === false) {
|
|
3825
|
-
globalThis[_dao][DBType.Sqlite][_primaryDB] = db;
|
|
3826
|
-
flag = true;
|
|
3827
|
-
}
|
|
3828
|
-
globalThis[_dao][DBType.Sqlite][key] = db;
|
|
3829
|
-
}
|
|
3830
|
-
}
|
|
3831
|
-
}
|
|
3832
|
-
if (options.SqliteRemote) {
|
|
3833
|
-
if (typeof options.SqliteRemote.db === 'string') {
|
|
3834
|
-
await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
|
|
3835
|
-
globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
|
|
3836
|
-
}
|
|
3837
|
-
else {
|
|
3838
|
-
let flag = false;
|
|
3839
|
-
for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
|
|
3840
|
-
await options.SqliteRemote.service.initDB(fileName);
|
|
3841
|
-
const db = new SqliteRemote(options.SqliteRemote.service, fileName);
|
|
3842
|
-
if (flag === false) {
|
|
3843
|
-
globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
|
|
3844
|
-
flag = true;
|
|
3845
|
-
}
|
|
3846
|
-
globalThis[_dao][DBType.SqliteRemote][key] = db;
|
|
3847
|
-
}
|
|
3848
|
-
}
|
|
3849
|
-
}
|
|
3850
|
-
if (options.Redis) {
|
|
3851
|
-
const { Redis } = await import('ioredis');
|
|
3852
|
-
if (options.Redis['host']) {
|
|
3853
|
-
globalThis[_dao][DBType.Redis][_primaryDB] = new Redis(options.Redis);
|
|
3854
|
-
}
|
|
3855
|
-
else {
|
|
3856
|
-
let flag = false;
|
|
3857
|
-
for (const [key, option] of Object.entries(options.Redis)) {
|
|
3858
|
-
const db = new Redis(option);
|
|
3859
|
-
if (flag === false) {
|
|
3860
|
-
globalThis[_dao][DBType.Redis][_primaryDB] = db;
|
|
3861
|
-
flag = true;
|
|
3862
|
-
}
|
|
3863
|
-
globalThis[_dao][DBType.Redis][key] = db;
|
|
3864
|
-
}
|
|
3865
|
-
}
|
|
3866
|
-
const clients = Object.values(globalThis[_dao][DBType.Redis]);
|
|
3867
|
-
const Redlock = await import('redlock');
|
|
3868
|
-
globalThis[_dao][DBType.RedisLock] = new Redlock.default(clients, {
|
|
3869
|
-
// The expected clock drift; for more details see:
|
|
3870
|
-
// http://redis.io/topics/distlock
|
|
3871
|
-
driftFactor: 0.01, // multiplied by lock ttl to determine drift time
|
|
3872
|
-
// The max number of times Redlock will attempt to lock a resource
|
|
3873
|
-
// before erroring.
|
|
3874
|
-
retryCount: 10,
|
|
3875
|
-
// the time in ms between attempts
|
|
3876
|
-
retryDelay: 200, // time in ms
|
|
3877
|
-
// the max time in ms randomly added to retries
|
|
3878
|
-
// to improve performance under high contention
|
|
3879
|
-
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
|
|
3880
|
-
retryJitter: 200, // time in ms
|
|
3881
|
-
// The minimum remaining time on a lock before an extension is automatically
|
|
3882
|
-
// attempted with the `using` API.
|
|
3883
|
-
automaticExtensionThreshold: 500, // time in ms
|
|
3884
|
-
});
|
|
3885
|
-
const { EventEmitter } = await import('events');
|
|
3886
|
-
const event = new EventEmitter({ captureRejections: true });
|
|
3887
|
-
event.on('error', error => {
|
|
3888
|
-
logger.error('event-bus', error);
|
|
3889
|
-
});
|
|
3890
|
-
globalThis[_EventBus] = event;
|
|
3891
|
-
}
|
|
3892
|
-
};
|