baja-lite 1.1.2 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +7 -0
- package/.eslintrc.cjs +89 -0
- package/.prettierrc +4 -0
- package/ci.js +29 -0
- package/package-cjs.json +17 -0
- package/package.json +7 -7
- package/{boot-remote.js → src/boot-remote.ts} +7 -6
- package/{boot.js → src/boot.ts} +39 -32
- package/{code.js → src/code.ts} +67 -64
- package/{convert-xml.js → src/convert-xml.ts} +155 -105
- package/src/enum.ts +71 -0
- package/src/error.ts +11 -0
- package/src/fn.ts +295 -0
- package/{index.d.ts → src/index.ts} +11 -11
- package/{list.js → src/list.ts} +9 -8
- package/src/math.ts +405 -0
- package/src/object.ts +247 -0
- package/src/set-ex.ts +374 -0
- package/src/sql.ts +5281 -0
- package/{sqlite.js → src/sqlite.ts} +52 -53
- package/src/string.ts +111 -0
- package/{test-mysql.js → src/test-mysql.ts} +126 -135
- package/src/test-postgresql.ts +79 -0
- package/{test-sqlite.js → src/test-sqlite.ts} +80 -89
- package/{test-xml.js → src/test-xml.ts} +1 -1
- package/src/test.ts +2 -0
- package/src/wx/base.ts +76 -0
- package/src/wx/mini.ts +133 -0
- package/src/wx/organ.ts +290 -0
- package/{wx/types.d.ts → src/wx/types.ts} +10 -21
- package/src/wx.ts +3 -0
- package/test.json +0 -0
- package/tsconfig.base.json +80 -0
- package/tsconfig.cjs.json +42 -0
- package/tsconfig.json +44 -0
- package/xml/event-report.xml +13 -0
- package/yarn.lock +1493 -0
- package/boot-remote.d.ts +0 -2
- package/boot.d.ts +0 -2
- package/code.d.ts +0 -2
- package/convert-xml.d.ts +0 -10
- package/enum.d.ts +0 -18
- package/enum.js +0 -59
- package/error.d.ts +0 -5
- package/error.js +0 -13
- package/fn.d.ts +0 -128
- package/fn.js +0 -172
- package/index.js +0 -11
- package/list.d.ts +0 -10
- package/math.d.ts +0 -83
- package/math.js +0 -451
- package/object.d.ts +0 -83
- package/object.js +0 -222
- package/set-ex.d.ts +0 -198
- package/set-ex.js +0 -338
- package/sql.d.ts +0 -1858
- package/sql.js +0 -5025
- package/sqlite.d.ts +0 -32
- package/string.d.ts +0 -17
- package/string.js +0 -105
- package/test-mysql.d.ts +0 -2
- package/test-postgresql.d.ts +0 -2
- package/test-postgresql.js +0 -90
- package/test-sqlite.d.ts +0 -1
- package/test-xml.d.ts +0 -1
- package/test.d.ts +0 -1
- package/test.js +0 -2
- package/wx/base.d.ts +0 -11
- package/wx/base.js +0 -78
- package/wx/index.d.ts +0 -3
- package/wx/index.js +0 -3
- package/wx/mini.d.ts +0 -45
- package/wx/mini.js +0 -102
- package/wx/organ.d.ts +0 -65
- package/wx/organ.js +0 -171
- package/wx/types.js +0 -1
- /package/{README.md → Readme.md} +0 -0
|
@@ -1,30 +1,39 @@
|
|
|
1
1
|
import LGet from 'lodash.get';
|
|
2
|
-
export
|
|
2
|
+
export interface XML {
|
|
3
|
+
type: 'tag' | 'text';
|
|
4
|
+
name: string;
|
|
5
|
+
id?: string;
|
|
6
|
+
voidElement: boolean;
|
|
7
|
+
attrs: Record<string, string>;
|
|
8
|
+
children: XML[];
|
|
9
|
+
content: string;
|
|
10
|
+
}
|
|
11
|
+
export const convert = function (childrens: XML[], param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
3
12
|
let statement = '';
|
|
4
|
-
for (let i = 0, children; children = childrens[i]
|
|
13
|
+
for (let i = 0, children: XML; children = childrens[i]!; i++) {
|
|
5
14
|
// Convert SQL statement recursively
|
|
6
15
|
statement += convertChildren(children, param, parentIds, myBatisMapper);
|
|
7
16
|
}
|
|
8
17
|
// Check not converted Parameters
|
|
9
18
|
const regexList = ['\\#{\\S*}', '\\${\\S*}'];
|
|
10
|
-
for (let i = 0, regexString; regexString = regexList[i]
|
|
19
|
+
for (let i = 0, regexString: string; regexString = regexList[i]!; i++) {
|
|
11
20
|
var checkParam = statement.match(regexString);
|
|
12
21
|
if (checkParam != null && checkParam.length > 0) {
|
|
13
22
|
throw new Error("Parameter " + checkParam.join(",") + " is not converted.");
|
|
14
23
|
}
|
|
15
24
|
}
|
|
16
25
|
return statement;
|
|
17
|
-
}
|
|
18
|
-
const convertChildren = function (children, param, parentIds, myBatisMapper) {
|
|
19
|
-
param
|
|
26
|
+
}
|
|
27
|
+
const convertChildren = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
28
|
+
param ??= {};
|
|
20
29
|
if (!isDict(param)) {
|
|
21
30
|
throw new Error('Parameter argument should be Key-Value type or Null.');
|
|
22
31
|
}
|
|
23
32
|
if (children.type == 'text') {
|
|
24
33
|
// Convert Parameters
|
|
25
34
|
return convertParameters(children, param);
|
|
26
|
-
|
|
27
|
-
else if (children.type == 'tag') {
|
|
35
|
+
|
|
36
|
+
} else if (children.type == 'tag') {
|
|
28
37
|
switch (children.name.toLowerCase()) {
|
|
29
38
|
case 'if':
|
|
30
39
|
return convertIf(children, param, parentIds, myBatisMapper);
|
|
@@ -45,69 +54,74 @@ const convertChildren = function (children, param, parentIds, myBatisMapper) {
|
|
|
45
54
|
default:
|
|
46
55
|
throw new Error('XML is not well-formed character or markup. Consider using CDATA section.');
|
|
47
56
|
}
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
57
|
+
} else {
|
|
50
58
|
return '';
|
|
51
59
|
}
|
|
52
|
-
}
|
|
53
|
-
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const convertParameters = function (children: XML, param: Record<string, any>) {
|
|
54
63
|
let convertString = children.content;
|
|
64
|
+
|
|
55
65
|
try {
|
|
56
66
|
convertString = convertParametersInner('#', convertString, param);
|
|
57
67
|
convertString = convertParametersInner('$', convertString, param);
|
|
58
|
-
}
|
|
59
|
-
catch (err) {
|
|
68
|
+
} catch (err) {
|
|
60
69
|
throw new Error('Error occurred during convert parameters.');
|
|
61
70
|
}
|
|
71
|
+
|
|
62
72
|
try {
|
|
63
73
|
// convert CDATA string
|
|
64
74
|
convertString = convertString.replace(/(\&\;)/g, '&');
|
|
65
75
|
convertString = convertString.replace(/(\<\;)/g, '<');
|
|
66
76
|
convertString = convertString.replace(/(\>\;)/g, '>');
|
|
67
77
|
convertString = convertString.replace(/(\"\;)/g, '"');
|
|
68
|
-
}
|
|
69
|
-
catch (err) {
|
|
78
|
+
} catch (err) {
|
|
70
79
|
throw new Error('Error occurred during convert CDATA section.');
|
|
71
80
|
}
|
|
81
|
+
|
|
72
82
|
return convertString;
|
|
73
|
-
}
|
|
74
|
-
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const isObject = function (variable: any) {
|
|
75
86
|
return typeof variable === 'object' && variable !== null;
|
|
76
|
-
}
|
|
77
|
-
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const isArray = function (variable: any) {
|
|
78
90
|
return isObject(variable) && variable.hasOwnProperty('length');
|
|
79
|
-
}
|
|
80
|
-
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const convertParametersInner = function (change: string, convertString: string, param: Record<string, any>) {
|
|
81
94
|
const stringReg = new RegExp('(\\' + change + '\\{[a-zA-Z0-9._\\$]+\\})', 'g');
|
|
82
95
|
let stringTarget = convertString.match(stringReg);
|
|
96
|
+
|
|
83
97
|
if (stringTarget != null && stringTarget.length > 0) {
|
|
84
98
|
const _stringTarget = uniqueArray(stringTarget);
|
|
85
|
-
|
|
99
|
+
|
|
100
|
+
let target: string | undefined;
|
|
86
101
|
for (let i = 0; i < _stringTarget.length; i++) {
|
|
87
102
|
target = _stringTarget[i];
|
|
88
|
-
const t = target
|
|
103
|
+
const t = target!.replace(change + '{', '').replace('}', '');
|
|
89
104
|
let tempParamKey = LGet(param, t);
|
|
90
105
|
if (tempParamKey !== undefined) {
|
|
91
106
|
const reg = new RegExp('\\' + change + '{' + t + '}', 'g');
|
|
107
|
+
|
|
92
108
|
if (tempParamKey === null) {
|
|
93
109
|
tempParamKey = 'NULL';
|
|
94
110
|
convertString = convertString.replace(reg, tempParamKey);
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
111
|
+
} else {
|
|
97
112
|
if (change == '#') {
|
|
98
113
|
// processing JSON fields structures
|
|
99
114
|
if (isObject(tempParamKey) || isArray(tempParamKey)) {
|
|
100
115
|
tempParamKey = JSON.stringify(tempParamKey);
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
116
|
+
} else {
|
|
103
117
|
tempParamKey = tempParamKey.toString().replace(/"/g, '\\\"');
|
|
104
118
|
tempParamKey = mysqlRealEscapeParam(tempParamKey);
|
|
105
119
|
}
|
|
120
|
+
|
|
106
121
|
tempParamKey = tempParamKey.replace(/'/g, "''");
|
|
107
|
-
const replaceWith = "'" + tempParamKey + "'"
|
|
122
|
+
const replaceWith = "'" + tempParamKey + "'"
|
|
108
123
|
convertString = convertString.replace(reg, () => replaceWith);
|
|
109
|
-
}
|
|
110
|
-
else if (change == '$') {
|
|
124
|
+
} else if (change == '$') {
|
|
111
125
|
convertString = convertString.replace(reg, tempParamKey);
|
|
112
126
|
}
|
|
113
127
|
}
|
|
@@ -115,122 +129,134 @@ const convertParametersInner = function (change, convertString, param) {
|
|
|
115
129
|
}
|
|
116
130
|
}
|
|
117
131
|
return convertString;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const convertIf = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
135
|
+
let evalString = children.attrs['test']!;
|
|
121
136
|
try {
|
|
122
137
|
// Create Evaluate string
|
|
123
138
|
evalString = replaceEvalString(evalString, param);
|
|
139
|
+
|
|
124
140
|
evalString = evalString.replace(/ and /gi, ' && ');
|
|
125
141
|
evalString = evalString.replace(/ or /gi, ' || ');
|
|
142
|
+
|
|
126
143
|
// replace == to === for strict evaluate
|
|
127
144
|
// TODO: fix != null & != ''
|
|
128
145
|
// evalString = evalString.replace(/==/g, '===');
|
|
129
146
|
// evalString = evalString.replace(/!=/g, '!==');
|
|
147
|
+
|
|
130
148
|
evalString = evalString.replace(/^'(.*?)'\.equalsIgnoreCase\( ([a-zA-Z]+\.[a-zA-Z]+) \)/i, `($2 && $2.toUpperCase() === '$1'.toUpperCase())`);
|
|
131
149
|
evalString = evalString.replace(/\('(.*?)'\.equalsIgnoreCase\( ([a-zA-Z]+\.[a-zA-Z]+) \)/i, `(($2 && $2.toUpperCase() === '$1'.toUpperCase())`);
|
|
132
|
-
|
|
133
|
-
catch (err) {
|
|
150
|
+
|
|
151
|
+
} catch (err) {
|
|
134
152
|
throw new Error('Error occurred during convert <if> element.');
|
|
135
153
|
}
|
|
154
|
+
|
|
136
155
|
// Execute Evaluate string
|
|
137
156
|
try {
|
|
138
157
|
if (eval(evalString)) {
|
|
139
158
|
let convertString = '';
|
|
140
|
-
for (let i = 0, nextChildren; nextChildren = children['children'][i]
|
|
159
|
+
for (let i = 0, nextChildren: XML; nextChildren = children['children'][i]!; i++) {
|
|
141
160
|
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
142
161
|
}
|
|
143
162
|
return convertString;
|
|
144
|
-
|
|
145
|
-
else {
|
|
163
|
+
|
|
164
|
+
} else {
|
|
146
165
|
return '';
|
|
147
166
|
}
|
|
148
|
-
}
|
|
149
|
-
catch (e) {
|
|
167
|
+
} catch (e) {
|
|
150
168
|
return '';
|
|
151
169
|
}
|
|
152
|
-
}
|
|
153
|
-
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const convertForeach = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
154
173
|
try {
|
|
155
174
|
const collection = eval('param.' + children.attrs['collection']);
|
|
156
|
-
const item = children.attrs['item']
|
|
175
|
+
const item = children.attrs['item']!;
|
|
157
176
|
const open = (children.attrs['open'] == null) ? '' : children.attrs['open'];
|
|
158
177
|
const close = (children.attrs['close'] == null) ? '' : children.attrs['close'];
|
|
159
178
|
const separator = (children.attrs['separator'] == null) ? '' : children.attrs['separator'];
|
|
160
|
-
|
|
179
|
+
|
|
180
|
+
const foreachTexts: string[] = [];
|
|
161
181
|
let coll = null;
|
|
162
182
|
for (let j = 0; j < collection.length; j++) {
|
|
163
183
|
coll = collection[j];
|
|
164
184
|
const foreachParam = param;
|
|
165
185
|
foreachParam[item] = coll;
|
|
186
|
+
|
|
166
187
|
let foreachText = '';
|
|
167
|
-
for (let k = 0, nextChildren; nextChildren = children.children[k]
|
|
188
|
+
for (let k = 0, nextChildren: XML; nextChildren = children.children[k]!; k++) {
|
|
168
189
|
let fText = convertChildren(nextChildren, foreachParam, parentIds, myBatisMapper);
|
|
169
190
|
fText = fText.replace(/^\s*$/g, '');
|
|
170
191
|
if (fText != null && fText.length > 0) {
|
|
171
192
|
foreachText += fText;
|
|
172
193
|
}
|
|
173
194
|
}
|
|
195
|
+
|
|
174
196
|
if (foreachText != null && foreachText.length > 0) {
|
|
175
197
|
foreachTexts.push(foreachText);
|
|
176
198
|
}
|
|
177
199
|
}
|
|
200
|
+
|
|
178
201
|
return (open + foreachTexts.join(separator) + close);
|
|
179
|
-
}
|
|
180
|
-
catch (err) {
|
|
202
|
+
} catch (err) {
|
|
181
203
|
throw new Error('Error occurred during convert <foreach> element.');
|
|
182
204
|
}
|
|
183
|
-
}
|
|
184
|
-
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const convertChoose = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
185
208
|
try {
|
|
186
209
|
for (let i = 0, whenChildren; whenChildren = children.children[i]; i++) {
|
|
187
210
|
if (whenChildren.type == 'tag' && whenChildren.name.toLowerCase() == 'when') {
|
|
188
211
|
let evalString = whenChildren.attrs.test;
|
|
212
|
+
|
|
189
213
|
// Create Evaluate string
|
|
190
214
|
evalString = replaceEvalString(evalString, param);
|
|
215
|
+
|
|
191
216
|
evalString = evalString.replace(/ and /gi, ' && ');
|
|
192
217
|
evalString = evalString.replace(/ or /gi, ' || ');
|
|
218
|
+
|
|
193
219
|
// Execute Evaluate string
|
|
194
220
|
try {
|
|
195
221
|
if (eval(evalString)) {
|
|
196
222
|
// If <when> condition is true, do it.
|
|
197
223
|
let convertString = '';
|
|
198
|
-
for (let k = 0, nextChildren; nextChildren = whenChildren.children[k]; k++) {
|
|
224
|
+
for (let k = 0, nextChildren: XML; nextChildren = whenChildren.children[k]; k++) {
|
|
199
225
|
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
200
226
|
}
|
|
201
227
|
return convertString;
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
228
|
+
} else {
|
|
204
229
|
continue;
|
|
205
230
|
}
|
|
206
|
-
}
|
|
207
|
-
catch (e) {
|
|
231
|
+
} catch (e) {
|
|
208
232
|
continue;
|
|
209
233
|
}
|
|
210
|
-
}
|
|
211
|
-
else if (whenChildren.type == 'tag' && whenChildren.name.toLowerCase() == 'otherwise') {
|
|
234
|
+
} else if (whenChildren.type == 'tag' && whenChildren.name.toLowerCase() == 'otherwise') {
|
|
212
235
|
// If reached <otherwise> tag, do it.
|
|
213
236
|
let convertString = '';
|
|
214
|
-
for (let k = 0, nextChildren; nextChildren = whenChildren.children[k]; k++) {
|
|
237
|
+
for (let k = 0, nextChildren: XML; nextChildren = whenChildren.children[k]; k++) {
|
|
215
238
|
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
216
239
|
}
|
|
217
240
|
return convertString;
|
|
218
241
|
}
|
|
219
242
|
}
|
|
243
|
+
|
|
220
244
|
// If there is no suitable when and otherwise, just return null.
|
|
221
245
|
return '';
|
|
222
|
-
|
|
223
|
-
catch (err) {
|
|
246
|
+
|
|
247
|
+
} catch (err) {
|
|
224
248
|
throw new Error('Error occurred during convert <choose> element.');
|
|
225
249
|
}
|
|
226
|
-
}
|
|
227
|
-
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const convertTrimWhere = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
228
253
|
let convertString = '';
|
|
229
|
-
let prefix;
|
|
230
|
-
let prefixOverrides;
|
|
231
|
-
let suffix;
|
|
232
|
-
let suffixOverrides;
|
|
233
|
-
let globalSet;
|
|
254
|
+
let prefix: string | undefined;
|
|
255
|
+
let prefixOverrides: string | undefined;
|
|
256
|
+
let suffix: string | undefined;
|
|
257
|
+
let suffixOverrides: string | undefined;
|
|
258
|
+
let globalSet: string | undefined;
|
|
259
|
+
|
|
234
260
|
try {
|
|
235
261
|
switch (children.name.toLowerCase()) {
|
|
236
262
|
case 'trim':
|
|
@@ -248,86 +274,102 @@ const convertTrimWhere = function (children, param, parentIds, myBatisMapper) {
|
|
|
248
274
|
default:
|
|
249
275
|
throw new Error('Error occurred during convert <trim/where> element.');
|
|
250
276
|
}
|
|
277
|
+
|
|
251
278
|
// Convert children first.
|
|
252
|
-
for (let j = 0, nextChildren; nextChildren = children.children[j]
|
|
279
|
+
for (let j = 0, nextChildren: XML; nextChildren = children.children[j]!; j++) {
|
|
253
280
|
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
254
281
|
}
|
|
282
|
+
|
|
255
283
|
// Remove prefixOverrides
|
|
256
284
|
let trimRegex = new RegExp('(^)([\\s]*?)(' + prefixOverrides + ')', globalSet);
|
|
257
285
|
convertString = convertString.replace(trimRegex, '');
|
|
258
286
|
// Remove suffixOverrides
|
|
259
287
|
trimRegex = new RegExp('(' + suffixOverrides + ')([\\s]*?)($)', globalSet);
|
|
260
288
|
convertString = convertString.replace(trimRegex, '');
|
|
289
|
+
|
|
261
290
|
if (children.name.toLowerCase() != 'trim') {
|
|
262
291
|
trimRegex = new RegExp('(' + prefixOverrides + ')([\\s]*?)($)', globalSet);
|
|
263
292
|
convertString = convertString.replace(trimRegex, '');
|
|
264
293
|
}
|
|
294
|
+
|
|
265
295
|
// Add Prefix if String is not empty.
|
|
266
296
|
trimRegex = new RegExp('([a-zA-Z])', 'g');
|
|
267
297
|
const w = convertString.match(trimRegex);
|
|
298
|
+
|
|
268
299
|
if (w != null && w.length > 0) {
|
|
269
300
|
convertString = prefix + ' ' + convertString;
|
|
270
301
|
if (suffix) {
|
|
271
|
-
convertString = convertString + ' ' + suffix
|
|
302
|
+
convertString = convertString + ' ' + suffix
|
|
272
303
|
}
|
|
273
304
|
}
|
|
305
|
+
|
|
274
306
|
// Remove comma(,) before WHERE
|
|
275
307
|
if (children.name.toLowerCase() != 'where') {
|
|
276
308
|
const regex = new RegExp('(,)([\\s]*?)(where)', 'gi');
|
|
277
309
|
convertString = convertString.replace(regex, ' WHERE ');
|
|
278
310
|
}
|
|
311
|
+
|
|
279
312
|
return convertString;
|
|
280
|
-
}
|
|
281
|
-
catch (err) {
|
|
313
|
+
} catch (err) {
|
|
282
314
|
throw new Error('Error occurred during convert <' + children.name.toLowerCase() + '> element.');
|
|
283
315
|
}
|
|
284
|
-
}
|
|
285
|
-
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const convertSet = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
286
319
|
let convertString = '';
|
|
320
|
+
|
|
287
321
|
try {
|
|
288
322
|
// Convert children first.
|
|
289
|
-
for (let j = 0, nextChildren; nextChildren = children.children[j]
|
|
323
|
+
for (let j = 0, nextChildren: XML; nextChildren = children.children[j]!; j++) {
|
|
290
324
|
convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
|
|
291
325
|
}
|
|
326
|
+
|
|
292
327
|
// Remove comma repeated more than 2.
|
|
293
328
|
let regex = new RegExp('(,)(,|\\s){2,}', 'g');
|
|
294
329
|
convertString = convertString.replace(regex, ',\n');
|
|
330
|
+
|
|
295
331
|
// Remove first comma if exists.
|
|
296
332
|
regex = new RegExp('(^)([\\s]*?)(,)', 'g');
|
|
297
333
|
convertString = convertString.replace(regex, '');
|
|
334
|
+
|
|
298
335
|
// Remove last comma if exists.
|
|
299
336
|
regex = new RegExp('(,)([\\s]*?)($)', 'g');
|
|
300
337
|
convertString = convertString.replace(regex, '');
|
|
338
|
+
|
|
301
339
|
convertString = ' SET ' + convertString;
|
|
302
340
|
return convertString;
|
|
303
|
-
}
|
|
304
|
-
catch (err) {
|
|
341
|
+
} catch (err) {
|
|
305
342
|
throw new Error('Error occurred during convert <set> element.');
|
|
306
343
|
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const convertBind = function (children: XML, param: Record<string, any>) {
|
|
347
|
+
let evalString = children.attrs["value"]!;
|
|
348
|
+
|
|
310
349
|
// Create Evaluate string
|
|
311
350
|
evalString = replaceEvalString(evalString, param);
|
|
312
|
-
|
|
351
|
+
|
|
352
|
+
param[children.attrs["name"]!] = eval(evalString);
|
|
353
|
+
|
|
313
354
|
return param;
|
|
314
|
-
}
|
|
315
|
-
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const convertInclude = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
|
|
316
358
|
try {
|
|
317
359
|
// Add Properties to param
|
|
318
|
-
for (let j = 0, nextChildren; nextChildren = children.children[j]
|
|
360
|
+
for (let j = 0, nextChildren: XML; nextChildren = children.children[j]!; j++) {
|
|
319
361
|
if (nextChildren.type == 'tag' && nextChildren.name == 'property') {
|
|
320
|
-
param[nextChildren.attrs['name']] = nextChildren.attrs['value'];
|
|
362
|
+
param[nextChildren.attrs['name']!] = nextChildren.attrs['value'];
|
|
321
363
|
}
|
|
322
364
|
}
|
|
323
|
-
}
|
|
324
|
-
catch (err) {
|
|
365
|
+
} catch (err) {
|
|
325
366
|
throw new Error('Error occurred during read <property> element in <include> element.');
|
|
326
367
|
}
|
|
368
|
+
|
|
327
369
|
try {
|
|
328
|
-
let refid = convertParametersInner('#', children.attrs['refid']
|
|
370
|
+
let refid = convertParametersInner('#', children.attrs['refid']!, param);
|
|
329
371
|
refid = convertParametersInner('$', refid, param);
|
|
330
|
-
let mapper;
|
|
372
|
+
let mapper: XML[] | undefined;
|
|
331
373
|
for (const psqlid of parentIds) {
|
|
332
374
|
mapper = myBatisMapper[`${psqlid}.${refid}`];
|
|
333
375
|
if (mapper) {
|
|
@@ -335,30 +377,34 @@ const convertInclude = function (children, param, parentIds, myBatisMapper) {
|
|
|
335
377
|
}
|
|
336
378
|
}
|
|
337
379
|
let statement = '';
|
|
338
|
-
for (let i = 0, children; children = mapper[i]
|
|
380
|
+
for (let i = 0, children: XML; children = mapper![i]!; i++) {
|
|
339
381
|
statement += convertChildren(children, param, parentIds, myBatisMapper);
|
|
340
382
|
}
|
|
341
383
|
return statement;
|
|
342
|
-
}
|
|
343
|
-
catch (err) {
|
|
384
|
+
} catch (err) {
|
|
344
385
|
throw new Error('Error occurred during convert refid attribute in <include> element.');
|
|
345
386
|
}
|
|
346
|
-
|
|
347
|
-
|
|
387
|
+
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const isDict = function (v: any) {
|
|
348
391
|
return typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date);
|
|
349
|
-
}
|
|
350
|
-
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const replaceEvalString = function (evalString: string, param: Record<string, any>) {
|
|
351
395
|
const keys = Object.keys(param);
|
|
396
|
+
|
|
352
397
|
for (let i = 0; i < keys.length; i++) {
|
|
353
398
|
let replacePrefix = '';
|
|
354
399
|
let replacePostfix = '';
|
|
355
|
-
let paramRegex;
|
|
356
|
-
|
|
400
|
+
let paramRegex: RegExp | undefined;
|
|
401
|
+
|
|
402
|
+
if (isDict(param[keys[i]!])) {
|
|
357
403
|
replacePrefix = ' param.';
|
|
358
404
|
replacePostfix = '';
|
|
405
|
+
|
|
359
406
|
paramRegex = new RegExp('(^|[^a-zA-Z0-9_])(' + keys[i] + '\\.)([a-zA-Z0-9_]+)', 'g');
|
|
360
|
-
}
|
|
361
|
-
else {
|
|
407
|
+
} else {
|
|
362
408
|
replacePrefix = ' param.';
|
|
363
409
|
replacePostfix = ' ';
|
|
364
410
|
paramRegex = new RegExp('(^|[^a-zA-Z0-9_])(' + keys[i] + ')($|[^a-zA-Z0-9_])', 'g');
|
|
@@ -366,24 +412,27 @@ const replaceEvalString = function (evalString, param) {
|
|
|
366
412
|
evalString = evalString.replace(paramRegex, ('$1' + replacePrefix + '$2' + replacePostfix + '$3'));
|
|
367
413
|
}
|
|
368
414
|
return evalString;
|
|
369
|
-
}
|
|
370
|
-
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const uniqueArray = function (a: RegExpMatchArray) {
|
|
371
418
|
const seen = {};
|
|
372
|
-
const out = [];
|
|
419
|
+
const out: string[] = [];
|
|
373
420
|
const len = a.length;
|
|
374
421
|
let j = 0;
|
|
375
422
|
for (let i = 0; i < len; i++) {
|
|
376
|
-
const item = a[i]
|
|
423
|
+
const item = a[i]!;
|
|
377
424
|
if (seen[item] !== 1) {
|
|
378
425
|
seen[item] = 1;
|
|
379
426
|
out[j++] = item;
|
|
380
427
|
}
|
|
381
428
|
}
|
|
382
429
|
return out;
|
|
383
|
-
}
|
|
430
|
+
}
|
|
431
|
+
|
|
384
432
|
const mysqlRealEscapeParam = function (param) {
|
|
385
433
|
if (typeof param != 'string')
|
|
386
434
|
return param;
|
|
435
|
+
|
|
387
436
|
return param.replace(/[\0\x08\x09\x1a\n\r''\\\%]/g, function (char) {
|
|
388
437
|
switch (char) {
|
|
389
438
|
case '\0':
|
|
@@ -407,4 +456,5 @@ const mysqlRealEscapeParam = function (param) {
|
|
|
407
456
|
return char;
|
|
408
457
|
}
|
|
409
458
|
});
|
|
410
|
-
}
|
|
459
|
+
}
|
|
460
|
+
|
package/src/enum.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { _enums } from "./sql.js";
|
|
2
|
+
|
|
3
|
+
export class Enum {
|
|
4
|
+
private _value: string;
|
|
5
|
+
private _desc: string;
|
|
6
|
+
private _config: string[];
|
|
7
|
+
constructor(value: string, desc: string, ...config: string[]) {
|
|
8
|
+
this._value = value;
|
|
9
|
+
this._desc = desc;
|
|
10
|
+
this._config = config;
|
|
11
|
+
}
|
|
12
|
+
eq(value: string | number | undefined | null): boolean {
|
|
13
|
+
if (value === undefined) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (value === null) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
if (typeof value === 'number') {
|
|
20
|
+
return this._value === `${value}`;
|
|
21
|
+
}
|
|
22
|
+
return this._value === `${value}`;
|
|
23
|
+
}
|
|
24
|
+
value(): string {
|
|
25
|
+
return this._value;
|
|
26
|
+
}
|
|
27
|
+
desc(): string {
|
|
28
|
+
return this._desc;
|
|
29
|
+
}
|
|
30
|
+
config(): string[] {
|
|
31
|
+
return this._config;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export type EnumMap = Record<string, Enum>;
|
|
35
|
+
export type GlobalArray = Record<string, Array<[string, string]>>;
|
|
36
|
+
export type GlobalMap = Record<string, Record<string, string>>;
|
|
37
|
+
export interface EnmuJson {
|
|
38
|
+
GlobalArray: GlobalArray;
|
|
39
|
+
GlobalMap: GlobalMap;
|
|
40
|
+
}
|
|
41
|
+
let configData: EnmuJson | null = null;
|
|
42
|
+
export const getEnums = (GlobalValues?: EnumMap): EnmuJson => {
|
|
43
|
+
if (!GlobalValues) {
|
|
44
|
+
return globalThis[_enums];
|
|
45
|
+
}
|
|
46
|
+
if (configData) {
|
|
47
|
+
return configData;
|
|
48
|
+
}
|
|
49
|
+
const result: EnmuJson = {
|
|
50
|
+
GlobalArray: {},
|
|
51
|
+
GlobalMap: {}
|
|
52
|
+
};
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
54
|
+
Object.keys(GlobalValues).forEach((item) => {
|
|
55
|
+
// const guess = /([\w\W]+)_([^_]+)$/.exec(item);
|
|
56
|
+
const guess = item.replace(new RegExp(`_${GlobalValues[item]!.value()}`, 'i'), '');
|
|
57
|
+
if (guess) {
|
|
58
|
+
if (!result.GlobalArray[guess]) {
|
|
59
|
+
result.GlobalArray[guess] = [];
|
|
60
|
+
}
|
|
61
|
+
result.GlobalArray[guess]!.push([GlobalValues[item]!.value(), GlobalValues[item]!.desc()]);
|
|
62
|
+
|
|
63
|
+
if (!result.GlobalMap[guess]) {
|
|
64
|
+
result.GlobalMap[guess] = {};
|
|
65
|
+
}
|
|
66
|
+
result.GlobalMap[guess]![GlobalValues[item]!.value()] = GlobalValues[item]!.desc();
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
configData = result;
|
|
70
|
+
return result;
|
|
71
|
+
};
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const Throw = {
|
|
2
|
+
if(test: boolean, message: string | Error | any) {
|
|
3
|
+
if (test === true) this.now(message);
|
|
4
|
+
},
|
|
5
|
+
ifNot(test: boolean, message: string | Error | any) {
|
|
6
|
+
if (test !== true) this.now(message);
|
|
7
|
+
},
|
|
8
|
+
now(message: string | Error | any) {
|
|
9
|
+
throw typeof message === 'string' ? new Error(message) : message;
|
|
10
|
+
}
|
|
11
|
+
};
|